From 2114d1df97be1b2fc3dddf7f7b3b145ce8bd2645 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:49:44 -0500 Subject: [PATCH] Change --docker-socket-path to --docker-host and allow for this to be passed in as an env var Co-authored-by: Willem Wyndham Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> --- cmd/soroban-cli/src/commands/network/shared.rs | 10 +++++----- cmd/soroban-cli/src/commands/network/start.rs | 15 ++++++--------- cmd/soroban-cli/src/commands/network/stop.rs | 8 ++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/cmd/soroban-cli/src/commands/network/shared.rs b/cmd/soroban-cli/src/commands/network/shared.rs index 4186d168f..f64491c59 100644 --- a/cmd/soroban-cli/src/commands/network/shared.rs +++ b/cmd/soroban-cli/src/commands/network/shared.rs @@ -3,7 +3,7 @@ use core::fmt; use bollard::{ClientVersion, Docker}; use clap::ValueEnum; -pub const DOCKER_SOCKET_PATH_HELP: &str = "Optional argument to override the default docker socket path. This is useful when you are using a non-standard docker socket path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock"; +pub const DOCKER_HOST_HELP: &str = "Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock"; // DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate const DEFAULT_TIMEOUT: u64 = 120; @@ -34,10 +34,10 @@ impl fmt::Display for Network { } pub async fn connect_to_docker( - docker_socket_path: &Option, + docker_host: &Option, ) -> Result { - let docker = if docker_socket_path.is_some() { - let socket = docker_socket_path.as_ref().unwrap(); + let docker = if docker_host.is_some() { + let socket = docker_host.as_ref().unwrap(); let connection = Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)?; check_docker_connection(&connection).await?; connection @@ -68,7 +68,7 @@ pub async fn check_docker_connection(docker: &Docker) -> Result<(), bollard::err Ok(_version) => Ok(()), Err(err) => { println!( - "⛔️ Failed to connect to the Docker daemon at {client_addr:?}. Is the docker daemon running?\nℹ️ Running a local Stellar network requires a Docker-compatible container runtime.\nℹ️ Please note that if you are using Docker Desktop, you may need to utilize the `--docker-socket-path` flag to pass in the location of the docker socket on your machine.\n" + "⛔️ Failed to connect to the Docker daemon at {client_addr:?}. Is the docker daemon running?\nℹ️ Running a local Stellar network requires a Docker-compatible container runtime.\nℹ️ Please note that if you are using Docker Desktop, you may need to utilize the `--docker-host` flag to pass in the location of the docker socket on your machine.\n" ); Err(err) } diff --git a/cmd/soroban-cli/src/commands/network/start.rs b/cmd/soroban-cli/src/commands/network/start.rs index 31bc77d1d..77fbb208e 100644 --- a/cmd/soroban-cli/src/commands/network/start.rs +++ b/cmd/soroban-cli/src/commands/network/start.rs @@ -7,7 +7,7 @@ use bollard::{ }; use futures_util::TryStreamExt; -use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_SOCKET_PATH_HELP}; +use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_HOST_HELP}; const DEFAULT_PORT_MAPPING: &str = "8000:8000"; const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart"; @@ -23,8 +23,8 @@ pub struct Cmd { /// Network to start pub network: Network, - #[arg(short = 'd', long, help = DOCKER_SOCKET_PATH_HELP)] - pub docker_socket_path: Option, + #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] + pub docker_host: Option, /// Optional argument to specify the limits for the local network only #[arg(short = 'l', long)] @@ -55,7 +55,7 @@ impl Cmd { } async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> { - let docker = connect_to_docker(&cmd.docker_socket_path).await?; + let docker = connect_to_docker(&cmd.docker_host).await?; let image = get_image_name(cmd); docker @@ -107,11 +107,8 @@ async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> { let stop_message = format!( "ℹ️ To stop this container run: soroban network stop {network} {additional_flags}", network = &cmd.network, - additional_flags = if cmd.docker_socket_path.is_some() { - format!( - "--docker-socket-path {}", - cmd.docker_socket_path.as_ref().unwrap() - ) + additional_flags = if cmd.docker_host.is_some() { + format!("--docker-host {}", cmd.docker_host.as_ref().unwrap()) } else { String::new() } diff --git a/cmd/soroban-cli/src/commands/network/stop.rs b/cmd/soroban-cli/src/commands/network/stop.rs index 2897efc2a..b121f3f66 100644 --- a/cmd/soroban-cli/src/commands/network/stop.rs +++ b/cmd/soroban-cli/src/commands/network/stop.rs @@ -1,4 +1,4 @@ -use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_SOCKET_PATH_HELP}; +use crate::commands::network::shared::{connect_to_docker, Network, DOCKER_HOST_HELP}; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -11,14 +11,14 @@ pub struct Cmd { /// Network to stop pub network: Network, - #[arg(short = 'd', long, help = DOCKER_SOCKET_PATH_HELP)] - pub docker_socket_path: Option, + #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] + pub docker_host: Option, } impl Cmd { pub async fn run(&self) -> Result<(), Error> { let container_name = format!("stellar-{}", self.network); - let docker = connect_to_docker(&self.docker_socket_path).await?; + let docker = connect_to_docker(&self.docker_host).await?; println!("ℹ️ Stopping container: {container_name}"); docker.stop_container(&container_name, None).await.unwrap(); println!("✅ Container stopped: {container_name}");