Skip to content

Commit

Permalink
Change --docker-socket-path to --docker-host and allow for this to be…
Browse files Browse the repository at this point in the history
… passed in as an env var

Co-authored-by: Willem Wyndham <[email protected]>
Co-authored-by: Leigh McCulloch <[email protected]>
  • Loading branch information
3 people committed Feb 21, 2024
1 parent 1f6f0ae commit 2114d1d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
10 changes: 5 additions & 5 deletions cmd/soroban-cli/src/commands/network/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,10 +34,10 @@ impl fmt::Display for Network {
}

pub async fn connect_to_docker(
docker_socket_path: &Option<String>,
docker_host: &Option<String>,
) -> Result<Docker, bollard::errors::Error> {
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
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/soroban-cli/src/commands/network/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<String>,
#[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")]
pub docker_host: Option<String>,

/// Optional argument to specify the limits for the local network only
#[arg(short = 'l', long)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/soroban-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String>,
#[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")]
pub docker_host: Option<String>,
}

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}");
Expand Down

0 comments on commit 2114d1d

Please sign in to comment.