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

Add working_dir, user, privileged Parameters for Executing Container #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions examples/containerexec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use futures::StreamExt;
use shiplift::{tty::TtyChunk, Docker, ExecContainerOptions};
use shiplift::{tty::TtyChunk, Docker, ExecContainerOptions, ExecContainerUserFormat};
use std::{env, str::from_utf8};

#[tokio::main]
Expand All @@ -13,9 +13,12 @@ async fn main() {
.cmd(vec![
"bash",
"-c",
"echo -n \"echo VAR=$VAR on stdout\"; echo -n \"echo VAR=$VAR on stderr\" >&2",
"echo -n \"user: `id`, dir: `pwd`, echo VAR=$VAR on stdout\"; echo -n \"echo VAR=$VAR on stderr\" >&2",
])
.env(vec!["VAR=value"])
.privileged(true)
.user(ExecContainerUserFormat::UidGid { uid: 0, gid: 0 })
.working_dir("/tmp")
.attach_stdout(true)
.attach_stderr(true)
.build();
Expand Down
58 changes: 58 additions & 0 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl<'docker> Exec<'docker> {
pub struct ExecContainerOptions {
params: HashMap<&'static str, Vec<String>>,
params_bool: HashMap<&'static str, bool>,
params_str: HashMap<&'static str, String>,
}

impl ExecContainerOptions {
Expand All @@ -217,6 +218,13 @@ impl ExecContainerOptions {
);
}

for (k, v) in &self.params_str {
body.insert(
(*k).to_owned(),
serde_json::to_value(v).map_err(Error::SerdeJsonError)?,
);
}

serde_json::to_string(&body).map_err(Error::from)
}
}
Expand All @@ -225,6 +233,7 @@ impl ExecContainerOptions {
pub struct ExecContainerOptionsBuilder {
params: HashMap<&'static str, Vec<String>>,
params_bool: HashMap<&'static str, bool>,
params_str: HashMap<&'static str, String>,
}

impl ExecContainerOptionsBuilder {
Expand Down Expand Up @@ -256,6 +265,47 @@ impl ExecContainerOptionsBuilder {
self
}

/// The working directory for the exec process inside the container
pub fn working_dir(
&mut self,
working_dir: &str,
) -> &mut Self {
self.params_str.insert("WorkingDir", working_dir.to_owned());
self
}

/// The user, and optionally, group to run the exec process inside the container. Format is one of: user, user:group, uid, or uid:gid
pub fn user(
&mut self,
user: ExecContainerUserFormat,
) -> &mut Self {
self.params_str.insert(
"User",
match user {
ExecContainerUserFormat::User(user) => user,
ExecContainerUserFormat::Uid(uid) => {
format!("{uid}", uid = uid)
}
ExecContainerUserFormat::UserGroup { user, group } => {
format!("{user}:{group}", user = user, group = group)
}
ExecContainerUserFormat::UidGid { uid, gid } => {
format!("{uid}:{gid}", uid = uid, gid = gid)
}
},
);
self
}

/// Runs the exec process with extended privileges
pub fn privileged(
&mut self,
privileged: bool,
) -> &mut Self {
self.params_bool.insert("Privileged", privileged);
self
}

/// Attach to stdout of the exec command
pub fn attach_stdout(
&mut self,
Expand All @@ -278,10 +328,18 @@ impl ExecContainerOptionsBuilder {
ExecContainerOptions {
params: self.params.clone(),
params_bool: self.params_bool.clone(),
params_str: self.params_str.clone(),
}
}
}

pub enum ExecContainerUserFormat {
User(String),
Uid(u64),
UserGroup { user: String, group: String },
UidGid { uid: u64, gid: u64 },
}

/// Interface for creating volumes
#[derive(Serialize, Debug)]
pub struct ExecResizeOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use crate::{
},
docker::{Docker, EventsOptions},
errors::{Error, Result},
exec::{Exec, ExecContainerOptions, ExecResizeOptions},
exec::{Exec, ExecContainerOptions, ExecContainerUserFormat, ExecResizeOptions},
image::{
BuildOptions, Image, ImageFilter, ImageListOptions, Images, PullOptions, RegistryAuth,
TagOptions,
Expand Down