[pve-devel] [RFC v2] Use JSONSchema to parse vzdump config

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Jul 27 11:02:43 CEST 2015


Instead of a lot of hardcoded if's use JSONSchema::parse_config to
parse and validate vzdump.conf. To do that $confdesc was extended
to match a valid schema.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 PVE/VZDump.pm | 58 +++++++++++++++++-----------------------------------------
 1 file changed, 17 insertions(+), 41 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 805123d..b5c6591 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -27,6 +27,8 @@ my $logdir = '/var/log/vzdump';
 
 my @plugins = qw();
 
+my $confdesc;
+
 # Load available plugins
 my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
 foreach my $plug (@pve_vzdump_classes) {
@@ -181,7 +183,7 @@ sub read_vzdump_defaults {
 
     my $fn = "/etc/vzdump.conf";
 
-    my $res = {
+    my $defaults = {
 	bwlimit => 0,
 	ionice => 7,
 	size => 1024,
@@ -193,48 +195,22 @@ sub read_vzdump_defaults {
     };
 
     my $fh = IO::File->new ("<$fn");
-    return $res if !$fh;
-    
-    my $line;
-    while (defined ($line = <$fh>)) {
-	next if $line =~ m/^\s*$/;
-	next if $line =~ m/^\#/;
-
-	if ($line =~ m/tmpdir:\s*(.*\S)\s*$/) {
-	    $res->{tmpdir} = $1;
-	} elsif ($line =~ m/dumpdir:\s*(.*\S)\s*$/) {
-	    $res->{dumpdir} = $1;
-	} elsif ($line =~ m/storage:\s*(\S+)\s*$/) {
-	    $res->{storage} = $1;
-	} elsif ($line =~ m/script:\s*(.*\S)\s*$/) {
-	    $res->{script} = $1;
-	} elsif ($line =~ m/bwlimit:\s*(\d+)\s*$/) {
-	    $res->{bwlimit} = int($1);
-	} elsif ($line =~ m/ionice:\s*([0-8])\s*$/) {
-	    $res->{ionice} = int($1);
-	} elsif ($line =~ m/lockwait:\s*(\d+)\s*$/) {
-	    $res->{lockwait} = int($1);
-	} elsif ($line =~ m/stopwait:\s*(\d+)\s*$/) {
-	    $res->{stopwait} = int($1);
-	} elsif ($line =~ m/stop:\s*(\d+)\s*$/) {
-	    $res->{stop} = int($1);
-	} elsif ($line =~ m/size:\s*(\d+)\s*$/) {
-	    $res->{size} = int($1);
-	} elsif ($line =~ m/maxfiles:\s*(\d+)\s*$/) {
-	    $res->{maxfiles} = int($1);
-	} elsif ($line =~ m/exclude-path:\s*(.*)\s*$/) {
-	    $res->{'exclude-path'} = PVE::Tools::split_args($1); 
-	} elsif ($line =~ m/mode:\s*(stop|snapshot|suspend)\s*$/) {
-	    $res->{mode} = $1;
-	} elsif($line =~ m/pigz:\s*(\d+)\s*/) {
-	    $res->{pigz} = $1;
-	} else {
-	    debugmsg ('warn', "unable to parse configuration file '$fn' - error at line " . $., undef, 1);
-	}
+    return $defaults if !$fh;
 
-    }
+    my $raw = do {
+	local $/ = undef;
+	<$fh>;
+    };
     close ($fh);
 
+    my $conf_schema = { type => 'object', properties => $confdesc, };
+
+    my $res = PVE::JSONSchema::parse_config($conf_schema, $fn, $raw);
+
+    foreach my $key (keys %$defaults) {
+	$res->{$key} = $defaults->{$key} if !$res->{$key};
+    }
+
     return $res;
 }
 
@@ -1105,7 +1081,7 @@ sub exec_backup {
     unlink $pidfile;
 }
 
-my $confdesc = {
+$confdesc = {
     vmid => {
 	type => 'string', format => 'pve-vmid-list',		
 	description => "The ID of the VM you want to backup.",
-- 
2.1.4




More information about the pve-devel mailing list