Skip to content

Commit

Permalink
Factor out shared code from start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 8, 2024
1 parent dbda60a commit af28742
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 87 deletions.
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/commands/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::config::locator;
pub mod add;
pub mod ls;
pub mod rm;
pub mod shared;
pub mod start;
pub mod stop;

Expand Down
41 changes: 41 additions & 0 deletions cmd/soroban-cli/src/commands/network/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use core::fmt;

use bollard::{ClientVersion, Docker};
use clap::ValueEnum;

// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate
const DEFAULT_TIMEOUT: u64 = 120;
const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion {
major_version: 1,
minor_version: 40,
};

#[derive(ValueEnum, Debug, Clone, PartialEq)]
pub enum Network {
Local,
Testnet,
Futurenet,
Pubnet,
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let variant_str = match self {
Network::Local => "local",
Network::Testnet => "testnet",
Network::Futurenet => "futurenet",
Network::Pubnet => "pubnet",
};

write!(f, "{variant_str}")
}
}

pub fn connect_to_docker(docker_socket_path: &Option<String>) -> Docker {
if docker_socket_path.is_some() {
let socket = docker_socket_path.as_ref().unwrap();
Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap()
} else {
Docker::connect_with_socket_defaults().unwrap()
}
}
49 changes: 6 additions & 43 deletions cmd/soroban-cli/src/commands/network/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,19 @@ use bollard::{
container::{Config, CreateContainerOptions, StartContainerOptions},
image::CreateImageOptions,
service::{HostConfig, PortBinding},
ClientVersion, Docker,
};
use clap::ValueEnum;
use core::fmt;
use futures_util::TryStreamExt;
use std::collections::HashMap;

use crate::commands::network::shared::connect_to_docker;
use crate::commands::network::shared::Network;

const DEFAULT_PORT_MAPPING: &str = "8000:8000";
const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart";

// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate
const DEFAULT_TIMEOUT: u64 = 120;
const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion {
major_version: 1,
minor_version: 40,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {}

#[derive(ValueEnum, Debug, Clone, PartialEq)]
pub enum Network {
Local,
Testnet,
Futurenet,
Pubnet,
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let variant_str = match self {
Network::Local => "local",
Network::Testnet => "testnet",
Network::Futurenet => "futurenet",
Network::Pubnet => "pubnet",
};

write!(f, "{}", variant_str)
}
}

#[derive(Debug, clap::Parser, Clone)]
pub struct Cmd {
/// network to start
Expand Down Expand Up @@ -82,7 +54,7 @@ impl Cmd {
}

async fn run_docker_command(cmd: &Cmd) {
let docker = connect_to_docker(cmd);
let docker = connect_to_docker(&cmd.docker_socket_path);

let image = get_image_name(cmd);
let create_image_options = Some(CreateImageOptions {
Expand Down Expand Up @@ -124,15 +96,6 @@ async fn run_docker_command(cmd: &Cmd) {
.await;
}

fn connect_to_docker(cmd: &Cmd) -> Docker {
if cmd.docker_socket_path.is_some() {
let socket = cmd.docker_socket_path.as_ref().unwrap();
Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap()
} else {
Docker::connect_with_socket_defaults().unwrap()
}
}

fn get_container_args(cmd: &Cmd) -> Vec<String> {
let enable_soroban_rpc = if cmd.disable_soroban_rpc {
String::new()
Expand Down Expand Up @@ -173,7 +136,7 @@ fn get_image_name(cmd: &Cmd) -> String {
/// The port mapping in the bollard crate is formatted differently than the docker CLI. In the docker CLI, we usually specify exposed ports as `-p HOST_PORT:CONTAINER_PORT`. But with the bollard crate, it is expecting the port mapping to be a map of the container port (with the protocol) to the host port.
fn get_port_mapping(cmd: &Cmd) -> HashMap<String, Option<Vec<PortBinding>>> {
let mut port_mapping_hash = HashMap::new();
for port_mapping in cmd.ports_mapping.iter() {
for port_mapping in &cmd.ports_mapping {
let ports_vec: Vec<&str> = port_mapping.split(':').collect();
let from_port = ports_vec[0];
let to_port = ports_vec[1];
Expand All @@ -182,7 +145,7 @@ fn get_port_mapping(cmd: &Cmd) -> HashMap<String, Option<Vec<PortBinding>>> {
format!("{to_port}/tcp"),
Some(vec![PortBinding {
host_ip: None,
host_port: Some(format!("{from_port}")),
host_port: Some(from_port.to_string()),
}]),
);
}
Expand Down
47 changes: 3 additions & 44 deletions cmd/soroban-cli/src/commands/network/stop.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
use core::fmt;

use bollard::{ClientVersion, Docker};
use clap::ValueEnum;
use crate::commands::network::shared::connect_to_docker;
use crate::commands::network::shared::Network;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to stop container: {0}")]
StopContainerError(#[from] bollard::errors::Error),
}

// DEFAULT_TIMEOUT and API_DEFAULT_VERSION are from the bollard crate
const DEFAULT_TIMEOUT: u64 = 120;
const API_DEFAULT_VERSION: &ClientVersion = &ClientVersion {
major_version: 1,
minor_version: 40,
};

// TODO: move to a shared module
#[derive(ValueEnum, Debug, Clone, PartialEq)]
pub enum Network {
Local,
Testnet,
Futurenet,
Pubnet,
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let variant_str = match self {
Network::Local => "local",
Network::Testnet => "testnet",
Network::Futurenet => "futurenet",
Network::Pubnet => "pubnet",
};

write!(f, "{}", variant_str)
}
}

#[derive(Debug, clap::Parser, Clone)]
pub struct Cmd {
/// network container to stop
Expand All @@ -51,20 +20,10 @@ pub struct Cmd {
impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = format!("stellar-{}", self.network);
let docker = connect_to_docker(self);
let docker = connect_to_docker(&self.docker_socket_path);
println!("Stopping container: {container_name}");
docker.stop_container(&container_name, None).await.unwrap();

Ok(())
}
}

//TODO: move to a shared module
fn connect_to_docker(cmd: &Cmd) -> Docker {
if cmd.docker_socket_path.is_some() {
let socket = cmd.docker_socket_path.as_ref().unwrap();
Docker::connect_with_socket(socket, DEFAULT_TIMEOUT, API_DEFAULT_VERSION).unwrap()
} else {
Docker::connect_with_socket_defaults().unwrap()
}
}

0 comments on commit af28742

Please sign in to comment.