This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_host_sys.rs
354 lines (324 loc) · 11.7 KB
/
prepare_host_sys.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
extern crate dagrs;
//extern crate sys_mount;
use dagrs::{EnvVar, Inputval, Retval, TaskTrait};
#[allow(unused)]
use libparted::{Device, Disk, FileSystemType, Partition, PartitionFlag, PartitionType};
#[allow(unused)]
use walkdir::WalkDir;
use cmd_lib::*;
use log::{error, info, warn};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::fs::File;
use std::process::Command;
use crate::utils::ProgramEndingFlag;
use crate::{utils, vars};
#[allow(unused)]
pub struct CleanOldConfig {}
impl TaskTrait for CleanOldConfig {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
//TODO: 删除环境变量配置
//卸载目录
//删除目录配置
//
Retval::empty()
}
}
impl utils::ProgramEndingFlag for CleanOldConfig {}
//TODO:添加全局重置脚本
#[allow(unused)]
pub struct ResetAllSystemConfig {}
impl TaskTrait for ResetAllSystemConfig {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
// reset bash profile
// reset target dirs
// reset system auto mount
//
Retval::empty()
}
}
pub struct PreparingSoftware {}
impl utils::ProgramEndingFlag for PreparingSoftware {}
impl PreparingSoftware {
/// 下载软件包和软件包补丁
// #[allow(unused)]
// fn download_packages(&self) {
// // let all_packages = &vars::ALL_PACKAGES.all_packages;
// // let all_patches = &vars::ALL_PACKAGES.package_patches;
// let all_packages = &vars::PACKAGES.package_info;
// let all_patches = &vars::PACKAGES.package_patches;
//
// let mut dls = Vec::new();
//
// for package in all_packages {
// dls.push(package.url.as_str());
// //TODO:!!!!!
// }
// for patch in all_patches {
// dls.push(patch.url.as_str());
// }
// info!("{:?}", dls);
// let status = utils::new_downlaod("./source".to_string(), &dls[..]);
// match status {
// Ok(v) => info!("ok"),
// Err(e) => error!("error {:?}", e),
// }
// }
fn preparing_base_software(&self) {
//软件包和补丁列表
// let all_packages = &vars::ALL_PACKAGES.all_packages;
// let patches = &vars::ALL_PACKAGES.package_patches;
let all_packages = &vars::PACKAGES.package_info;
let all_patches = &vars::PACKAGES.package_patches;
//软件包下载状态记录
let mut pack_status = HashMap::new();
for package in all_packages {
//
let mut try_download_times = 0;
//检查是否已经存在数据,原本存在就不需要下载
if utils::check_download_before(
&package.package_name,
&vars::BASE_CONFIG.path.package_build,
) {
continue;
}
while try_download_times < 5 {
utils::delete_failed_download_pack(
&package.package_name,
&vars::BASE_CONFIG.path.package_build,
);
match utils::download(
vars::BASE_CONFIG.path.package_build.clone(),
package.url.clone(),
) {
Ok(v) => {
match v {
true => {
//下载成功
try_download_times = 0;
pack_status.insert(&package.package_name, v);
break;
}
false => {
//下载失败,重试
try_download_times += 1;
continue;
}
}
}
Err(_e) => {
//下载命令执行失败,重试
//FIXME:下载命令执行失败应该print命令然后终止
try_download_times += 1;
continue;
}
}
}
if try_download_times == 5 {
pack_status.insert(&package.package_name, false);
}
}
//下载补丁
for patch in all_patches {
//重试次数
let mut try_download_times = 0;
//检查是否存在
//FIXME:目录不是补丁目录,需要调整
if utils::check_download_before(
&patch.patch_name,
&(vars::BASE_CONFIG.path.package_source.clone()
+ &vars::BASE_CONFIG.path.package_patches),
) {
continue;
}
while try_download_times < 5 {
//大于5次,下载失败
//TODO:调整成由var配置的部分
//并且添加对patches的存在性判断
match utils::download(
vars::BASE_CONFIG.path.package_source.clone()
+ &vars::BASE_CONFIG.path.package_patches,
patch.url.clone(),
) {
Ok(v) => match v {
true => {
try_download_times = 0;
pack_status.insert(&patch.patch_name, v);
break;
}
false => {
try_download_times += 1;
continue;
}
},
Err(_e) => {
try_download_times += 1;
continue;
}
}
}
if try_download_times == 5 {
pack_status.insert(&patch.patch_name, false);
}
}
for (package_name, download_status) in pack_status {
info!("{} : {}", package_name, download_status);
self.try_set_flag(download_status);
}
}
}
impl TaskTrait for PreparingSoftware {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
self.check_flag();
self.preparing_base_software();
//self.download_packages();
Retval::empty()
}
}
//备份环境变量配置文件
pub struct BackupBashProfile {}
impl utils::ProgramEndingFlag for BackupBashProfile {}
impl TaskTrait for BackupBashProfile {
fn run(&self, mut input: Inputval, _env: EnvVar) -> Retval {
self.check_flag();
match input.get::<bool>(0).unwrap() {
//无需备份返回true
true => Retval::new(true),
false => {
//需要备份返回false
let src_path = "/root/.bash_profile";
let target_path = "/root/.bash_profile.bak";
match fs::rename(src_path, target_path) {
Ok(_v) => {
info!("Back up bash profile success");
}
Err(e) => {
error!("Back up bash profile failed {}", e);
self.try_set_flag(false);
}
}
Retval::new(false)
}
}
}
}
// TODO 完全替换成rust实现
pub struct PreparingEnv {}
impl utils::ProgramEndingFlag for PreparingEnv {}
impl PreparingEnv {
#[allow(unused)]
fn prepare_env(&self) {
// let packages = &vars::HOST_PACKAGES;
// let mut cmd: String = vars::BASE_CONFIG.host_install_cmd.clone();
// for package in &packages.host_packages {
// cmd += &package;
// cmd += " ";
// }
// println!("{}", &cmd);
//
// let output = Command::new("bash")
// .arg("-c")
// .arg(cmd)
// .status()
// .expect("failed to execute process");
// assert!(output.success());
let mut link_list = HashMap::new();
link_list.insert("sh", "bash");
link_list.insert("yacc", "bison");
link_list.insert("awk", "gawk");
fs::remove_file("/usr/bin/sh").unwrap_or_else(|why| {
println!("! {:?}", why.kind());
});
fs::remove_file("/usr/bin/yacc").unwrap_or_else(|why| {
println!("! {:?}", why.kind());
});
fs::remove_file("/usr/bin/awk").unwrap_or_else(|why| {
println!("! {:?}", why.kind());
});
for link in &link_list {
// TODO: 换成unix::fs::symlink
let cmd = format!("sudo ln -s /usr/bin/{} /usr/bin/{}", &link.1, &link.0);
let create_link_res = Command::new("bash")
.arg("-c")
.arg(cmd)
.status()
.expect("failed to create link");
if !create_link_res.success() {
println!("failed to create link");
}
}
}
#[allow(unused)]
fn check() {}
}
//准备宿主机目标安装分区的目录
//FIXME:确保每一次运行的时候都能检查是否mount了分区
pub struct PreparingDirs {}
impl utils::ProgramEndingFlag for PreparingDirs {}
impl TaskTrait for PreparingDirs {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
self.check_flag();
let script_path = vars::BASE_CONFIG.scripts_path.root.clone()
+ &vars::BASE_CONFIG.scripts_path.prepare
+ "prepare_host_dirs.sh";
// let script = RunScript::new("other_script/create_dirs.sh", RunType::SH);
// let script = RunScript::new(&script_path, RunType::SH);
let stdout_file = File::create("/root/prepare.log").unwrap();
let stderr_file = stdout_file.try_clone().unwrap();
match Command::new("/bin/bash")
.arg("-e")
.arg(script_path)
.stdout(stdout_file)
.stderr(stderr_file)
.status()
{
Ok(_v) => (),
Err(_e) => self.try_set_flag(false),
};
// let res = script.exec(None);
Retval::empty()
}
}
pub struct MountTargetPart {}
impl utils::ProgramEndingFlag for MountTargetPart {}
impl TaskTrait for MountTargetPart {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
Retval::empty()
}
}
//检查并配置环境变量
pub struct CheckEnv {}
impl utils::ProgramEndingFlag for CheckEnv {}
impl TaskTrait for CheckEnv {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
self.check_flag();
//从配置文件中读出环境变量配置,设定到本进程上。
for env in &vars::BASE_CONFIG.envs {
match utils::env_status(env.name.clone()) {
Ok(v) => {
if v.clone() == env.value.clone() {
info!("Get {} env : {}", env.name.clone(), v.clone());
} else {
env::set_var(env.name.clone(), env.value.clone());
warn!(
"Get {} env , reset value from {} to {}",
env.name.clone(),
v.clone(),
env.value.clone()
);
}
}
Err(_e) => {
warn!(
"Nou found env {} , set new value {}",
env.name.clone(),
env.value.clone()
);
env::set_var(env.name.clone(), env.value.clone());
}
}
}
Retval::new(true)
}
}