This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
71 lines (59 loc) · 2.13 KB
/
main.rs
File metadata and controls
71 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use chrono;
use std::{env, fs, path::Path};
use tiny_ce::{create, delete, start, CONTAINER_ROOT_PATH};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() == 3 && args[1] == "cs" {
create_start_centos(&args[2]);
return;
}
if args.len() == 2 && args[1] == "del" {
delete_all();
return;
}
let help = [
"Usage: tiny-ce [command]",
"",
"Commands:",
" cs <bundle-path> 创建并启动一个容器",
" del 删除所有容器",
"",
"Example: 在 test/centos 下制作容器的 rootfs 并启动",
" $ cd ./test/centos && mkdir rootfs",
" $ docker export $(docker create centos:centos7.9.2009) | tar -C rootfs -xvf -",
" $ tiny-ce cs ./",
]
.join("\n");
println!("{}", help);
}
/// 创建并启动一个容器
fn create_start_centos(bundle: &String) {
let datetime = chrono::offset::Local::now();
let container_id = format!("container-centos-{}", datetime.format("%m%d-%H%M%S"));
let container_path = format!("{}/{}", CONTAINER_ROOT_PATH, container_id);
println!("启动和创建容器:");
println!(" · bundle = {}", bundle);
println!(" · id = {}", container_id);
println!(" · 容器临时文件路径 = {}", container_path);
println!();
println!("创建中...");
create(&container_id, Path::new(bundle.as_str()));
println!("启动中...");
start(&container_id);
}
/// 删除所有容器, 并打印被删除容器的 id.
fn delete_all() {
let container_path_list = fs::read_dir(CONTAINER_ROOT_PATH)
.expect(format!("无法访问 {} .", CONTAINER_ROOT_PATH).as_str());
let container_id_list: Vec<String> = container_path_list
.into_iter()
.map(|path| path.unwrap().path())
.filter(|path| path.is_dir())
.map(|path| path.file_name().unwrap().to_string_lossy().to_string())
.collect();
for id in container_id_list.iter() {
delete(id);
println!("id = {} 的容器被删除了.", id);
}
println!("已删除 {} 个容器.", container_id_list.len());
}