]> git.proxmox.com Git - pve-manager.git/blob - PVE/APLInfo.pm
update shipped appliance info index
[pve-manager.git] / PVE / APLInfo.pm
1 package PVE::APLInfo;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use LWP::UserAgent;
8 use POSIX qw(strftime);
9
10 use PVE::SafeSyslog;
11 use PVE::Storage;
12 use PVE::Tools qw(run_command);
13 use PVE::pvecfg;
14
15 my $LOGFILE = "/var/log/pveam.log";
16 my $APL_INFO_DIRECTORY = "/var/lib/pve-manager/apl-info";
17
18 sub logmsg {
19 my ($logfd, $msg) = @_;
20
21 chomp $msg;
22
23 my $tstr = strftime ("%F %H:%M:%S", localtime);
24
25 foreach my $line (split (/\n/, $msg)) {
26 print $logfd "$tstr $line\n";
27 }
28 }
29
30 sub read_aplinfo_from_fh {
31 my ($fh, $list, $source, $update) = @_;
32
33 local $/ = "";
34
35 while (my $rec = <$fh>) {
36 chomp $rec;
37
38 my $res = {};
39
40 while ($rec) {
41
42 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
43 $res->{headline} = $1;
44 my $long = $2 || '';
45 $long =~ s/\n\s+/ /g;
46 $long =~ s/^\s+//g;
47 $long =~ s/\s+$//g;
48 $res->{description} = $long;
49 } elsif ($rec =~ s/^Version:\s*(.*\S)\s*\n//i) {
50 my $version = $1;
51 if ($version =~ m/^(\d[a-zA-Z0-9\.\+\-\:\~]*)(-(\d+))?$/) {
52 $res->{version} = $version;
53 } else {
54 my $msg = "unable to parse appliance record: version = '$version'\n";
55 $update ? die $msg : warn $msg;
56 }
57 } elsif ($rec =~ s/^Type:\s*(.*\S)\s*\n//i) {
58 my $type = $1;
59 if ($type =~ m/^(openvz|lxc)$/) {
60 $res->{type} = $type;
61 } else {
62 my $msg = "unable to parse appliance record: unknown type '$type'\n";
63 $update ? die $msg : warn $msg;
64 }
65 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
66 $res->{lc $1} = $2;
67 } else {
68 my $msg = "unable to parse appliance record: $rec\n";
69 $update ? die $msg : warn $msg;
70 $res = {};
71 last;
72 }
73 }
74
75 if ($res->{'package'} eq 'pve-web-news' && $res->{description}) {
76 $list->{'all'}->{$res->{'package'}} = $res;
77 next;
78 }
79
80 $res->{section} = 'unknown' if !$res->{section};
81
82 if ($res->{'package'} && $res->{type} && $res->{os} && $res->{version} &&
83 $res->{infopage}) {
84 my $template;
85 if ($res->{location}) {
86 $template = $res->{location};
87 $template =~ s|.*/([^/]+$PVE::Storage::vztmpl_extension_re)$|$1|;
88 if ($res->{location} !~ m|^([a-zA-Z]+)\://|) {
89 # relative localtion (no http:// prefix)
90 $res->{location} = "$source/$res->{location}";
91 }
92 } else {
93 my $arch = $res->{architecture} || 'i386';
94 $template = "$res->{os}-$res->{package}_$res->{version}_$arch.tar.gz";
95 $template =~ s/$res->{os}-$res->{os}-/$res->{os}-/;
96 $res->{location} = "$source/$res->{section}/$template";
97 }
98 $res->{source} = $source;
99 $res->{template} = $template;
100 $list->{$res->{section}}->{$template} = $res;
101 $list->{'all'}->{$template} = $res;
102 } else {
103 my $msg = "found incomplete appliance records\n";
104 $update ? die $msg : warn $msg;
105 }
106 }
107 }
108
109 sub read_aplinfo {
110 my ($filename, $list, $source, $update) = @_;
111
112 my $fh = IO::File->new("<$filename") ||
113 die "unable to open file '$filename' - $!\n";
114
115 eval { read_aplinfo_from_fh($fh, $list, $source, $update); };
116 my $err = $@;
117
118 close($fh);
119
120 die $err if $err;
121
122 return $list;
123 }
124
125 sub url_get {
126 my ($ua, $url, $file, $logfh) = @_;
127
128 my $req = HTTP::Request->new(GET => $url);
129
130 logmsg ($logfh, "start download $url");
131 my $res = $ua->request($req, $file);
132
133 if ($res->is_success) {
134 logmsg ($logfh, "download finished: " . $res->status_line);
135 return 0;
136 }
137
138 logmsg ($logfh, "download failed: " . $res->status_line);
139
140 return 1;
141 }
142
143 sub download_aplinfo {
144 my ($ua, $aplinfo, $logfd) = @_;
145
146 my $aplsrcurl = "$aplinfo->{url}/$aplinfo->{file}.gz";
147 my $aplsigurl = "$aplinfo->{url}/$aplinfo->{file}.asc";
148 my $host = $aplinfo->{host};
149
150 my $tmp = "$APL_INFO_DIRECTORY/pveam-${host}.tmp.$$";
151 my $tmpgz = "$tmp.gz";
152 my $sigfn = "$tmp.asc";
153
154 eval {
155
156 if (url_get($ua, $aplsigurl, $sigfn, $logfd) != 0) {
157 die "update failed - no signature file '$sigfn'\n";
158 }
159
160 if (url_get($ua, $aplsrcurl, $tmpgz, $logfd) != 0) {
161 die "update failed - no data file '$aplsrcurl'\n";
162 }
163
164 eval { run_command(["gunzip", "-f", $tmpgz]) };
165 die "update failed: unable to unpack '$tmpgz'\n" if $@;
166
167 # verify signature
168 my $trustedkeyring = "/usr/share/doc/pve-manager/trustedkeys.gpg";
169 my $cmd = "/usr/bin/gpgv -q --keyring $trustedkeyring $sigfn $tmp";
170
171 my $logfunc = sub { logmsg($logfd, "signature verification: $_[0]"); };
172 eval {
173 run_command($cmd, outfunc => $logfunc, errfunc => $logfunc);
174 };
175 die "unable to verify signature - $@\n" if $@;
176
177 # test syntax
178 eval { read_aplinfo($tmp, {}, $aplinfo->{url}, 1) };
179 die "update failed: $@" if $@;
180
181 rename($tmp, "$APL_INFO_DIRECTORY/$host") or
182 die "update failed: unable to store data: $!\n";
183
184 logmsg($logfd, "update successful");
185 };
186
187 my $err = $@;
188
189 unlink $tmp;
190 unlink $tmpgz;
191 unlink $sigfn;
192
193 die $err if $err;
194 }
195
196 sub get_apl_sources {
197 my $sources = [
198 {
199 host => "download.proxmox.com",
200 url => "http://download.proxmox.com/images",
201 file => 'aplinfo-pve-8.dat',
202 },
203 {
204 host => "releases.turnkeylinux.org",
205 url => "https://releases.turnkeylinux.org/pve",
206 file => 'aplinfo.dat',
207 },
208 ];
209
210 return $sources;
211 }
212
213 sub update {
214 my ($proxy) = @_;
215
216 my $logfile_size = -s $LOGFILE || 0;
217 if ($logfile_size > 1024 * 256) {
218 rename($LOGFILE, "$LOGFILE.0") or warn "failed to rotate log file $LOGFILE - $!\n";
219 }
220 my $logfd = IO::File->new (">>$LOGFILE");
221 logmsg($logfd, "starting update");
222
223 my $ua = LWP::UserAgent->new;
224 my $release = PVE::pvecfg::release();
225 $ua->agent("PVE/$release");
226
227 if ($proxy) {
228 $ua->proxy(['http', 'https'], $proxy);
229 } else {
230 $ua->env_proxy;
231 }
232
233 my $sources = get_apl_sources();
234
235 mkdir $APL_INFO_DIRECTORY;
236
237 my @dlerr = ();
238 foreach my $info (@$sources) {
239 eval {
240 download_aplinfo($ua, $info, $logfd);
241 };
242 if (my $err = $@) {
243 logmsg ($logfd, $err);
244 push @dlerr, $info->{url};
245 }
246 }
247
248 close($logfd);
249
250 return 0 if scalar(@dlerr);
251
252 return 1;
253 }
254
255 sub load_data {
256
257 my $sources = get_apl_sources();
258
259 my $list = {};
260 foreach my $info (@$sources) {
261 eval {
262 my $host = $info->{host};
263 read_aplinfo("$APL_INFO_DIRECTORY/$host", $list, $info->{url});
264 };
265 warn $@ if $@;
266 }
267
268 return $list;
269 }
270
271 1;
272