[pve-devel] r5052 - pve-storage/pve2

svn-commits at proxmox.com svn-commits at proxmox.com
Wed Aug 25 14:04:24 CEST 2010


Author: dietmar
Date: 2010-08-25 12:04:24 +0000 (Wed, 25 Aug 2010)
New Revision: 5052

Modified:
   pve-storage/pve2/ChangeLog
   pve-storage/pve2/pvesm
Log:
* pvesm: use new PVE::CLIHandler



Modified: pve-storage/pve2/ChangeLog
===================================================================
--- pve-storage/pve2/ChangeLog	2010-08-25 12:02:59 UTC (rev 5051)
+++ pve-storage/pve2/ChangeLog	2010-08-25 12:04:24 UTC (rev 5052)
@@ -1,5 +1,7 @@
 2010-08-25  Proxmox Support Team  <support at proxmox.com>
 
+	* pvesm: use new PVE::CLIHandler
+
 	* PVE/API2/Storage.pm: create extra upload method, because this
 	have different 'proxy' requirements that normal 'create'
 

Modified: pve-storage/pve2/pvesm
===================================================================
--- pve-storage/pve2/pvesm	2010-08-25 12:02:59 UTC (rev 5051)
+++ pve-storage/pve2/pvesm	2010-08-25 12:04:24 UTC (rev 5052)
@@ -13,8 +13,10 @@
 use PVE::API2::Storage::Status;
 use PVE::API2::Storage::Scan;
 
-use Data::Dumper; # fixme: remove 
+use PVE::CLIHandler;
 
+use base qw(PVE::CLIHandler);
+
 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
 die "please run as root\n" if $> != 0;
@@ -27,102 +29,154 @@
 
 $rpcenv->set_user('root'); 
 
