[pve-devel] [PATCH 2/2] moving more binary initialization code into prepare()

Wolfgang Bumiller w.bumiller at proxmox.com
Fri May 29 13:44:50 CEST 2015


These two are less trivial cases: They used to initialize the nodename
right away and use it in the $cmddef command description.
I have left the references to $nodename there for clarity for now.
The printmanpod action speicifically ignores this section of the $cmddef
structure, and prepare gets called right after checking for this
argument. Then the prepare function updates the nodename to the resolved
one in the $cmddef hash.
---
 bin/pveceph         | 37 +++++++++++++++++++++++++------------
 bin/pvesubscription | 31 ++++++++++++++++++++-----------
 2 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/bin/pveceph b/bin/pveceph
index 18a4e8b..092c569 100755
--- a/bin/pveceph
+++ b/bin/pveceph
@@ -24,19 +24,34 @@ use PVE::CLIHandler;
 
 use base qw(PVE::CLIHandler);
 
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+my $cmddef;
+my $nodename;
 
-initlog ('pveceph');
+sub prepare {
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-die "please run as root\n" if $> != 0;
+    initlog ('pveceph');
 
-PVE::INotify::inotify_init();
+    die "please run as root\n" if $> != 0;
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+    PVE::INotify::inotify_init();
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam');
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
+
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
+
+    $nodename = PVE::INotify::nodename();
+
+    keys %$cmddef;
+    while (my ($arg, $data) = each %$cmddef) {
+	my $fixed_args = $data->[3];
+	if ($fixed_args && exists $fixed_args->{node}) {
+	    $fixed_args->{node} = $nodename;
+	}
+    }
+}
 
 my $upid_exit = sub {
     my $upid = shift;
@@ -44,8 +59,6 @@ my $upid_exit = sub {
     exit($status eq 'OK' ? 0 : -1);
 };
 
-my $nodename = PVE::INotify::nodename();
-
 __PACKAGE__->register_method ({
     name => 'purge',
     path => 'purge',
@@ -140,7 +153,7 @@ __PACKAGE__->register_method ({
 	return undef;
     }});
 
-my $cmddef = {
+$cmddef = {
     init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
     lspools => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
 	my $res = shift;
@@ -169,7 +182,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/pvesubscription b/bin/pvesubscription
index e54bc52..dfcc0c7 100755
--- a/bin/pvesubscription
+++ b/bin/pvesubscription
@@ -13,25 +13,34 @@ use PVE::API2::Subscription;
 
 use base qw(PVE::CLIHandler);
 
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+my $cmddef;
+my $nodename;
 
-initlog('pvesubscription');
+sub prepare {
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-die "please run as root\n" if $> != 0;
+    initlog('pvesubscription');
 
-PVE::INotify::inotify_init();
+    die "please run as root\n" if $> != 0;
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+    PVE::INotify::inotify_init();
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam'); 
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
-my $nodename = PVE::INotify::nodename();
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam'); 
 
+    $nodename = PVE::INotify::nodename();
 
+    keys %$cmddef;
+    while (my ($arg, $data) = each %$cmddef) {
+	$data->[3]->{node} = $nodename;
+    }
+}
 
-my $cmddef = {
+
+$cmddef = {
     update => [ 'PVE::API2::Subscription', 'update', undef, { node => $nodename } ],
     get => [ 'PVE::API2::Subscription', 'get', undef, { node => $nodename }, 
 	     sub {
@@ -45,7 +54,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;
 
-- 
2.1.4





More information about the pve-devel mailing list