-
Notifications
You must be signed in to change notification settings - Fork 202
/
Assets.pm
278 lines (253 loc) · 9.33 KB
/
Assets.pm
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
# Copyright (C) 2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package OpenQA::Schema::Result::Assets;
use base qw/DBIx::Class::Core/;
use strict;
use OpenQA::Utils;
use OpenQA::Scheduler::Scheduler 'job_notify_workers';
use Date::Format;
use Mojo::UserAgent;
use File::Spec::Functions 'splitpath';
use Try::Tiny;
use db_helpers;
our %types = map { $_ => 1 } qw/iso repo hdd other/;
__PACKAGE__->table('assets');
__PACKAGE__->load_components(qw/Timestamps/);
__PACKAGE__->add_columns(
id => {
data_type => 'integer',
is_auto_increment => 1,
},
type => {
data_type => 'text',
},
name => {
data_type => 'text',
},
size => {
data_type => 'bigint',
is_nullable => 1
});
__PACKAGE__->add_timestamps;
__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraint([qw/type name/]);
__PACKAGE__->has_many(jobs_assets => 'OpenQA::Schema::Result::JobsAssets', 'asset_id');
__PACKAGE__->many_to_many(jobs => 'jobs_assets', 'job');
sub _getDirSize {
my ($dir, $size) = @_;
$size //= 0;
opendir(my $dh, $dir) || return 0;
for my $dirContent (grep(!/^\.\.?/, readdir($dh))) {
$dirContent = "$dir/$dirContent";
if (-f $dirContent) {
my $fsize = -s $dirContent;
$size += $fsize;
}
elsif (-d $dirContent) {
$size = _getDirSize($dirContent, $size);
}
}
closedir($dh);
return $size;
}
sub disk_file {
my ($self) = @_;
sprintf("%s/%s/%s", $OpenQA::Utils::assetdir, $self->type, $self->name);
}
sub remove_from_disk {
my ($self) = @_;
my $file = $self->disk_file;
OpenQA::Utils::log_info("GRU: removing $file");
if ($self->type eq 'iso' || $self->type eq 'hdd') {
return unless -f $file;
unlink($file) || die "can't remove $file";
}
elsif ($self->type eq 'repo') {
use File::Path qw(remove_tree);
remove_tree($file) || die "can't remove $file";
}
}
sub ensure_size {
my ($self) = @_;
my $size = 0;
my @st = stat($self->disk_file);
if (@st) {
if ($self->type eq 'repo') {
return $self->size if defined($self->size);
$size = _getDirSize($self->disk_file);
}
else {
$size = $st[7];
return $self->size if defined($self->size) && $size == $self->size;
}
}
$self->update({size => $size}) if $size;
return $size;
}
# A GRU task...arguments are the URL to grab and the full path to save
# it in. scheduled in job_schedule_iso()
sub download_asset {
my ($app, $args) = @_;
my ($url, $dlpath) = @{$args};
# Bail if the dest file exists (in case multiple downloads of same ISO
# are scheduled)
return if (-e $dlpath);
my $dldir = (splitpath($dlpath))[1];
unless (-w $dldir) {
OpenQA::Utils::log_error("download_asset: cannot write to $dldir");
# we're not going to die because this is a gru task and we don't
# want to cause the Endless Gru Loop Of Despair, just return and
# let the jobs fail
job_notify_workers();
return;
}
# check URL is whitelisted for download. this should never fail;
# if it does, it means this task has been created without going
# through the ISO API controller, and that means either a code
# change we didn't think through or someone being evil
my @check = check_download_url($url, $app->config->{global}->{download_domains});
if (@check) {
my ($status, $host) = @check;
if ($status == 2) {
OpenQA::Utils::log_error("download_asset: no hosts are whitelisted for asset download!");
}
else {
OpenQA::Utils::log_error("download_asset: URL $url host $host is blacklisted!");
}
OpenQA::Utils::log_error("**API MAY HAVE BEEN BYPASSED TO CREATE THIS TASK!**");
job_notify_workers();
return;
}
OpenQA::Utils::log_debug("Downloading " . $url . " to " . $dlpath . "...");
my $ua = Mojo::UserAgent->new(max_redirects => 5);
my $tx = $ua->build_tx(GET => $url);
# Allow >16MiB downloads
# http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Large-file-downloads
$tx->res->max_message_size(0);
$tx = $ua->start($tx);
if ($tx->success) {
try {
$tx->res->content->asset->move_to($dlpath);
}
catch {
# again, we're trying not to die here, but log and return on fail
OpenQA::Utils::log_error("Error renaming temporary file to $dlpath: $_");
job_notify_workers();
return;
};
}
else {
# Clean up after ourselves. Probably won't exist, but just in case
OpenQA::Utils::log_error("Downloading failed! Deleting files");
unlink($dlpath);
}
# We want to notify workers either way: if we failed to download the ISO,
# we want the jobs to run and fail.
job_notify_workers();
}
# this is a GRU task - abusing the namespace
sub limit_assets {
my ($app) = @_;
my $groups = $app->db->resultset('JobGroups');
# keep track of all assets related to jobs
my %seen_asset;
my %toremove;
my %keep;
my $debug_keep = 0;
# we go through the group and keep the last X GB of it in %keep and the others
# in toremove. After that we remove all in %toremove that no other group put in %keep
# (assets can easily be in 2 groups - and both have different update ratios, it's up
# to the admin to configure the size limit)
while (my $g = $groups->next) {
my $sizelimit = $g->size_limit_gb * 1024 * 1024 * 1024;
my $assets = $app->db->resultset('JobsAssets')->search(
{
job_id => {-in => $g->jobs->get_column('id')->as_query}
},
{
prefetch => 'asset',
order_by => 'me.t_created desc',
});
while (my $a = $assets->next) {
# ignore predefined images
next if ($a->asset->type eq 'hdd' && $a->created_by == 0);
# distinct is a bit too tricky
next if ($seen_asset{$a->asset->id} // 0) == $g->id;
$seen_asset{$a->asset->id} = $g->id;
my $size = $a->asset->ensure_size;
if ($size > 0 && $sizelimit > 0) {
$keep{$a->asset_id} = sprintf "%s: %s/%s", $g->name, $a->asset->type, $a->asset->name;
}
else {
$toremove{$a->asset_id} = sprintf "%s/%s", $a->asset->type, $a->asset->name;
}
$sizelimit -= $size;
}
}
for my $id (keys %keep) {
OpenQA::Utils::log_debug("KEEP $toremove{$id} $keep{$id}") if $debug_keep;
delete $toremove{$id};
}
my $assets = $app->db->resultset('Assets')->search({id => {in => [sort keys %toremove]}}, {order_by => qw/t_created/});
while (my $a = $assets->next) {
$a->remove_from_disk;
$a->delete;
}
my $timecond = {"<" => time2str('%Y-%m-%d %H:%M:%S', time - 24 * 3600 * 2, 'UTC')};
$assets = $app->db->resultset('Assets')->search({t_created => $timecond, type => ['iso', 'repo'], id => {-not_in => [sort keys %seen_asset]}}, {order_by => [qw/type name/]});
while (my $a = $assets->next) {
OpenQA::Utils::log_debug("Asset " . $a->type . "/" . $a->name . " is not in any job group, DELETE from assets where id=" . $a->id . ";");
}
my $dh;
if (opendir($dh, $OpenQA::Utils::assetdir . "/iso")) {
my %isos;
while (readdir($dh)) {
next if $_ =~ m/CURRENT/;
next unless $_ =~ m/\.iso$/;
$isos{$_} = 0;
}
closedir($dh);
$assets = $app->db->resultset('Assets')->search({type => 'iso', name => {in => [keys %isos]}});
while (my $a = $assets->next) {
$isos{$a->name} = $a->id;
}
for my $iso (keys %isos) {
if ($isos{$iso} == 0) {
OpenQA::Utils::log_debug "File iso/$iso is not a registered asset";
}
}
}
if (opendir($dh, $OpenQA::Utils::assetdir . "/repo")) {
my %repos;
while (readdir($dh)) {
next if $_ eq '.' || $_ eq '..';
next if -l "$OpenQA::Utils::assetdir/repo/$_";
next unless -d "$OpenQA::Utils::assetdir/repo/$_";
$repos{$_} = 0;
}
closedir($dh);
$assets = $app->db->resultset('Assets')->search({type => 'repo', name => {in => [keys %repos]}});
while (my $a = $assets->next) {
$repos{$a->name} = $a->id;
}
for my $repo (keys %repos) {
if ($repos{$repo} == 0) {
OpenQA::Utils::log_debug "Directory repo/$repo is not a registered asset";
}
}
}
}
1;