diff --git a/control_plane/src/lib.rs b/control_plane/src/lib.rs index cd6636aada..7ce1cffb9c 100644 --- a/control_plane/src/lib.rs +++ b/control_plane/src/lib.rs @@ -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 diff --git a/control_plane/src/local_env.rs b/control_plane/src/local_env.rs index c2ad533a80..21046fc465 100644 --- a/control_plane/src/local_env.rs +++ b/control_plane/src/local_env.rs @@ -11,7 +11,7 @@ use std::fs; use std::path::PathBuf; use url::Url; -use crate::remotes; +pub type Remotes = BTreeMap; // // 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, + + 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 { 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(); diff --git a/control_plane/src/remotes.rs b/control_plane/src/remotes.rs deleted file mode 100644 index e1d2238357..0000000000 --- a/control_plane/src/remotes.rs +++ /dev/null @@ -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; - -fn remotes_path(local_env: &LocalEnv) -> PathBuf { - local_env.base_data_dir.join("remotes") -} - -pub fn load_remotes(local_env: &LocalEnv) -> Result { - 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(()) -} diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index 77cc05629f..3009203469 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -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(), } } diff --git a/zenith/src/main.rs b/zenith/src/main.rs index 02bc36c3a5..439ff4415b 100644 --- a/zenith/src/main.rs +++ b/zenith/src/main.rs @@ -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"), }