forked from unbit/uwsgi.it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopboxer.pl
184 lines (164 loc) · 5.56 KB
/
loopboxer.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
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
use bigint;
use LWP::UserAgent;
use JSON -support_by_pp;
use Config::IniFiles;
use POSIX qw(strftime);
use IO::Socket::INET;
# required for --log-master
STDOUT->autoflush(1);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $cfg = Config::IniFiles->new( -file => "/etc/uwsgi/local.ini" );
my $base_url = 'https://'.$cfg->val('uwsgi', 'api_domain').'/api/private';
my $ssl_key = $cfg->val('uwsgi', 'api_client_key_file');
my $ssl_cert = $cfg->val('uwsgi', 'api_client_cert_file');
for(;;) {
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_key_file => $ssl_key,
SSL_cert_file => $ssl_cert,
);
$ua->timeout(3);
my $response = $ua->get($base_url.'/loopboxes/');
if ($response->is_error or $response->code != 200 ) {
print date().' oops: '.$response->code.' '.$response->message."\n";
exit;
}
my $loopboxes = decode_json($response->decoded_content);
my $containers_json = undef;
# get json stats from the Emperor stats
my $s = IO::Socket::INET->new(PeerAddr => '127.0.0.1:5001');
if ($s) {
my $json = '';
for(;;) {
$s->recv(my $buf, 8192);
last unless $buf;
$json .= $buf;
}
$containers_json = decode_json($json);
}
foreach my $lb (@{$loopboxes}) {
my $pid = get_container_pid($lb->{uid}, $containers_json->{vassals});
next unless $pid;
if (check_mountpoint($pid, $lb->{uid}, $lb->{id}, $lb->{filename}, $lb->{mountpoint}, $loopboxes)) {
my $cmd = '/etc/uwsgi/loopbox mount /containers/'.$lb->{uid}.'/run/ns.socket /dev/loop'.$lb->{id}.' /containers/'.$lb->{uid}.'/'.$lb->{filename}.' /containers/'.$lb->{uid}.'/'.$lb->{mountpoint}.' '.$lb->{ro};
print date().' running '.$cmd."\n";
system($cmd.' >> /containers/'.$lb->{uid}.'/logs/emperor.log');
}
}
foreach(@{$containers_json->{vassals}}) {
if (!$_->{checked}) {
unmount_all($_);
}
}
sleep(30);
}
sub date {
return strftime "%Y-%m-%d %H:%M:%S", localtime;
}
sub get_container_pid {
my ($uid, $vassals) = @_;
foreach(@{$vassals}) {
if ($uid.'.ini' eq $_->{id}) {
$_->{checked} = 1;
return $_->{pid};
}
}
}
sub unmount_all {
my ($vassal) = @_;
open MOUNTS,'/proc/'.$vassal->{pid}.'/mounts';
while(<MOUNTS>) {
my ($device, $dir) = split /\s+/;
if ($device =~ /\/dev\/loop\d+/) {
my $uid = $vassal->{id};
$uid =~ s/\.ini$//;
umount($uid, $dir);
}
}
close(MOUNTS);
}
sub check_mountpoint {
my ($pid, $uid, $id, $filename, $mountpoint, $loopboxes) = @_;
my $ret = 1;
# first check if we need to umount devices
open MOUNTS,'/proc/'.$pid.'/mounts';
while(<MOUNTS>) {
my ($device, $dir) = split /\s+/;
if ($device =~ /\/dev\/loop(\d+)/) {
my $loop = $1;
my $found = 0;
foreach(@{$loopboxes}) {
if ($loop eq $_->{id}) {
$found = 1;
last;
}
}
unless($found) {
umount($uid, $dir);
}
}
}
close(MOUNTS);
open MOUNTS,'/proc/'.$pid.'/mounts';
while(<MOUNTS>) {
my ($device, $dir) = split /\s+/;
if ($device =~ /\/dev\/loop(\d+)/) {
my $loop = $1;
my $found = 0;
foreach(@{$loopboxes}) {
if ($loop eq $_->{id}) {
$found = 1;
last;
}
}
# exit in case of incongruence
last unless($found);
if ($device eq '/dev/loop'.$id) {
# check if the size of the file is changed
open SYSFS, '/sys/class/block/loop'.$id.'/size';
my $sectors = <SYSFS>;
close SYSFS;
my @st = stat('/containers/'.$uid.'/'.$filename);
# deleted file ?
unless(@st) {
umount($uid, $dir);
$ret = 0;
last;
}
# invalid file size ?
my $size = $st[7];
if ($size < (1024*1024)) {
umount($uid, $dir);
$ret = 0;
last;
}
# file decreased in size ?
$file_sectors = $size/512;
if ($file_sectors < $sectors) {
umount($uid, $dir);
$ret = 0;
last;
}
# at least 1 megabyte more
if ($file_sectors > $sectors + (2048)) {
umount($uid, $dir);
# calling resize2fs on a loopback device under a namespace
# make the kernel hang ... :( disable it for now
#my $cmd = '/etc/uwsgi/loopbox resize /containers/'.$uid.'/run/ns.socket /dev/loop'.$id.' /containers/'.$uid.'/'.$filename;
#print date().' running '.$cmd."\n";
#system($cmd);
}
$ret = 0;
last;
}
}
}
close(MOUNTS);
return $ret;
}
sub umount {
my ($uid, $dir) = @_;
my $cmd = '/etc/uwsgi/loopbox umount /containers/'.$uid.'/run/ns.socket '.$dir;
print date().' running '.$cmd."\n";
system($cmd.' >> /containers/'.$uid.'/logs/emperor.log');
}