[pve-devel] [PATCH v2] move preparations into prepare()

Wolfgang Bumiller w.bumiller at proxmox.com
Fri May 29 15:56:07 CEST 2015


Moving some preparation code found at the top level of some binaries
into a prepare() sub.
---
 bin/pveceph         | 25 ++++++++++++++-----------
 bin/pvedaemon       | 10 ++++++----
 bin/pveproxy        | 18 ++++++++++--------
 bin/pvestatd        | 17 ++++++++++-------
 bin/pvesubscription | 23 ++++++++++++-----------
 bin/spiceproxy      | 18 ++++++++++--------
 6 files changed, 62 insertions(+), 49 deletions(-)

diff --git a/bin/pveceph b/bin/pveceph
index 18a4e8b..a7467d9 100755
--- a/bin/pveceph
+++ b/bin/pveceph
@@ -24,19 +24,24 @@ use PVE::CLIHandler;
 
 use base qw(PVE::CLIHandler);
 
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+my $cmddef;
+my $nodename = PVE::INotify::nodename();
+
+sub prepare {
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-initlog ('pveceph');
+    initlog ('pveceph');
 
-die "please run as root\n" if $> != 0;
+    die "please run as root\n" if $> != 0;
 
-PVE::INotify::inotify_init();
+    PVE::INotify::inotify_init();
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam');
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
+}
 
 my $upid_exit = sub {
     my $upid = shift;
@@ -44,8 +49,6 @@ my $upid_exit = sub {
     exit($status eq 'OK' ? 0 : -1);
 };
 
-my $nodename = PVE::INotify::nodename();
-
 __PACKAGE__->register_method ({
     name => 'purge',
     path => 'purge',
@@ -169,7 +172,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, "pveceph", $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, "pveceph", $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit 0;
 
diff --git a/bin/pvedaemon b/bin/pvedaemon
index 75fe20d..5440a0c 100755
--- a/bin/pvedaemon
+++ b/bin/pvedaemon
@@ -34,11 +34,13 @@ my %daemon_options = (
     leave_children_open_on_reload => 1,
 );
 
-# create dir for dtach sockets
-mkdir "/var/run/dtach";
-
 my $daemon = __PACKAGE__->new('pvedaemon', $cmdline, %daemon_options); 
 
+sub prepare {
+    # create dir for dtach sockets
+    mkdir "/var/run/dtach";
+}
+
 sub init {
     my ($self) = @_;
 
@@ -83,7 +85,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/pveproxy b/bin/pveproxy
index 3864444..df7eb10 100755
--- a/bin/pveproxy
+++ b/bin/pveproxy
@@ -51,15 +51,17 @@ my %daemon_options = (
     pidfile => '/var/run/pveproxy/pveproxy.pid',
 );
 
-my $rundir="/var/run/pveproxy";
-if (mkdir($rundir, 0700)) { # only works at first start if we are root)
-    my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
-    my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
-    chown($uid, $gid, $rundir);
+my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
+
+sub prepare {
+    my $rundir="/var/run/pveproxy";
+    if (mkdir($rundir, 0700)) { # only works at first start if we are root)
+	my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
+	my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
+	chown($uid, $gid, $rundir);
+    }
 }
 
-my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options); 
-
 sub add_dirs {
     my ($result_hash, $alias, $subdir) = @_;
 
@@ -160,7 +162,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/pvestatd b/bin/pvestatd
index 3a4e8f2..6e66207 100755
--- a/bin/pvestatd
+++ b/bin/pvestatd
@@ -27,15 +27,18 @@ my $cmdline = [$0, @ARGV];
 my %daemon_options = (restart_on_error => 5, stop_wait_time => 5);
 
 my $daemon = __PACKAGE__->new('pvestatd', $cmdline, %daemon_options);
+my $nodename = PVE::INotify::nodename();
+my $restart_request;
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+sub prepare {
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam');
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
 
-my $nodename = PVE::INotify::nodename();
-my $restart_request = 0;
+    $restart_request = 0;
+}
 
 sub init {
     my ($self) = @_;
@@ -354,7 +357,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/pvesubscription b/bin/pvesubscription
index e54bc52..642fb84 100755
--- a/bin/pvesubscription
+++ b/bin/pvesubscription
@@ -13,22 +13,23 @@ use PVE::API2::Subscription;
 
 use base qw(PVE::CLIHandler);
 
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
-
-initlog('pvesubscription');
+my $nodename = PVE::INotify::nodename();
 
-die "please run as root\n" if $> != 0;
+sub prepare {
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-PVE::INotify::inotify_init();
+    initlog('pvesubscription');
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+    die "please run as root\n" if $> != 0;
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam'); 
+    PVE::INotify::inotify_init();
 
-my $nodename = PVE::INotify::nodename();
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
+}
 
 
 my $cmddef = {
@@ -45,7 +46,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, "pvesubscription", $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, "pvesubscription", $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit 0;
 
diff --git a/bin/spiceproxy b/bin/spiceproxy
index 07acf32..c053144 100755
--- a/bin/spiceproxy
+++ b/bin/spiceproxy
@@ -40,15 +40,17 @@ my %daemon_options = (
     pidfile => '/var/run/pveproxy/spiceproxy.pid',
     );
 
-my $rundir="/var/run/pveproxy";
-if (mkdir($rundir, 0700)) { # only works at first start if we are root)
-    my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
-    my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
-    chown($uid, $gid, $rundir);
-}
-
 my $daemon = __PACKAGE__->new('spiceproxy', $cmdline, %daemon_options); 
 
+sub prepare {
+    my $rundir="/var/run/pveproxy";
+    if (mkdir($rundir, 0700)) { # only works at first start if we are root)
+	my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
+	my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
+	chown($uid, $gid, $rundir);
+    }
+}
+
 sub init {
     my ($self) = @_;
 
@@ -101,7 +103,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
-- 
2.1.4





More information about the pve-devel mailing list