From aa404b60fe03c8c7023dedf59e8a0960150b05d9 Mon Sep 17 00:00:00 2001 From: Stas Kelvich Date: Sun, 27 Jun 2021 16:03:08 +0300 Subject: [PATCH] change mgmt json format; add cli flags --- Cargo.lock | 1 + Dockerfile | 1 + proxy/Cargo.toml | 1 + proxy/src/cplane_api.rs | 53 ++++++++++++++++++++++++++++++----------- proxy/src/main.rs | 27 ++++++++++++++++++--- proxy/src/mgmt.rs | 7 ++++-- proxy/src/proxy.rs | 4 ++-- 7 files changed, 73 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4736d7ce24..e65936c6af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1352,6 +1352,7 @@ version = "0.1.0" dependencies = [ "anyhow", "bytes", + "clap", "hex", "md5", "rand", diff --git a/Dockerfile b/Dockerfile index c0edfd9f51..8855c7b5e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -82,6 +82,7 @@ RUN apk add --update openssl build-base RUN apk --no-cache --update --repository https://dl-cdn.alpinelinux.org/alpine/edge/testing add rocksdb COPY --from=build /zenith/target/release/pageserver /usr/local/bin COPY --from=build /zenith/target/release/wal_acceptor /usr/local/bin +COPY --from=build /zenith/target/release/proxy /usr/local/bin COPY --from=pg-build /zenith/tmp_install /usr/local COPY docker-entrypoint.sh /docker-entrypoint.sh diff --git a/proxy/Cargo.toml b/proxy/Cargo.toml index 39ad50ad9b..9dfa2ec914 100644 --- a/proxy/Cargo.toml +++ b/proxy/Cargo.toml @@ -16,5 +16,6 @@ serde = "1" serde_json = "1" tokio = "1.7.1" tokio-postgres = "0.7.2" +clap = "2.33.0" zenith_utils = { path = "../zenith_utils" } diff --git a/proxy/src/cplane_api.rs b/proxy/src/cplane_api.rs index e8be84d8ab..8e869da9e8 100644 --- a/proxy/src/cplane_api.rs +++ b/proxy/src/cplane_api.rs @@ -1,22 +1,41 @@ use anyhow::{bail, Result}; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, net::SocketAddr}; +use std::{ + collections::HashMap, + net::{IpAddr, SocketAddr}, +}; pub struct CPlaneApi { - address: SocketAddr, + // address: SocketAddr, } #[derive(Serialize, Deserialize)] pub struct DatabaseInfo { - pub addr: SocketAddr, - pub connstr: String, + pub host: IpAddr, // TODO: allow host name here too + pub port: u16, + pub dbname: String, + pub user: String, + pub password: String, +} + +impl DatabaseInfo { + pub fn socket_addr(&self) -> SocketAddr { + SocketAddr::new(self.host, self.port) + } + + pub fn conn_string(&self) -> String { + format!( + "dbname={} user={} password={}", + self.dbname, self.user, self.password + ) + } } // mock cplane api impl CPlaneApi { - pub fn new(address: &SocketAddr) -> CPlaneApi { + pub fn new(_address: &SocketAddr) -> CPlaneApi { CPlaneApi { - address: address.clone(), + // address: address.clone(), } } @@ -53,15 +72,21 @@ impl CPlaneApi { pub fn get_database_uri(&self, _user: &String, _database: &String) -> Result { Ok(DatabaseInfo { - addr: "127.0.0.1:5432".parse()?, - connstr: "user=stas dbname=stas".into(), + host: "127.0.0.1".parse()?, + port: 5432, + dbname: "stas".to_string(), + user: "stas".to_string(), + password: "mypass".to_string(), }) } - pub fn create_database(&self, _user: &String, _database: &String) -> Result { - Ok(DatabaseInfo { - addr: "127.0.0.1:5432".parse()?, - connstr: "user=stas dbname=stas".into(), - }) - } + // pub fn create_database(&self, _user: &String, _database: &String) -> Result { + // Ok(DatabaseInfo { + // host: "127.0.0.1".parse()?, + // port: 5432, + // dbname: "stas".to_string(), + // user: "stas".to_string(), + // password: "mypass".to_string(), + // }) + // } } diff --git a/proxy/src/main.rs b/proxy/src/main.rs index 32bb759e80..5b66f4b986 100644 --- a/proxy/src/main.rs +++ b/proxy/src/main.rs @@ -12,6 +12,8 @@ use std::{ thread, }; +use clap::{App, Arg}; + use cplane_api::DatabaseInfo; mod cplane_api; @@ -26,7 +28,7 @@ pub struct ProxyConf { /// will notify us here, so that we can 'unfreeze' user session. pub mgmt_address: SocketAddr, - /// control plane address where we check auth and create clusters. + /// control plane address where we would check auth. pub cplane_address: SocketAddr, } @@ -36,9 +38,28 @@ pub struct ProxyState { } fn main() -> anyhow::Result<()> { + let arg_matches = App::new("Zenith proxy/router") + .arg( + Arg::with_name("proxy") + .short("p") + .long("proxy") + .takes_value(true) + .help("listen for incoming client connections on ip:port") + .default_value("127.0.0.1:4432"), + ) + .arg( + Arg::with_name("mgmt") + .short("m") + .long("mgmt") + .takes_value(true) + .help("listen for management callback connection on ip:port") + .default_value("127.0.0.1:7000"), + ) + .get_matches(); + let conf = ProxyConf { - proxy_address: "0.0.0.0:4000".parse()?, - mgmt_address: "0.0.0.0:8080".parse()?, + proxy_address: arg_matches.value_of("proxy").unwrap().parse()?, + mgmt_address: arg_matches.value_of("mgmt").unwrap().parse()?, cplane_address: "127.0.0.1:3000".parse()?, }; let state = ProxyState { diff --git a/proxy/src/mgmt.rs b/proxy/src/mgmt.rs index 87fd403d91..6fab902c8d 100644 --- a/proxy/src/mgmt.rs +++ b/proxy/src/mgmt.rs @@ -46,8 +46,11 @@ struct MgmtHandler { // "session_id": "71d6d03e6d93d99a", // "result": { // "Success": { -// "addr": "127.0.0.1:5432", -// "connstr": "user=stas dbname=stas" +// "host": "127.0.0.1", +// "port": 5432, +// "dbname": "stas", +// "user": "stas" +// "password": "mypass" // } // } // } diff --git a/proxy/src/proxy.rs b/proxy/src/proxy.rs index 750c02406f..3b43382bb8 100644 --- a/proxy/src/proxy.rs +++ b/proxy/src/proxy.rs @@ -227,8 +227,8 @@ databases without opening the browser. } async fn proxy_pass(pgb: PostgresBackend, db_info: DatabaseInfo) -> anyhow::Result<()> { - let mut socket = tokio::net::TcpStream::connect(db_info.addr).await?; - let config = db_info.connstr.parse::()?; + let mut socket = tokio::net::TcpStream::connect(db_info.socket_addr()).await?; + let config = db_info.conn_string().parse::()?; let _ = config.connect_raw(&mut socket, NoTls).await?; println!("Connected to pg, proxying");