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 pathutils.rs
354 lines (318 loc) · 10.1 KB
/
utils.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
use cmd_lib::*;
use dagrs::{EnvVar, Inputval, Retval, TaskTrait};
use log::{error, info};
use std::env;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::result::Result;
use glob::glob;
use std::os::unix::fs::chroot;
use std::process::Stdio;
use crate::vars::BASE_CONFIG;
pub struct InstallInfo {
pub dir_name: String,
pub package_name: String,
pub script_name: String,
pub script_path: String,
pub package_source_path: String,
pub package_target_path: String,
}
pub fn exec_chroot_script(script_path: PathBuf, dir: PathBuf) -> bool {
//日志输出文件
let stdout_file = match File::create("/root/log.log") {
Ok(v) => v,
Err(_e) => {
error!("not find root/log.log");
return false;
}
};
let stderr_file = match stdout_file.try_clone() {
Ok(v) => v,
Err(_e) => {
error!("try to clone stdout file error");
return false;
}
};
let stdio = Stdio::from(stdout_file);
let stderr = Stdio::from(stderr_file);
//取绝对路径
let abs_path = match fs::canonicalize(dir.as_path()) {
Ok(v) => v,
Err(_e) => {
error!("get abs path failed");
return false;
}
};
let output = match Command::new("/bin/bash")
.current_dir(abs_path)
.env_clear()
.env("PATH", "/usr/bin:/usr/sbin:/root/.cargo/bin")
.env("HOME", "/root")
.env("TERM", "$TERM")
.env("MAKEFLAGS", "-j8")
.env("NINJAJOBS", "8")
.arg("-e")
.arg(script_path.clone())
.stdout(stdio)
.stderr(stderr)
.status()
{
Ok(v) => v,
Err(_e) => {
error!("exec script {:?} failed", script_path);
return false;
}
};
output.success()
}
fn exec_build_script(script_path: PathBuf, dir: PathBuf) -> bool {
let stdout_file = match File::create("/root/log.log") {
Ok(v) => v,
Err(_e) => return false,
};
let stderr_file = match stdout_file.try_clone() {
Ok(v) => v,
Err(_e) => return false,
};
let stdio = Stdio::from(stdout_file);
let stderr = Stdio::from(stderr_file);
let abs_path = match fs::canonicalize(dir.as_path()) {
Ok(v) => v,
Err(_e) => return false,
};
let output = match Command::new("/bin/bash")
.current_dir(abs_path)
.env("MAKEFLAGS", "-j8")
.arg("-e")
.arg(script_path)
.stdout(stdio)
.stderr(stderr)
.status()
{
Ok(v) => v,
Err(_e) => return false,
};
output.success()
}
//FIXME:看情况是否需要单独的配置步骤
pub struct ConfigChrootEnv {}
impl ProgramEndingFlag for ConfigChrootEnv {}
impl TaskTrait for ConfigChrootEnv {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
Retval::empty()
}
}
pub struct EnterChroot {}
impl ProgramEndingFlag for EnterChroot {}
impl TaskTrait for EnterChroot {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
self.check_flag();
// let chmod_script_path = "chroot_scripts/chown.sh";
let chmod_script_path: PathBuf = [
&BASE_CONFIG.scripts_path.root,
&BASE_CONFIG.scripts_path.chroot,
"target_path_chown.sh",
]
.iter()
.collect();
match Command::new("/bin/bash")
.arg("-e")
.arg(chmod_script_path)
.status()
{
Ok(v) => match v.success() {
true => {}
false => {
self.try_set_flag(false);
}
},
Err(e) => {
error!("Failed chown target path , Err msg:{}", e);
self.try_set_flag(false);
}
};
let mount_virt_fsystem: PathBuf = [
&BASE_CONFIG.scripts_path.root,
&BASE_CONFIG.scripts_path.chroot,
"mount_vfsys.sh",
]
.iter()
.collect();
match Command::new("/bin/bash")
.arg("-e")
.arg(mount_virt_fsystem)
.status()
{
Ok(v) => match v.success() {
true => {}
false => {
self.try_set_flag(false);
}
},
Err(e) => {
error!("Failed mount virt file system , Err msg:{}", e);
self.try_set_flag(false);
}
};
match chroot("/mnt/lfs") {
Ok(_v) => {
info!("success chroot to /mnt/lfs ");
}
Err(e) => {
error!("chroot to /mnt/lfs failed , Err msg {}", e);
self.try_set_flag(false);
}
}
match std::env::set_current_dir("/") {
Ok(_v) => {
info!("success chroot to /mnt/lfs ");
}
Err(e) => {
error!("Set current dir failed , Err msg {}", e);
self.try_set_flag(false);
}
}
Retval::empty()
}
}
pub fn env_status(env: String) -> Result<String, bool> {
if let Ok(v) = env::var(env) {
return Ok(v);
}
Err(false)
}
pub fn download(target_path: String, url: String) -> Result<bool, Box<dyn Error>> {
let cmd = format!("wget -P {} {}", target_path, url);
let output = Command::new("/bin/bash").arg("-c").arg(cmd).status()?;
Ok(output.success())
}
pub fn install_package(
package_info: InstallInfo,
chroot_flag: bool,
) -> Result<bool, Box<dyn Error>> {
info!("package source path :{}; package name :{}; script path :{}; script name :{}; package_target_path :{};",&package_info.package_source_path,&package_info.package_name,&package_info.script_path,&package_info.script_name,&package_info.package_target_path);
//软件包相对工作目录的路径
let package_path =
match glob(&(package_info.package_source_path.clone() + &package_info.package_name + "*"))?
.filter_map(Result::ok)
.next()
{
Some(v) => v,
None => return Err(format!("Not found package {:?}", package_info.package_name).into()),
};
info!(
"Package name : {:?} ; Package path : {:?} ;",
&package_info.package_name, &package_path,
);
//预先准备的脚本文件路径
let script_full_path =
match glob(&(package_info.script_path.clone() + &package_info.script_name + "*sh"))?
.filter_map(Result::ok)
.next()
{
Some(v) => v,
None => {
return Err(format!(
"Not found script {:?}, script path {:?}",
package_info.package_name,
package_info.script_path.clone()
)
.into())
}
};
info!(
"Package name : {:?} ; Package path : {:?} ; Script path : {:?}",
&package_info.package_name, &package_path, &script_full_path
);
let output = Command::new("/usr/bin/tar")
.arg("xf")
.arg(package_path)
.arg("-C")
.arg(package_info.package_target_path.clone())
.output()
.expect("error");
let out = String::from_utf8(output.stdout).unwrap();
info!("{}", out);
//解压好的程序包路径
let target_path =
match glob(&(package_info.package_target_path + &package_info.dir_name + "*/"))?
// let target_path = match glob(&("sources/".to_owned() + &package_name + "*/"))
.filter_map(Result::ok)
.next()
{
Some(v) => v,
None => {
return Err(format!("Can not get target path {}", package_info.package_name).into())
} //None => panic!("Not found target path {:?}", package_name),
};
//最终脚本文件在程序包中的路径
let script_target_path: PathBuf = [
target_path.clone(),
(package_info.script_name.clone() + ".sh").into(),
]
.iter()
.collect();
info!(
"Script source path : {:?} ; Script target path : {:?} ;",
target_path, script_target_path
);
// println!("{:?} : {:?}", &target_path, script_target_path);
fs::copy(script_full_path, &script_target_path)?;
//脚本文件名字
let fin_script_name = match script_target_path.file_name() {
Some(v) => v,
None => return Err("get filename failed".into()),
};
let status = match chroot_flag {
true => exec_chroot_script(fin_script_name.into(), target_path.clone()),
false => exec_build_script(fin_script_name.into(), target_path.clone()),
};
//let status = exec_build_script(fin_script_name.into(), target_path.clone());
fs::remove_dir_all(target_path.clone()).unwrap();
match status {
true => {
info!("Package {:?} install success", package_info.package_name);
Ok(true)
}
false => Err(format!("Package {:?} install failed", target_path).into()),
}
}
pub fn delete_failed_download_pack(target_pack_name: &str, target_path: &str) {
match glob(&(target_path.to_string() + target_pack_name)) {
Ok(v) => {
if let Some(v) = v.filter_map(Result::ok).next() {
fs::remove_file(v).unwrap()
}
}
Err(_e) => {}
}
}
pub fn check_download_before(target_pack_name: &str, target_path: &str) -> bool {
match glob(&(target_path.to_string() + target_pack_name)) {
Ok(v) => v.filter_map(Result::ok).next().is_some(),
Err(_e) => false,
}
}
pub trait ProgramEndingFlag {
fn check_flag(&self) {
let target_path = Path::new("stop");
if target_path.exists() {
panic!("Program Ending");
}
}
fn try_set_flag(&self, flag: bool) {
let target_path = Path::new("./stop");
match flag {
false => {
//FIXME:直接unwrap虽然可以,但是不太美观
File::create(target_path).unwrap();
panic!("Program Ending");
}
true => (),
}
}
}