bollard/
swarm.rs

1//! Swarm API: Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.
2#![allow(deprecated)]
3use crate::docker::BodyType;
4
5use hyper::Method;
6use serde::{Deserialize, Serialize};
7
8use bytes::Bytes;
9use http::request::Builder;
10use http_body_util::Full;
11
12use std::cmp::Eq;
13use std::hash::Hash;
14
15use super::Docker;
16use crate::errors::Error;
17
18use crate::models::*;
19
20/// Swam configuration used in the [Init Swarm API](Docker::init_swarm())
21#[derive(Debug, Clone, Default, Serialize, Deserialize)]
22#[deprecated(
23    since = "0.19.0",
24    note = "use the OpenAPI generated bollard::models::SwarmInitRequest"
25)]
26#[serde(rename_all = "PascalCase")]
27pub struct InitSwarmOptions<T>
28where
29    T: Into<String> + Eq + Hash,
30{
31    /// Listen address (format: <ip|interface>[:port])
32    pub listen_addr: T,
33    /// Externally reachable address advertised to other nodes.
34    pub advertise_addr: T,
35}
36
37impl<T> From<InitSwarmOptions<T>> for SwarmInitRequest
38where
39    T: Into<String> + Eq + Hash,
40{
41    fn from(opts: InitSwarmOptions<T>) -> Self {
42        SwarmInitRequest {
43            listen_addr: Some(opts.listen_addr.into()),
44            advertise_addr: Some(opts.advertise_addr.into()),
45            ..Default::default()
46        }
47    }
48}
49
50/// Swam configuration used in the [Join Swarm API](Docker::join_swarm())
51#[derive(Debug, Clone, Default, Serialize)]
52#[deprecated(
53    since = "0.19.0",
54    note = "use the OpenAPI generated bollard::models::SwarmJoinRequest"
55)]
56pub struct JoinSwarmOptions<T>
57where
58    T: Into<String> + Serialize,
59{
60    /// Externally reachable address advertised to other nodes.
61    pub advertise_addr: T,
62    /// Secret token for joining this swarm
63    pub join_token: T,
64}
65
66impl<T> From<JoinSwarmOptions<T>> for SwarmJoinRequest
67where
68    T: Into<String> + Serialize,
69{
70    fn from(opts: JoinSwarmOptions<T>) -> Self {
71        SwarmJoinRequest {
72            advertise_addr: Some(opts.advertise_addr.into()),
73            join_token: Some(opts.join_token.into()),
74            ..Default::default()
75        }
76    }
77}
78
79/// Swam configuration used in the [Leave Swarm API](Docker::leave_swarm())
80#[derive(Debug, Copy, Clone, Default, Serialize)]
81#[deprecated(
82    since = "0.19.0",
83    note = "use the OpenAPI generated bollard::query_parameters::LeaveSwarmOptions and associated LeaveSwarmOptionsBuilder"
84)]
85pub struct LeaveSwarmOptions {
86    /// Force to leave to swarm.
87    pub force: bool,
88}
89
90impl From<LeaveSwarmOptions> for crate::query_parameters::LeaveSwarmOptions {
91    fn from(opts: LeaveSwarmOptions) -> Self {
92        crate::query_parameters::LeaveSwarmOptionsBuilder::default()
93            .force(opts.force)
94            .build()
95    }
96}
97
98impl Docker {
99    /// ---
100    ///
101    /// # Init Swarm
102    ///
103    /// Initialize a new swarm.
104    ///
105    /// # Arguments
106    ///
107    ///  - [Init Swarm Options](InitSwarmOptions) struct.
108    ///
109    /// # Returns
110    ///
111    ///  - A String wrapped in a
112    ///    Future.
113    ///
114    /// # Examples
115    ///
116    /// ```rust
117    /// # use bollard::Docker;
118    /// # let docker = Docker::connect_with_http_defaults().unwrap();
119    /// # use bollard::swarm::InitSwarmOptions;
120    ///
121    /// use std::default::Default;
122    ///
123    /// let config = InitSwarmOptions {
124    ///     advertise_addr: "127.0.0.1",
125    ///     listen_addr: "0.0.0.0:2377"
126    /// };
127    ///
128    /// docker.init_swarm(config);
129    /// ```
130    pub async fn init_swarm(&self, config: impl Into<SwarmInitRequest>) -> Result<String, Error> {
131        let url = "/swarm/init";
132
133        let req = self.build_request(
134            url,
135            Builder::new().method(Method::POST),
136            None::<String>,
137            Docker::serialize_payload(Some(config.into())),
138        );
139
140        self.process_into_value(req).await
141    }
142
143    /// ---
144    ///
145    /// # Inspect Swarm
146    ///
147    /// Inspect swarm.
148    ///
149    /// # Arguments
150    ///
151    /// # Returns
152    ///
153    ///  - [Swarm](swarm) struct, wrapped in a Future.
154    ///
155    /// # Examples
156    ///
157    /// ```rust
158    /// # use bollard::Docker;
159    /// # let docker = Docker::connect_with_http_defaults().unwrap();
160    ///
161    /// docker.inspect_swarm();
162    /// ```
163    pub async fn inspect_swarm(&self) -> Result<Swarm, Error> {
164        let url = "/swarm";
165
166        let req = self.build_request(
167            url,
168            Builder::new().method(Method::GET),
169            None::<String>,
170            Ok(BodyType::Left(Full::new(Bytes::new()))),
171        );
172
173        self.process_into_value(req).await
174    }
175
176    /// ---
177    ///
178    /// # Join a Swarm
179    ///
180    /// # Arguments
181    ///
182    ///  - [Join Swarm Options](JoinSwarmOptions) struct.
183    ///
184    /// # Returns
185    ///
186    ///  - unit type `()`, wrapped in a Future.
187    ///
188    /// # Examples
189    ///
190    /// ```rust
191    /// # use bollard::Docker;
192    /// # let docker = Docker::connect_with_http_defaults().unwrap();
193    /// # use bollard::swarm::JoinSwarmOptions;
194    ///
195    /// let config = JoinSwarmOptions {
196    ///     advertise_addr: "127.0.0.1",
197    ///     join_token: "token",
198    /// };
199    /// docker.join_swarm(config);
200    /// ```
201    pub async fn join_swarm(&self, config: impl Into<SwarmJoinRequest>) -> Result<(), Error> {
202        let url = "/swarm/join";
203
204        let req = self.build_request(
205            url,
206            Builder::new().method(Method::POST),
207            None::<String>,
208            Docker::serialize_payload(Some(config.into())),
209        );
210
211        self.process_into_unit(req).await
212    }
213
214    /// ---
215    ///
216    /// # Leave a Swarm
217    ///
218    /// # Arguments
219    ///
220    /// # Returns
221    ///
222    ///  - unit type `()`, wrapped in a Future.
223    ///
224    /// # Examples
225    ///
226    /// ```rust
227    /// # use bollard::Docker;
228    /// # use bollard::query_parameters::LeaveSwarmOptions;
229    /// # let docker = Docker::connect_with_http_defaults().unwrap();
230    ///
231    /// docker.leave_swarm(None::<LeaveSwarmOptions>);
232    /// ```
233    pub async fn leave_swarm(
234        &self,
235        options: Option<impl Into<crate::query_parameters::LeaveSwarmOptions>>,
236    ) -> Result<(), Error> {
237        let url = "/swarm/leave";
238
239        let req = self.build_request(
240            url,
241            Builder::new().method(Method::POST),
242            options.map(Into::into),
243            Ok(BodyType::Left(Full::new(Bytes::new()))),
244        );
245
246        self.process_into_unit(req).await
247    }
248}