This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_kernel
executable file
·150 lines (128 loc) · 3.78 KB
/
get_kernel
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
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/switch/;
no if $] >= 5.018, warnings => "experimental::smartmatch";
use Mojo::UserAgent;
use Mojo::DOM;
use List::Util qw/any/;
use File::Slurp qw/read_file/;
use POSIX qw/setsid/;
use Getopt::Long qw(:config gnu_compat no_permute no_getopt_compat pass_through);
use lib::abs 'lib';
use Linux::Kernel qw/name_from_link extract_version check_kernel_version/;
use Linux::Kernel::Get qw/link_to_latest_kernel link_to_kernel_version/;
use Module::Loader;
use Try::Tiny;
BEGIN {
eval {
require Smart::Comments;
Smart::Comments->import();
}
}
my %config;
$config{working_dir} = '.';
$config{daemon} = 0;
$config{minutes} = 10;
$config{plugins} = [];
$config{version} = undef;
$config{config} = undef;
GetOptions(
'daemon|d!' => \$config{daemon},
'minutes|m=i' => \$config{minutes},
'dir=s' => \$config{working_dir},
'load-plugins=s@' => \$config{plugins},
'config=s' => \$config{config},
'version=s' => \$config{version},
) or die "Wrong command line arguments.\n";
$config{plugins} = [split(/,/, join(',', @{$config{plugins}}))];
unless (-d $config{working_dir} && -r _ && -x _) {
die "Can't open directory $config{working_dir}\n";
}
if ($config{config} && !-r $config{config}) {
die "Can't read config file $config{config}\n";
}
die "Can't parse kernel version $config{version}\n"
if $config{version} && check_kernel_version($config{version});
die "Conflicting options --daemon and explicit version specification\n"
if $config{version} && $config{daemon};
#only plugin configuration options in config file
#in the form of "option=value"
my @config_options;
if ($config{config}) {
foreach (read_file($config{config})) {
chomp;
next
if /\A\h*+\Z/;
if (/\A([\w-]++)=/) {
my $arg = "--plugin-" . $_;
push @config_options, $arg;
} else {
warn "Can't parse option '$_' in $config{config}. Skipping...\n";
}
}
unshift @ARGV, @config_options;
}
my $loader = Module::Loader->new(max_depth => 1);
my @plugins;
my %pmap = map {lc(substr($_, rindex($_, ':') + 1)), $_} $loader->search('Linux::Kernel::Plugin');
foreach my $p (@{$config{plugins}}) {
if (any {$p eq $_} keys %pmap) {
### LOADING PLUGIN: $p
$loader->load($pmap{$p});
push @plugins, $pmap{$p}->process_options(\%config, \@plugins);
} else {
die "There is no plugin: $p\n";
}
}
@plugins = sort {
die("Can't use plugin " . (ref $a) . " and " . (ref $b) . " combined.\n")
if $a->priority == $b->priority;
$a->priority <=> $b->priority
} @plugins;
if (@ARGV) {
my @unknown_args = grep {
my $a = $_;
!any {$a eq $_} @config_options
} @ARGV;
if (@unknown_args) {
die "Unknown command line arguments: " . join(', ', @unknown_args) . "\n";
}
}
### CONFIGURATION: %config
if ($config{daemon}) {
my $pid = fork();
die "can't fork: $!"
unless defined $pid;
if ($pid) {
print "$pid\n";
exit 0;
}
setsid();
open(STDIN, '</dev/null'); # TODO: logging
open(STDOUT, '>/dev/null');
open(STDERR, '>&STDOUT');
}
my $ua = Mojo::UserAgent->new(max_redirects => 3);
my $previous_link = '';
my $link;
CHECK:
$link = $config{version} ? link_to_kernel_version($config{version}) : link_to_latest_kernel($ua);
if ($link && ($link ne $previous_link)) {
my %opts = (link => $link, name => name_from_link($link), version => extract_version($link), ua => $ua);
foreach (@plugins) {
my $last = 0;
try { # TODO:
$_->action(\%opts)
} catch {
when (/^FAIL:/) {die $_}
default {$last = 1}
};
last if $last;
}
$previous_link = $link;
}
if ($config{daemon}) {
sleep $config{minutes} * 60;
goto CHECK;
}