From b3fad40c4bba7a722b422b458f04d9f3129eed90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=B3=E5=85=89=E5=B0=91=E5=B9=B4?= <849317537@qq.com> Date: Tue, 30 Jul 2024 10:10:29 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 82 ++++++++++++++++++++++++++++++++++++++-------------- Cargo.toml | 3 +- src/error.rs | 26 +++++++++++++++-- src/main.rs | 58 +++++++++++++++++++++++++++++-------- 4 files changed, 131 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91ca9fe..b319ec5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "getrandom" version = "0.2.15" @@ -126,6 +132,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "heck" version = "0.5.0" @@ -133,16 +145,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] -name = "is_terminal_polyfill" -version = "1.70.1" +name = "indexmap" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] [[package]] -name = "itoa" -version = "1.0.11" +name = "is_terminal_polyfill" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "libc" @@ -193,17 +209,10 @@ dependencies = [ "clap", "nix", "serde", - "serde_json", "toml", "uuid", ] -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" - [[package]] name = "serde" version = "1.0.204" @@ -225,14 +234,11 @@ dependencies = [ ] [[package]] -name = "serde_json" -version = "1.0.121" +name = "serde_spanned" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ab380d7d9f22ef3f21ad3e6c1ebe8e4fc7a2000ccba2e4d71fc96f15b2cb609" +checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" dependencies = [ - "itoa", - "memchr", - "ryu", "serde", ] @@ -255,11 +261,36 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.11" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +checksum = "81967dd0dd2c1ab0bc3468bd7caecc32b8a4aa47d0c8c695d8c2b2108168d62c" dependencies = [ "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d9f8729f5aea9562aac1cc0441f5d6de3cff1ee0c5d67293eeca5eb36ee7c16" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", ] [[package]] @@ -361,3 +392,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.6.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b480ae9340fc261e6be3e95a1ba86d54ae3f9171132a73ce8d4bbaf68339507c" +dependencies = [ + "memchr", +] diff --git a/Cargo.toml b/Cargo.toml index 2a01296..148e7d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,7 @@ edition = "2021" [dependencies] uuid = {version = "1.3", features = ["v4"]} -toml = "0.5" +toml = "0.8.8" clap = {version = "4.5.0", features = ["derive"]} nix = {version = "0.29", features = ["sched", "process", "hostname", "mount", "fs", "env", "user", "term"]} serde = { version = "1.0", features = ["derive"] } -toml = "0.8" \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index 336ec7d..51eeb6f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,6 @@ use std::{error::Error, fmt::{Debug, Display, Formatter}}; use nix::errno::Errno; +use toml; pub type Result = std::result::Result; @@ -7,7 +8,9 @@ pub type Result = std::result::Result; pub enum RockerError { IoError(std::io::Error), OtherError(String), - ErrnoError(Errno) + ErrnoError(Errno), + TomlSerError(toml::ser::Error), + TomlDeError(toml::de::Error) } @@ -17,7 +20,9 @@ impl Display for RockerError { match self { Self::IoError(e) => write!(f, "{}", e), Self::OtherError(s) => write!(f, "{}", s), - Self::ErrnoError(e) => write!(f, "{}", e) + Self::ErrnoError(e) => write!(f, "{}", e), + Self::TomlSerError(e) => write!(f, "{}", e) + Self::TomlDeError(e) => write!(f, "{}", e) } } } @@ -27,7 +32,9 @@ impl Error for RockerError { match self { Self::IoError(e) => Some(e), Self::OtherError(_) => None, - Self::ErrnoError(e) => Some(e) + Self::ErrnoError(e) => Some(e), + Self::TomlSerError(e) => Some(e), + Self::TomlDeError(e) => Some(e) } } } @@ -43,3 +50,16 @@ impl From for RockerError { Self::OtherError(format!("errno: {err}")) } } + + +impl From for RockerError { + fn from(err: toml::ser::Error) -> Self { + Self::TomlSerError(err) + } +} + +impl From for RockerError { + fn from(err: toml::de::Error) -> Self { + Self::TomlDeError(err) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 10f210c..0ebbdf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,6 @@ use nix::sys::wait::{wait, waitpid, waitid, WaitPidFlag}; use nix::unistd::{chdir, chroot, dup2, execv, pivot_root, setuid, sleep, Gid, Pid, Uid, User, setgroups}; use nix::mount::{mount, MntFlags, MsFlags, umount2, umount}; use nix::env::clearenv; -use nix::pty; -use serde::Deserialize; use std::path::{Path, PathBuf}; use clap::Parser; use error::{Result, RockerError}; @@ -18,7 +16,7 @@ mod error; use uuid; use std::{io, fs}; use toml; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; static WORKSPACE: &str = "/root/rocker"; static USER_NAME: &str = "rocker"; @@ -38,6 +36,14 @@ struct RockerArgs { log: bool, #[arg(long)] wait: bool, + + // --logs container_id + #[arg(long)] + logs: Option, + + // --ps + #[arg(long)] + ps: bool, } @@ -279,33 +285,61 @@ fn run_container(cmd: &String, wait:bool, log:bool, volume_path: &PathBuf) -> Re Ok(_container_id) } -#[derive(Deserialize)] +#[derive(Deserialize, Serialize, Debug)] struct ContainerInfo { id: String, + run: String, image: String, - volume: String, + volume: Option, + env: Option, } -fn save_container_info(container_id: &String) -> Result<()> { - let container_info_path = Path::new(WORKSPACE).join("containers").join(container_id).join("container.json"); +fn save_container_info(args: &RockerArgs, container_id: &String) -> Result<()> { + let container_info_path = Path::new(WORKSPACE).join("containers").join(container_id).join("info.toml"); let container_info = ContainerInfo { - id: container_id.to_string(), - image: "".to_string(), - volume: "".to_string(), + id: container_id.clone(), + run: args.run.as_ref().unwrap().clone(), + image: args.image.as_ref().unwrap().clone(), + volume: None, + env: None }; let toml_str = toml::to_string(&container_info)?; - // println!("") + fs::write(container_info_path, toml_str)?; + Ok(()) +} +/// 读取所有容器的状态 +fn show_containers() -> Result<()> { + let containers_path = Path::new(WORKSPACE).join("containers"); + + for entry in fs::read_dir(containers_path)? { + let path = entry?.path(); + let info_path = path.join("info.toml"); + let info_str = fs::read_to_string(info_path)?; + let container_info: ContainerInfo = toml::from_str(&info_str)?; + println!("{container_info:?}"); + } Ok(()) } fn main() -> Result<()>{ let args = RockerArgs::parse(); + // run if let (Some(cmd), Some(image_name)) = (&args.run, &args.image) { let volume_path = extend_image(image_name)?; let container_id = run_container(cmd, args.wait, args.log, &volume_path)?; - + save_container_info(&args, &container_id)?; + } else if args.ps { + show_containers()? } + + // exec + + // logs + + // rm + + Ok(()) } \ No newline at end of file