-
Notifications
You must be signed in to change notification settings - Fork 63
feat: launch module from docker image #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,5 @@ eyre.workspace = true | |
|
|
||
| tree_hash.workspace = true | ||
| tree_hash_derive.workspace = true | ||
|
|
||
| bollard = "0.16.1" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| use std::process::Stdio; | ||
|
|
||
| use cb_common::{ | ||
| config::{CommitBoostConfig, CONFIG_PATH_ENV, MODULE_ID_ENV}, | ||
| config::{CommitBoostConfig, ModuleSource, CONFIG_PATH_ENV, MODULE_ID_ENV}, | ||
| utils::print_logo, | ||
| }; | ||
| use cb_crypto::service::SigningService; | ||
|
|
@@ -69,24 +69,53 @@ impl Args { | |
| Command::Start { config: config_path } => { | ||
| let config = CommitBoostConfig::from_file(&config_path); | ||
|
|
||
| // Initialize Docker client | ||
| let docker = bollard::Docker::connect_with_local_defaults().expect("Failed to connect to Docker"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we currently expect that CB and the docker engine run on the same machine. However, the way we connect to the docker daemon is modifiable if more complex needs arise: ref. |
||
|
|
||
| if let Some(modules) = config.modules { | ||
| let signer_config = config.signer.expect("missing signer config with modules"); | ||
| // start signing server | ||
| tokio::spawn(SigningService::run(config.chain, signer_config)); | ||
|
|
||
| // this mocks the commit boost client starting containers, processes etc | ||
| let mut child_handles = Vec::with_capacity(modules.len()); | ||
|
|
||
| for module in modules { | ||
| let child = std::process::Command::new(module.path) | ||
| .env(MODULE_ID_ENV, module.id) | ||
| .env(CONFIG_PATH_ENV, &config_path) | ||
| .spawn() | ||
| .expect("failed to start process"); | ||
| match module.source { | ||
| ModuleSource::DockerImageId(docker_image) => { | ||
| let config = bollard::container::Config { | ||
| image: Some(docker_image.clone()), | ||
| host_config: Some(bollard::secret::HostConfig { | ||
| binds: { | ||
| let full_config_path = std::fs::canonicalize(&config_path).unwrap().to_string_lossy().to_string(); | ||
| Some(vec![format!("{}:{}", full_config_path, "/config.toml")]) | ||
| }, | ||
| network_mode: Some(String::from("host")), // Use the host network | ||
|
||
| ..Default::default() | ||
| }), | ||
| env: Some(vec![ | ||
| format!("{}={}", MODULE_ID_ENV, module.id), | ||
| format!("{}={}", CONFIG_PATH_ENV, "/config.toml"), | ||
| ]), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| child_handles.push(child); | ||
| } | ||
| let container = docker.create_container::<&str, String>(None, config).await?; | ||
| let container_id = container.id; | ||
| docker.start_container::<String>(&container_id, None).await?; | ||
| println!("Started container: {} from image {}", container_id, &docker_image); | ||
| }, | ||
| ModuleSource::Path(path) => { | ||
| let child = std::process::Command::new(path) | ||
| .env(MODULE_ID_ENV, module.id) | ||
| .env(CONFIG_PATH_ENV, &config_path) | ||
| .spawn() | ||
| .expect("failed to start process"); | ||
|
|
||
| // start signing server | ||
| tokio::spawn(SigningService::run(config.chain, signer_config)); | ||
| child_handles.push(child); | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // start pbs server | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,13 +113,57 @@ const fn default_u256() -> U256 { | |
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| pub enum ModuleSource { | ||
| #[serde(rename = "path")] | ||
| Path(String), | ||
| #[serde(rename = "docker_image")] | ||
| DockerImageId(String), | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize)] | ||
| #[serde(deny_unknown_fields)] | ||
| pub struct ModuleConfig<T = ()> { | ||
| pub id: String, | ||
| pub path: String, | ||
| #[serde(flatten)] | ||
| pub source: ModuleSource, | ||
| #[serde(flatten)] | ||
| pub extra: T, | ||
| } | ||
|
|
||
| impl<'de, T> Deserialize<'de> for ModuleConfig<T> | ||
|
||
| where | ||
| T: Deserialize<'de>, | ||
| { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| #[derive(Deserialize)] | ||
| struct InnerModuleConfig<T> { | ||
| id: String, | ||
| path: Option<String>, | ||
| docker_image: Option<String>, | ||
| #[serde(flatten)] | ||
| extra: T, | ||
| } | ||
|
|
||
| let inner = InnerModuleConfig::deserialize(deserializer)?; | ||
|
|
||
| let source = match (inner.path, inner.docker_image) { | ||
| (Some(path), None) => ModuleSource::Path(path), | ||
| (None, Some(docker_image)) => ModuleSource::DockerImageId(docker_image), | ||
| (Some(_), Some(_)) => return Err(de::Error::custom("Cannot have both `path` and `docker_image`")), | ||
| (None, None) => return Err(de::Error::custom("Must have either `path` or `docker_image`")), | ||
| }; | ||
|
|
||
| Ok(ModuleConfig { | ||
| id: inner.id, | ||
| source, | ||
| extra: inner.extra, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| pub struct StartModuleConfig<T = ()> { | ||
| pub chain: Chain, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the example image that must be available in the local registry before starting commit-boost.
You can find the example source code in this repo.
NOTE: Whilst we're still in development, you will have to modify the
Cargo.tomlof the example module to point to a correct source for thecb-...dependencies.