From 5047c229c74489d7afec5c32f08a05e09be18f82 Mon Sep 17 00:00:00 2001 From: utam0k Date: Mon, 1 Nov 2021 13:39:35 +0900 Subject: [PATCH] adapt to the delte command. --- crates/libcontainer/src/config.rs | 29 +++++++++++++------ .../src/container/container_delete.rs | 20 +++++-------- .../src/container/init_builder.rs | 5 ++-- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/crates/libcontainer/src/config.rs b/crates/libcontainer/src/config.rs index c87e7af3..eea0287e 100644 --- a/crates/libcontainer/src/config.rs +++ b/crates/libcontainer/src/config.rs @@ -1,25 +1,36 @@ -use std::{fs, path::Path}; +use std::{ + fs, + path::{Path, PathBuf}, +}; -use anyhow::Result; +use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; use oci_spec::runtime::{Hooks, Spec}; +use crate::utils; + // TODO: comments and examples -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct YoukiConfig { pub hooks: Option, + pub cgroup_path: PathBuf, } -impl<'a> From<&'a Spec> for YoukiConfig { - fn from(spec: &'a Spec) -> Self { - YoukiConfig { +impl<'a> YoukiConfig { + pub fn from_spec(spec: &'a Spec, container_id: &str) -> Result { + Ok(YoukiConfig { hooks: spec.hooks().clone(), - } + cgroup_path: utils::get_cgroup_path( + spec.linux() + .as_ref() + .context("no linux in spec")? + .cgroups_path(), + container_id, + ), + }) } -} -impl YoukiConfig { pub fn save>(&self, path: P) -> Result<()> { let file = fs::File::create(path.as_ref())?; serde_json::to_writer(&file, self)?; diff --git a/crates/libcontainer/src/container/container_delete.rs b/crates/libcontainer/src/container/container_delete.rs index c8cac31e..a78042e9 100644 --- a/crates/libcontainer/src/container/container_delete.rs +++ b/crates/libcontainer/src/container/container_delete.rs @@ -1,4 +1,5 @@ use super::{Container, ContainerStatus}; +use crate::config::YoukiConfig; use crate::hooks; use crate::utils; use anyhow::{bail, Context, Result}; @@ -36,10 +37,11 @@ impl Container { log::debug!("container status: {:?}", self.status()); if self.can_delete() { if self.root.exists() { - let spec = self.spec().with_context(|| { - format!("failed to load runtime spec for container {}", self.id()) - })?; - log::debug!("spec: {:?}", spec); + let config = + YoukiConfig::load(self.root.join("config.json")).with_context(|| { + format!("failed to load runtime spec for container {}", self.id()) + })?; + log::debug!("config: {:?}", config); // remove the directory storing container state log::debug!("remove dir {:?}", self.root); @@ -47,13 +49,7 @@ impl Container { format!("failed to remove container dir {}", self.root.display()) })?; - let cgroups_path = utils::get_cgroup_path( - spec.linux() - .as_ref() - .context("no linux in spec")? - .cgroups_path(), - self.id(), - ); + let cgroups_path = utils::get_cgroup_path(&Some(config.cgroup_path), self.id()); // remove the cgroup created for the container // check https://man7.org/linux/man-pages/man7/cgroups.7.html @@ -68,7 +64,7 @@ impl Container { format!("failed to remove cgroup {}", cgroups_path.display()) })?; - if let Some(hooks) = spec.hooks() { + if let Some(hooks) = config.hooks.as_ref() { hooks::run_hooks(hooks.poststop().as_ref(), Some(self)) .with_context(|| "failed to run post stop hooks")?; } diff --git a/crates/libcontainer/src/container/init_builder.rs b/crates/libcontainer/src/container/init_builder.rs index 1c61f591..095d430e 100644 --- a/crates/libcontainer/src/container/init_builder.rs +++ b/crates/libcontainer/src/container/init_builder.rs @@ -41,14 +41,15 @@ impl<'a> InitContainerBuilder<'a> { pub fn build(self) -> Result { let spec = self.load_spec()?; let container_dir = self.create_container_dir()?; - let config: YoukiConfig = (&spec).into(); - config.save(&container_dir.join("config.json"))?; let mut container = self.create_container_state(&container_dir)?; container .set_systemd(self.use_systemd) .set_annotations(spec.annotations().clone()); + let config = YoukiConfig::from_spec(&spec, container.id())?; + config.save(&container_dir.join("config.json"))?; + unistd::chdir(&container_dir)?; let notify_path = container_dir.join(NOTIFY_FILE); // convert path of root file system of the container to absolute path