]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/APLInfo.pm
appliance index: warn if log rotation fails
[pve-manager.git] / PVE / APLInfo.pm
index 3bb261fcb4c9cde26b33fb29732069eea100cb8d..affdde8ecdbe9dce66c8168fc6078f32d7f58252 100644 (file)
@@ -2,12 +2,16 @@ package PVE::APLInfo;
 
 use strict;
 use warnings;
+
 use IO::File;
-use PVE::SafeSyslog;
-use PVE::Tools;
 use LWP::UserAgent;
 use POSIX qw(strftime);
 
+use PVE::SafeSyslog;
+use PVE::Storage;
+use PVE::Tools qw(run_command);
+use PVE::pvecfg;
+
 my $logfile = "/var/log/pveam.log";
 my $aplinfodir = "/var/lib/pve-manager/apl-info";
 
@@ -16,7 +20,7 @@ sub logmsg {
 
     chomp $msg;
 
-    my $tstr = strftime ("%b %d %H:%M:%S", localtime);
+    my $tstr = strftime ("%F %H:%M:%S", localtime);
 
     foreach my $line (split (/\n/, $msg)) {
        print $logfd "$tstr $line\n";
@@ -30,7 +34,7 @@ sub read_aplinfo_from_fh {
 
     while (my $rec = <$fh>) {
        chomp $rec;
-       
+
        my $res = {};
 
        while ($rec) {
@@ -62,25 +66,25 @@ sub read_aplinfo_from_fh {
                $res->{lc $1} = $2;
            } else {
                my $msg = "unable to parse appliance record: $rec\n";
-               $update ? die $msg : warn $msg;         
+               $update ? die $msg : warn $msg;
                $res = {};
                last;
            }
        }
-       
+
        if ($res->{'package'} eq 'pve-web-news' && $res->{description}) {
-           $list->{'all'}->{$res->{'package'}} = $res;     
+           $list->{'all'}->{$res->{'package'}} = $res;
            next;
        }
 
        $res->{section} = 'unknown' if !$res->{section};
-       
+
        if ($res->{'package'} && $res->{type} && $res->{os} && $res->{version} &&
            $res->{infopage}) {
            my $template;
            if ($res->{location}) {
                $template = $res->{location};
-               $template =~ s|.*/([^/]+.tar.[gx]z)$|$1|;
+               $template =~ s|.*/([^/]+$PVE::Storage::vztmpl_extension_re)$|$1|;
                if ($res->{location} !~ m|^([a-zA-Z]+)\://|) {
                    # relative localtion (no http:// prefix)
                    $res->{location} = "$source/$res->{location}";
@@ -97,7 +101,7 @@ sub read_aplinfo_from_fh {
            $list->{'all'}->{$template} = $res;
        } else {
            my $msg = "found incomplete appliance records\n";
-           $update ? die $msg : warn $msg;             
+           $update ? die $msg : warn $msg;
        }
     }
 }
@@ -114,7 +118,7 @@ sub read_aplinfo {
     close($fh);
 
     die $err if $err;
-    
+
     return $list;
 }
 
@@ -137,10 +141,11 @@ sub url_get {
 }
 
 sub download_aplinfo {
-    my ($ua, $aplurl, $host, $logfd) = @_;
+    my ($ua, $aplinfo, $logfd) = @_;
 
-    my $aplsrcurl = "$aplurl/aplinfo.dat.gz";
-    my $aplsigurl = "$aplurl/aplinfo.dat.asc";
+    my $aplsrcurl = "$aplinfo->{url}/$aplinfo->{file}.gz";
+    my $aplsigurl = "$aplinfo->{url}/$aplinfo->{file}.asc";
+    my $host = $aplinfo->{host};
 
     my $tmp = "$aplinfodir/pveam-${host}.tmp.$$";
     my $tmpgz = "$tmp.gz";
@@ -155,41 +160,28 @@ sub download_aplinfo {
        if (url_get($ua, $aplsrcurl, $tmpgz, $logfd) != 0) {
            die "update failed - no data file '$aplsrcurl'\n";
        }
-       eval {
-           PVE::Tools::run_command(["gunzip", "-f", $tmpgz]);
-       };
-       die "update failed: unable to unpack '$tmpgz'\n" if $@;
-
 
+       eval { run_command(["gunzip", "-f", $tmpgz]) };
+       die "update failed: unable to unpack '$tmpgz'\n" if $@;
 
        # verify signature
        my $trustedkeyring = "/usr/share/doc/pve-manager/trustedkeys.gpg";
        my $cmd = "/usr/bin/gpgv -q --keyring $trustedkeyring $sigfn $tmp";
 
+       my $logfunc = sub { logmsg($logfd, "signature verification: $_[0]"); };
        eval {
-           my $logfunc = sub {
-               my $line = shift;
-               logmsg($logfd, "signature verification: $line");
-           };
-
-           PVE::Tools::run_command($cmd,
-                                   outfunc => $logfunc,
-                                   errfunc => $logfunc);
+           run_command($cmd, outfunc => $logfunc, errfunc => $logfunc);
        };
        die "unable to verify signature - $@\n" if $@;
 
        # test syntax
-       eval { 
-           read_aplinfo($tmp, {}, $aplurl, 1);
-       };
+       eval { read_aplinfo($tmp, {}, $aplinfo->{url}, 1) };
        die "update failed: $@" if $@;
 
-       if (!rename($tmp, "$aplinfodir/$host")) {
-           die "update failed: unable to store data\n";
-       }
+       rename($tmp, "$aplinfodir/$host") or
+           die "update failed: unable to store data: $!\n";
 
-       logmsg($logfd, "update sucessful");
+       logmsg($logfd, "update successful");
     };
 
     my $err = $@;
@@ -202,26 +194,35 @@ sub download_aplinfo {
 }
 
 sub get_apl_sources {
-    my $urls = [];
-    push @$urls, "http://download.proxmox.com/images";
-    push @$urls, "https://releases.turnkeylinux.org/pve";
-
-    return $urls;
+    my $sources = [
+       {
+           host => "download.proxmox.com",
+           url => "http://download.proxmox.com/images",
+           file => 'aplinfo-pve-8.dat',
+       },
+       {
+           host => "releases.turnkeylinux.org",
+           url => "https://releases.turnkeylinux.org/pve",
+           file => 'aplinfo.dat',
+       },
+    ];
+
+    return $sources;
 }
 
 sub update {
     my ($proxy) = @_;
 
-    my $size;
-    if (($size = (-s $logfile) || 0) > (1024*50)) {
-       rename($logfile, "$logfile.0");
+    my $logfile_size = -s $logfile || 0;
+    if ($logfile_size > 1024 * 256) {
+       rename($logfile, "$logfile.0") or warn "failed to rotate log file $logfile - $!\n";
     }
     my $logfd = IO::File->new (">>$logfile");
     logmsg($logfd, "starting update");
 
     my $ua = LWP::UserAgent->new;
-    $ua->agent("PVE/1.0");
+    my $release = PVE::pvecfg::release();
+    $ua->agent("PVE/$release");
 
     if ($proxy) {
        $ua->proxy(['http', 'https'], $proxy);
@@ -229,22 +230,20 @@ sub update {
        $ua->env_proxy;
     }
 
-    my $urls = get_apl_sources();
+    my $sources = get_apl_sources();
 
     mkdir $aplinfodir;
 
     my @dlerr = ();
-    foreach my $aplurl (@$urls) {
-       eval { 
-           my $uri = URI->new($aplurl);
-           my $host = $uri->host();
-           download_aplinfo($ua, $aplurl, $host, $logfd); 
+    foreach my $info (@$sources) {
+       eval {
+           download_aplinfo($ua, $info, $logfd);
        };
        if (my $err = $@) {
            logmsg ($logfd, $err);
-           push @dlerr, $aplurl; 
+           push @dlerr, $info->{url};
        }
-    } 
+    }
 
     close($logfd);
 
@@ -255,17 +254,13 @@ sub update {
 
 sub load_data {
 
-   my $urls = get_apl_sources();
+   my $sources = get_apl_sources();
 
     my $list = {};
-
-    foreach my $aplurl (@$urls) {
-
-       eval { 
-
-           my $uri = URI->new($aplurl);
-           my $host = $uri->host();
-           read_aplinfo("$aplinfodir/$host", $list, $aplurl);
+    foreach my $info (@$sources) {
+       eval {
+           my $host = $info->{host};
+           read_aplinfo("$aplinfodir/$host", $list, $info->{url});
        };
        warn $@ if $@;
     }