Commit eb914937 authored by Leigh Stoller's avatar Leigh Stoller

Refactor the image history code into a function in the Image library

so that it can be called from the PG code.
parent c06ebd69
#!/usr/bin/perl -wT
#
# Copyright (c) 2007-2012 University of Utah and the Flux Group.
# Copyright (c) 2007-2013 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
......@@ -328,6 +328,15 @@ sub Create($$$$$$$$)
$query .= ",description=$desc";
$query .= ",hash=" . DBQuoteSpecial($argref->{'hash'})
if (exists($argref->{'hash'}));
#
# If this is set in the environment, we use it. This allows
# us to track actual geni users, since all of that happens
# as geniuser. We do not bother to set this for local users.
#
if (exists($ENV{'GENIURN'}) && $ENV{'GENIURN'} ne "") {
$query .= ",creator_urn=". DBQuoteSpecial($ENV{'GENIURN'});
}
if (! DBQueryWarn($query)) {
DBQueryWarn("unlock tables");
......@@ -812,16 +821,31 @@ sub UnLockTables($)
#
# Mark the update time in the record,
#
sub MarkUpdateTime($;$)
sub MarkUpdate($$;$)
{
my ($self, $stamp) = @_;
my ($self, $user, $stamp) = @_;
# Must be a real reference.
return -1
if (! ref($self));
my $imageid = $self->imageid();
my $imageid = $self->imageid();
my $updater = "";
my $updater_idx = "";
my $updater_urn = "";
if (defined($user)) {
$updater = ", updater='" . $user->uid() . "'";
$updater_idx = ", updater_idx='" . $user->uid_idx() . "'";
}
#
# If this is set in the environment, we use it. This allows
# us to track actual geni users, since all of that happens
# as geniuser. We do not bother to set this for local users.
#
if (exists($ENV{'GENIURN'}) && $ENV{'GENIURN'} ne "") {
$updater_urn = ", updater_urn=". DBQuoteSpecial($ENV{'GENIURN'});
}
if (defined($stamp)) {
$stamp="FROM_UNIXTIME($stamp)";
}
......@@ -831,6 +855,7 @@ sub MarkUpdateTime($;$)
return -1
if (! DBQueryWarn("update images set updated=$stamp " .
" $updater $updater_idx $updater_urn ".
"where imageid='$imageid'"));
return 0;
......@@ -1024,5 +1049,120 @@ sub RevokeAccess($$)
return 0;
}
#
# Class method to record image history info.
#
sub RecordImageHistory($$$$$$)
{
my ($class, $experiment, $action, $log_session, $impotent, $noderef) = @_;
require Node;
# Flush info; we want most recent.
$experiment->Refresh();
my $exptidx = $experiment->idx();
my $pid = $experiment->pid();
my $eid = $experiment->eid();
my $state = $experiment->state();
my $rsrcidx = $experiment->rsrcidx();
my $lastrsrc = $experiment->lastrsrc();
$log_session = 'NULL' if (!defined($log_session));
my %prev_alloc;
my $cant_find_prev_alloc = 0;
if ($state eq 'modify_reswap') {
if (!defined($lastrsrc)) {
print STDERR "lastrsrc not set during swapmod\n";
return -1;
}
my $db_result =
DBQueryWarn("select node_id from image_history ".
"where rsrcidx=$lastrsrc");
return -1
if (!$db_result);
if ($db_result->numrows() < 1) {
print STDERR "could not find previous state (rsrcidx=$lastrsrc) ".
"in image_history table, won't be able to determine ".
"newly allocated nodes\n";
$cant_find_prev_alloc = 1;
}
while (my $n = $db_result->fetchrow) {
$prev_alloc{$n} = 1;
}
}
foreach my $node_id (keys(%{$noderef})) {
my $node = Node->Lookup($node_id);
if (! defined($node)) {
print STDERR "No DB record for $node_id\n";
next;
}
# Need uptodate info below.
$node->FlushReserved();
my $ref = $noderef->{$node_id};
my ($osid, $imageid) = @{$ref};
my $newly_alloc = (exists($prev_alloc{$node_id}) ? 0 : 1);
$newly_alloc = 'NULL'
if ($cant_find_prev_alloc);
my $history_result =
DBQueryWarn("select max(history_id) ".
" from node_history where node_id = '$node_id'");
return -1
if (! $history_result);
if (! $history_result->numrows) {
nohistory:
print STDERR "No node_history for $node_id, ".
"cannot log image usage for node.\n";
next;
}
my ($node_history_id) = $history_result->fetchrow_array();
if (!defined($node_history_id)) {
goto nohistory;
}
my $vname = $node->vname();
my $erole = $node->erole();
my $phys_type = $node->type();
if (!defined($vname)) {
print STDERR "$node is no longer reserved!\n";
next;
}
# Ick. Should go into the Node object.
my ($virt_result) =
DBQueryWarn("select osname,type from virt_nodes ".
"where exptidx='$exptidx' and vname='$vname'");
return -1
if (!$virt_result);
if (!$virt_result->numrows) {
print STDERR "No virt info for node $node_id ($vname)\n";
next;
}
my ($osname, $req_type) = $virt_result->fetchrow_array();
my $req_os = defined $osname ? ($osname ? 1 : 0) : 'NULL';
$erole = 'delay' if $erole eq 'delaynode';
$req_type = $erole unless defined $req_type;
next
if ($impotent);
DBQueryWarn("insert into image_history ".
"(stamp, node_history_id, node_id, ".
" action, newly_alloc, rsrcidx, log_session, ".
" req_type, phys_type, req_os, osid, imageid) ".
"values(UNIX_TIMESTAMP(now()), ".
" $node_history_id, '$node_id', '$action', ".
" $newly_alloc, $rsrcidx, ".
" $log_session, '$req_type', '$phys_type', ".
" $req_os, $osid, $imageid)")
or return -1;
}
return 0;
}
# _Always_ make sure that this 1 is at the end of the file...
1;
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
# Copyright (c) 2000-2013 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
......@@ -625,92 +625,27 @@ if ($exit_code == 0) {
# Record some stats on the OS requested and the images loaded to
# the image_history table. Put in in an eval loop to catch any
# errors so they are non-fatal.
use strict;
eval {
my ($exptidx, $state) =
DBQuerySingleFatal("select idx,state from experiments ".
" where pid='$pid' and eid='$eid'");
my ($rsrcidx,$lastrsrc) =
DBQuerySingleFatal("select rsrcidx,lastrsrc from experiment_stats ".
" where exptidx=$exptidx");
my $log_session = tblog_session();
my %prev_alloc;
my $cant_find_prev_alloc = 0;
if ($state eq 'modify_reswap') {
die_noretry("lastrsrc not set during swapmod")
unless defined $lastrsrc;
my $db_result =
DBQueryFatal("select node_id from image_history where rsrcidx = $lastrsrc");
if ($db_result->numrows() < 1) {
tbwarn("could not find previous state (rsrcidx=$lastrsrc) ".
"in image_history table, won't be able to determine ".
"newly allocated nodes");
$cant_find_prev_alloc = 1;
my %todo;
# Collect the list of nodes and store the osid requested
# and the imageid loaded (if any).
foreach my $object (@{$MyStruct->OperationList()}) {
foreach my $node_id (keys %{$object->{NODES}}) {
my $node = $object->{NODES}{$node_id};
my $osinfo = $node->_bootosinfo();
my $osid = $osinfo->osid();
my $op = $node->_setupoperation();
my $imageid = 0;
# Only set the imageid if the disk needed a reload
if ($op == $RELOAD) {
my $image = $node->_loadimage();
$imageid = $image->imageid();
}
while (my $n = $db_result->fetchrow) {
$prev_alloc{$n} = 1;
}
}
my %todo;
# Collect the list of nodes and store the osid requested
# and the imageid loaded (if any).
foreach my $object (@{$MyStruct->OperationList()}) {
foreach my $node_id (keys %{$object->{NODES}}) {
my $node = $object->{NODES}{$node_id};
my $osinfo = $node->_bootosinfo();
my $osid = $osinfo->osid();
my $op = $node->_setupoperation();
my $imageid = 0;
# Only set the imageid if the disk needed a reload
if ($op == $RELOAD) {
my $image = $node->_loadimage();
$imageid = $image->imageid();
}
$todo{$node_id} = [$osid, $imageid];
}
}
foreach my $node_id ( keys(%todo) ) {
my ($osid, $imageid) = @{$todo{$node_id}};
my $newly_alloc = exists $prev_alloc{$node_id} ? 0 : 1;
$newly_alloc = 'NULL' if $cant_find_prev_alloc;
my ($node_history_id)
= DBQuerySingleFatal("select max(history_id) ".
" from node_history where node_id = '$node_id'");
if (! defined $node_history_id) {
tbwarn("No node_history for $node_id, can't log image usage for node.");
next;
}
my ($erole, $osname, $req_type, $phys_type)
= DBQuerySingleFatal("select r.erole, v.osname, v.type, n.type ".
" from reserved as r ".
" left join virt_nodes as v using (vname, exptidx) ".
" left join nodes as n using (node_id) ".
"where r.node_id = '$node_id'");
my $req_os = defined $osname ? ($osname ? 1 : 0) : 'NULL';
$erole = 'delay' if $erole eq 'delaynode';
$req_type = $erole unless defined $req_type;
DBQueryFatal("insert into image_history ".
"(stamp, node_history_id, node_id, ".
" action, newly_alloc, rsrcidx, log_session, ".
" req_type, phys_type, req_os, osid, imageid) ".
"values(UNIX_TIMESTAMP(), ".
" $node_history_id, '$node_id', 'os_setup', ".
" $newly_alloc, $rsrcidx, ".
" $log_session, '$req_type', '$phys_type', ".
" $req_os, $osid, $imageid)")
if (!$impotent);
$todo{$node_id} = [$osid, $imageid];
}
};
if ($@) {
tbwarn "Unable to log image usage to image_history table.\n", $@;
}
Image->RecordImageHistory($experiment, 'os_setup',
tblog_session(), $impotent, \%todo);
}
#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment