remove pageserver remotes support since we do not have tests for that and feature itself is delayed (#136)

This commit is contained in:
Dmitry Rodionov
2021-09-14 15:30:59 +03:00
committed by Dmitry
parent a2498f3e67
commit dc897fb864
3 changed files with 24 additions and 122 deletions

View File

@@ -4,20 +4,17 @@
// Now it also provides init method which acts like a stub for proper installation
// script which will use local paths.
//
use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use hex;
use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::{collections::BTreeMap, env};
use url::Url;
use zenith_utils::auth::{encode_from_key_path, Claims, Scope};
use zenith_utils::postgres_backend::AuthType;
use zenith_utils::zid::ZTenantId;
pub type Remotes = BTreeMap<String, String>;
//
// This data structures represent deserialized zenith CLI config
//
@@ -35,8 +32,8 @@ pub struct LocalEnv {
// to four separate paths and match OS-specific installation layout.
pub pg_distrib_dir: PathBuf,
// Path to pageserver binary. Empty for remote pageserver.
pub zenith_distrib_dir: Option<PathBuf>,
// Path to pageserver binary.
pub zenith_distrib_dir: PathBuf,
// keeping tenant id in config to reduce copy paste when running zenith locally with single tenant
#[serde(with = "hex")]
@@ -50,8 +47,6 @@ pub struct LocalEnv {
// used to issue tokens during e.g pg start
pub private_key_path: PathBuf,
pub remotes: Remotes,
}
impl LocalEnv {
@@ -64,11 +59,7 @@ impl LocalEnv {
}
pub fn pageserver_bin(&self) -> Result<PathBuf> {
Ok(self
.zenith_distrib_dir
.as_ref()
.ok_or_else(|| anyhow!("Can not manage remote pageserver"))?
.join("pageserver"))
Ok(self.zenith_distrib_dir.join("pageserver"))
}
pub fn pg_data_dirs_path(&self) -> PathBuf {
@@ -97,11 +88,7 @@ fn base_path() -> PathBuf {
//
// Initialize a new Zenith repository
//
pub fn init(
remote_pageserver: Option<&str>,
tenantid: ZTenantId,
auth_type: AuthType,
) -> Result<()> {
pub fn init(tenantid: ZTenantId, auth_type: AuthType) -> Result<()> {
// check if config already exists
let base_path = base_path();
if base_path.exists() {
@@ -165,39 +152,21 @@ pub fn init(
let auth_token =
encode_from_key_path(&Claims::new(None, Scope::PageServerApi), &private_key_path)?;
let conf = if let Some(addr) = remote_pageserver {
// check that addr is parsable
let _uri = Url::parse(addr).map_err(|e| anyhow!("{}: {}", addr, e))?;
// Find zenith binaries.
let zenith_distrib_dir = env::current_exe()?.parent().unwrap().to_owned();
if !zenith_distrib_dir.join("pageserver").exists() {
anyhow::bail!("Can't find pageserver binary.",);
}
LocalEnv {
pageserver_connstring: format!("postgresql://{}/", addr),
pg_distrib_dir,
zenith_distrib_dir: None,
base_data_dir: base_path,
remotes: BTreeMap::default(),
tenantid,
auth_token,
auth_type,
private_key_path,
}
} else {
// Find zenith binaries.
let zenith_distrib_dir = env::current_exe()?.parent().unwrap().to_owned();
if !zenith_distrib_dir.join("pageserver").exists() {
anyhow::bail!("Can't find pageserver binary.",);
}
LocalEnv {
pageserver_connstring: "postgresql://127.0.0.1:6400".to_string(),
pg_distrib_dir,
zenith_distrib_dir: Some(zenith_distrib_dir),
base_data_dir: base_path,
remotes: BTreeMap::default(),
tenantid,
auth_token,
auth_type,
private_key_path,
}
let conf = LocalEnv {
pageserver_connstring: "postgresql://127.0.0.1:6400".to_string(),
pg_distrib_dir,
zenith_distrib_dir,
base_data_dir: base_path,
tenantid,
auth_token,
auth_type,
private_key_path,
};
fs::create_dir_all(conf.pg_data_dirs_path())?;
@@ -225,12 +194,3 @@ pub fn load_config() -> Result<LocalEnv> {
let config = fs::read_to_string(repopath.join("config"))?;
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(())
}

View File

@@ -1,10 +1,9 @@
use anyhow::{anyhow, bail};
use anyhow::anyhow;
use anyhow::{Context, Result};
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use control_plane::compute::ComputeControlPlane;
use control_plane::local_env::{self, LocalEnv};
use control_plane::local_env;
use control_plane::storage::PageServerNode;
use std::collections::btree_map::Entry;
use std::collections::HashMap;
use std::process::exit;
use std::str::FromStr;
@@ -29,7 +28,7 @@ struct BranchTreeEl {
// This utility helps to manage zenith installation. That includes following:
// * Management of local postgres installations running on top of the
// pageserver.
// * Providing CLI api to the pageserver (local or remote)
// * Providing CLI api to the pageserver
// * TODO: export/import to/from usual postgres
fn main() -> Result<()> {
let timeline_arg = Arg::with_name("timeline")
@@ -49,12 +48,6 @@ fn main() -> Result<()> {
.subcommand(
SubCommand::with_name("init")
.about("Initialize a new Zenith repository")
.arg(
Arg::with_name("remote-pageserver")
.long("remote-pageserver")
.required(false)
.value_name("pageserver-url"),
)
.arg(
Arg::with_name("enable-auth")
.long("enable-auth")
@@ -108,34 +101,17 @@ fn main() -> Result<()> {
)
)
)
.subcommand(
SubCommand::with_name("remote")
.setting(AppSettings::ArgRequiredElseHelp)
.about("Manage remote pagerservers")
.subcommand(
SubCommand::with_name("add")
.about("Add a new remote pageserver")
.arg(Arg::with_name("name").required(true))
.arg(
Arg::with_name("url")
.help("PostgreSQL connection URI")
.required(true),
),
),
)
.get_matches();
// Create config file
if let ("init", Some(init_match)) = matches.subcommand() {
let tenantid = ZTenantId::generate();
let pageserver_uri = init_match.value_of("pageserver-url");
let auth_type = if init_match.is_present("enable-auth") {
AuthType::ZenithJWT
} else {
AuthType::Trust
};
local_env::init(pageserver_uri, tenantid, auth_type)
.with_context(|| "Failed to create config file")?;
local_env::init(tenantid, auth_type).with_context(|| "Failed to create config file")?;
}
// all other commands would need config
@@ -213,13 +189,6 @@ fn main() -> Result<()> {
}
}
("remote", Some(remote_match)) => {
if let Err(e) = handle_remote(remote_match, &env) {
eprintln!("remote operation failed: {}", e);
exit(1);
}
}
_ => {}
};
@@ -491,29 +460,3 @@ fn handle_pg(pg_match: &ArgMatches, env: &local_env::LocalEnv) -> Result<()> {
Ok(())
}
fn handle_remote(remote_match: &ArgMatches, local_env: &LocalEnv) -> Result<()> {
match remote_match.subcommand() {
("add", Some(args)) => {
let name = args.value_of("name").unwrap();
let url = args.value_of("url").unwrap();
// validate the URL
postgres::Config::from_str(url)?;
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),
}
local_env::save_config(&new_local_env)?;
}
_ => bail!("unknown command"),
}
Ok(())
}

View File

@@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
// Zenith ID is a 128-bit random ID.
// Used to represent various identifiers. Provides handy utility methods and impls.
// TODO (LizardWizzard) figure out best way to remove boiler plate with trait impls caused by newtype pattern
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
struct ZId([u8; 16]);