main
阳光少年 1 year ago
parent 8d8939624b
commit b3fad40c4b

82
Cargo.lock generated

@ -115,6 +115,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.15" version = "0.2.15"
@ -126,6 +132,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -133,16 +145,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "is_terminal_polyfill" name = "indexmap"
version = "1.70.1" version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]] [[package]]
name = "itoa" name = "is_terminal_polyfill"
version = "1.0.11" version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]] [[package]]
name = "libc" name = "libc"
@ -193,17 +209,10 @@ dependencies = [
"clap", "clap",
"nix", "nix",
"serde", "serde",
"serde_json",
"toml", "toml",
"uuid", "uuid",
] ]
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.204" version = "1.0.204"
@ -225,14 +234,11 @@ dependencies = [
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_spanned"
version = "1.0.121" version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ab380d7d9f22ef3f21ad3e6c1ebe8e4fc7a2000ccba2e4d71fc96f15b2cb609" checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
dependencies = [ dependencies = [
"itoa",
"memchr",
"ryu",
"serde", "serde",
] ]
@ -255,11 +261,36 @@ dependencies = [
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.11" version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" checksum = "81967dd0dd2c1ab0bc3468bd7caecc32b8a4aa47d0c8c695d8c2b2108168d62c"
dependencies = [ dependencies = [
"serde", "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]] [[package]]
@ -361,3 +392,12 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.6.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b480ae9340fc261e6be3e95a1ba86d54ae3f9171132a73ce8d4bbaf68339507c"
dependencies = [
"memchr",
]

@ -5,8 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
uuid = {version = "1.3", features = ["v4"]} uuid = {version = "1.3", features = ["v4"]}
toml = "0.5" toml = "0.8.8"
clap = {version = "4.5.0", features = ["derive"]} clap = {version = "4.5.0", features = ["derive"]}
nix = {version = "0.29", features = ["sched", "process", "hostname", "mount", "fs", "env", "user", "term"]} nix = {version = "0.29", features = ["sched", "process", "hostname", "mount", "fs", "env", "user", "term"]}
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.8"

@ -1,5 +1,6 @@
use std::{error::Error, fmt::{Debug, Display, Formatter}}; use std::{error::Error, fmt::{Debug, Display, Formatter}};
use nix::errno::Errno; use nix::errno::Errno;
use toml;
pub type Result<T> = std::result::Result<T, RockerError>; pub type Result<T> = std::result::Result<T, RockerError>;
@ -7,7 +8,9 @@ pub type Result<T> = std::result::Result<T, RockerError>;
pub enum RockerError { pub enum RockerError {
IoError(std::io::Error), IoError(std::io::Error),
OtherError(String), OtherError(String),
ErrnoError(Errno) ErrnoError(Errno),
TomlSerError(toml::ser::Error),
TomlDeError(toml::de::Error)
} }
@ -17,7 +20,9 @@ impl Display for RockerError {
match self { match self {
Self::IoError(e) => write!(f, "{}", e), Self::IoError(e) => write!(f, "{}", e),
Self::OtherError(s) => write!(f, "{}", s), 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 { match self {
Self::IoError(e) => Some(e), Self::IoError(e) => Some(e),
Self::OtherError(_) => None, 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<Errno> for RockerError {
Self::OtherError(format!("errno: {err}")) Self::OtherError(format!("errno: {err}"))
} }
} }
impl From<toml::ser::Error> for RockerError {
fn from(err: toml::ser::Error) -> Self {
Self::TomlSerError(err)
}
}
impl From<toml::de::Error> for RockerError {
fn from(err: toml::de::Error) -> Self {
Self::TomlDeError(err)
}
}

@ -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::unistd::{chdir, chroot, dup2, execv, pivot_root, setuid, sleep, Gid, Pid, Uid, User, setgroups};
use nix::mount::{mount, MntFlags, MsFlags, umount2, umount}; use nix::mount::{mount, MntFlags, MsFlags, umount2, umount};
use nix::env::clearenv; use nix::env::clearenv;
use nix::pty;
use serde::Deserialize;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use clap::Parser; use clap::Parser;
use error::{Result, RockerError}; use error::{Result, RockerError};
@ -18,7 +16,7 @@ mod error;
use uuid; use uuid;
use std::{io, fs}; use std::{io, fs};
use toml; use toml;
use serde::Deserialize; use serde::{Deserialize, Serialize};
static WORKSPACE: &str = "/root/rocker"; static WORKSPACE: &str = "/root/rocker";
static USER_NAME: &str = "rocker"; static USER_NAME: &str = "rocker";
@ -38,6 +36,14 @@ struct RockerArgs {
log: bool, log: bool,
#[arg(long)] #[arg(long)]
wait: bool, wait: bool,
// --logs container_id
#[arg(long)]
logs: Option<String>,
// --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) Ok(_container_id)
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Debug)]
struct ContainerInfo { struct ContainerInfo {
id: String, id: String,
run: String,
image: String, image: String,
volume: String, volume: Option<String>,
env: Option<String>,
} }
fn save_container_info(container_id: &String) -> Result<()> { fn save_container_info(args: &RockerArgs, container_id: &String) -> Result<()> {
let container_info_path = Path::new(WORKSPACE).join("containers").join(container_id).join("container.json"); let container_info_path = Path::new(WORKSPACE).join("containers").join(container_id).join("info.toml");
let container_info = ContainerInfo { let container_info = ContainerInfo {
id: container_id.to_string(), id: container_id.clone(),
image: "".to_string(), run: args.run.as_ref().unwrap().clone(),
volume: "".to_string(), image: args.image.as_ref().unwrap().clone(),
volume: None,
env: None
}; };
let toml_str = toml::to_string(&container_info)?; 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(()) Ok(())
} }
fn main() -> Result<()>{ fn main() -> Result<()>{
let args = RockerArgs::parse(); let args = RockerArgs::parse();
// run
if let (Some(cmd), Some(image_name)) = (&args.run, &args.image) { if let (Some(cmd), Some(image_name)) = (&args.run, &args.image) {
let volume_path = extend_image(image_name)?; let volume_path = extend_image(image_name)?;
let container_id = run_container(cmd, args.wait, args.log, &volume_path)?; 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(()) Ok(())
} }
Loading…
Cancel
Save