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 2011/08/03 20:43:07 UTC

svn commit: r1153606 [3/3] - in /incubator/vcl/trunk/managementnode/lib/VCL: ./ Module/Provisioning/VMware/

Modified: incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vSphere_SDK.pm
URL: http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vSphere_SDK.pm?rev=1153606&r1=1153605&r2=1153606&view=diff
==============================================================================
--- incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vSphere_SDK.pm (original)
+++ incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vSphere_SDK.pm Wed Aug  3 18:43:06 2011
@@ -337,6 +337,7 @@ sub get_vm_power_state {
    }
 	
 	my $power_state = $vm->runtime->powerState->val;
+	
 	my $return_power_state;
 	if ($power_state =~ /on/i) {
 		$return_power_state = 'on';
@@ -1831,17 +1832,18 @@ sub initialize {
 	my $vmhost_hostname = $self->data->get_vmhost_hostname();
 	my $vmhost_username = $self->data->get_vmhost_profile_username();
 	my $vmhost_password = $self->data->get_vmhost_profile_password();
+	my $vmhost_profile_id = $self->data->get_vmhost_profile_id();
 	
 	if (!$vmhost_hostname) {
 		notify($ERRORS{'WARNING'}, 0, "VM host name could not be retrieved");
 		return;
 	}
 	elsif (!$vmhost_username) {
-		notify($ERRORS{'DEBUG'}, 0, "unable to use vSphere SDK, VM host username is not configured in the database for the VM profile");
+		notify($ERRORS{'DEBUG'}, 0, "unable to use vSphere SDK, VM host username is not configured in the database for VM profile: $vmhost_profile_id");
 		return;
 	}
 	elsif (!$vmhost_password) {
-		notify($ERRORS{'DEBUG'}, 0, "unable to use vSphere SDK, VM host password is not configured in the database for the VM profile");
+		notify($ERRORS{'DEBUG'}, 0, "unable to use vSphere SDK, VM host password is not configured in the database for VM profile: $vmhost_profile_id");
 		return;
 	}
 	
@@ -2176,6 +2178,93 @@ sub _get_datastore_info {
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 create_snapshot
+
+ Parameters  : $vmx_file_path, $name (optional)
+ Returns     : boolean
+ Description : Creates a snapshot of the VM.
+
+=cut
+
+sub create_snapshot {
+	my $self = shift;
+	if (ref($self) !~ /VCL::Module/i) {
+		notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a function, it must be called as a class method");
+		return;
+	}
+	
+	# Get the vmx path argument and convert it to a datastore path
+	my $vmx_path = $self->_get_datastore_path(shift) || return;
+	
+	my $snapshot_name = shift || ("VCL: " . convert_to_datetime());
+	
+	# Override the die handler
+	local $SIG{__DIE__} = sub{};
+	
+	my $vm;
+	eval { $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'config.files.vmPathName' => $vmx_path}); };
+	if (!$vm) {
+		notify($ERRORS{'WARNING'}, 0, "unable to create snapshop because VM is not registered: $vmx_path");
+		return;
+   }
+	
+	eval { $vm->CreateSnapshot(name => $snapshot_name,
+										memory => 0,
+										quiesce => 0,
+										);
+			};
+	
+	if ($@) {
+		notify($ERRORS{'WARNING'}, 0, "failed to create snapshot of VM: $vmx_path, error:\n$@");
+		return;
+	}
+	
+	notify($ERRORS{'DEBUG'}, 0, "created snapshot '$snapshot_name' of VM: $vmx_path");
+	return 1;
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
+=head2 snapshot_exists
+
+ Parameters  : $vmx_file_path
+ Returns     : boolean
+ Description : Determines if a snapshot exists for the VM.
+
+=cut
+
+sub snapshot_exists {
+	my $self = shift;
+	if (ref($self) !~ /VCL::Module/i) {
+		notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a function, it must be called as a class method");
+		return;
+	}
+	
+	# Get the vmx path argument and convert it to a datastore path
+	my $vmx_path = $self->_get_datastore_path(shift) || return;
+	
+	# Override the die handler because fileManager may call it
+	local $SIG{__DIE__} = sub{};
+	
+	my $vm;
+	eval { $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'config.files.vmPathName' => $vmx_path}); };
+	if (!$vm) {
+		notify($ERRORS{'WARNING'}, 0, "unable to determine if snapshot exists because VM is not registered: $vmx_path");
+		return;
+   }
+	
+	if (defined($vm->snapshot)) {
+		notify($ERRORS{'DEBUG'}, 0, "snapshot exists for VM: $vmx_path");
+		return 1;
+	}
+	else {
+		notify($ERRORS{'DEBUG'}, 0, "snapshot does NOT exist for VM: $vmx_path");
+		return 0;
+	}
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 1;
 __END__
 

Modified: incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vmware_cmd.pm
URL: http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vmware_cmd.pm?rev=1153606&r1=1153605&r2=1153606&view=diff
==============================================================================
--- incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vmware_cmd.pm (original)
+++ incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/vmware_cmd.pm Wed Aug  3 18:43:06 2011
@@ -748,6 +748,47 @@ sub _get_datastore_info {
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 create_snapshot
+
+ Parameters  : $vmx_file_path
+ Returns     : boolean
+ Description : Creates a snapshot of the VM.
+
+=cut
+
+sub create_snapshot {
+	my $self = shift;
+	if (ref($self) !~ /VCL::Module/i) {
+		notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a function, it must be called as a class method");
+		return;
+	}
+	
+	# Get the vmx file path argument
+	my $vmx_file_path = shift;
+	if (!$vmx_file_path) {
+		notify($ERRORS{'WARNING'}, 0, "vmx file path argument was not supplied");
+		return;
+	}
+	
+	my $command = "vmrun snapshot \"$vmx_file_path\"";
+	
+	my ($exit_status, $output) = $self->vmhost_os->execute($command, 1);
+	if (!defined($output)) {
+		notify($ERRORS{'WARNING'}, 0, "failed to execute vmrun to create a snapshot of VM: $vmx_file_path, command: '$command'");
+		return;
+	}
+	elsif ($exit_status != 0 || grep(/error/i, @$output)) {
+		notify($ERRORS{'WARNING'}, 0, "error occurred executing vmrun to create a snapshot of VM: $vmx_file_path, command: '$command', output:\n" . join("\n", @$output));
+		return;
+	}
+	else {
+		notify($ERRORS{'OK'}, 0, "created snapshot of VM: $vmx_file_path, output:\n" . join("\n", @$output));
+		return 1;
+	}
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 1;
 __END__
 

Modified: incubator/vcl/trunk/managementnode/lib/VCL/utils.pm
URL: http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/utils.pm?rev=1153606&r1=1153605&r2=1153606&view=diff
==============================================================================
--- incubator/vcl/trunk/managementnode/lib/VCL/utils.pm (original)
+++ incubator/vcl/trunk/managementnode/lib/VCL/utils.pm Wed Aug  3 18:43:06 2011
@@ -5075,15 +5075,16 @@ EOF
 	# Retrieve the image info
 	my $imagerevision_image_id = $imagerevision_info->{imageid};
 	my $imagerevision_image_info = get_image_info($imagerevision_image_id);
+	if (!$imagerevision_image_info) {
+		notify($ERRORS{'WARNING'}, 0, "failed to retrieve imagerevision info, image info could not be retrieved for image ID: $imagerevision_image_id");
+		return;
+	}
 	$imagerevision_info->{image} = $imagerevision_image_info;
 	
 	# Retrieve the imagerevision user info
-	my $imagerevision_user_id = $imagerevision_info->{userid};
-	my $imagerevision_user_info = get_user_info($imagerevision_user_id);
-	my $imagerevision_user_info_address = sprintf('%x', $imagerevision_user_info);
-	$imagerevision_info->{user_address} = $imagerevision_user_info_address;
-	$imagerevision_info->{user} = $imagerevision_user_info;
+	$imagerevision_info->{user} = get_user_info($imagerevision_info->{userid});
 	
+	# Add the info to %ENV so it doesn't need to be retrieved from the database again
 	$ENV{imagerevision_info}{$imagerevision_identifier} = $imagerevision_info;
 	#notify($ERRORS{'DEBUG'}, 0, "retrieved info from database for imagerevision '$imagerevision_identifier':\n" . format_data($ENV{imagerevision_info}{$imagerevision_identifier}));
 	return $ENV{imagerevision_info}{$imagerevision_identifier};
@@ -5598,7 +5599,7 @@ sub run_ssh_command {
 			notify($ERRORS{'WARNING'}, 0, "attempt $attempts/$max_attempts: failed to execute SSH command on $node: '$command', exit status: $exit_status, output:\n$ssh_output_formatted");
 			next;
 		}
-		elsif ($exit_status == 255 && $ssh_command !~ /(vmware-cmd|vim-cmd|vmkfstools)/i) {
+		elsif ($exit_status == 255 && $ssh_command !~ /(vmware-cmd|vim-cmd|vmkfstools|vmrun)/i) {
 			notify($ERRORS{'WARNING'}, 0, "attempt $attempts/$max_attempts: failed to execute SSH command on $node: '$command', exit status: $exit_status, SSH exits with the exit status of the remote command or with 255 if an error occurred, output:\n$ssh_output_formatted") if $output_level;
 			next;
 		}
@@ -8073,7 +8074,7 @@ EOF
 	
 	# Check to make sure row was returned
 	if (!@selected_rows) {
-		notify($ERRORS{'OK'}, 0, "user was not found in the database: '$user_identifier', SQL statement:\n$select_statement");
+		notify($ERRORS{'WARNING'}, 0, "user was not found in the database: '$user_identifier'");
 		return;
 	}
 	elsif (scalar @selected_rows > 1) {
@@ -10421,7 +10422,7 @@ EOF
 		}
 	}
 	
-	notify($ERRORS{'DEBUG'}, 0, "retrieved OS info:\n" . format_data(\%info));
+	#notify($ERRORS{'DEBUG'}, 0, "retrieved OS info:\n" . format_data(\%info));
 	return \%info;
 }