From c79dd8d4580d044f9bac7318788bc660da19c14a Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Thu, 23 Feb 2023 13:19:39 -0500 Subject: [PATCH] compute_ctl: support for fetching spec from control plane (#3610) --- Cargo.lock | 1 + compute_tools/Cargo.toml | 1 + compute_tools/src/bin/compute_ctl.rs | 36 +++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index dab3d12263..c97e9a196a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -854,6 +854,7 @@ dependencies = [ "opentelemetry", "postgres", "regex", + "reqwest", "serde", "serde_json", "tar", diff --git a/compute_tools/Cargo.toml b/compute_tools/Cargo.toml index f8c3481f57..46b0e80896 100644 --- a/compute_tools/Cargo.toml +++ b/compute_tools/Cargo.toml @@ -17,6 +17,7 @@ regex.workspace = true serde.workspace = true serde_json.workspace = true tar.workspace = true +reqwest = { workspace = true, features = ["json"] } tokio = { workspace = true, features = ["rt", "rt-multi-thread"] } tokio-postgres.workspace = true tracing.workspace = true diff --git a/compute_tools/src/bin/compute_ctl.rs b/compute_tools/src/bin/compute_ctl.rs index 49cf1cd347..a4e9262072 100644 --- a/compute_tools/src/bin/compute_ctl.rs +++ b/compute_tools/src/bin/compute_ctl.rs @@ -65,6 +65,9 @@ fn main() -> Result<()> { let spec = matches.get_one::("spec"); let spec_path = matches.get_one::("spec-path"); + let compute_id = matches.get_one::("compute-id"); + let control_plane_uri = matches.get_one::("control-plane-uri"); + // Try to use just 'postgres' if no path is provided let pgbin = matches.get_one::("pgbin").unwrap(); @@ -77,8 +80,27 @@ fn main() -> Result<()> { let path = Path::new(sp); let file = File::open(path)?; serde_json::from_reader(file)? + } else if let Some(id) = compute_id { + if let Some(cp_base) = control_plane_uri { + let cp_uri = format!("{cp_base}/management/api/v1/{id}/spec"); + let jwt: String = match std::env::var("NEON_CONSOLE_JWT") { + Ok(v) => v, + Err(_) => "".to_string(), + }; + + reqwest::blocking::Client::new() + .get(cp_uri) + .header("Authorization", jwt) + .send()? + .json()? + } else { + panic!( + "must specify --control-plane-uri \"{:#?}\" and --compute-id \"{:#?}\"", + control_plane_uri, compute_id + ); + } } else { - panic!("cluster spec should be provided via --spec or --spec-path argument"); + panic!("compute spec should be provided via --spec or --spec-path argument"); } } }; @@ -227,6 +249,18 @@ fn cli() -> clap::Command { .long("spec-path") .value_name("SPEC_PATH"), ) + .arg( + Arg::new("compute-id") + .short('i') + .long("compute-id") + .value_name("COMPUTE_ID"), + ) + .arg( + Arg::new("control-plane-uri") + .short('p') + .long("control-plane-uri") + .value_name("CONTROL_PLANE"), + ) } #[test]