From bdf8a57bdb7b35d67a2a73e8ff36afdc14eba186 Mon Sep 17 00:00:00 2001 From: Manabu Sugimoto Date: Wed, 24 Aug 2022 18:31:04 +0900 Subject: [PATCH] runk: Move delete logic to libcontainer Move delete logic to `libcontainer` crate to make the code clean like other commands. Fixes: #4975 Signed-off-by: Manabu Sugimoto --- src/tools/runk/libcontainer/src/container.rs | 60 +++++++++++++++++++- src/tools/runk/src/commands/delete.rs | 59 +------------------ 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/tools/runk/libcontainer/src/container.rs b/src/tools/runk/libcontainer/src/container.rs index 9a3c0fa61d..b90ee9f6f4 100644 --- a/src/tools/runk/libcontainer/src/container.rs +++ b/src/tools/runk/libcontainer/src/container.rs @@ -15,10 +15,10 @@ use nix::{ sys::signal::SIGKILL, unistd::{chdir, unlink, Pid}, }; -use oci::ContainerState; +use oci::{ContainerState, State as OCIState}; use procfs; use rustjail::{ - container::{BaseContainer, LinuxContainer, EXEC_FIFO_FILENAME}, + container::{self, BaseContainer, LinuxContainer, EXEC_FIFO_FILENAME}, process::{Process, ProcessOperations}, }; use scopeguard::defer; @@ -113,6 +113,62 @@ impl Container { Ok(()) } + pub async fn delete(&self, force: bool, logger: &Logger) -> Result<()> { + let status = &self.status; + let spec = status + .config + .spec + .as_ref() + .ok_or_else(|| anyhow!("spec config was not present in the status"))?; + + let oci_state = OCIState { + version: status.oci_version.clone(), + id: status.id.clone(), + status: self.state, + pid: status.pid, + bundle: status + .bundle + .to_str() + .ok_or_else(|| anyhow!("invalid bundle path"))? + .to_string(), + annotations: spec.annotations.clone(), + }; + + if spec.hooks.is_some() { + let hooks = spec + .hooks + .as_ref() + .ok_or_else(|| anyhow!("hooks config was not present"))?; + for h in hooks.poststop.iter() { + container::execute_hook(logger, h, &oci_state).await?; + } + } + + match oci_state.status { + ContainerState::Stopped => { + self.destroy()?; + } + ContainerState::Created => { + // Kill an init process + self.kill(SIGKILL, false)?; + self.destroy()?; + } + _ => { + if force { + self.kill(SIGKILL, true)?; + self.destroy()?; + } else { + return Err(anyhow!( + "cannot delete container {} that is not stopped", + &status.id + )); + } + } + } + + Ok(()) + } + pub fn pause(&self) -> Result<()> { if self.state != ContainerState::Running && self.state != ContainerState::Created { return Err(anyhow!( diff --git a/src/tools/runk/src/commands/delete.rs b/src/tools/runk/src/commands/delete.rs index ead8aa47eb..1eb6dfd5f0 100644 --- a/src/tools/runk/src/commands/delete.rs +++ b/src/tools/runk/src/commands/delete.rs @@ -6,13 +6,6 @@ use anyhow::{anyhow, Result}; use libcontainer::{container::Container, status::Status}; use liboci_cli::Delete; -use nix::{ - sys::signal::SIGKILL, - sys::signal::{kill, Signal}, - unistd::Pid, -}; -use oci::{ContainerState, State as OCIState}; -use rustjail::container; use slog::{info, Logger}; use std::{fs, path::Path}; @@ -29,57 +22,7 @@ pub async fn run(opts: Delete, root: &Path, logger: &Logger) -> Result<()> { fs::remove_dir_all(status_dir)?; return Ok(()); }; - - let status = &container.status; - let spec = status - .config - .spec - .as_ref() - .ok_or_else(|| anyhow!("spec config was not present in the status"))?; - - let oci_state = OCIState { - version: status.oci_version.clone(), - id: status.id.clone(), - status: container.state, - pid: status.pid, - bundle: status - .bundle - .to_str() - .ok_or_else(|| anyhow!("invalid bundle path"))? - .to_string(), - annotations: spec.annotations.clone(), - }; - - if spec.hooks.is_some() { - let hooks = spec - .hooks - .as_ref() - .ok_or_else(|| anyhow!("hooks config was not present"))?; - for h in hooks.poststop.iter() { - container::execute_hook(logger, h, &oci_state).await?; - } - } - - match oci_state.status { - ContainerState::Stopped => { - container.destroy()?; - } - ContainerState::Created => { - kill(Pid::from_raw(status.pid), Some(Signal::SIGKILL))?; - container.destroy()?; - } - _ => { - if opts.force { - container.kill(SIGKILL, true)?; - container.destroy()?; - } else { - return Err(anyhow!( - "cannot delete container {} that is not stopped", - container_id - )); - } - } - } + container.delete(opts.force, logger).await?; info!(&logger, "delete command finished successfully");