-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathclean.rs
494 lines (467 loc) · 16.8 KB
/
clean.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use std::collections::HashSet;
use std::fs::{read_dir, read_link, remove_file, metadata};
use std::io::{self, stdout, stderr};
use std::ffi::OsStr;
use std::path::Path;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use std::os::unix::fs::MetadataExt;
use argparse::{ArgumentParser, PushConst, StoreTrue, StoreOption};
use scan_dir::ScanDir;
use humantime;
use crate::config::command::MainCommand::{Supervise, Command, CapsuleCommand};
use crate::config::volumes::Volume::Persistent;
use crate::container::util::clean_dir;
use crate::file_util::{read_visible_entries, Lock};
use crate::wrapper::build::get_version_hash;
use super::setup;
use super::Wrapper;
#[derive(Clone, Copy)]
enum Action {
Temporary,
Old,
Unused,
Everything,
Transient,
Volumes,
UnusedVolumes,
}
pub fn clean_cmd(wrapper: &Wrapper, cmdline: Vec<String>)
-> Result<i32, String>
{
let mut global = false;
let mut dry_run = false;
let mut actions = vec!();
let mut duration = None::<humantime::Duration>;
{
let mut ap = ArgumentParser::new();
ap.set_description("
Performs various cleanup tasks
");
ap.refer(&mut actions)
.add_option(&["--tmp", "--tmp-folders"],
PushConst(Action::Temporary),
"Clean temporary containers (failed builds)")
.add_option(&["--old", "--old-containers"], PushConst(Action::Old), "
Clean old versions of containers (those which doesn't have a \
symlink in .vagga)")
.add_option(&["--unused"], PushConst(Action::Unused), "
Clean unused containers, or versions thereof. (This is not \
`--old` for historical reasons, we will probably merge the \
commands later on)")
.add_option(&["--transient"], PushConst(Action::Transient),
"Clean unneeded transient folders (left from containers with
`write-mode` set to transient-something). The pid of process
is checked for liveness first.")
.add_option(&["--everything"], PushConst(Action::Everything),
"Clean whole `.vagga` folder. Useful when deleting a project.
With ``--global`` cleans whole storage-dir and cache-dir")
.add_option(&["--unused-volumes"], PushConst(Action::UnusedVolumes),
"Remove `!Persistent` volumes that are not used by any \
command or container of the current config")
.add_option(&["--volumes"], PushConst(Action::Volumes),
"Remove all `!Persistent` volumes. So they are reinitialized \
on the next start of the command")
.required();
ap.refer(&mut global)
.add_option(&["--global"], StoreTrue,
"Apply cleanup command to all the projects
in the `storage-dir`. Works only \
if `storage-dir` is configured in settings");
ap.refer(&mut dry_run)
.add_option(&["-n", "--dry-run"], StoreTrue,
"Dry run. Don't delete everything, just print");
ap.refer(&mut duration)
.add_option(&["--at-least"], StoreOption, "
Only in combination with `--unused`. Treat as unused \
containers that are unused for specified time, rather than \
the ones not used by current version of config");
match ap.parse(cmdline, &mut stdout(), &mut stderr()) {
Ok(()) => {}
Err(0) => return Ok(0),
Err(_) => {
return Ok(1);
}
}
}
if global && wrapper.ext_settings.storage_dir.is_none() {
error!("The --global flag is only meaningful if you configure \
storage-dir in settings");
return Ok(2);
}
let duration = duration.map(|x| x.into());
for action in actions.iter() {
let res = match *action {
Action::Temporary => clean_temporary(wrapper, global, dry_run),
Action::Old => clean_old(wrapper, global, dry_run),
Action::Unused => {
if global {
if let Some(duration) = duration {
global_clean_unused(wrapper, duration, dry_run)
} else {
panic!("no global cleanup without --at-least");
}
} else {
clean_unused(wrapper, duration, dry_run)
}
}
Action::Transient => clean_transient(wrapper, global, dry_run),
Action::Everything => clean_everything(wrapper, global, dry_run),
Action::UnusedVolumes => {
clean_volumes(wrapper, global, dry_run, false)
}
Action::Volumes => {
clean_volumes(wrapper, global, dry_run, true)
}
};
match res {
Ok(()) => {}
Err(err) => {
error!("Error cleaning up: {}", err);
return Ok(3);
}
}
}
return Ok(0);
}
fn clean_dir_wrapper(path: &Path,
remove_dir_itself: bool, dry_run: bool) -> Result<(), String> {
// TODO(tailhook) chroot to dir for removing
if dry_run {
println!("Would remove {:?}", path);
} else {
let mut n = path.to_path_buf().into_os_string();
n.push(".lock");
let lock_name = Path::new(&n);
let lock_guard = if lock_name.exists() {
match Lock::exclusive(&lock_name) {
Ok(x) => Some(x),
Err(e) => {
error!("Failed to lock {:?}: {}, skipping", lock_name, e);
return Ok(());
}
}
} else {
None
};
debug!("Removing {:?}", path);
clean_dir(path, remove_dir_itself)?;
if let Some(_lock) = lock_guard {
remove_file(lock_name)
.map_err(|e| format!("Error removing lock file {:?}: {}",
lock_name, e))?;
}
}
Ok(())
}
fn clean_everything(wrapper: &Wrapper, global: bool, dry_run: bool)
-> Result<(), String>
{
if global {
if let Some(ref cache_dir) = wrapper.ext_settings.cache_dir {
clean_dir_wrapper(&cache_dir, false, dry_run)?;
}
if let Some(ref storage_dir) = wrapper.ext_settings.storage_dir {
clean_dir_wrapper(&storage_dir, false, dry_run)?;
}
} else {
let base = match setup::get_vagga_base(
wrapper.project_root, wrapper.ext_settings)?
{
Some(base) => base,
None => {
warn!("No vagga directory exists");
return Ok(());
}
};
clean_dir_wrapper(&base, true, dry_run)?;
let inner = wrapper.project_root.join(".vagga");
if base != inner {
clean_dir_wrapper(&inner, true, dry_run)?;
}
}
return Ok(());
}
fn clean_temporary(wrapper: &Wrapper, global: bool, dry_run: bool)
-> Result<(), String>
{
if global {
panic!("Global cleanup is not implemented yet");
}
let base = match setup::get_vagga_base(
wrapper.project_root, wrapper.ext_settings)?
{
Some(base) => base,
None => {
warn!("No vagga directory exists");
return Ok(());
}
};
let roots = base.join(".roots");
for entry in try_msg!(read_dir(&roots),
"Can't read dir {r:?}: {err}", r=roots)
{
let entry = try_msg!(entry, "Can't read dir {r:?}: {err}", r=roots);
let typ = try_msg!(entry.file_type(),
"Can't stat {p:?}: {err}", p=entry.path());
if typ.is_dir() &&
entry.file_name()[..].to_str().map(|n| n.starts_with(".tmp"))
.unwrap_or(false)
{
clean_dir_wrapper(&entry.path(), true, dry_run)?;
}
}
return Ok(());
}
fn clean_old(wrapper: &Wrapper, global: bool, dry_run: bool)
-> Result<(), String>
{
if global {
panic!("Global cleanup is not implemented yet");
}
let base = match setup::get_vagga_base(
wrapper.project_root, wrapper.ext_settings)?
{
Some(base) => base,
None => {
warn!("No vagga directory exists");
return Ok(());
}
};
let useful: HashSet<String> = try_msg!(
read_visible_entries(&wrapper.project_root.join(".vagga")),
"Can't read vagga directory: {err}")
.into_iter()
.filter_map(|path| read_link(&path)
.map_err(|e| warn!("Can't readlink {:?}: {}", path, e))
.ok()
.and_then(|f| {
// The container name is next to the last component
f.iter().rev().nth(1)
.and_then(|x| x.to_str()).map(ToString::to_string)
}))
.collect();
info!("Useful images {:?}", useful);
clean_dirs_except(&base.join(".roots"), &useful, dry_run)?;
return Ok(());
}
fn clean_transient(wrapper: &Wrapper, global: bool, dry_run: bool)
-> Result<(), String>
{
if global {
panic!("Global cleanup is not implemented yet");
}
let base = match setup::get_vagga_base(
wrapper.project_root, wrapper.ext_settings)?
{
Some(base) => base,
None => {
warn!("No vagga directory exists");
return Ok(());
}
};
let procfs = Path::new("/proc");
for entry in try_msg!(read_dir(&base.join(".transient")),
"Can't read .vagga/.transient dir: {err}")
{
let entry = try_msg!(entry, "Error reading .vagga/transient: {err}");
if let Some(fname) = entry.file_name()[..].to_str() {
if let Some(idx) = fname.find('.') {
if u32::from_str(&fname[idx+1..]).is_ok() &&
procfs.join(&fname[idx+1..]).exists()
{
continue;
}
}
}
clean_dir_wrapper(&entry.path(), true, dry_run)?;
}
return Ok(());
}
fn clean_dirs_except<P: AsRef<Path>>(roots: P, useful: &HashSet<String>,
dry_run: bool)
-> Result<(), String>
{
let roots = roots.as_ref();
for entry in try_msg!(read_dir(&roots),
"Can't read dir {dir:?}: {err}", dir=roots)
{
let entry = try_msg!(entry,
"Can't read dir {dir:?}: {err}", dir=roots);
let path = entry.path();
let typ = try_msg!(entry.file_type(),
"Can't stat {p:?}: {err}", p=path);
if !typ.is_dir() {
if path.extension() == Some(OsStr::new("lock")) &&
path.with_extension("").is_dir()
{
debug!("Skipping lock file {:?}", path);
} else {
match remove_file(&path) {
Ok(()) => {}
// File is deleted while we were scanning
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(ref e) => {
return Err(
format!("Can't remove file {:?}: {}", path, e));
}
}
}
} else if !typ.is_dir() || entry.file_name()[..].to_str()
.map(|n| !useful.contains(&n.to_string()))
.unwrap_or(false)
{
clean_dir_wrapper(&entry.path(), true, dry_run)?;
}
}
Ok(())
}
fn global_clean_unused(wrapper: &Wrapper, duration: Duration,
dry_run: bool)
-> Result<(), String>
{
let unixtime = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let cut_off = (unixtime - duration).as_secs() as i64;
let is_cache_dir = |p: &Path| {
wrapper.ext_settings.cache_dir.as_ref()
.map(|x| x == p)
.unwrap_or(false)
};
let storage_dir = wrapper.ext_settings.storage_dir.as_ref().unwrap();
let mut proj_num = 0;
let mut to_remove = 0;
let mut to_keep = 0;
ScanDir::dirs().read(&storage_dir, |iter| {
for (entry, name) in iter {
let path = entry.path();
if is_cache_dir(&path) {
continue;
}
proj_num += 1;
info!("Scanning project {}", name);
let mut useful: HashSet<String> = HashSet::new();
let roots = path.join(".roots");
ScanDir::dirs().skip_hidden(false).read(&roots, |iter| {
for (entry, name) in iter {
let luse_path = entry.path().join("last_use");
match metadata(&luse_path) {
Ok(ref meta) if meta.mtime() > cut_off => {
useful.insert(name);
to_keep += 1;
}
Ok(_) => {
to_remove += 1;
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(e) => {
error!("Error trying to stat {:?}: {}",
luse_path, e);
}
}
}
}).map_err(|e| {
error!("Error reading {:?}: {}", roots, e);
}).ok();
info!("Useful images {:?}", useful);
clean_dirs_except(&roots, &useful, dry_run)
.map_err(|e| error!("Error cleaning {:?}: {}", roots, e))
.ok(); // TODO(tailhook) propagate the errorneous exit code?
}
}).map_err(|e| {
format!("Error reading storage dir {:?}: {}", storage_dir, e)
})?;
info!("Scanned {} projects, keeping {} images, removed {}",
proj_num, to_keep, to_remove);
Ok(())
}
fn clean_unused(wrapper: &Wrapper, duration: Option<Duration>,
dry_run: bool)
-> Result<(), String>
{
setup::setup_base_filesystem(
wrapper.project_root, wrapper.ext_settings)?;
let mut useful: HashSet<String> = HashSet::new();
if let Some(duration) = duration {
let unixtime = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let cut_off = (unixtime - duration).as_secs() as i64;
ScanDir::dirs().skip_hidden(false).read("/vagga/base/.roots", |iter| {
for (entry, name) in iter {
let luse_path = entry.path().join("last_use");
match metadata(&luse_path) {
Ok(ref meta) if meta.mtime() > cut_off => {
useful.insert(name);
}
Ok(_) => {}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
Err(e) => {
error!("Error trying to stat {:?}: {}", luse_path, e);
}
}
}
}).map_err(|e| {
error!("Error reading `.vagga/.roots`: {}", e);
}).ok();
} else {
for (name, _) in &wrapper.config.containers {
if let Some(version) = get_version_hash(name, wrapper)? {
useful.insert(format!("{}.{}", name, &version[..8]));
}
}
}
info!("Useful images {:?}", useful);
clean_dirs_except("/vagga/base/.roots", &useful, dry_run)?;
return Ok(());
}
fn clean_volumes(wrapper: &Wrapper, global: bool, dry_run: bool, all: bool)
-> Result<(), String>
{
if global {
panic!("Global cleanup is not implemented yet");
}
let base = match setup::get_vagga_base(
wrapper.project_root, wrapper.ext_settings)?
{
Some(base) => base,
None => {
warn!("No vagga directory exists");
return Ok(());
}
};
let volume_dir = base.join(".volumes");
let mut useful = HashSet::new();
if !all {
for (_, container) in &wrapper.config.containers {
for (_, vol) in &container.volumes {
if let Persistent(ref p) = *vol {
useful.insert(p.name.clone());
}
}
}
for (_, command) in &wrapper.config.commands {
match *command {
Command(ref cmd) => {
for (_, vol) in &cmd.volumes {
if let Persistent(ref p) = *vol {
useful.insert(p.name.clone());
}
}
}
CapsuleCommand(_) => {
// novolumes
},
Supervise(ref cmd) => {
for (_, child) in &cmd.children {
for (_, vol) in child.get_volumes() {
if let Persistent(ref p) = *vol {
useful.insert(p.name.clone());
}
}
}
}
}
}
}
info!("Useful volumes {:?}", useful);
clean_dirs_except(volume_dir, &useful, dry_run)?;
return Ok(());
}