[pve-devel] [PATCH ha-manager] use split_args and shellquote instead of strange regex

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Mar 15 12:23:32 CET 2016


Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/HA/FenceConfig.pm                       | 15 ++++++++-------
 src/test/fence_cfgs/complex-params-1.cfg.expect |  4 ++--
 src/test/fence_cfgs/simple-1.cfg.expect         |  6 +++---
 src/test/fence_cfgs/simple-2.cfg.expect         | 18 +++++++++---------
 4 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/src/PVE/HA/FenceConfig.pm b/src/PVE/HA/FenceConfig.pm
index 50fa247..d7a611e 100644
--- a/src/PVE/HA/FenceConfig.pm
+++ b/src/PVE/HA/FenceConfig.pm
@@ -26,9 +26,7 @@ sub parse_config {
 	    if ($line =~ m/^(device|connect)\s+(\S+)\s+(\S+)\s+(.+)$/) {
 		my ($command, $dev_name, $target) = ($1, $2, $3);
 
-		# allow spaces, and other special chars inside of quoted strings
-		# with escape support
-		my @arg_array = $4 =~ /(\w+(?:=(?:(?:\"(?:[^"\\]|\\.)*\")|\S+))?)/g;
+		my $arg_array = PVE::Tools::split_args($4);
 
 		my $dev_number = 1; # default
 
@@ -47,7 +45,7 @@ sub parse_config {
 
 		    $dev->{sub_devs}->{$dev_number} = {
 			agent => $target,
-			args => [ @arg_array ]
+			args =>  $arg_array,
 		    };
 		    $dev->{priority} = $priority++ if !$dev->{priority};
 
@@ -70,7 +68,7 @@ sub parse_config {
 		    die "node '$node' already connected to device '$dev_name:$dev_number'\n" 
 			if $sdev->{node_args}->{$node};
 
-		    $sdev->{node_args}->{$node} = [ @arg_array ];
+		    $sdev->{node_args}->{$node} = $arg_array;
 
 		    $config->{$dev_name}->{sub_devs}->{$dev_number} = $sdev;
 		}
@@ -119,9 +117,12 @@ sub gen_arg_str {
 
     my @shell_args = ();
     foreach my $arg (@arguments) {
+	my ($key, $val) = split /=/, $arg;
 	# we need to differ long and short opts!
-	my $prefix = (length($arg) == 1) ? '-' : '--';
-	push @shell_args, "$prefix$arg";
+	my $prefix = (length($key) == 1) ? '-' : '--';
+	# shellquote values and add them again
+	$key  .= '='. PVE::Tools::shellquote($val) if $val;
+	push @shell_args, "$prefix$key";
     }
 
     return join (' ', @shell_args);
diff --git a/src/test/fence_cfgs/complex-params-1.cfg.expect b/src/test/fence_cfgs/complex-params-1.cfg.expect
index 820103d..12034b2 100644
--- a/src/test/fence_cfgs/complex-params-1.cfg.expect
+++ b/src/test/fence_cfgs/complex-params-1.cfg.expect
@@ -1,6 +1,6 @@
 [try] 0
-[node1-cmd] fence_pve --ip="192.168.15.38" --username="fäöµ€" --password="12\"345" --plug=500 -x
-[node2-cmd] fence_pve --ip="192.168.15.38" --username="fäöµ€" --password="12\"345" --plug=501
+[node1-cmd] fence_pve --ip=192.168.15.38 --username='fäöµ€' --password='12"345' --plug=500 -x
+[node2-cmd] fence_pve --ip=192.168.15.38 --username='fäöµ€' --password='12"345' --plug=501
 [node3-cmd] none
 [try] 1
 [node1-cmd] none
diff --git a/src/test/fence_cfgs/simple-1.cfg.expect b/src/test/fence_cfgs/simple-1.cfg.expect
index ddbf81d..9e2eddb 100644
--- a/src/test/fence_cfgs/simple-1.cfg.expect
+++ b/src/test/fence_cfgs/simple-1.cfg.expect
@@ -1,7 +1,7 @@
 [try] 0
-[node1-cmd] fence_pve --ip="192.168.15.38" --username=fence --password="12345" --action=off --plug=500
-[node2-cmd] fence_pve --ip="192.168.15.38" --username=fence --password="12345" --action=off --plug=501
-[node3-cmd] fence_pve --ip="192.168.15.38" --username=fence --password="12345" --action=off --plug=502
+[node1-cmd] fence_pve --ip=192.168.15.38 --username=fence --password=12345 --action=off --plug=500
+[node2-cmd] fence_pve --ip=192.168.15.38 --username=fence --password=12345 --action=off --plug=501
+[node3-cmd] fence_pve --ip=192.168.15.38 --username=fence --password=12345 --action=off --plug=502
 [try] 1
 [node1-cmd] none
 [node2-cmd] none
diff --git a/src/test/fence_cfgs/simple-2.cfg.expect b/src/test/fence_cfgs/simple-2.cfg.expect
index 2e6e289..3aef7c6 100644
--- a/src/test/fence_cfgs/simple-2.cfg.expect
+++ b/src/test/fence_cfgs/simple-2.cfg.expect
@@ -1,15 +1,15 @@
 [try] 0
-[node1-cmd] fence_pve --ip="192.168.XX.XX" --password="12345" --action=off --plug=100
-[node2-cmd] fence_pve --ip="192.168.XX.XX" --password="12345" --action=off --plug=101
-[node3-cmd] fence_pve --ip="192.168.XX.XX" --password="12345" --action=off --plug=102
+[node1-cmd] fence_pve --ip=192.168.XX.XX --password=12345 --action=off --plug=100
+[node2-cmd] fence_pve --ip=192.168.XX.XX --password=12345 --action=off --plug=101
+[node3-cmd] fence_pve --ip=192.168.XX.XX --password=12345 --action=off --plug=102
 [try] 1
-[node1-cmd] fence_ilo --ip="192.168.XY.XY" --password="54321" --action=off --plug=100
-[node2-cmd] fence_ilo --ip="192.168.XY.XY" --password="54321" --action=off --plug=101
-[node3-cmd] fence_ilo --ip="192.168.XY.XY" --password="54321" --action=off --plug=102
+[node1-cmd] fence_ilo --ip=192.168.XY.XY --password=54321 --action=off --plug=100
+[node2-cmd] fence_ilo --ip=192.168.XY.XY --password=54321 --action=off --plug=101
+[node3-cmd] fence_ilo --ip=192.168.XY.XY --password=54321 --action=off --plug=102
 [try] 2
-[node1-cmd] fence_apc_snmp --ip="192.168.XY.YXY" --password="12345" --action=off --plug=100
-[node2-cmd] fence_apc_snmp --ip="192.168.XY.YXY" --password="12345" --action=off --plug=101
-[node3-cmd] fence_apc_snmp --ip="192.168.XY.YXY" --password="12345" --action=off --plug=102
+[node1-cmd] fence_apc_snmp --ip=192.168.XY.YXY --password=12345 --action=off --plug=100
+[node2-cmd] fence_apc_snmp --ip=192.168.XY.YXY --password=12345 --action=off --plug=101
+[node3-cmd] fence_apc_snmp --ip=192.168.XY.YXY --password=12345 --action=off --plug=102
 [try] 3
 [node1-cmd] none
 [node2-cmd] none
-- 
2.1.4





More information about the pve-devel mailing list