-
Notifications
You must be signed in to change notification settings - Fork 165
Add Swarm support #484
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
Merged
Merged
Add Swarm support #484
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d3397f
Swarm support from previous forked branch with updates
6960795
fix cargo format
89f6462
tests(swarm): Feature lock and circleci task for swarm test
fussybeaver 9ff337b
fix(clippy): Clippy swarm fixes
fussybeaver e41fc4a
docs(swarm): Fix swarm doctests
fussybeaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| edition = "2021" | ||
| ignore = [ | ||
| 'codegen/*' | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| //! Swarm API: Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines. | ||
| use crate::docker::BodyType; | ||
|
|
||
| use hyper::Method; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use bytes::Bytes; | ||
| use http::request::Builder; | ||
| use http_body_util::Full; | ||
|
|
||
| use std::cmp::Eq; | ||
| use std::hash::Hash; | ||
|
|
||
| use super::Docker; | ||
| use crate::errors::Error; | ||
|
|
||
| use crate::models::*; | ||
|
|
||
| /// Swam configuration used in the [Init Swarm API](Docker::init_swarm()) | ||
| #[derive(Debug, Clone, Default, Serialize, Deserialize)] | ||
| #[serde(rename_all = "PascalCase")] | ||
| pub struct InitSwarmOptions<T> | ||
| where | ||
| T: Into<String> + Eq + Hash, | ||
| { | ||
| /// Listen address (format: <ip|interface>[:port]) | ||
| pub listen_addr: T, | ||
| /// Externally reachable address advertised to other nodes. | ||
| pub advertise_addr: T, | ||
| } | ||
|
|
||
| /// Swam configuration used in the [Join Swarm API](Docker::join_swarm()) | ||
| #[derive(Debug, Clone, Default, Serialize)] | ||
| pub struct JoinSwarmOptions<T> | ||
| where | ||
| T: Into<String> + Serialize, | ||
| { | ||
| /// Externally reachable address advertised to other nodes. | ||
| pub advertise_addr: T, | ||
| /// Secret token for joining this swarm | ||
| pub join_token: T, | ||
| } | ||
|
|
||
| /// Swam configuration used in the [Leave Swarm API](Docker::leave_swarm()) | ||
| #[derive(Debug, Copy, Clone, Default, Serialize)] | ||
| pub struct LeaveSwarmOptions { | ||
| /// Force to leave to swarm. | ||
| pub force: bool, | ||
| } | ||
|
|
||
| impl Docker { | ||
| /// --- | ||
| /// | ||
| /// # Init Swarm | ||
| /// | ||
| /// Initialize a new swarm. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - [Init Swarm Options](InitSwarmOptions) struct. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// - A String wrapped in a | ||
| /// Future. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use bollard::Docker; | ||
| /// # let docker = Docker::connect_with_http_defaults().unwrap(); | ||
| /// # use bollard::swarm::InitSwarmOptions; | ||
| /// | ||
| /// use std::default::Default; | ||
| /// | ||
| /// let config = InitSwarmOptions { | ||
| /// advertise_addr: "127.0.0.1", | ||
| /// listen_addr: "0.0.0.0:2377" | ||
| /// }; | ||
| /// | ||
| /// docker.init_swarm(config); | ||
| /// ``` | ||
| pub async fn init_swarm<T>(&self, config: InitSwarmOptions<T>) -> Result<String, Error> | ||
| where | ||
| T: Into<String> + Eq + Hash + Serialize, | ||
| { | ||
| let url = "/swarm/init"; | ||
|
|
||
| let req = self.build_request( | ||
| url, | ||
| Builder::new().method(Method::POST), | ||
| None::<String>, | ||
| Docker::serialize_payload(Some(config)), | ||
| ); | ||
|
|
||
| self.process_into_value(req).await | ||
| } | ||
|
|
||
| /// --- | ||
| /// | ||
| /// # Inspect Swarm | ||
| /// | ||
| /// Inspect swarm. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// - [Swarm](swarm) struct, wrapped in a Future. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use bollard::Docker; | ||
| /// # let docker = Docker::connect_with_http_defaults().unwrap(); | ||
| /// | ||
| /// docker.inspect_swarm(); | ||
| /// ``` | ||
| pub async fn inspect_swarm(&self) -> Result<Swarm, Error> { | ||
| let url = "/swarm"; | ||
|
|
||
| let req = self.build_request( | ||
| url, | ||
| Builder::new().method(Method::GET), | ||
| None::<String>, | ||
| Ok(BodyType::Left(Full::new(Bytes::new()))), | ||
| ); | ||
|
|
||
| self.process_into_value(req).await | ||
| } | ||
|
|
||
| /// --- | ||
| /// | ||
| /// # Join a Swarm | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - [Join Swarm Options](JoinSwarmOptions) struct. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// - unit type `()`, wrapped in a Future. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use bollard::Docker; | ||
| /// # let docker = Docker::connect_with_http_defaults().unwrap(); | ||
| /// # use bollard::swarm::JoinSwarmOptions; | ||
| /// | ||
| /// let config = JoinSwarmOptions { | ||
| /// advertise_addr: "127.0.0.1", | ||
| /// join_token: "token", | ||
| /// }; | ||
| /// docker.join_swarm(config); | ||
| /// ``` | ||
| pub async fn join_swarm<T>(&self, config: JoinSwarmOptions<T>) -> Result<(), Error> | ||
| where | ||
| T: Into<String> + Eq + Hash + Serialize, | ||
| { | ||
| let url = "/swarm/join"; | ||
|
|
||
| let req = self.build_request( | ||
| url, | ||
| Builder::new().method(Method::POST), | ||
| None::<String>, | ||
| Docker::serialize_payload(Some(config)), | ||
| ); | ||
|
|
||
| self.process_into_unit(req).await | ||
| } | ||
|
|
||
| /// --- | ||
| /// | ||
| /// # Leave a Swarm | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// - unit type `()`, wrapped in a Future. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use bollard::Docker; | ||
| /// # let docker = Docker::connect_with_http_defaults().unwrap(); | ||
| /// | ||
| /// docker.leave_swarm(None); | ||
| /// ``` | ||
| pub async fn leave_swarm(&self, options: Option<LeaveSwarmOptions>) -> Result<(), Error> { | ||
| let url = "/swarm/leave"; | ||
|
|
||
| let req = self.build_request( | ||
| url, | ||
| Builder::new().method(Method::POST), | ||
| options, | ||
| Ok(BodyType::Left(Full::new(Bytes::new()))), | ||
| ); | ||
|
|
||
| self.process_into_unit(req).await | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #[macro_use] | ||
| pub mod common; | ||
|
|
||
| #[cfg(feature = "test_swarm")] | ||
| async fn swarm_test(docker: bollard::Docker) -> Result<(), bollard::errors::Error> { | ||
| use bollard::swarm::*; | ||
|
|
||
| // init swarm | ||
| let config = InitSwarmOptions { | ||
| listen_addr: "0.0.0.0:2377", | ||
| advertise_addr: "127.0.0.1", | ||
| }; | ||
| let _ = &docker.init_swarm(config).await?; | ||
|
|
||
| // inspect swarm | ||
| let inspection_result = &docker.inspect_swarm().await?; | ||
| assert!( | ||
| inspection_result | ||
| .join_tokens | ||
| .as_ref() | ||
| .unwrap() | ||
| .worker | ||
| .as_ref() | ||
| .unwrap() | ||
| .len() | ||
| > 0 | ||
| ); | ||
|
|
||
| // leave swarm | ||
| let config = LeaveSwarmOptions { force: true }; | ||
| let _ = &docker.leave_swarm(Some(config)).await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(feature = "test_swarm")] | ||
| #[test] | ||
| fn integration_test_swarm() { | ||
| use crate::common::run_runtime; | ||
| use bollard::Docker; | ||
| use tokio::runtime::Runtime; | ||
| connect_to_docker_and_run!(swarm_test); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can Dependabot be enabled? The latest release is 28.1.