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

Implemented the rm command and the force and volumes flags #54

Merged
merged 3 commits into from
Aug 11, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[[#54]](https://github.com/MitchellBerend/docker-manager/pull/54) Implement rm command and most of it's flags

[[#53]](https://github.com/MitchellBerend/docker-manager/pull/53) Implement start command

[[#52]](https://github.com/MitchellBerend/docker-manager/pull/52) Add a identity file flag option
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ to support all docker commands on remote nodes.
| LOGS | |
| PS | |
| RESTART | |
| RM | |
| START | |
| STOP | |
| SYSTEM | |
Expand Down
13 changes: 13 additions & 0 deletions src/cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ pub enum Command {
container_id: String,
},

///Remove one or more containers
Rm {
container_id: String,

/// Force the removal of a running container (uses SIGKILL)
#[arg(short, long)]
force: bool,

/// Remove anonymous volumes associated with the container
#[arg(short, long)]
volumes: bool,
},

/// Starts a given container unless 2 or more containers are found on remote nodes
Start {
/// Container name or id
Expand Down
25 changes: 25 additions & 0 deletions src/cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,28 @@ impl<'a> PsFlags<'a> {
v
}
}

pub struct RmFlags {
pub force: bool,
pub volume: bool,
}

impl RmFlags {
pub fn new(force: bool, volume: bool) -> Self {
Self { force, volume }
}

pub fn flags(&self) -> Vec<&'static str> {
let mut v: Vec<&str> = vec![];

if self.force {
v.push("-f")
}

if self.volume {
v.push("-v")
}

v
}
}
13 changes: 12 additions & 1 deletion src/client/connector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

use crate::cli::flags::{ExecFlags, ImagesFlags, LogsFlags, PsFlags};
use crate::cli::flags::{ExecFlags, ImagesFlags, LogsFlags, PsFlags, RmFlags};
use crate::cli::Command;
use crate::utility::command;

Expand Down Expand Up @@ -171,6 +171,17 @@ impl Node {
Err(e) => Err(NodeError::SessionError(self.address.clone(), e)),
}
}
Command::Rm {
container_id,
force,
volumes,
} => {
let flags = RmFlags::new(force, volumes);
match command::run_rm(&self.address, session, sudo, &container_id, flags).await {
Ok(result) => Ok(result),
Err(e) => Err(NodeError::SessionError(self.address.clone(), e)),
}
}
Command::Start { container_id } => {
match command::run_start(&self.address, session, sudo, &container_id).await {
Ok(result) => Ok(result),
Expand Down
41 changes: 40 additions & 1 deletion src/utility/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::flags::{ExecFlags, ImagesFlags, LogsFlags, PsFlags};
use crate::cli::flags::{ExecFlags, ImagesFlags, LogsFlags, PsFlags, RmFlags};
use crate::cli::{System, SystemCommand};

pub async fn run_exec(
Expand Down Expand Up @@ -248,6 +248,45 @@ pub async fn run_restart(
Ok(rv)
}

pub async fn run_rm(
hostname: &str,
session: openssh::Session,
sudo: bool,
container_id: &str,
flags: RmFlags,
) -> Result<String, openssh::Error> {
let mut command = vec!["rm", container_id];

for flag in flags.flags() {
command.push(flag)
}

let _output = match sudo {
true => {
session
.command("sudo")
.arg("docker")
.args(command)
.output()
.await
}
false => session.command("docker").args(command).output().await,
};

let output = match _output {
Ok(output) => output,
Err(e) => return Err(e),
};

let mut rv: String = format!("{}\n", hostname);
match output.status.code().unwrap() {
0 => rv.push_str(std::str::from_utf8(&output.stdout).unwrap_or("")),
_ => rv.push_str(std::str::from_utf8(&output.stderr).unwrap_or("")),
};

Ok(rv)
}

pub async fn run_start(
hostname: &str,
session: openssh::Session,
Expand Down
41 changes: 41 additions & 0 deletions src/utility/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,47 @@ pub async fn run_command(
}
}
}
Command::Rm {
container_id,
force,
volumes,
} => {
let node_containers: Vec<(String, String)> =
find_container(client, &container_id, sudo, true, identity_file).await;

match node_containers.len() {
0 => {
vec![Err(CommandError::NoNodesFound(container_id))]
}
1 => {
// unwrap is safe here since we .unwrap()check if there is exactly 1 element
let node_tuple = node_containers.get(0).unwrap().to_owned();
let node = Node::new(node_tuple.1);
match node
.run_command(
Command::Rm {
container_id,
force,
volumes,
},
sudo,
identity_file,
)
.await
{
Ok(s) => vec![Ok(s)],
Err(e) => vec![Err(CommandError::NodeError(e))],
}
}
_ => {
let nodes = node_containers
.iter()
.map(|(_, result)| result.clone())
.collect::<Vec<String>>();
vec![Err(CommandError::MutlipleNodesFound(nodes))]
}
}
}
Command::Start { container_id } => {
let node_containers: Vec<(String, String)> =
find_container(client, &container_id, sudo, true, identity_file).await;
Expand Down