]> xenbits.xensource.com Git - people/royger/osstest.git/commitdiff
osstest: allow to perform multiple anoints in the same transaction
authorRoger Pau Monne <roger.pau@citrix.com>
Wed, 20 Feb 2019 08:50:09 +0000 (09:50 +0100)
committerRoger Pau Monne <roger.pau@citrix.com>
Wed, 20 Feb 2019 08:50:09 +0000 (09:50 +0100)
In the same way allow to perform several fetches in the same retrieve
transaction.

Further patches will anoint a FreeBSD image and a binary ports tree
in the same transaction, and it's required to do it in the same
transaction in order to avoid inconsistencies when fetching them.

Note that most of the changes in this patch is code movement in order
to place the database accessors inside of a loop that iterates over
the input parameters.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
mg-anoint

index d09124b3066ba9300d0b2f4275895d8384b9c452..08447b8e3c306c3c4ae4f4b3b304c6a403734711 100755 (executable)
--- a/mg-anoint
+++ b/mg-anoint
 #
 #  ./mg-anoint destroy REFKEY
 #
-#  ./mg-anoint anoint [ANOINT-OPTION...] REFKEY FLIGHT JOB
+#  ./mg-anoint anoint [ANOINT-OPTION...] REFKEY FLIGHT JOB [REFKEY FLIGHT JOB...]
 #     ANOINT-OPTIONs are:
 #        --allow-blessed=BLESSING,...       default is from `prepare'
 #        --allow-job-status=STATUS,...      default is only `pass'
 #
-#  ./mg-anoint retrieve [--tolerate-unprepared] REFKEY
+#  ./mg-anoint retrieve [--tolerate-unprepared] REFKEY [REFKEY...]
 #      => FLIGHT JOB
 #         if nothing anointed yet, prints nothing and exits 0
 #         if anointment not prepared, fails
@@ -189,8 +189,7 @@ sub cmd_anoint {
            die "unknown option $_ ?";
        }
     }
-    die unless @ARGV==3;
-    my ($refkey, $flight, $job) = @ARGV;
+    die unless @ARGV%3==0;
 
     fail_unless_can_anoint();
     prep_queries();
