You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@vcl.apache.org by ar...@apache.org on 2015/04/01 21:40:40 UTC

svn commit: r1670760 - in /vcl/trunk/managementnode/lib/VCL: makeproduction.pm utils.pm

Author: arkurth
Date: Wed Apr  1 19:40:39 2015
New Revision: 1670760

URL: http://svn.apache.org/r1670760
Log:
VCL-849
Updated utils.pm::is_request_deleted to only return true if the request state or laststate is deleted. It had been returning true if either was makeproduction.pm.  Changed is_request_deleted to utilize get_request_current_state_name - the query was duplicated.

Added utils.pm::set_production_imagerevision. Moved this code from makeproduction.pm.

Fixed how makeproduction.pm::process sets log.ending. It had been setting it to 'EOR' regardless of the previous state. Changed so ending is set to 'deleted' if the previous state is new or reserved, and 'released' if inuse.

Modified:
    vcl/trunk/managementnode/lib/VCL/makeproduction.pm
    vcl/trunk/managementnode/lib/VCL/utils.pm

Modified: vcl/trunk/managementnode/lib/VCL/makeproduction.pm
URL: http://svn.apache.org/viewvc/vcl/trunk/managementnode/lib/VCL/makeproduction.pm?rev=1670760&r1=1670759&r2=1670760&view=diff
==============================================================================
--- vcl/trunk/managementnode/lib/VCL/makeproduction.pm (original)
+++ vcl/trunk/managementnode/lib/VCL/makeproduction.pm Wed Apr  1 19:40:39 2015
@@ -78,113 +78,39 @@ use VCL::utils;
 
 =head2 process
 
