diff mbox

[OSSTEST,2/2] mg-allocate: Provide command line way to list allocated resources

Message ID 1475771750-12776-2-git-send-email-ian.jackson@eu.citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ian Jackson Oct. 6, 2016, 4:35 p.m. UTC
Freely shareable resources don't appear in the plan, and the plan is
not always immediately updated, and is generally not always a
convenient interface.  Provide a command line way to list allocated
resources.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
---
 mg-allocate | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
diff mbox

Patch

diff --git a/mg-allocate b/mg-allocate
index ef57bb8..3b3fa72 100755
--- a/mg-allocate
+++ b/mg-allocate
@@ -2,6 +2,7 @@ 
 #
 # usage:
 #  ./mg-allocate [<options>] <resource-spec>...
+#  ./mg-allocate [-l] [-l] [-l]
 #
 # <resource-spec> syntax:
 #   [!][<type>/]<name>[/<share>]      type defaults to 'host'
@@ -45,6 +46,15 @@ 
 #                  as if they were free.  This allows us to steal
 #                  resources from other tasks.  May be repeated.
 #
+#   -l | --list    Instead of allocating (or deallocating), simply list
+#                  allocated resources.
+#
+#                  -l: resources owned by this task (this user).
+#                  -ll: resources owned by all tasks
+#                  -lll: include "administrative" resources
+#
+#                  Not compatible with other options.
+#
 # <task-spec> must exist (and be in a format valid for OSSTEST_TASK).
 
 # This is part of "osstest", an automated testing framework for Xen.
@@ -77,6 +87,7 @@  $|=1;
 
 our $tid;
 our %magictask;
+our $list_only;
 our $donate_spec;
 our $donate_taskid;
 our @steal_specs;
@@ -491,6 +502,9 @@  while (@ARGV && $ARGV[0] =~ m/^[-0-9]/) {
                                              1);
         } elsif (s/^\-U/-/) {
             $ENV{OSSTEST_RESOURCE_PRIORITY} //= -1000000;
+        } elsif (s/^\-l/-/ || s/^--list$/--/) {
+	    $list_only++;
+	    die "-l may be repeated only thrice\n" if $list_only > 3;
         } elsif (s/^--as$/-/) {
 	    die "--as needs task\n" unless @ARGV;
 	    $ENV{OSSTEST_TASK} = shift @ARGV;
@@ -506,6 +520,84 @@  while (@ARGV && $ARGV[0] =~ m/^[-0-9]/) {
     }
 }
 
+$list_only = 1 if !@ARGV;
+
+if ($list_only) {
+    die "-l (--list) specified (or implied) with other options or arguments\n"
+	if $donate_spec || @steal_specs || @ARGV || $duration;
+
+    db_retry($dbh_tests, [], sub {
+	my $resbasetypeqtxt = <<END;
+                ( CASE WHEN restype LIKE 'share-%'
+                       THEN SUBSTRING(restype FROM 7)
+                       ELSE restype
+                        END ) AS resbasetype
+END
+        my $orderqtxt = "restype, resname, shareix";
+	my $tid;
+	my $lq;
+	if ($list_only==1) {
+	    $tid = findtask();
+            $lq = $dbh_tests->prepare(<<END);
+		SELECT *,
+$resbasetypeqtxt
+                  FROM resources
+                 WHERE owntaskid=?
+		 ORDER BY resbasetype, $orderqtxt
+END
+	    $lq->execute($tid);
+	} else {
+	    $tid = '';
+	    my @hidemagic = qw(allocatable);
+	    if ($list_only<3) {
+		push @hidemagic, qw(preparing shared);
+	    }
+	    my $hiderefkey = join ' OR ',
+		map { "tasks.refkey='$_'" }
+		@hidemagic;
+	    $lq = $dbh_tests->prepare(<<END);
+		SELECT *,
+$resbasetypeqtxt
+                  FROM resources
+                  JOIN tasks
+                    ON owntaskid=taskid
+                 WHERE tasks.live
+                   AND NOT (tasks.type='magic' AND $hiderefkey)
+              ORDER BY resbasetype,
+                       tasks.type, tasks.refkey,
+                       $orderqtxt
+END
+	    $lq->execute();
+	}
+	my $resbasetype = '';
+	while (my $c = $lq->fetchrow_hashref()) {
+	    if ($c->{resbasetype} ne $resbasetype) {
+		printf "===== %s =====\n", $c->{resbasetype};
+		$resbasetype = $c->{resbasetype};
+	    }
+	    if ($c->{owntaskid} ne $tid) {
+		my ($spec,$desc) = task_spec_desc($c);
+		printf "------ %s -- %s ------\n", $spec, $desc;
+		$tid = $c->{owntaskid};
+	    }
+	    printf " %-40s ", (join "/", map { $c->{$_} }
+			      qw(restype resname shareix));
+	    my $restype = $c->{restype};
+	    my $resname = $c->{resname};
+	    if ($restype eq 'share-host') {
+		print "S/$resname";
+	    } elsif ($restype eq 'host') {
+		print "$resname";
+	    } elsif ($restype eq 'share-flight') {
+		print "F/$resname";
+	    }
+	    print "\n";
+	}
+        db_retry_abort();
+    });
+    exit 0;
+}
+
 if (defined $donate_spec) {
     die "--donate specified with deallocations, too confusing\n"
 	if grep { m/^!/ } @ARGV;