generated from krisnova/rust-nova
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcells.rs
161 lines (138 loc) · 5.09 KB
/
cells.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -------------------------------------------------------------------------- *\
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* -------------------------------------------------------------------------- *
* Copyright 2022 - 2024, the aurae contributors *
* SPDX-License-Identifier: Apache-2.0 *
\* -------------------------------------------------------------------------- */
#![allow(unused)]
use proto::cells::{
Cell, CellServiceAllocateRequest, CellServiceStartRequest, Executable,
};
fn generate_cell_name(parent_name: Option<&str>) -> String {
if let Some(parent_name) = parent_name {
format!("{parent_name}/ae-e2e-{}", uuid::Uuid::new_v4())
} else {
format!("ae-e2e-{}", uuid::Uuid::new_v4())
}
}
struct CellBuilder {
parent: Option<String>,
isolate_process: bool,
}
impl CellBuilder {
pub fn new() -> Self {
Self { parent: None, isolate_process: false }
}
pub fn parent_cell_name(&mut self, parent_cell_name: String) -> &mut Self {
self.parent = Some(parent_cell_name);
self
}
pub fn isolate_process(&mut self) -> &mut Self {
self.isolate_process = true;
self
}
pub fn build(&self) -> Cell {
let cell_name = generate_cell_name(self.parent.as_deref());
Cell {
name: cell_name,
cpu: None,
cpuset: None,
memory: None,
isolate_network: false,
isolate_process: self.isolate_process,
}
}
}
pub struct CellServiceAllocateRequestBuilder {
cell_builder: CellBuilder,
}
impl CellServiceAllocateRequestBuilder {
pub fn new() -> Self {
Self { cell_builder: CellBuilder::new() }
}
pub fn parent_cell_name(&mut self, parent_cell_name: String) -> &mut Self {
let _ = self.cell_builder.parent_cell_name(parent_cell_name);
self
}
pub fn isolate_process(&mut self) -> &mut Self {
let _ = self.cell_builder.isolate_process();
self
}
pub fn build(&self) -> CellServiceAllocateRequest {
CellServiceAllocateRequest { cell: Some(self.cell_builder.build()) }
}
}
struct ExecutableBuilder {
name: String,
command: String,
description: String,
}
impl ExecutableBuilder {
pub fn new() -> Self {
Self {
name: format!("ae-sleeper-{}", uuid::Uuid::new_v4()),
command: "sleep 400".to_string(),
description: String::from("description"),
}
}
pub fn executable_name(&mut self, name: String) -> &mut Self {
self.name = name;
self
}
pub fn build(&self) -> Executable {
Executable {
name: self.name.clone(),
command: self.command.clone(),
description: self.description.clone(),
}
}
}
pub(crate) struct CellServiceStartRequestBuilder {
cell_name: Option<String>,
executable_builder: ExecutableBuilder,
uid: Option<u32>,
gid: Option<u32>,
}
impl CellServiceStartRequestBuilder {
pub fn new() -> Self {
Self {
cell_name: None,
executable_builder: ExecutableBuilder::new(),
uid: None,
gid: None,
}
}
pub fn cell_name(&mut self, cell_name: String) -> &mut Self {
self.cell_name = Some(cell_name);
self
}
pub fn executable_name(&mut self, name: String) -> &mut Self {
let _ = self.executable_builder.executable_name(name);
self
}
pub fn uid(&mut self, uid: u32) -> &mut Self {
self.uid = Some(uid);
self
}
pub fn gid(&mut self, gid: u32) -> &mut Self {
self.gid = Some(gid);
self
}
pub fn build(&self) -> CellServiceStartRequest {
assert!(self.cell_name.is_some(), "cell_name needs to be set");
CellServiceStartRequest {
cell_name: self.cell_name.clone(),
executable: Some(self.executable_builder.build()),
uid: self.uid,
gid: self.gid,
}
}
}