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

Ensure youki runs under podman #613

Merged
merged 1 commit into from
Jan 17, 2022
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
7 changes: 7 additions & 0 deletions crates/libcgroups/src/systemd/dbus/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::time::Duration;
pub trait SystemdClient {
fn is_system(&self) -> bool;

fn transient_unit_exists(&self, unit_name: &str) -> bool;

fn start_transient_unit(
&self,
container_name: &str,
Expand Down Expand Up @@ -67,6 +69,11 @@ impl SystemdClient for Client {
self.system
}

fn transient_unit_exists(&self, unit_name: &str) -> bool {
let proxy = self.create_proxy();
proxy.get_unit(unit_name).is_ok()
}

/// start_transient_unit is a higher level API for starting a unit
/// for a specific container under systemd.
/// See https://www.freedesktop.org/wiki/Software/systemd/dbus for more details.
Expand Down
16 changes: 11 additions & 5 deletions crates/libcgroups/src/systemd/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,13 @@ impl CgroupManager for Manager {

fn remove(&self) -> Result<()> {
log::debug!("remove {}", self.unit_name);
self.client
.stop_transient_unit(&self.unit_name)
.with_context(|| {
format!("could not remove control group {}", self.destructured_path)
})?;
if self.client.transient_unit_exists(&self.unit_name) {
self.client
.stop_transient_unit(&self.unit_name)
.with_context(|| {
format!("could not remove control group {}", self.destructured_path)
})?;
}

Ok(())
}
Expand Down Expand Up @@ -405,6 +407,10 @@ mod tests {
true
}

fn transient_unit_exists(&self, _: &str) -> bool {
true
}

fn start_transient_unit(
&self,
_container_name: &str,
Expand Down
6 changes: 5 additions & 1 deletion crates/youki/src/commands/delete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use crate::commands::load_container;
use crate::commands::{container_exists, load_container};
use anyhow::{Context, Result};
use std::path::PathBuf;

use liboci_cli::Delete;

pub fn delete(args: Delete, root_path: PathBuf) -> Result<()> {
utam0k marked this conversation as resolved.
Show resolved Hide resolved
log::debug!("start deleting {}", args.container_id);
if !container_exists(&root_path, &args.container_id)? && args.force {
return Ok(());
}

let mut container = load_container(root_path, &args.container_id)?;
container
.delete(args.force)
Expand Down
27 changes: 22 additions & 5 deletions crates/youki/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use anyhow::{bail, Context, Result};
use std::{fs, path::Path};
use std::{
fs,
path::{Path, PathBuf},
};

use libcgroups::common::CgroupManager;
use libcontainer::container::Container;
Expand All @@ -21,12 +24,21 @@ pub mod start;
pub mod state;
pub mod update;

fn load_container<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<Container> {
fn construct_container_root<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<PathBuf> {
// resolves relative paths, symbolic links etc. and get complete path
let root_path = fs::canonicalize(&root_path)
.with_context(|| format!("failed to canonicalize {}", root_path.as_ref().display()))?;
let root_path = fs::canonicalize(&root_path).with_context(|| {
format!(
"failed to canonicalize {} for container {}",
root_path.as_ref().display(),
container_id
)
})?;
// the state of the container is stored in a directory named after the container id
let container_root = root_path.join(container_id);
Ok(root_path.join(container_id))
}

fn load_container<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<Container> {
let container_root = construct_container_root(root_path, container_id)?;
if !container_root.exists() {
bail!("container {} does not exist.", container_id)
}
Expand All @@ -35,6 +47,11 @@ fn load_container<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<Co
.with_context(|| format!("could not load state for container {}", container_id))
}

fn container_exists<P: AsRef<Path>>(root_path: P, container_id: &str) -> Result<bool> {
let container_root = construct_container_root(root_path, container_id)?;
Ok(container_root.exists())
}

fn create_cgroup_manager<P: AsRef<Path>>(
root_path: P,
container_id: &str,
Expand Down