- Parameters  : 
- Returns     : exits with status 0 if successful, 1 if failed
+ Parameters  : none
+ Returns     : boolean
  Description : Processes a reservation in the makeproduction state.
  
 =cut
 
 sub process {
 	my $self = shift;
-	my $request_data                    = $self->data->get_request_data();
-	my $image_name                      = $self->data->get_image_name();
-
-	# Update the image and imagerevision tables:
-	#    image.name = imagename of new production revision
-	#    image.test = 0
-	#    image.lastupdate = now
-	#    imagerevision.production = 1 for revision specified in hash
-	#    imagerevision.production = 0 for all other revisions associated with this image
-	if ($self->set_imagerevision_to_production()) {
-		notify($ERRORS{'OK'}, 0, "successfully updated image and imagerevision tables");
-	}
-	else {
-		$self->reservation_failed("unable to update the image and imagerevision tables");
-	}
-
-	# Notify owner that image is in production mode
-	if ($self->notify_imagerevision_to_production()) {
-		notify($ERRORS{'OK'}, 0, "successfully notified owner that $image_name is in production mode");
+	my $image_name = $self->data->get_image_name();
+	my $imagerevision_id = $self->data->get_imagerevision_id();
+	my $request_laststate_name = $self->data->get_request_laststate_name();
+	
+	if (!set_production_imagerevision($imagerevision_id)) {
+		$self->reservation_failed("failed to set imagerevision ID $imagerevision_id to production for image $image_name");
 	}
-	else {
+	
+	# Notify owner that image revision is production
+	if (!$self->notify_imagerevision_to_production()) {
 		$self->reservation_failed("failed to notify owner that $image_name is in production mode");
 	}
-
-	# Update the request state to deleted, leave the computer state alone, exit
-	switch_state($request_data, 'deleted', '', 'EOR', '1');
-
-} ## end sub process
-
-#/////////////////////////////////////////////////////////////////////////////
-
-=head2 set_imagerevision_to_production
-
- Parameters  : None, uses image and image revision set in DataStructure
- Returns     : 1 if successful, 0 if failed
- Description : Changes the production image revision for a given image.
-               It sets the imagerevision.production column to 1 for the
-					imagerevision specified in the DataStructure, and all other
-					image revisions to 0 for the same image.
- 
-=cut
-
-sub set_imagerevision_to_production {
-	my $self = shift;
-	my $image_id                        = $self->data->get_image_id();
-	my $image_name                      = $self->data->get_image_name();
-	my $imagerevision_id                = $self->data->get_imagerevision_id();
-	
-	# Check the variables necessary to update the database
-	if (!defined $image_id) {
-		notify($ERRORS{'WARNING'}, 0, "unable to change production imagerevision, image id is not defined");
-		return 0;
-	}
-	elsif ($image_id <= 0) {
-		notify($ERRORS{'WARNING'}, 0, "unable to change production imagerevision, image id is $image_id");
-		return 0;
-	}
-	if (!defined $imagerevision_id) {
-		notify($ERRORS{'WARNING'}, 0, "unable to change production imagerevision, imagerevision id is not defined");
-		return 0;
-	}
-	elsif ($imagerevision_id <= 0) {
-		notify($ERRORS{'WARNING'}, 0, "unable to change production imagerevision, imagerevision id is $image_id");
-		return 0;
-	}
-
-	# Clear production flag for all image revisions
-	# Set the correct image revision to production
-	# Update the image name, set test = 0, and lastupdate to now
-	my $sql_statement = "
-	UPDATE
-	image,
-	imagerevision imagerevision_production,
-	imagerevision imagerevision_others
-	SET
-	image.name = imagerevision_production.imagename,
-	image.test = 0,
-	image.lastupdate = NOW(),
-	imagerevision_production.production = 1,
-	imagerevision_others.production = 0
-	WHERE
-	image.id = '$image_id'
-	AND imagerevision_production.imageid = image.id
-	AND imagerevision_others.imageid = image.id
-	AND imagerevision_production.id = '$imagerevision_id'
-	AND imagerevision_others.id != imagerevision_production.id
-	";
-	
-	# Call the database execute subroutine
-	if (database_execute($sql_statement)) {
-		notify($ERRORS{'OK'}, 0, "imagerevision $imagerevision_id set to production for image $image_name");
-		return 1;
+	
+	my $log_ending;
+	my $computer_state;
+	if ($request_laststate_name =~ /(new|reserved)/) {
+		$log_ending = 'deleted';
+		$computer_state = 'available';
 	}
-	else {
-		notify($ERRORS{'WARNING'}, 0, "failed to set imagerevision $imagerevision_id to production for image $image_name");
-		return 0;
+	elsif ($request_laststate_name =~ /(inuse)/) {
+		$log_ending = 'released';
 	}
-
-} ## end sub _update_flags
+	
+	$self->state_exit('deleted', $computer_state, $log_ending);
+} ## end sub process
 
 #/////////////////////////////////////////////////////////////////////////////
 
@@ -227,7 +153,6 @@ END
 		notify($ERRORS{'WARNING'}, 0, "failed to send email message to $user_email");
 		return 0;
 	}
-
 } ## end sub _notify_owner
 
 #/////////////////////////////////////////////////////////////////////////////

Modified: vcl/trunk/managementnode/lib/VCL/utils.pm
URL: http://svn.apache.org/viewvc/vcl/trunk/managementnode/lib/VCL/utils.pm?rev=1670760&r1=1670759&r2=1670760&view=diff
==============================================================================
--- vcl/trunk/managementnode/lib/VCL/utils.pm (original)
+++ vcl/trunk/managementnode/lib/VCL/utils.pm Wed Apr  1 19:40:39 2015
@@ -245,6 +245,7 @@ our @EXPORT = qw(
 	setup_print_ok
 	setup_print_warning
 	setup_print_wrap
+	set_production_imagerevision
 	sleep_uninterrupted
 	sort_by_file_name
 	stopwatch
@@ -1660,61 +1661,32 @@ sub update_reservation_password {
 =head2 is_request_deleted
 
  Parameters  : $request_id
- Returns     : return 1 if request state or laststate is set to deleted or if request does not exist
-               return 0 if request exists and neither request state nor laststate is set to deleted1 success 0 failure
- Description : checks if request has been deleted
+ Returns     : boolean
+ Description : Checks if the request state or laststate is deleted.
 
 =cut
 
 sub is_request_deleted {
-
 	my ($request_id) = @_;
-	my ($package, $filename, $line, $sub) = caller(0);
-
-	# Check the passed parameter
-	if (!(defined($request_id))) {
-		notify($ERRORS{'WARNING'}, 0, "request ID was not specified");
-		return 0;
+	if (!defined($request_id)) {
+		notify($ERRORS{'WARNING'}, 0, "request ID argument was not specified");
+		return;
 	}
-
-	# Create the select statement
-	my $select_statement = "
-	SELECT
-	request.stateid AS currentstate_id,
-	request.laststateid AS laststate_id,
-	currentstate.name AS currentstate_name,
-	laststate.name AS laststate_name
-	FROM
-	request, state currentstate, state laststate
-	WHERE
-	request.id = $request_id
-	AND request.stateid = currentstate.id
-	AND request.laststateid = laststate.id
-	";
-
-	# Call the database select subroutine
-	# This will return an array of one or more rows based on the select statement
-	my @selected_rows = database_select($select_statement);
-
-	# Check to make sure 1 row was returned
-	if (scalar @selected_rows == 0) {
+	
+	my ($state_name, $laststate_name) = get_request_current_state_name($request_id);
+	if (!$state_name || !$laststate_name) {
+		notify($ERRORS{'WARNING'}, 0, "request $request_id state data could not be retrieved, assuming request is deleted and was removed from the database, returning true");
 		return 1;
 	}
-	elsif (scalar @selected_rows > 1) {
-		notify($ERRORS{'WARNING'}, 0, "" . scalar @selected_rows . " rows were returned from database select");
-		return 0;
-	}
 	
-	my $state_name     = $selected_rows[0]{currentstate_name};
-	my $laststate_name = $selected_rows[0]{laststate_name};
-
-	#notify($ERRORS{'DEBUG'}, 0,"state=$state_name, laststate=$laststate_name");
-	
-	if ($state_name =~ /(deleted|makeproduction)/ || $laststate_name =~ /(deleted|makeproduction)/) {
+	if ($state_name =~ /(deleted)/ || $laststate_name =~ /(deleted)/) {
+		notify($ERRORS{'DEBUG'}, 0, "request $request_id state: $state_name/$laststate_name, returning true");
 		return 1;
 	}
-
-	return 0;
+	else {
+		notify($ERRORS{'DEBUG'}, 0, "request $request_id state: $state_name/$laststate_name, returning false");
+		return 0;
+	}
 } ## end sub is_request_deleted
 
 #/////////////////////////////////////////////////////////////////////////////
@@ -3725,6 +3697,58 @@ EOF
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 set_production_imagerevision
+
+ Parameters  : $imagerevision_id
+ Returns     : boolean
+ Description : Sets the production flag to 1 for the image revision specified by
+               the argument. Sets production to 0 for all other revisions of the
+               image. Sets image.name to imagerevision.imagename of the
+               production revision. Sets the image.test flag to 0.
+ 
+=cut
+
+sub set_production_imagerevision {
+	my ($imagerevision_id) = @_;
+	if (!defined($imagerevision_id)) {
+		notify($ERRORS{'WARNING'}, 0, "imagerevision ID argument was not supplied");
+		return;
+	}
+	
+	# Delete cached data
+	delete $ENV{production_imagerevision_info};
+	
+	my $sql_statement = <<EOF;
+UPDATE
+image,
+imagerevision imagerevision_production,
+imagerevision imagerevision_others
+SET
+image.name = imagerevision_production.imagename,
+image.test = 0,
+image.lastupdate = NOW(),
+imagerevision_production.production = 1,
+imagerevision_others.production = 0
+WHERE
+imagerevision_production.id = $imagerevision_id
+AND imagerevision_production.imageid = image.id
+AND imagerevision_others.imageid = imagerevision_production.imageid
+AND imagerevision_others.id != imagerevision_production.id
+EOF
+	
+	# Call the database execute subroutine
+	if (database_execute($sql_statement)) {
+		notify($ERRORS{'OK'}, 0, "imagerevision $imagerevision_id set to production");
+		return 1;
+	}
+	else {
+		notify($ERRORS{'WARNING'}, 0, "failed to set imagerevision $imagerevision_id to production");
+		return 0;
+	}
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 =head2 get_imagemeta_info
 
  Parameters  : $imagemeta_id, $no_cache (optional)