Skip to content
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

Make ContainerOptions Deserializable #217

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interfaces for building various structures

use crate::{errors::Error, Result};
use serde::Serialize;
use serde::{Serialize, Deserialize};
use serde_json::{self, json, map::Map, Value};
use std::{
cmp::Eq,
Expand Down Expand Up @@ -531,10 +531,10 @@ impl ContainerListOptionsBuilder {
}

/// Interface for building a new docker container from an existing image
#[derive(Serialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct ContainerOptions {
pub name: Option<String>,
params: HashMap<&'static str, Value>,
params: HashMap<String, Value>,
}

/// Function to insert a JSON value into a tree where the desired
Expand Down Expand Up @@ -602,20 +602,21 @@ impl ContainerOptions {
}
}

#[derive(Default)]
#[derive(Default, Deserialize, Serialize)]
pub struct ContainerOptionsBuilder {
name: Option<String>,
params: HashMap<&'static str, Value>,
params: HashMap<String, Value>,
}

impl ContainerOptionsBuilder {
pub(crate) fn new(image: &str) -> Self {
let mut params = HashMap::new();

params.insert("Image", Value::String(image.to_owned()));
params.insert("Image".into(), json!(image));
ContainerOptionsBuilder { name: None, params }
}

/// Specify the container name
pub fn name(
&mut self,
name: &str,
Expand All @@ -624,12 +625,21 @@ impl ContainerOptionsBuilder {
self
}

/// Specify the container image
pub fn image(
&mut self,
image: &str,
) -> &mut Self {
self.params.insert("Image".into(), json!(image));
self
}

/// Specify the working dir (corresponds to the `-w` docker cli argument)
pub fn working_dir(
&mut self,
working_dir: &str,
) -> &mut Self {
self.params.insert("WorkingDir", json!(working_dir));
self.params.insert("WorkingDir".into(), json!(working_dir));
self
}

Expand All @@ -638,18 +648,19 @@ impl ContainerOptionsBuilder {
&mut self,
volumes: Vec<&str>,
) -> &mut Self {
self.params.insert("HostConfig.Binds", json!(volumes));
self.params.insert("HostConfig.Binds".into(), json!(volumes));
self
}

/// enable all exposed ports on the container to be mapped to random, available, ports on the host
/// Enable all exposed ports on the container to be mapped to random, available, ports on the host
pub fn publish_all_ports(
&mut self,
) -> &mut Self {
self.params.insert("HostConfig.PublishAllPorts", json!(true));
self.params.insert("HostConfig.PublishAllPorts".into(), json!(true));
self
}

//
pub fn expose(
&mut self,
srcport: u32,
Expand Down Expand Up @@ -679,7 +690,7 @@ impl ContainerOptionsBuilder {
);

self.params
.insert("HostConfig.PortBindings", json!(port_bindings));
.insert("HostConfig.PortBindings".into(), json!(port_bindings));

// Replicate the port bindings over to the exposed ports config
let mut exposed_ports: HashMap<String, Value> = HashMap::new();
Expand All @@ -688,7 +699,7 @@ impl ContainerOptionsBuilder {
exposed_ports.insert(key.to_string(), json!(empty_config));
}

self.params.insert("ExposedPorts", json!(exposed_ports));
self.params.insert("ExposedPorts".into(), json!(exposed_ports));

self
}
Expand Down Expand Up @@ -722,7 +733,7 @@ impl ContainerOptionsBuilder {
exposed_ports.insert(key.to_string(), json!(empty_config));
}

self.params.insert("ExposedPorts", json!(exposed_ports));
self.params.insert("ExposedPorts".into(), json!(exposed_ports));

self
}
Expand All @@ -731,15 +742,15 @@ impl ContainerOptionsBuilder {
&mut self,
links: Vec<&str>,
) -> &mut Self {
self.params.insert("HostConfig.Links", json!(links));
self.params.insert("HostConfig.Links".into(), json!(links));
self
}

pub fn memory(
&mut self,
memory: u64,
) -> &mut Self {
self.params.insert("HostConfig.Memory", json!(memory));
self.params.insert("HostConfig.Memory".into(), json!(memory));
self
}

Expand All @@ -750,15 +761,15 @@ impl ContainerOptionsBuilder {
cpu_shares: u32,
) -> &mut Self {
self.params
.insert("HostConfig.CpuShares", json!(cpu_shares));
.insert("HostConfig.CpuShares".into(), json!(cpu_shares));
self
}

pub fn labels(
&mut self,
labels: &HashMap<&str, &str>,
) -> &mut Self {
self.params.insert("Labels", json!(labels));
self.params.insert("Labels".into(), json!(labels));
self
}

Expand All @@ -767,8 +778,8 @@ impl ContainerOptionsBuilder {
&mut self,
attach: bool,
) -> &mut Self {
self.params.insert("AttachStdin", json!(attach));
self.params.insert("OpenStdin", json!(attach));
self.params.insert("AttachStdin".into(), json!(attach));
self.params.insert("OpenStdin".into(), json!(attach));
self
}

Expand All @@ -777,7 +788,7 @@ impl ContainerOptionsBuilder {
&mut self,
attach: bool,
) -> &mut Self {
self.params.insert("AttachStdout", json!(attach));
self.params.insert("AttachStdout".into(), json!(attach));
self
}

Expand All @@ -786,7 +797,7 @@ impl ContainerOptionsBuilder {
&mut self,
attach: bool,
) -> &mut Self {
self.params.insert("AttachStderr", json!(attach));
self.params.insert("AttachStderr".into(), json!(attach));
self
}

Expand All @@ -795,71 +806,71 @@ impl ContainerOptionsBuilder {
&mut self,
tty: bool,
) -> &mut Self {
self.params.insert("Tty", json!(tty));
self.params.insert("Tty".into(), json!(tty));
self
}

pub fn extra_hosts(
&mut self,
hosts: Vec<&str>,
) -> &mut Self {
self.params.insert("HostConfig.ExtraHosts", json!(hosts));
self.params.insert("HostConfig.ExtraHosts".into(), json!(hosts));
self
}

pub fn volumes_from(
&mut self,
volumes: Vec<&str>,
) -> &mut Self {
self.params.insert("HostConfig.VolumesFrom", json!(volumes));
self.params.insert("HostConfig.VolumesFrom".into(), json!(volumes));
self
}

pub fn network_mode(
&mut self,
network: &str,
) -> &mut Self {
self.params.insert("HostConfig.NetworkMode", json!(network));
self.params.insert("HostConfig.NetworkMode".into(), json!(network));
self
}

pub fn env(
&mut self,
envs: Vec<&str>,
) -> &mut Self {
self.params.insert("Env", json!(envs));
self.params.insert("Env".into(), json!(envs));
self
}

pub fn cmd(
&mut self,
cmds: Vec<&str>,
) -> &mut Self {
self.params.insert("Cmd", json!(cmds));
self.params.insert("Cmd".into(), json!(cmds));
self
}

pub fn entrypoint(
&mut self,
entrypoint: &str,
) -> &mut Self {
self.params.insert("Entrypoint", json!(entrypoint));
self.params.insert("Entrypoint".into(), json!(entrypoint));
self
}

pub fn capabilities(
&mut self,
capabilities: Vec<&str>,
) -> &mut Self {
self.params.insert("HostConfig.CapAdd", json!(capabilities));
self.params.insert("HostConfig.CapAdd".into(), json!(capabilities));
self
}

pub fn devices(
&mut self,
devices: Vec<HashMap<String, String>>,
) -> &mut Self {
self.params.insert("HostConfig.Devices", json!(devices));
self.params.insert("HostConfig.Devices".into(), json!(devices));
self
}

Expand All @@ -868,7 +879,7 @@ impl ContainerOptionsBuilder {
log_driver: &str,
) -> &mut Self {
self.params
.insert("HostConfig.LogConfig.Type", json!(log_driver));
.insert("HostConfig.LogConfig.Type".into(), json!(log_driver));
self
}

Expand All @@ -878,10 +889,10 @@ impl ContainerOptionsBuilder {
maximum_retry_count: u64,
) -> &mut Self {
self.params
.insert("HostConfig.RestartPolicy.Name", json!(name));
.insert("HostConfig.RestartPolicy.Name".into(), json!(name));
if name == "on-failure" {
self.params.insert(
"HostConfig.RestartPolicy.MaximumRetryCount",
"HostConfig.RestartPolicy.MaximumRetryCount".into(),
json!(maximum_retry_count),
);
}
Expand All @@ -892,23 +903,23 @@ impl ContainerOptionsBuilder {
&mut self,
set: bool,
) -> &mut Self {
self.params.insert("HostConfig.AutoRemove", json!(set));
self.params.insert("HostConfig.AutoRemove".into(), json!(set));
self
}

pub fn userns_mode(
&mut self,
mode: &str,
) -> &mut Self {
self.params.insert("HostConfig.UsernsMode", json!(mode));
self.params.insert("HostConfig.UsernsMode".into(), json!(mode));
self
}

pub fn privileged(
&mut self,
set: bool,
) -> &mut Self {
self.params.insert("HostConfig.Privileged", json!(set));
self.params.insert("HostConfig.Privileged".into(), json!(set));
self
}

Expand Down