-# fixme:
-sub print_usage {
-    my $msg = shift;
+__PACKAGE__->register_method ({
+    name => 'path', 
+    path => 'path',
+    method => 'GET',
+    description => "Get filesystem path for specified volume",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    volume => {
+		description => "Volume identifier",
+		type => 'string', format => 'pve-volume-id',
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    
+    code => sub {
+	my ($param) = @_;
 
-    print STDERR "ERROR: $msg\n" if $msg;
-    print STDERR "USAGE: pvesm <COMMAND> [OPTIONS]\n";
-}
+	my $cfg = read_file('storagecfg');
 
-if (!scalar (@ARGV)) {
-    print_usage ("no command specified");
-    exit (-1);
-}
+	my $path = PVE::Storage::path ($cfg, $param->{volume});
 
-sub print_content {
-    my ($sid, $list) = @_;
+	print "$path\n";
 
-    my $maxlenname = 0;
-    foreach my $info (@$list) {
+	return undef;
 
-	my $volname = $info->{volname};
-	my $volid = "$sid:$volname";
-	my $sidlen =  length ($volid);
-	$maxlenname = $sidlen if $sidlen > $maxlenname;
-    }
+    }});
 
-    foreach my $info (@$list) {
-	my $volname = $info->{volname};
-	my $volid = "$sid:$volname";
-	next if !$info->{vmid};
+__PACKAGE__->register_method ({
+    name => 'lock', 
+    path => 'lock',
+    method => 'PUT',
+    description => "Lock specified storage",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    storage => { 
+		type => 'string', format => 'pve-storage-id',
+	    },
+	    timeout => {
+		type => 'integer',
+		minimum => 1,
+		optional => 1,
+	    }
+	},
+    },
+    returns => { type => 'null' },
+    
+    code => sub {
+	my ($param) = @_;
 
-	printf "%-${maxlenname}s %5s %10d %d\n", $volid, 
-	$info->{format}, $info->{size}, $info->{vmid}; 
-    }
+	my $storeid = $param->{storage};
+	my $timeout = $param->{timeout};
 
-    foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
-	next if $info->{vmid};
-	my $volname = $info->{volname};
-	my $volid = "$sid:$volname";
+	$timeout = 10 if !$timeout;
 
-	printf "%-${maxlenname}s %5s %10d\n", $volid, 
-	$info->{format}, $info->{size}; 
-    }
-}
+	PVE::Storage::parse_storage_id ($storeid);
 
-my $cmd = shift;
+	my $lockdir = "/var/run/pve-storage";
+	mkpath $lockdir;
 
-if ($cmd eq 'add') {
+	my $filename = "$lockdir/lock-$storeid.lock";
 
-    my $opts = {};
+	my $fh;
 
-    $opts->{storage} = shift;
+	eval {
 
-    PVE::API2::Storage::Config->cli_handler('create', \@ARGV, $opts);
+	    $SIG{PIPE} = sub { die "got signal - broken pipe\n"; };
+	    $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "got signal\n";  };
 
-} elsif ($cmd eq 'set') {
+	    local $SIG{ALRM} = sub { die "got timeout\n"; };
 
-    my $opts = {};
+	    alarm ($timeout);
 
-    $opts->{storage} = shift;
+	    $fh = new IO::File (">>$filename") ||
+		die "can't open lock for storage '$storeid' - $!\n";
 
-    PVE::API2::Storage::Config->cli_handler('update', \@ARGV, $opts);
+	    if (!flock ($fh, LOCK_EX|LOCK_NB)) {
+		print STDERR "trying to aquire storage lock '$storeid' ...";
+		if (!flock ($fh, LOCK_EX)) {
+		    print STDERR " failed\n";
+		    die "can't aquire lock for storage '$storeid' - $!\n";
+		}
+		print STDERR " OK\n";
+	    }
+	    alarm (0);
+	};
+	my $err = $@;
 
-} elsif ($cmd eq 'remove') {
+	alarm (0);
 
-    my $opts = {};
+	if ($err) {
+	    close ($fh) if $fh;
+	    print "lock $storeid: $err\n";
+	    *STDOUT->flush();
+	    return undef;
+	}
 
-    $opts->{storage} = shift;
+	print "lock $storeid: success\n";
+	*STDOUT->flush();
 
-    PVE::API2::Storage::Config->cli_handler('delete', \@ARGV, $opts);
+	while (my $line = <>) {
+	    chomp $line;
+	    last if $line =~ m/^release$/;
+	}
 
-} elsif ($cmd eq 'enable') {
+	close ($fh);
 
-    my $opts = {};
+	return undef;
+    }});
 
-    $opts->{storage} = shift;
-    $opts->{disable} = 0;
+my $print_content = sub {
+    my ($list) = @_;
 
-    PVE::API2::Storage::Config->cli_handler('update', \@ARGV, $opts);
+    # fixme: my $volid = "$sid:$volname"; ?
 
-} elsif ($cmd eq 'disable') {
+    my $maxlenname = 0;
+    foreach my $info (@$list) {
 
-    my $opts = {};
+	my $volname = $info->{volname};
+	my $volid = "$volname";
+	my $sidlen =  length ($volid);
+	$maxlenname = $sidlen if $sidlen > $maxlenname;
+    }
 
-    $opts->{storage} = shift;
-    $opts->{disable} = 1;
+    foreach my $info (@$list) {
+	my $volname = $info->{volname};
+	my $volid = "$volname";
+	next if !$info->{vmid};
 
-    PVE::API2::Storage::Config->cli_handler('update', \@ARGV, $opts);
+	printf "%-${maxlenname}s %5s %10d %d\n", $volid, 
+	$info->{format}, $info->{size}, $info->{vmid}; 
+    }
 
-} elsif ($cmd eq 'status') {
+    foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
+	next if $info->{vmid};
+	my $volname = $info->{volname};
+	my $volid = "$volname";
 
-    my $opts = {};
+	printf "%-${maxlenname}s %5s %10d\n", $volid, 
+	$info->{format}, $info->{size}; 
+    }
+};
 
