[pve-devel] [PATCH v4 manager 1/5] unified the classification of the passed command for all shell apis

Tim Marx t.marx at proxmox.com
Mon Jan 28 14:55:47 CET 2019


As discussed on the pve-devel list [0] I extracted the identical chunks from each
shell api into one function as basis for future changes.

[0] https://pve.proxmox.com/pipermail/pve-devel/2019-January/035387.html

Signed-off-by: Tim Marx <t.marx at proxmox.com>
---
 PVE/API2/Nodes.pm | 86 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 51 insertions(+), 35 deletions(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 52f537ba..24bb6a1b 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -697,6 +697,25 @@ __PACKAGE__->register_method({
 
 my $sslcert;
 
+my $cmdMapper = {
+    'login' => [ '/bin/login', '-f', 'root' ],
+    'upgrade' => [ '/usr/bin/pveupgrade', '--shell' ],
+};
+
+sub get_shell_command  {
+    my ($user, $shellcmd) = @_;
+
+    if ($user eq 'root at pam') {
+	if (exists($cmdMapper->{$shellcmd})) {
+	    return $cmdMapper->{$shellcmd};
+	} else {
+	    return [ '/bin/login', '-f', 'root' ];
+	}
+    } else {
+	return [ '/bin/login' ];
+    }
+}
+
 __PACKAGE__->register_method ({
     name => 'vncshell', 
     path => 'vncshell',  
@@ -717,6 +736,13 @@ __PACKAGE__->register_method ({
 		optional => 1,
 		default => 0,
 	    },
+	    cmd => {
+		type => 'string',
+		description => "Run specific command or default to login.",
+		enum => [keys %$cmdMapper],
+		optional => 1,
+		default => 'login',
+	    },
 	    websocket => {
 		optional => 1,
 		type => 'boolean',
@@ -782,20 +808,11 @@ __PACKAGE__->register_method ({
 	# so we select the fastest chipher here (or 'none'?)
 	my $remcmd = $remip ? 
 	    ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : [];
-
-	my $shcmd;
-
-	if ($user eq 'root at pam') {
-	    if ($param->{upgrade}) {
-		my $upgradecmd = "pveupgrade --shell";
-		$upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
-		$shcmd = [ '/bin/bash', '-c', $upgradecmd ];
-	    } else {
-		$shcmd = [ '/bin/login', '-f', 'root' ];
-	    }
-	} else {
-	    $shcmd = [ '/bin/login' ];
+	
+	if ($param->{upgrade}) {
+	    $param->{cmd} = 'upgrade';
 	}
+	my $shcmd = get_shell_command($user, $param->{cmd});
 
 	my $timeout = 10; 
 
@@ -876,6 +893,13 @@ __PACKAGE__->register_method ({
 		optional => 1,
 		default => 0,
 	    },
+	    cmd => {
+		type => 'string',
+		description => "Run specific command or default to login.",
+		enum => [keys %$cmdMapper],
+		optional => 1,
+		default => 'login',
+	    },
 	},
     },
     returns => {
@@ -915,17 +939,10 @@ __PACKAGE__->register_method ({
 	my $remcmd = $remip ?
 	    ['/usr/bin/ssh', '-e', 'none', '-t', $remip , '--'] : [];
 
-	my $concmd;
-
-	if ($user eq 'root at pam') {
-	    if ($param->{upgrade}) {
-		$concmd = [ '/usr/bin/pveupgrade', '--shell' ];
-	    } else {
-		$concmd = [ '/bin/login', '-f', 'root' ];
-	    }
-	} else {
-	    $concmd = [ '/bin/login' ];
+	if ($param->{upgrade}) {
+	    $param->{cmd} = 'upgrade';
 	}
+	my $shcmd = get_shell_command($user, $param->{cmd});
 
 	my $realcmd = sub {
 	    my $upid = shift;
@@ -934,7 +951,7 @@ __PACKAGE__->register_method ({
 
 	    my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
 		       '--perm', 'Sys.Console',  '--'];
-	    push  @$cmd, @$remcmd, @$concmd;
+	    push  @$cmd, @$remcmd, @$shcmd;
 
 	    PVE::Tools::run_command($cmd);
 	};
@@ -1023,6 +1040,13 @@ __PACKAGE__->register_method ({
 		optional => 1,
 		default => 0,
 	    },
+	    cmd => {
+		type => 'string',
+		description => "Run specific command or default to login.",
+		enum => [keys %$cmdMapper],
+		optional => 1,
+		default => 'login',
+	    },
 	},
     },
     returns => get_standard_option('remote-viewer-config'),
@@ -1044,18 +1068,10 @@ __PACKAGE__->register_method ({
 	my $authpath = "/nodes/$node";
 	my $permissions = 'Sys.Console';
 
-	my $shcmd;
-
-	if ($user eq 'root at pam') {
-	    if ($param->{upgrade}) {
-		my $upgradecmd = "pveupgrade --shell";
-		$shcmd = [ '/bin/bash', '-c', $upgradecmd ];
-	    } else {
-		$shcmd = [ '/bin/login', '-f', 'root' ];
-	    }
-	} else {
-	    $shcmd = [ '/bin/login' ];
+	if ($param->{upgrade}) {
+	    $param->{cmd} = 'upgrade';
 	}
+	my $shcmd = get_shell_command($user, $param->{cmd});
 
 	my $title = "Shell on '$node'";
 
-- 
2.11.0




More information about the pve-devel mailing list