-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockermor.pl
78 lines (67 loc) · 3.21 KB
/
dockermor.pl
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
#!/usr/bin/perl
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# @File docker mor.pl
# @Created May 30, 2017 10:27:01 AM
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
use Date::Parse;
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure(qw(posix_default no_ignore_case));
use POSIX qw(strftime);
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
my $command_line_options = (
all => 0,
dry_run => 0,
dry_run_prefix => "[DOCKERMOR DRY RUN]: ",
older_than_days => 0,
resource_type => ""
);
my $docker_images_query = qx/docker images --format "{{.ID}}:\t{{.CreatedAt}}\t{{.Repository}}"/;
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
GetOptions(
"all" => \$command_line_options{all},
"dry-run" => \$command_line_options{dry_run},
"dry-run-prefix=s" => \$command_line_options{dry_run_prefix},
"older-than-days=i" => \$command_line_options{older_than_days},
"resource-type=s" => \$command_line_options{resource_type}
) or die "Please provide the correct arguments";
# TODO: How to keep previous values with Getopt::Long?
if ($command_line_options{dry_run_prefix} == "")
{
$command_line_options{dry_run_prefix} = "[DOCKERMOR DRY RUN]: ";
}
# Create a hash from the rest of the command line input
my %requested_resoures = map { $_ => 1 } @ARGV;
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
if ($command_line_options{resource_type} == "image")
{
foreach my $row (split(/\n/, $docker_images_query))
{
my @columns = split('\t', $row);
my $creation_date = str2time($columns[1]);
my $image_name = $columns[2];
my $cutoff_date = time() - ($command_line_options{older_than_days} * 24 * 60 * 60);
if (($command_line_options{"all"} || exists($requested_resoures{$image_name})) &&
$cutoff_date > $creation_date)
{
if ($command_line_options{dry_run} == 1)
{
print "$command_line_options{dry_run_prefix}docker image $image_name will be deleted\n";
}
else
{
system("docker rmi -f $image_name") == 0 or die('could not remove docker image $image_name');
print "docker image $image_name deleted\n";
}
}
}
}
# ---------------------------------------------------------------------------- #