store remotes in main config file

This commit is contained in:
Stas Kelvich
2021-05-18 01:49:11 +03:00
committed by Patrick Insinger
parent 23be5021f8
commit 45b1495f37
5 changed files with 23 additions and 36 deletions

View File

@@ -12,7 +12,6 @@ use std::path::Path;
pub mod compute;
pub mod local_env;
pub mod remotes;
pub mod storage;
/// Read a PID file

View File

@@ -11,7 +11,7 @@ use std::fs;
use std::path::PathBuf;
use url::Url;
use crate::remotes;
pub type Remotes = BTreeMap<String, String>;
//
// This data structures represent deserialized zenith CLI config
@@ -32,6 +32,8 @@ pub struct LocalEnv {
// Path to pageserver binary. Empty for remote pageserver.
pub zenith_distrib_dir: Option<PathBuf>,
pub remotes: Remotes,
}
impl LocalEnv {
@@ -112,6 +114,7 @@ pub fn init(remote_pageserver: Option<&str>) -> Result<()> {
pg_distrib_dir,
zenith_distrib_dir: None,
base_data_dir: base_path,
remotes: BTreeMap::default(),
}
} else {
// Find zenith binaries.
@@ -125,13 +128,12 @@ pub fn init(remote_pageserver: Option<&str>) -> Result<()> {
pg_distrib_dir,
zenith_distrib_dir: Some(zenith_distrib_dir),
base_data_dir: base_path,
remotes: BTreeMap::default(),
}
};
let toml = toml::to_string(&conf)?;
let toml = toml::to_string_pretty(&conf)?;
fs::write(conf.base_data_dir.join("config"), toml)?;
// initialize remotes file
remotes::save_remotes(&conf, &BTreeMap::default())?;
Ok(())
}
@@ -154,6 +156,15 @@ pub fn load_config() -> Result<LocalEnv> {
toml::from_str(config.as_str()).map_err(|e| e.into())
}
// Save config. We use that to change set of remotes from CLI itself.
pub fn save_config(conf: &LocalEnv) -> Result<()> {
let config_path = base_path().join("config");
let conf_str = toml::to_string_pretty(conf)?;
fs::write(config_path, conf_str)?;
Ok(())
}
// Find the directory where the binaries were put (i.e. target/debug/)
pub fn cargo_bin_dir() -> PathBuf {
let mut pathbuf = std::env::current_exe().unwrap();

View File

@@ -1,25 +0,0 @@
//! Utility module for managing the remote pageserver file
//! Currently a TOML file is used to map remote names to URIs
use anyhow::Result;
use std::collections::BTreeMap;
use std::{fs, path::PathBuf};
use crate::local_env::LocalEnv;
pub type Remotes = BTreeMap<String, String>;
fn remotes_path(local_env: &LocalEnv) -> PathBuf {
local_env.base_data_dir.join("remotes")
}
pub fn load_remotes(local_env: &LocalEnv) -> Result<Remotes> {
let remotes_str = fs::read_to_string(remotes_path(local_env))?;
Ok(toml::from_str(&remotes_str)?)
}
pub fn save_remotes(local_env: &LocalEnv, remotes: &Remotes) -> Result<()> {
let remotes_str = toml::to_string_pretty(remotes)?;
fs::write(remotes_path(local_env), remotes_str)?;
Ok(())
}

View File

@@ -1,5 +1,5 @@
use anyhow::{bail, Result};
use std::sync::{atomic::AtomicBool, Arc};
use std::{collections::BTreeMap, sync::{atomic::AtomicBool, Arc}};
use std::{
convert::TryInto,
fs::{self, File, OpenOptions},
@@ -42,6 +42,7 @@ pub fn create_test_env(testname: &str) -> LocalEnv {
pg_distrib_dir: Path::new(env!("CARGO_MANIFEST_DIR")).join("../tmp_install"),
zenith_distrib_dir: Some(local_env::cargo_bin_dir()),
base_data_dir: base_path,
remotes: BTreeMap::default(),
}
}

View File

@@ -5,8 +5,8 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use std::collections::HashMap;
use std::process::exit;
use control_plane::storage::PageServerNode;
use control_plane::{compute::ComputeControlPlane, local_env};
use control_plane::{local_env::LocalEnv, remotes};
use control_plane::compute::ComputeControlPlane;
use control_plane::local_env::{self, LocalEnv};
use pageserver::{branches::BranchInfo, ZTimelineId};
use zenith_utils::lsn::Lsn;
@@ -257,7 +257,6 @@ fn handle_pg(pg_match: &ArgMatches, env: &local_env::LocalEnv) -> Result<()> {
}
fn handle_remote(remote_match: &ArgMatches, local_env: &LocalEnv) -> Result<()> {
let mut remotes = remotes::load_remotes(local_env)?;
match remote_match.subcommand() {
("add", Some(args)) => {
let name = args.value_of("name").unwrap();
@@ -266,14 +265,16 @@ fn handle_remote(remote_match: &ArgMatches, local_env: &LocalEnv) -> Result<()>
// validate the URL
postgres::Config::from_str(url)?;
match remotes.entry(name.to_string()) {
let mut new_local_env = local_env.clone();
match new_local_env.remotes.entry(name.to_string()) {
Entry::Vacant(vacant) => {
vacant.insert(url.to_string());
}
Entry::Occupied(_) => bail!("origin '{}' already exists", name),
}
remotes::save_remotes(local_env, &remotes)?;
local_env::save_config(&new_local_env)?;
}
_ => bail!("unknown command"),
}