pve 2.17 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/perl -w

use strict;
use IO::File;
use File::Find;
use File::stat;

8
use PVE::INotify;
9 10 11 12 13
use PVE::Cluster;
use PVE::APLInfo;
use PVE::SafeSyslog;
use PVE::RPCEnvironment;
use PVE::API2::Subscription;
14
use PVE::API2::APT;
15

16 17
initlog ('pvedailycron', 'daemon');

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
die "please run as root\n" if $> != 0;

$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';

PVE::INotify::inotify_init();

my $rpcenv = PVE::RPCEnvironment->init('cli');

$rpcenv->init_request();
$rpcenv->set_language($ENV{LANG});
$rpcenv->set_user('root@pam'); 

my $nodename = PVE::INotify::nodename();

eval { PVE::API2::Subscription->update({ node => $nodename }); };
if (my $err = $@) {
34
    syslog ('err', "update subscription info failed: $err");
35 36
}

37 38 39
my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
eval { PVE::APLInfo::update($dccfg->{http_proxy}); };
if (my $err = $@) {
40 41 42 43 44 45 46
    syslog ('err', "update appliance info failed - see /var/log/pveam.log for details");
}

if (my $info = PVE::INotify::read_file('subscription')) {
    # We assume that users with subscriptions want informations
    # about new packages.
    if ($info->{status} eq 'Active') {
47
	eval { PVE::API2::APT->update_database({ node => $nodename, notify => 1, quiet => 1 }); };
48 49 50 51
	if (my $err = $@) {
	    syslog ('err', "update apt database failed: $err");
	}
    }
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

sub cleanup_tasks {

    my $taskdir = "/var/log/pve/tasks";
    my $filename = "$taskdir/index.1";

    my $fh = IO::File->new($filename, O_RDONLY);
    return if !$fh;

    my $endtime = 0;
    while (defined(my $line = <$fh>)) {
	if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
	    $endtime = hex($3);
	    last;
	}
    }
    close($fh);

    return if !$endtime;

    # print "delete task older that $endtime\n" . localtime($endtime) . "\n";

    my $count = 0;

    my $wanted = sub {
	my $filename = $_;

	return if $filename !~ m/^UPID:/;

	my $st;
	if (($st = stat($filename)) && ($st->mtime < $endtime)) {
	    unlink($filename);
	    $count++;
	}
    };

89
    foreach my $subdir (qw(0 1 2 3 4 5 6 7 8 9 A B C D E F)) {
90 91 92 93 94 95 96 97 98 99 100 101
	my $path = "$taskdir/$subdir";
	find($wanted, $path);
    }

    if ($count) {
	syslog('info', "cleanup removed $count task logs");
    }
}

cleanup_tasks();

exit (0);