-    $opts->{node} = read_file('hostname');
+my $print_status = sub {
+    my $res = shift;
 
-    my $res = PVE::API2::Storage::Status->cli_handler('index', \@ARGV, $opts);
-
     my $maxlen = 0;
     foreach my $res (@$res) {
 	my $storeid = $res->{storage};
@@ -139,153 +193,46 @@
 	$res->{type},$res->{disable}, $res->{active},
 	$res->{avail}, $res->{used}, $per; 
     }
+};
 
-} elsif ($cmd eq 'list') {
+my $cmddef = {
+    add => [ "PVE::API2::Storage::Config", 'create', ['storage'] ],
+    set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
+    remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
+    status => [ "PVE::API2::Storage::Status", 'index', [], 
+		{ node => read_file('hostname') }, $print_status ],
+    list => [ "PVE::API2::Storage::Content", 'index', ['storage'], 
+	      { node => read_file('hostname') }, $print_content ],
+    alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'], 
+	       { node => read_file('hostname') }, sub {
+		   my $volid = shift;
+		   print "sucessfuly created '$volid'\n";
+	       }],
+    free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
+	      { node => read_file('hostname') } ],
+    scan => [ "PVE::API2::Storage::Scan", 'scan', ['method', 'server'],
+	      { node => read_file('hostname') }, sub  {
+		  my $res = shift;
 
-   my $opts = {};
+		  my $maxlen = 0;
+		  foreach my $k (keys %$res) {
+		      my $len = length ($k);
+		      $maxlen = $len if $len > $maxlen;
+		  }
+		  foreach my $k (keys %$res) {
+		      printf "%-${maxlen}s $res->{$k}\n", $k;
+		  }
+	      }],
+    path => [ __PACKAGE__, 'path', ['volume']],
+    lock => [ __PACKAGE__, 'lock', ['storage']],
+};
 
-   $opts->{storage} = shift;
-   $opts->{node} = read_file('hostname');
+my $cmd = shift;
 
-   my $res = PVE::API2::Storage::Content->cli_handler('index', \@ARGV, $opts);
+PVE::CLIHandler::handle_cmd($cmddef, "pvesm", $cmd, \@ARGV);
 
-   print_content($opts->{storage}, $res);
+exit 0;
 
