1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-05-30 19:36:09 +02:00
youki/crates/container/src/container/container.rs

349 lines
9.8 KiB
Rust
Raw Normal View History

2021-08-14 09:20:58 +02:00
use std::collections::HashMap;
2021-06-23 18:10:54 +02:00
use std::ffi::OsString;
2021-03-27 12:08:13 +01:00
use std::fs;
use std::path::{Path, PathBuf};
2021-03-27 12:08:13 +01:00
use anyhow::Result;
2021-06-23 18:10:54 +02:00
use chrono::DateTime;
2021-03-27 12:08:13 +01:00
use nix::unistd::Pid;
2021-06-23 18:10:54 +02:00
use chrono::Utc;
use oci_spec::runtime::Spec;
2021-03-27 12:08:13 +01:00
use procfs::process::Process;
2021-07-22 15:17:28 +02:00
use crate::syscall::syscall::create_syscall;
2021-06-23 18:10:54 +02:00
2021-03-27 12:08:13 +01:00
use crate::container::{ContainerStatus, State};
2021-06-08 16:24:37 +02:00
/// Structure representing the container data
#[derive(Debug, Clone)]
2021-03-27 12:08:13 +01:00
pub struct Container {
2021-06-08 16:24:37 +02:00
// State of the container
2021-03-27 12:08:13 +01:00
pub state: State,
2021-06-08 16:24:37 +02:00
// indicated the directory for the root path in the container
2021-03-27 12:08:13 +01:00
pub root: PathBuf,
}
impl Default for Container {
fn default() -> Self {
Self {
state: State::default(),
root: PathBuf::from("/run/youki"),
}
}
}
2021-03-27 12:08:13 +01:00
impl Container {
pub fn new(
container_id: &str,
status: ContainerStatus,
pid: Option<i32>,
bundle: &Path,
container_root: &Path,
2021-03-27 12:08:13 +01:00
) -> Result<Self> {
let container_root = fs::canonicalize(container_root)?;
let state = State::new(container_id, status, pid, bundle.to_path_buf());
2021-03-27 12:08:13 +01:00
Ok(Self {
state,
root: container_root,
})
}
pub fn id(&self) -> &str {
2021-09-28 00:46:57 +02:00
&self.state.id
2021-03-27 12:08:13 +01:00
}
pub fn can_start(&self) -> bool {
self.state.status.can_start()
}
pub fn can_kill(&self) -> bool {
self.state.status.can_kill()
}
pub fn can_delete(&self) -> bool {
self.state.status.can_delete()
}
2021-07-15 01:09:04 +02:00
pub fn can_exec(&self) -> bool {
self.state.status == ContainerStatus::Running
}
2021-07-17 18:25:10 +02:00
pub fn can_pause(&self) -> bool {
self.state.status.can_pause()
}
pub fn can_resume(&self) -> bool {
self.state.status.can_resume()
}
2021-09-23 23:05:35 +02:00
pub fn bundle(&self) -> &PathBuf {
&self.state.bundle
}
pub fn set_annotations(&mut self, annotations: Option<HashMap<String, String>>) -> &mut Self {
self.state.annotations = annotations;
self
}
2021-03-27 12:08:13 +01:00
pub fn pid(&self) -> Option<Pid> {
self.state.pid.map(Pid::from_raw)
}
2021-09-23 23:05:35 +02:00
pub fn set_pid(&mut self, pid: i32) -> &mut Self {
self.state.pid = Some(pid);
self
2021-06-23 18:10:54 +02:00
}
pub fn created(&self) -> Option<DateTime<Utc>> {
self.state.created
}
pub fn creator(&self) -> Option<OsString> {
if let Some(uid) = self.state.creator {
2021-07-01 22:24:06 +02:00
let command = create_syscall();
2021-06-23 18:10:54 +02:00
let user_name = command.get_pwuid(uid);
if let Some(user_name) = user_name {
return Some((&*user_name).to_owned());
}
}
None
}
2021-09-23 23:05:35 +02:00
pub fn set_creator(&mut self, uid: u32) -> &mut Self {
self.state.creator = Some(uid);
self
2021-07-15 01:09:04 +02:00
}
2021-09-23 23:05:35 +02:00
pub fn systemd(&self) -> Option<bool> {
self.state.use_systemd
2021-07-15 01:09:04 +02:00
}
2021-09-23 23:05:35 +02:00
pub fn set_systemd(&mut self, should_use: bool) -> &mut Self {
self.state.use_systemd = Some(should_use);
2021-08-14 09:13:40 +02:00
self
}
2021-09-23 23:05:35 +02:00
pub fn status(&self) -> ContainerStatus {
self.state.status
2021-07-15 01:09:04 +02:00
}
2021-09-23 23:05:35 +02:00
pub fn set_status(&mut self, status: ContainerStatus) -> &mut Self {
2021-06-23 18:10:54 +02:00
let created = match (status, self.state.created) {
(ContainerStatus::Created, None) => Some(Utc::now()),
_ => self.state.created,
};
2021-09-23 23:05:35 +02:00
self.state.created = created;
self.state.status = status;
2021-06-23 18:10:54 +02:00
2021-09-23 23:05:35 +02:00
self
}
pub fn refresh_status(&mut self) -> Result<()> {
let new_status = match self.pid() {
Some(pid) => {
// Note that Process::new does not spawn a new process
// but instead creates a new Process structure, and fill
// it with information about the process with given pid
if let Ok(proc) = Process::new(pid.as_raw()) {
use procfs::process::ProcState;
2021-10-10 05:35:49 +02:00
match proc.stat.state()? {
2021-09-23 23:05:35 +02:00
ProcState::Zombie | ProcState::Dead => ContainerStatus::Stopped,
_ => match self.status() {
ContainerStatus::Creating
| ContainerStatus::Created
| ContainerStatus::Paused => self.status(),
_ => ContainerStatus::Running,
},
}
} else {
ContainerStatus::Stopped
}
}
None => ContainerStatus::Stopped,
};
self.set_status(new_status);
Ok(())
}
pub fn refresh_state(&mut self) -> Result<&mut Self> {
let state = State::load(&self.root)?;
self.state = state;
Ok(self)
2021-03-27 12:08:13 +01:00
}
pub fn load(container_root: PathBuf) -> Result<Self> {
let state = State::load(&container_root)?;
2021-09-23 23:05:35 +02:00
let mut container = Self {
2021-03-27 12:08:13 +01:00
state,
root: container_root,
2021-09-23 23:05:35 +02:00
};
container.refresh_status()?;
Ok(container)
}
pub fn save(&self) -> Result<()> {
log::debug!("Save container status: {:?} in {:?}", self, self.root);
self.state.save(&self.root)
2021-03-27 12:08:13 +01:00
}
2021-07-17 18:25:10 +02:00
pub fn spec(&self) -> Result<Spec> {
let spec = Spec::load(self.root.join("config.json"))?;
Ok(spec)
2021-07-17 18:25:10 +02:00
}
2021-03-27 12:08:13 +01:00
}
2021-09-02 16:04:35 +02:00
#[cfg(test)]
mod tests {
use super::*;
2021-10-09 14:57:01 +02:00
use crate::utils::create_temp_dir;
use serial_test::serial;
2021-09-02 16:04:35 +02:00
#[test]
fn test_get_set_pid() {
let mut container = Container::default();
assert_eq!(container.pid(), None);
2021-09-23 23:05:35 +02:00
container.set_pid(1);
2021-09-02 16:04:35 +02:00
assert_eq!(container.pid(), Some(Pid::from_raw(1)));
}
#[test]
2021-10-10 05:35:49 +02:00
fn test_basic_getter() -> Result<()> {
2021-10-09 15:09:15 +02:00
let mut container = Container::new(
"container_id",
2021-10-09 15:09:15 +02:00
ContainerStatus::Creating,
2021-09-02 16:04:35 +02:00
None,
&PathBuf::from("."),
&PathBuf::from("."),
2021-10-10 05:35:49 +02:00
)?;
2021-09-02 16:04:35 +02:00
2021-10-09 15:09:15 +02:00
// testing id
assert_eq!(container.id(), "container_id");
2021-10-09 15:09:15 +02:00
// testing bundle path
2021-09-02 16:04:35 +02:00
assert_eq!(container.bundle(), &PathBuf::from("."));
2021-10-09 15:09:15 +02:00
// testing root path
2021-10-10 05:35:49 +02:00
assert_eq!(container.root, fs::canonicalize(PathBuf::from("."))?);
2021-10-09 15:09:15 +02:00
// testing created
assert_eq!(container.created(), None);
container.set_status(ContainerStatus::Created);
assert!(container.created().is_some());
2021-10-10 05:35:49 +02:00
Ok(())
2021-09-02 16:04:35 +02:00
}
2021-10-09 10:07:18 +02:00
#[test]
fn test_set_annotations() {
let mut container = Container::default();
assert_eq!(container.state.annotations, None);
let mut annotations = std::collections::HashMap::with_capacity(1);
annotations.insert(
"org.criu.config".to_string(),
"/etc/special-youki-criu-options".to_string(),
);
container.set_annotations(Some(annotations.clone()));
assert_eq!(container.state.annotations, Some(annotations));
}
2021-10-09 10:18:05 +02:00
#[test]
fn test_get_set_systemd() {
let mut container = Container::default();
assert_eq!(container.systemd(), None);
container.set_systemd(true);
assert_eq!(container.systemd(), Some(true));
container.set_systemd(false);
assert_eq!(container.systemd(), Some(false));
}
2021-10-09 11:09:39 +02:00
#[test]
fn test_get_set_creator() {
let mut container = Container::default();
assert_eq!(container.creator(), None);
container.set_creator(1000);
assert_eq!(container.creator(), Some(OsString::from("youki")));
}
2021-10-09 14:57:01 +02:00
#[test]
#[serial]
2021-10-10 05:35:49 +02:00
fn test_refresh_load_save_state() -> Result<()> {
let tmp_dir = create_temp_dir("test_refresh_load_save_state")?;
2021-10-09 14:57:01 +02:00
let mut container_1 = Container::new(
"container_id_1",
ContainerStatus::Created,
None,
&PathBuf::from("."),
tmp_dir.path(),
2021-10-10 05:35:49 +02:00
)?;
2021-10-09 14:57:01 +02:00
2021-10-10 05:35:49 +02:00
container_1.save()?;
let container_2 = Container::load(tmp_dir.path().to_path_buf())?;
2021-10-09 14:57:01 +02:00
assert_eq!(container_1.state.id, container_2.state.id);
2021-10-09 15:09:15 +02:00
assert_eq!(container_2.state.status, ContainerStatus::Stopped);
2021-10-09 14:57:01 +02:00
container_1.state.id = "container_id_1_modified".to_string();
2021-10-10 05:35:49 +02:00
container_1.save()?;
container_1.refresh_state()?;
2021-10-09 15:09:15 +02:00
assert_eq!(container_1.state.id, "container_id_1_modified".to_string());
2021-10-10 05:35:49 +02:00
Ok(())
2021-10-09 14:57:01 +02:00
}
2021-10-09 15:27:44 +02:00
#[test]
#[serial]
2021-10-10 05:35:49 +02:00
fn test_get_spec() -> Result<()> {
let tmp_dir = create_temp_dir("test_get_spec")?;
2021-10-09 15:27:44 +02:00
use oci_spec::runtime::Spec;
let spec = Spec::default();
2021-10-10 05:35:49 +02:00
spec.save(tmp_dir.path().join("config.json"))?;
2021-10-09 15:27:44 +02:00
let container = Container {
root: tmp_dir.path().to_path_buf(),
..Default::default()
};
2021-10-10 05:35:49 +02:00
container.spec()?;
Ok(())
2021-10-09 15:27:44 +02:00
}
2021-10-09 16:48:50 +02:00
#[test]
#[serial]
2021-10-10 05:35:49 +02:00
fn test_get_set_refresh_status() -> Result<()> {
2021-10-09 16:48:50 +02:00
// there already has a full and well-tested flow of status in state.rs
// so we just let the coverage run through those can_xxx functions.
let mut container = Container::default();
assert_eq!(container.status(), ContainerStatus::Creating);
assert!(!container.can_start());
assert!(!container.can_kill());
assert!(!container.can_delete());
assert!(!container.can_exec());
assert!(!container.can_pause());
assert!(!container.can_resume());
// no PID case
2021-10-10 05:35:49 +02:00
container.refresh_status()?;
2021-10-09 16:48:50 +02:00
assert_eq!(container.status(), ContainerStatus::Stopped);
// with PID case but PID not exists
container.set_pid(-1);
2021-10-10 05:35:49 +02:00
container.refresh_status()?;
2021-10-09 16:48:50 +02:00
assert_eq!(container.status(), ContainerStatus::Stopped);
// with PID case
container.set_pid(1);
2021-10-09 16:48:50 +02:00
container.set_status(ContainerStatus::Paused);
2021-10-10 05:35:49 +02:00
container.refresh_status()?;
2021-10-09 16:48:50 +02:00
assert_eq!(container.status(), ContainerStatus::Paused);
container.set_status(ContainerStatus::Running);
2021-10-10 05:35:49 +02:00
container.refresh_status()?;
2021-10-09 16:48:50 +02:00
assert_eq!(container.status(), ContainerStatus::Running);
2021-10-10 05:35:49 +02:00
Ok(())
2021-10-09 16:48:50 +02:00
}
2021-09-02 16:04:35 +02:00
}