[pve-devel] [pve-container cli cleanup 5/5] pct cpuset: use print_api_result

Dietmar Maurer dietmar at proxmox.com
Thu Aug 9 11:43:52 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 src/PVE/CLI/pct.pm | 83 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 45 insertions(+), 38 deletions(-)

diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index 87e15ed..18ea856 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -649,6 +649,25 @@ __PACKAGE__->register_method({
 	return PVE::LXC::Config->lock_config($vmid, $code);
     }});
 
+
+PVE::JSONSchema::register_renderer('cpuset', sub {
+    my $members = shift;
+
+    my $line = '';
+
+    my $last = $members->[-1];
+
+    my $has_id = { map { $_ => 1 } @$members };
+
+    for (my $id = 0; $id <= $last; $id++) {
+	my $empty = ' ' x length("$id");
+	$line .= ' ' if $id;
+	$line .= $has_id->{$id} ? $id : $empty;
+    }
+
+    return $line;
+});
+
 __PACKAGE__->register_method ({
     name => 'cpusets',
     path => 'cpusets',
@@ -658,21 +677,33 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => {},
     },
-    returns => { type => 'null'},
+    returns => {
+	type => 'array',
+	items => {
+	    type => 'object',
+	    properties => {
+		vmid => get_standard_option('pve-vmid'),
+		cpuset => {
+		    description => "Array of used CPU IDs",
+		    type => 'array',
+		    items => { type => 'integer' },
+		    renderer => 'cpuset',
+		},
+	    },
+	}
+    },
     code => sub {
 	my ($param) = @_;
 
 	my $cgv1 = PVE::LXC::get_cgroup_subsystems();
 	if (!$cgv1->{cpuset}) {
-	    print "cpuset cgroup not available\n";
-	    return undef;
+	    warn "cpuset cgroup not available\n";
+	    return [];
 	}
 
-	my $ctlist = PVE::LXC::config_list();
+	my $data = [];
 
-	my $len = 0;
-	my $id_len = 0;
-	my $res = {};
+	my $ctlist = PVE::LXC::config_list();
 
 	foreach my $vmid (sort keys %$ctlist) {
 	    next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid";
@@ -682,38 +713,11 @@ __PACKAGE__->register_method ({
 		warn $err;
 		next;
 	    }
-	    my @cpuset_members = $cpuset->members();
-
-	    my $line = ': ';
-
-	    my $last = $cpuset_members[-1];
-
-	    for (my $id = 0; $id <= $last; $id++) {
-		my $empty = ' ' x length("$id");
-		$line .= ' ' . ($cpuset->has($id) ? $id : $empty);
-	    }
-	    $len = length($line) if length($line) > $len;
-	    $id_len = length($vmid) if length($vmid) > $id_len;
-
-	    $res->{$vmid} = $line;
+	    my $obj = { vmid => $vmid , cpuset => [ $cpuset->members() ] };
+	    push @$data, $obj;
 	}
 
-	my @vmlist = sort keys %$res;
-
-	if (scalar(@vmlist)) {
-	    my $header = '-' x ($len + $id_len) . "\n";
-
-	    print $header;
-	    foreach my $vmid (@vmlist) {
-		print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid});
-	    }
-	    print $header;
-
-	} else {
-	    print "no running containers\n";
-	}
-
-	return undef;
+	return $data;
     }});
 
 our $cmddef = {
@@ -797,7 +801,10 @@ our $cmddef = {
 
     template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
 
-    cpusets => [ __PACKAGE__, 'cpusets', []],
+    cpusets => [ __PACKAGE__, 'cpusets', [], undef, sub {
+	my ($data, $schema, $options) = @_;
+	PVE::CLIFormatter::print_api_result($data, $schema, ['vmid', 'cpuset'], $options);
+    }, $PVE::RESTHandler::standard_output_options],
 
 };
 
-- 
2.11.0




More information about the pve-devel mailing list