mirror of
https://github.com/containers/youki
synced 2024-11-23 09:21:57 +01:00
adapt to the delte command.
This commit is contained in:
parent
d68cf57b8b
commit
5047c229c7
@ -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<Hooks>,
|
||||
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<Self> {
|
||||
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<P: AsRef<Path>>(&self, path: P) -> Result<()> {
|
||||
let file = fs::File::create(path.as_ref())?;
|
||||
serde_json::to_writer(&file, self)?;
|
||||
|
@ -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")?;
|
||||
}
|
||||
|
@ -41,14 +41,15 @@ impl<'a> InitContainerBuilder<'a> {
|
||||
pub fn build(self) -> Result<Container> {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user