-} elsif ($cmd eq 'alloc') {
-
-   my $opts = {};
-
-   $opts->{node} = read_file('hostname');
-
-   my $volid = PVE::API2::Storage::Content->cli_handler('create', \@ARGV, $opts);
-
-   print "sucessfuly created '$volid'\n";
-
-} elsif ($cmd eq 'free') {
-
-    if (scalar (@ARGV) != 1) {
-	die "wrong number of arguments\n";
-    }
-
-    my $opts = {};
-
-    $opts->{node} = read_file('hostname');
-    $opts->{volume} = shift;
-
-    PVE::API2::Storage::Content->cli_handler('delete', \@ARGV, $opts);
-
-} elsif ($cmd eq 'scan') {
-
-    if (scalar (@ARGV) != 2) {
-	die "wrong number of arguments\n";
-    }
-
-    my $opts = {};
-
-    $opts->{method} = shift;
-    $opts->{server} = shift;
-
-    my $res = PVE::API2::Storage::Scan->cli_handler('scan', \@ARGV, $opts);
-
-    my $maxlen = 0;
-    foreach my $k (keys %$res) {
-	my $len = length ($k);
-	$maxlen = $len if $len > $maxlen;
-    }
-    foreach my $k (keys %$res) {
-	printf "%-${maxlen}s $res->{$k}\n", $k;
-    }
-
-} elsif ($cmd eq 'path') {
-
-    my $opts = {};
-
-    if (scalar (@ARGV) != 1) {
-	die "wrong number of arguments\n";
-    }
-
-    my $volid = shift;
-
-    my $cfg = read_file('storagecfg');
-
-    my $path = PVE::Storage::path ($cfg, $volid);
-
-    print "$path\n";
-
-} elsif ($cmd eq 'lock') {
-
-    if (scalar (@ARGV) != 1 && scalar (@ARGV) != 2) {
-	die "wrong number of arguments\n";
-    }
-
-    my $storeid = shift;
-    my $timeout = shift;
-
-    $timeout = 10 if !$timeout;
-
-    PVE::Storage::parse_storage_id ($storeid);
-
-    die "got strange value for timeout ('$timeout')\n" if $timeout !~ m/^\d+$/;
-
-    my $lockdir = "/var/run/pve-storage";
-    mkpath $lockdir;
-
-    my $filename = "$lockdir/lock-$storeid.lock";
-
-    my $fh;
-
-    $SIG{PIPE} = sub { die "got signal - broken pipe\n"; };
-    $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "got signal\n";  };
-
-    eval {
-
-	local $SIG{ALRM} = sub { die "got timeout\n"; };
-
-	alarm ($timeout);
-
-	$fh = new IO::File (">>$filename") ||
-	    die "can't open lock for storage '$storeid' - $!\n";
-
-	if (!flock ($fh, LOCK_EX|LOCK_NB)) {
-	    print STDERR "trying to aquire storage lock '$storeid' ...";
-	    if (!flock ($fh, LOCK_EX)) {
-		print STDERR " failed\n";
-		die "can't aquire lock for storage '$storeid' - $!\n";
-	    }
-	    print STDERR " OK\n";
-	}
-	alarm (0);
-    };
-    my $err = $@;
-
-    alarm (0);
-
-    if ($err) {
-	close ($fh) if $fh;
-	print "lock $storeid: $err\n";
-	*STDOUT->flush();
-	exit (0);
-    }
-
-    print "lock $storeid: success\n";
-    *STDOUT->flush();
-
-    while (my $line = <>) {
-	chomp $line;
-	last if $line =~ m/^release$/;
-    }
-
-    close ($fh);
-
-    exit (0);
-
-} else {
-    print_usage ("unknown command '$cmd'");
-    exit (-1);
-}
-
-exit (0);
-
 __END__
 
 =head1 NAME
@@ -309,10 +256,12 @@
     pvesm add <STORAGE_ID> lvm --vgname <VGNAME>
     pvesm add <STORAGE_ID> iscsi --portal <HOST[:PORT]> --target <TARGET> 
 
-    # enable/disable storage pools
-    pvesm enable <STORAGE_ID>
-    pvesm disable <STORAGE_ID>
+    # disable storage pools
+    pvesm set <STORAGE_ID> --disable 1
 
+    # enable storage pools
+    pvesm set <STORAGE_ID> --disable 0
+
     # change/set storage options
     pvesm set <STORAGE_ID> <OPTIONS>
     pvesm set <STORAGE_ID> --shared 1
@@ -333,23 +282,20 @@
     # free volumes (warning: destroy/deletes all volume data)
     pvesm free <VOLUME_ID>
 
-    # list storage pools
-    pvesm list
+    # list storage status
+    pvesm status
 
-    # list all disk volumes
-    pvesm list --all
-    
     # list storage contents
-    pvesm list --id <STORAGE_ID> [--vmid <VMID>]
+    pvesm list <STORAGE_ID> [--vmid <VMID>]
 
     # list volumes allocated by VMID
-    pvesm list --vmid <VMID> [--id <STORAGE_ID>]
+    pvesm list <STORAGE_ID> --vmid <VMID> 
 
     # list iso images
-    pvesm list --iso [--id <STORAGE_ID>]
+    pvesm list <STORAGE_ID> --iso 
 
     # list openvz templates
-    pvesm list --vztmpl [--id <STORAGE_ID>]
+    pvesm list <STORAGE_ID> --vztmpl
 
     # show filesystem path for a volume
     pvesm path <VOLUME_ID>




More information about the pve-devel mailing list