@@ -228,69 +227,74 @@ END
 
     db_retry($dbh_tests, [], sub {
        @o = ();
-        $task_q->execute($refkey);
-
-       # find the task row (ie, the anointment kind)
-       my ($task, $refinfo) = $task_q->fetchrow_array();
-       die "no such anointment kind \`$refkey' (no prepare?)\n"
-           unless defined $task;
-       my %params;
-       foreach (split /\s+/, $refinfo) {
-           die unless m/=/;
-           $params{$`} = $';
-       }
-       my %blessings;
-       $blessings{$_}++ foreach
-           grep /./,
-           (split /,/, $params{blessings}),
-           (split /,/, $allow_blessed);
-
-       my %jobstatus;
-       $jobstatus{pass}++;
-       $jobstatus{$_}++ foreach grep /./, split /,/, $allow_jobstatus;
-
-       # check the to-be-anointed flight's blessing
-       $newflight_q->execute($flight);
-       my $frow = $newflight_q->fetchrow_hashref();
-       die "flight $flight missing" unless $frow;
-       die "flight $flight not started" unless defined $frow->{started};
-
-       # check the job status
-       $newjob_q->execute($flight, $job);
-       my ($jstatus) = $newjob_q->fetchrow_array();
-       die "job $flight.$job missing" unless defined $jstatus;
-       die "job $flight.$job status $jstatus" unless $jobstatus{$jstatus};
-
-       push @o, "flight $flight blessed $frow->{blessing}".
-                " started ".show_abs_time($frow->{started});
-
-       die "flight $flight blessing $frow->{blessing}".
-           " (not $params{blessings} / $allow_blessed)"
-           unless $blessings{ $frow->{blessing} };
-
-       # check to-be-annointed flight is most recent
-       $mostrecent_q->execute($task);
-       my $mostrecent = $mostrecent_q->fetchrow_hashref();
-       die "flight $flight not newer than $mostrecent->{flight}"
-           unless $frow->{started} > ($mostrecent->{started} // 0);
-
-       # expire old anointments
-       $count_q->execute($task);
-       my ($current) = $count_q->fetchrow_array();
-       my $want_delete = ($current+1) - $params{keep};
-       push @o, "anointment $refkey: currently $current anointed";
-       if ($want_delete > 0) {
-           $todelete_q->execute($task, $want_delete);
-           while (my $d = $todelete_q->fetchrow_hashref()) {
-               push @o, " expiring $d->{flight}.$d->{job} [/$d->{shareix}]".
-                   " started ".show_abs_time($d->{started});
-               $delete_res_q->execute($task, $d->{flight}, $d->{shareix});
-           }
-       }
 
-       # at last!
-       $insert_q->execute($flight,$task,$task,$job);
-       push @o, "anointed $flight.$job";
+       for (my $i=0; $i < @ARGV; $i+=3) {
+               my ($refkey, $flight, $job) = @ARGV[$i..$i+2];
+
+               $task_q->execute($refkey);
+
+               # find the task row (ie, the anointment kind)
+               my ($task, $refinfo) = $task_q->fetchrow_array();
+               die "no such anointment kind \`$refkey' (no prepare?)\n"
+                   unless defined $task;
+               my %params;
+               foreach (split /\s+/, $refinfo) {
+                   die unless m/=/;
+                   $params{$`} = $';
+               }
+               my %blessings;
+               $blessings{$_}++ foreach
+                   grep /./,
+                   (split /,/, $params{blessings}),
+                   (split /,/, $allow_blessed);
+
+               my %jobstatus;
+               $jobstatus{pass}++;
+               $jobstatus{$_}++ foreach grep /./, split /,/, $allow_jobstatus;
+
+               # check the to-be-anointed flight's blessing
+               $newflight_q->execute($flight);
+               my $frow = $newflight_q->fetchrow_hashref();
+               die "flight $flight missing" unless $frow;
+               die "flight $flight not started" unless defined $frow->{started};
+
+               # check the job status
+               $newjob_q->execute($flight, $job);
+               my ($jstatus) = $newjob_q->fetchrow_array();
+               die "job $flight.$job missing" unless defined $jstatus;
+               die "job $flight.$job status $jstatus" unless $jobstatus{$jstatus};
+
+               push @o, "flight $flight blessed $frow->{blessing}".
+                        " started ".show_abs_time($frow->{started});
+
+               die "flight $flight blessing $frow->{blessing}".
+                   " (not $params{blessings} / $allow_blessed)"
+                   unless $blessings{ $frow->{blessing} };
+
+               # check to-be-annointed flight is most recent
+               $mostrecent_q->execute($task);
+               my $mostrecent = $mostrecent_q->fetchrow_hashref();
+               die "flight $flight not newer than $mostrecent->{flight}"
+                   unless $frow->{started} > ($mostrecent->{started} // 0);
+
+               # expire old anointments
+               $count_q->execute($task);
+               my ($current) = $count_q->fetchrow_array();
+               my $want_delete = ($current+1) - $params{keep};
+               push @o, "anointment $refkey: currently $current anointed";
+               if ($want_delete > 0) {
+                   $todelete_q->execute($task, $want_delete);
+                   while (my $d = $todelete_q->fetchrow_hashref()) {
+                       push @o, " expiring $d->{flight}.$d->{job} [/$d->{shareix}]".
+                           " started ".show_abs_time($d->{started});
+                       $delete_res_q->execute($task, $d->{flight}, $d->{shareix});
+                   }
+               }
+
+               # at last!
+               $insert_q->execute($flight,$task,$task,$job);
+               push @o, "anointed $flight.$job";
+       }
     });
     pr_o();
 }    
@@ -301,26 +305,31 @@ sub cmd_retrieve {
        shift @ARGV;
        $tolerate_unprepared = 1;
     }
-    die unless @ARGV==1;
+    die unless @ARGV>=1;
     die if $ARGV[0] =~ m/^-/;
-    my ($refkey) = @ARGV;
 
     empty_unless_can_anoint();
     prep_queries();
 
     db_retry($dbh_tests, [], sub {
         @o = ();
-        $task_q->execute($refkey);
-       my ($task) = $task_q->fetchrow_array();
-       die "no such anointment kind \`$refkey'"
-           unless defined $task or $tolerate_unprepared;
-
-       $mostrecent_q->execute($task);
-       my $row = $mostrecent_q->fetchrow_hashref();
-       if ($row) {
-           push @o, "$row->{flight} $row->{job}";
-       } else {
-           print STDERR "warning: nothing anointed $refkey\n";
+
+       foreach my $refkey (@ARGV) {
+               $task_q->execute($refkey);
+               my ($task) = $task_q->fetchrow_array();
+               die "no such anointment kind \`$refkey'"
+                   unless defined $task or $tolerate_unprepared;
+
+               $mostrecent_q->execute($task);
+               my $row = $mostrecent_q->fetchrow_hashref();
+               if ($row) {
+                   push @o, "$row->{flight} $row->{job}";
+               } else {
+                   # Push an error line so the number of output lines
+                   # matches the number of REFKEYs requested.
+                   push @o, "ERROR";
+                   print STDERR "warning: nothing anointed $refkey\n";
+               }
        }
     });
     pr_o();