[pve-devel] [PATCH] improve pveupdate with random start time

Wolfgang Link w.link at proxmox.com
Tue Sep 1 12:47:34 CEST 2015


this is important to avoid that all PVE nodes start the update procedure at the same time
---
 bin/Makefile            |   5 ++-
 bin/cron/Makefile       |   6 ---
 bin/cron/daily/Makefile |  15 -------
 bin/cron/daily/pve      | 101 ------------------------------------------------
 bin/pveupdate           | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 debian/conffiles        |   1 -
 debian/postinst         |  15 +++++++
 7 files changed, 119 insertions(+), 125 deletions(-)
 delete mode 100644 bin/cron/Makefile
 delete mode 100644 bin/cron/daily/Makefile
 delete mode 100644 bin/cron/daily/pve
 create mode 100644 bin/pveupdate

diff --git a/bin/Makefile b/bin/Makefile
index fb8e89a..7782738 100644
--- a/bin/Makefile
+++ b/bin/Makefile
@@ -1,6 +1,6 @@
 include ../defines.mk
 
-SUBDIRS = init.d cron ocf test
+SUBDIRS = init.d ocf test
 
 SCRIPTS =  			\
 	pveceph			\
@@ -16,7 +16,8 @@ SCRIPTS =  			\
 	pvesubscription		\
 	pvemailforward.pl	\
 	pveupgrade		\
-	pveperf
+	pveperf			\
+	pveupdate
 
 MANS = 				\
 	pveceph.1		\
diff --git a/bin/cron/Makefile b/bin/cron/Makefile
deleted file mode 100644
index 937df58..0000000
--- a/bin/cron/Makefile
+++ /dev/null
@@ -1,6 +0,0 @@
-SUBDIRS = daily
-
-all: ${SUBDIRS}
-
-%:
-	set -e && for i in ${SUBDIRS}; do ${MAKE} -C $$i $@; done
diff --git a/bin/cron/daily/Makefile b/bin/cron/daily/Makefile
deleted file mode 100644
index f4bfb40..0000000
--- a/bin/cron/daily/Makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-include ../../../defines.mk
-
-all:
-
-.PHONY: install 
-install: pve
-	install -d ${CRONDAILYDIR}
-	install -m 0755 pve ${CRONDAILYDIR}
-
-.PHONY: distclean
-distclean: clean
-
-.PHONY: clean
-clean:
-	rm -rf *~
diff --git a/bin/cron/daily/pve b/bin/cron/daily/pve
deleted file mode 100644
index 47f15a2..0000000
--- a/bin/cron/daily/pve
+++ /dev/null
@@ -1,101 +0,0 @@
-#!/usr/bin/perl -w
-
-use strict;
-use IO::File;
-use File::Find;
-use File::stat;
-
-use PVE::INotify;
-use PVE::Cluster;
-use PVE::APLInfo;
-use PVE::SafeSyslog;
-use PVE::RPCEnvironment;
-use PVE::API2::Subscription;
-use PVE::API2::APT;
-
-initlog ('pvedailycron', 'daemon');
-
-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 at pam'); 
-
-my $nodename = PVE::INotify::nodename();
-
-eval { PVE::API2::Subscription->update({ node => $nodename }); };
-if (my $err = $@) {
-    syslog ('err', "update subscription info failed: $err");
-}
-
-my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
-eval { PVE::APLInfo::update($dccfg->{http_proxy}); };
-if (my $err = $@) {
-    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') {
-	eval { PVE::API2::APT->update_database({ node => $nodename, notify => 1, quiet => 1 }); };
-	if (my $err = $@) {
-	    syslog ('err', "update apt database failed: $err");
-	}
-    }
-}
-
-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++;
-	}
-    };
-
-    foreach my $subdir (qw(0 1 2 3 4 5 6 7 8 9 A B C D E F)) {
-	my $path = "$taskdir/$subdir";
-	find($wanted, $path);
-    }
-
-    if ($count) {
-	syslog('info', "cleanup removed $count task logs");
-    }
-}
-
-cleanup_tasks();
-
-exit (0);
diff --git a/bin/pveupdate b/bin/pveupdate
new file mode 100644
index 0000000..e9a2305
--- /dev/null
+++ b/bin/pveupdate
@@ -0,0 +1,101 @@
+#!/usr/bin/perl -w
+
+use strict;
+use IO::File;
+use File::Find;
+use File::stat;
+
+use PVE::INotify;
+use PVE::Cluster;
+use PVE::APLInfo;
+use PVE::SafeSyslog;
+use PVE::RPCEnvironment;
+use PVE::API2::Subscription;
+use PVE::API2::APT;
+
+initlog ('pvedailycron', 'daemon');
+
+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 at pam');
+
+my $nodename = PVE::INotify::nodename();
+
+eval { PVE::API2::Subscription->update({ node => $nodename }); };
+if (my $err = $@) {
+    syslog ('err', "update subscription info failed: $err");
+}
+
+my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
+eval { PVE::APLInfo::update($dccfg->{http_proxy}); };
+if (my $err = $@) {
+    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') {
+	eval { PVE::API2::APT->update_database({ node => $nodename, notify => 1, quiet => 1 }); };
+	if (my $err = $@) {
+	    syslog ('err', "update apt database failed: $err");
+	}
+    }
+}
+
+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++;
+	}
+    };
+
+    foreach my $subdir (qw(0 1 2 3 4 5 6 7 8 9 A B C D E F)) {
+	my $path = "$taskdir/$subdir";
+	find($wanted, $path);
+    }
+
+    if ($count) {
+	syslog('info', "cleanup removed $count task logs");
+    }
+}
+
+cleanup_tasks();
+
+exit (0);
diff --git a/debian/conffiles b/debian/conffiles
index 2bb0027..0acb6aa 100644
--- a/debian/conffiles
+++ b/debian/conffiles
@@ -3,7 +3,6 @@
 /etc/init.d/pveproxy
 /etc/init.d/spiceproxy
 /etc/init.d/pvestatd
-/etc/cron.daily/pve
 /etc/vz/vznet.conf
 /etc/vzdump.conf
 /etc/logrotate.d/pve
diff --git a/debian/postinst b/debian/postinst
index bd17b0f..9498fab 100755
--- a/debian/postinst
+++ b/debian/postinst
@@ -43,6 +43,21 @@ case "$1" in
     # remove old APL dir
     rm -rf /var/lib/pve-manager/apl-available
 
+    # remove old cron.daily update job to randomize it
+    if test -e /etc/cron.daily/pve; then
+	echo "Remove old update script from cron.daily"
+	rm /etc/cron.daily/pve
+    fi
+
+    # create new daily randomize update cronjob if not exist
+    if !(test -e /etc/cron.d/pveupdate); then
+	MIN="$(shuf -i 0-59 -n 1)"
+
+	HOUR="$(shuf -i 6-10 -n 1)"
+	echo "Create cron job for daily update"
+	printf " ${MIN} ${HOUR} * * * root /usr/bin/pveupdate\n\n" > /etc/cron.d/pveupdate
+    fi
+
     if test ! -e /var/lib/pve-manager/apl-info/download.proxmox.com; then
 	mkdir -p /var/lib/pve-manager/apl-info
 	cp /usr/share/doc/pve-manager/aplinfo.dat /var/lib/pve-manager/apl-info/download.proxmox.com
-- 
2.1.4





More information about the pve-devel mailing list