Author: yoh
Date: Fri Jun 20 19:43:13 2014
New Revision: 1604260

URL: http://svn.apache.org/r1604260
Log:
use update_computer_private_ip in utils.pm instead of using /etc/hosts

Modified:
    vcl/trunk/managementnode/lib/VCL/Module/Provisioning/openstack.pm

Modified: vcl/trunk/managementnode/lib/VCL/Module/Provisioning/openstack.pm
URL: 
http://svn.apache.org/viewvc/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/openstack.pm?rev=1604260&r1=1604259&r2=1604260&view=diff
==============================================================================
--- vcl/trunk/managementnode/lib/VCL/Module/Provisioning/openstack.pm (original)
+++ vcl/trunk/managementnode/lib/VCL/Module/Provisioning/openstack.pm Fri Jun 
20 19:43:13 2014
@@ -46,7 +46,7 @@ use lib "$FindBin::Bin/../../..";
 use base qw(VCL::Module::Provisioning);
 
 # Specify the version of this module
-our $VERSION = '2.2.1';
+our $VERSION = '2.3.2';
 
 # Specify the version of Perl to use
 use 5.008000;
@@ -54,11 +54,14 @@ use 5.008000;
 use strict;
 use warnings;
 use diagnostics;
+use English qw( -no_match_vars );
+use IO::File;
+use Fcntl qw(:DEFAULT :flock);
+use File::Temp qw( tempfile );
+use List::Util qw( max );
 
-use VCL::DataStructure;
 use VCL::utils;
 
-use Fcntl qw(:DEFAULT :flock);
 
 #/////////////////////////////////////////////////////////////////////////////
 
@@ -97,46 +100,37 @@ sub initialize {
 =cut
 
 sub load {
-       my $self = shift;
-
-       #check to make sure this call is for the openstack module
-       if (ref($self) !~ /openstack/i) {
-               notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
-               return 0;
-       }
-
-       notify($ERRORS{'OK'}, 0, 
"****************************************************");
+        my $self = shift;
+        if (ref($self) !~ /openstack/i) {
+                notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
+                return;
+        }
 
-       # get various useful vars from the database
-       my $image_full_name      = $self->data->get_image_name;
-       my $computer_shortname   = $self->data->get_computer_short_name;
-       my $request_forimaging   = $self->data->get_request_forimaging();
+        my $reservation_id = $self->data->get_reservation_id() || return;
+        my $computer_id = $self->data->get_computer_id() || return;
+        my $computer_name = $self->data->get_computer_short_name() || return;
+        my $image_name = $self->data->get_image_name() || return;
+        my $vmhost_name = $self->data->get_vmhost_short_name() || return;
 
+        insertloadlog($reservation_id, $computer_id, "doesimageexists", "image 
exists $image_name");
 
-       notify($ERRORS{'OK'}, 0, "Query the host to see if the 
$computer_shortname currently exists");
+        insertloadlog($reservation_id, $computer_id, "startload", 
"$computer_name $image_name");
 
-       # power off the old instance if exists 
-       if(_pingnode($computer_shortname) || $request_forimaging == 0) 
-       {
-               if($self->_terminate_instances) {
-                       notify($ERRORS{'OK'}, 0, "Terminate the existing 
computer $computer_shortname");
-               }
-               else {
-                       notify($ERRORS{'DEBUG'}, 0, "No instance to terminate 
for $computer_shortname");
-               }               
-       }
+        # Remove existing VMs which were created for the reservation computer
+        if (!$self->_terminate_instances) {
+                notify($ERRORS{'WARNING'}, 0, "failed to remove existing VMs 
created for computer $computer_name on VM host: $vmhost_name");
+                return;
+        }
 
        # Create new instance 
-       my $instance_id = $self->_run_instances;
-       
-       if ($instance_id)
-       {
-               notify($ERRORS{'OK'}, 0, "The instance $instance_id is 
created\n");
+        if (!$self->_run_instances) {
+                notify($ERRORS{'WARNING'}, 0, "failed to create VMs for 
computer $computer_name on VM host: $vmhost_name");
+                return;
        }
-       else
-       {
-               notify($ERRORS{'CRITICAL'}, 0, "Fail to run the instance 
$instance_id");
-               return 0;
+       my $instance_id = $self->_get_instance_id;
+        if (!$instance_id) {
+                notify($ERRORS{'WARNING'}, 0, "failed to get the instance id 
for $computer_name");
+                return;
        }
 
        # Update the private ip of the instance in /etc/hosts file
@@ -147,7 +141,7 @@ sub load {
        else
        {
                notify($ERRORS{'CRITICAL'}, 0, "Fail to update private ip of 
the instance in /etc/hosts");
-               return 0;
+               return;
        }
 
 
@@ -197,34 +191,34 @@ sub capture {
         }
 
         my $image_name     = $self->data->get_image_name();
-        my $computer_shortname = $self->data->get_computer_short_name;
+        my $computer_name = $self->data->get_computer_short_name;
        my $instance_id;
        
-        if(_pingnode($computer_shortname))
+        if(_pingnode($computer_name))
         {
                $instance_id = $self->_get_instance_id;
                notify($ERRORS{'OK'}, 0, "instance id: $instance_id is done");
                if(!$instance_id)
                {
-                       notify($ERRORS{'DEBUG'}, 0, "unable to get instance id 
for $computer_shortname");
+                       notify($ERRORS{'DEBUG'}, 0, "unable to get instance id 
for $computer_name");
                        return 0;
                }
         }
        else {
-               notify($ERRORS{'DEBUG'}, 0, "unable to ping to 
$computer_shortname");
+               notify($ERRORS{'DEBUG'}, 0, "unable to ping to $computer_name");
                return 0;
        }
                
         if($self->_prepare_capture)
        {
-               notify($ERRORS{'OK'}, 0, "Prepare_Capture for 
$computer_shortname is done");
+               notify($ERRORS{'OK'}, 0, "Prepare_Capture for $computer_name is 
done");
        }
        
        my $new_image_name = $self->_image_create($instance_id);
 
        if($new_image_name)
        {
-               notify($ERRORS{'OK'}, 0, "Create Image for $computer_shortname 
is done");
+               notify($ERRORS{'OK'}, 0, "Create Image for $computer_name is 
done");
        }
 
        if($self->_insert_openstack_image_name($new_image_name))
@@ -253,7 +247,8 @@ sub _image_create{
                 notify($ERRORS{'OK'}, 0, "Acquire the Image Version: 
$image_version");
         }
 
-        my $image_description = $image_name . '-' . $imagerevision_comments;
+        my $image_description = $image_name . $imagerevision_comments;
+        #my $image_description = $image_name . '-' . $imagerevision_comments;
         my $capture_image = "nova image-create $instance_id 
$image_description";
         notify($ERRORS{'OK'}, 0, "New Image Capture Command: $capture_image");
         my $capture_image_output = `$capture_image`;
@@ -269,7 +264,8 @@ sub _image_create{
         if($run_describe_image_output  =~ 
m/^\|\s(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})/g )
         {
                 $openstack_image_id = $1;
-                $new_image_name = $openstack_image_id .'-v'. $image_version;
+                $new_image_name = $openstack_image_id . $image_version;
+                #$new_image_name = $openstack_image_id .'-v'. $image_version;
                 notify($ERRORS{'OK'}, 0, "The Openstack Image 
ID:$openstack_image_id");
                 notify($ERRORS{'OK'}, 0, "The New Image Name:$new_image_name");
                 return $new_image_name;
@@ -303,16 +299,16 @@ sub _prepare_capture {
         my $image_name     = $self->data->get_image_name();
 
         my $computer_id        = $self->data->get_computer_id;
-        my $computer_shortname = $self->data->get_computer_short_name;
-        my $computer_nodename  = $computer_shortname;
+        my $computer_name = $self->data->get_computer_short_name;
+        my $computer_nodename  = $computer_name;
         my $computer_hostname  = $self->data->get_computer_hostname;
         my $computer_type      = $self->data->get_computer_type;
 
         if (write_currentimage_txt($self->data)) {
-                notify($ERRORS{'OK'}, 0, "currentimage.txt updated on 
$computer_shortname");
+                notify($ERRORS{'OK'}, 0, "currentimage.txt updated on 
$computer_name");
         }
         else {
-                notify($ERRORS{'DEBUG'}, 0, "unable to update currentimage.txt 
on $computer_shortname");
+                notify($ERRORS{'DEBUG'}, 0, "unable to update currentimage.txt 
on $computer_name");
                 return 0;
         }
 
@@ -351,13 +347,11 @@ sub _insert_openstack_image_name {
         notify($ERRORS{'OK'}, 0, "SQL Insert is first time or requested_id : 
$requested_id");
 
         if (!$requested_id) {
-                notify($ERRORS{'OK'}, 0, "Successfully insert image name");
-               return 1;
-        }
-        else {
                 notify($ERRORS{'DEBUG'}, 0, "unable to insert image name");
                 return 0;
         }
+        notify($ERRORS{'OK'}, 0, "Successfully insert image name");
+       return 1;
 }
 
 sub _wait_for_copying_image {
@@ -404,124 +398,131 @@ sub _wait_for_copying_image {
 
 =head2 node_status
 
- Parameters  : $nodename, $log
- Returns     : array of related status checks
- Description : checks on sshd, currentimage
+ Parameters  : $computer_id or $hash->{computer}{id} (optional)
+ Returns     : string -- 'READY', 'POST_LOAD', or 'RELOAD'
+ Description : Checks the status of a VM. 'READY' is returned if the VM is
+               accessible via SSH, and the OS module's post-load tasks have 
+              run. 'POST_LOAD' is returned if the VM only needs to have 
+              the OS module's post-load tasks run before it is ready. 
+              'RELOAD' is returned otherwise.
 
 =cut
 
 sub node_status {
-       my $self = shift;
+       my $self;
 
-       my ($package, $filename, $line, $sub) = caller(0);
+        # Get the argument
+        my $argument = shift;
 
-       my $vmpath             = 0;
-       my $datastorepath      = 0;
-       my $vcl_requestedimagename = 0;
-       my $requestedimagename = 0;
-       my $vmhost_type        = 0;
-       my $vmhost_hostname    = 0;
-       my $vmhost_imagename   = 0;
-       my $image_os_type      = 0;
-       my $vmclient_shortname = 0;
-       my $request_forimaging = 0;
-       my $identity_keys      = 0;
-       my $log                = 0;
-       my $computer_node_name = 0;
+        # Check if this subroutine was called an an object method or an 
argument was passed
+        if (ref($argument) =~ /VCL::Module/i) {
+                $self = $argument;
+        }
+        elsif (!ref($argument) || ref($argument) eq 'HASH') {
+                # An argument was passed, check its type and determine the 
computer ID
+                my $computer_id;
+                if (ref($argument)) {
+                        # Hash reference was passed
+                        $computer_id = $argument->{id};
+                }
+                elsif ($argument =~ /^\d+$/) {
+                        # Computer ID was passed
+                        $computer_id = $argument;
+                }
+                else {
+                        # Computer name was passed
+                        ($computer_id) = get_computer_ids($argument);
+                }
 
-       # Set IAAS Environment 
-       notify($ERRORS{'OK'}, 0, "Set OpenStack Environment");
+                if ($computer_id) {
+                        notify($ERRORS{'DEBUG'}, 0, "computer ID: 
$computer_id");
+                }
 
+                else {
+                        notify($ERRORS{'WARNING'}, 0, "unable to determine 
computer ID from argument:\n" . format_data($argument));
+                        return;
+                }
 
-       # Check if subroutine was called as a class method
-       if (ref($self) !~ /openstack/i) {
-               notify($ERRORS{'OK'}, 0, "subroutine was called as a function");
-               if (ref($self) eq 'HASH') {
-                       $log = $self->{logfile};
-                       #notify($ERRORS{'DEBUG'}, $log, "self is a hash 
reference");
-                       $vcl_requestedimagename = 
$self->{imagerevision}->{imagename};
-                       $image_os_type      = $self->{image}->{OS}->{type};
-                       $computer_node_name = $self->{computer}->{hostname};
-                       $identity_keys      = $self->{managementnode}->{keys};
-
-               } ## end if (ref($self) eq 'HASH')
-               # Check if node_status returned an array ref
-               elsif (ref($self) eq 'ARRAY') {
-                       notify($ERRORS{'DEBUG'}, $log, "self is a array 
reference");
-               }
+                # Create a DataStructure object containing data for the 
computer specified as the argument
+                my $data;
+                eval {
+                        $data= new VCL::DataStructure({computer_identifier => 
$computer_id});
+                };
+                if ($EVAL_ERROR) {
+                        notify($ERRORS{'WARNING'}, 0, "failed to create 
DataStructure object for computer ID: $computer_id, error: $EVAL_ERROR");
+                        return;
+                }
+                elsif (!$data) {
+                        notify($ERRORS{'WARNING'}, 0, "failed to create 
DataStructure object for computer ID: $computer_id, DataStructure object is not 
defined");
+                        return;
+                }
+                else {
+                        notify($ERRORS{'DEBUG'}, 0, "created DataStructure 
object  for computer ID: $computer_id");
+                }
 
-               $vmclient_shortname = $1 if ($computer_node_name =~ 
/([-_a-zA-Z0-9]*)(\.?)/);
-       } ## end if (ref($self) !~ /esx/i)
-       else {
-               # try to contact vm
-               # $self->data->get_request_data;
-               # get state of vm
-               $vcl_requestedimagename = $self->data->get_image_name;
-               $image_os_type      = $self->data->get_image_os_type;
-               $vmclient_shortname = $self->data->get_computer_short_name;
-               $request_forimaging = $self->data->get_request_forimaging();
-               $identity_keys      = $self->data->get_management_node_keys;
-       } ## end else [ if (ref($self) !~ /esx/i)
-
-       notify($ERRORS{'OK'}, 0, "Entering node_status, checking status of 
$vmclient_shortname");
-       notify($ERRORS{'OK'}, 0, "request_for_imaging: $request_forimaging");
-       notify($ERRORS{'OK'}, 0, "requeseted image name: 
$vcl_requestedimagename");
-
-       my ($hostnode);
-
-       # Create a hash to store status components
-#      my %status;
-
-       # Initialize all hash keys here to make sure they're defined
-#      $status{status}       = 0;
-#      $status{currentimage} = 0;
-#      $status{ping}         = 0;
-#      $status{ssh}          = 0;
-#      $status{vmstate}      = 0;    #on or off
-#      $status{image_match}  = 0;
-
-       # Check if node is pingable
-#      notify($ERRORS{'OK'}, 0, "checking if $vmclient_shortname is pingable");
-#      if (_pingnode($vmclient_shortname)) {
-#              $status{ping} = 1;
-#              notify($ERRORS{'OK'}, 0, "$vmclient_shortname is pingable 
($status{ping})");
-#      }
-#      else {
-#              notify($ERRORS{'OK'}, 0, "$vmclient_shortname is not pingable 
($status{ping})");
-#              return $status{status};
-#      }
+                # Create a VMware object
+                my $object_type = 'VCL::Module::Provisioning::openstack';
+                if ($self = ($object_type)->new({data_structure => $data})) {
+                        notify($ERRORS{'DEBUG'}, 0, "created $object_type 
object to check the status of computer ID: $computer_id");
+                }
+                else {
+                        notify($ERRORS{'WARNING'}, 0, "failed to create 
$object_type object to check the status of computer ID: $computer_id");
+                        return;
+                }
+
+                # Create an OS object for the VMware object to access
+                if (!$self->create_os_object()) {
+                        notify($ERRORS{'WARNING'}, 0, "failed to create OS 
object");
+                        return;
+                }
+        }
+
+        my $reservation_id = $self->data->get_reservation_id();
+        my $computer_name = $self->data->get_computer_node_name();
+        my $image_name = $self->data->get_image_name();
+        my $request_forimaging = $self->data->get_request_forimaging();
+       my $imagerevision_id = $self->data->get_imagerevision_id();
 
+        notify($ERRORS{'DEBUG'}, 0, "attempting to check the status of 
computer $computer_name, image: $image_name");
+
+        # Create a hash reference and populate it with the default values
         my $status;
         $status->{currentimage} = '';
         $status->{ssh} = 0;
         $status->{image_match} = 0;
         $status->{status} = 'RELOAD';
 
-        notify($ERRORS{'OK'}, 0, "checking if $vmclient_shortname is 
pingable");
-        if (_pingnode($vmclient_shortname)) {
-                $status->{ping} = 1;
-                notify($ERRORS{'OK'}, 0, "$vmclient_shortname is pingable 
($status->{ping})");
-        }
-        else {
-                notify($ERRORS{'OK'}, 0, "$vmclient_shortname is not pingable 
($status->{ping})");
-                return $status->{ping}=0;
-        }
-
+        # Check if node is pingable and retrieve the power status if the 
reservation ID is 0
+        # The reservation ID will be 0 is this subroutine was not called as an 
object method, but with a computer ID argument
+        # The reservation ID will be 0 when called from healthcheck.pm
+        # The reservation ID will be > 0 if called from a normal VCL 
reservation
+        # Skip the ping and power status checks for a normal reservation to 
speed things up
+        if (!$reservation_id) {
+                if (_pingnode($computer_name)) {
+                        notify($ERRORS{'DEBUG'}, 0, "VM $computer_name is 
pingable");
+                        $status->{ping} = 1;
+                }
+                else {
+                        notify($ERRORS{'DEBUG'}, 0, "VM $computer_name is not 
pingable");
+                        $status->{ping} = 0;
+                }
 
+        }
 
        notify($ERRORS{'DEBUG'}, 0, "Trying to ssh...");
-
+        # Check if SSH is available
         if ($self->os->is_ssh_responding()) {
-                notify($ERRORS{'DEBUG'}, 0, "VM $computer_node_name is 
responding to SSH");
+                notify($ERRORS{'DEBUG'}, 0, "VM $computer_name is responding 
to SSH");
                 $status->{ssh} = 1;
         }
         else {
-                notify($ERRORS{'OK'}, 0, "VM $computer_node_name is not 
responding to SSH, returning 'RELOAD'");
+                notify($ERRORS{'OK'}, 0, "VM $computer_name is not responding 
to SSH, returning 'RELOAD'");
                 $status->{status} = 'RELOAD';
                 $status->{ssh} = 0;
-       
-               return $status;
-       }
+
+                # Skip remaining checks if SSH isn't available
+                return $status;
+        }
 
         my $current_image_revision_id = $self->os->get_current_image_info();
        $status->{currentimagerevision_id} = $current_image_revision_id;
@@ -531,15 +532,15 @@ sub node_status {
         my $vcld_post_load_status = 
$self->data->get_computer_currentimage_vcld_post_load();
 
         if (!$current_image_revision_id) {
-                notify($ERRORS{'OK'}, 0, "unable to retrieve image name from 
currentimage.txt on VM $computer_node_name, returning 'RELOAD'");
+                notify($ERRORS{'OK'}, 0, "unable to retrieve image name from 
currentimage.txt on VM $computer_name, returning 'RELOAD'");
                 return $status;
         }
-        elsif ($current_image_revision_id eq $vcl_requestedimagename) {
-                notify($ERRORS{'OK'}, 0, "currentimage.txt image 
$current_image_revision_id ($current_image_name) matches requested 
imagerevision_id $vcl_requestedimagename  on VM $computer_node_name");
+        elsif ($current_image_revision_id eq $imagerevision_id) {
+                notify($ERRORS{'OK'}, 0, "currentimage.txt image 
$current_image_revision_id ($current_image_name) matches requested 
imagerevision_id $imagerevision_id  on VM $computer_name");
                 $status->{image_match} = 1;
         }
         else {
-                notify($ERRORS{'OK'}, 0, "currentimage.txt imagerevision_id 
$current_image_revision_id ($current_image_name) does not match requested 
imagerevision_id $vcl_requestedimagename on VM $computer_node_name, returning 
'RELOAD'");
+                notify($ERRORS{'OK'}, 0, "currentimage.txt imagerevision_id 
$current_image_revision_id ($current_image_name) does not match requested 
imagerevision_id $imagerevision_id on VM $computer_name, returning 'RELOAD'");
                 return $status;
         }
 
@@ -560,8 +561,17 @@ sub node_status {
                notify($ERRORS{'OK'}, 0, "request_forimaging set, setting 
status to RELOAD");
        }
 
+        if ($vcld_post_load_status) {
+                notify($ERRORS{'DEBUG'}, 0, "OS module post_load tasks have 
been completed on VM $computer_name");
+                $status->{status} = 'READY';
+        }
+        else {
+                notify($ERRORS{'OK'}, 0, "OS module post_load tasks have not 
been completed on VM $computer_name, returning 'POST_LOAD'");
+                $status->{status} = 'POST_LOAD';
+        }
+
+
        notify($ERRORS{'DEBUG'}, 0, "returning node status hash reference 
(\$node_status->{status}=$status->{status})");
-       #return \%status;
        return $status;
 
 } ## end sub node_status
@@ -570,38 +580,36 @@ sub node_status {
 sub does_image_exist {
        my $self = shift;
        if (ref($self) !~ /openstack/i) {
-               notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
+               notify($ERRORS{'CRITICAL'}, 0, "does_image_exist() subroutine 
was called as a function, it must be called as a class method");
                return 0;
        }
 
-       my $image_fullname = $self->data->get_image_name();
-       my $image_os_type  = $self->data->get_image_os_type;
+       my $vcl_image_name = $self->data->get_image_name();
 
        # Match image name between VCL database and openstack Hbase database
-        my $image_name = _match_image_name($image_fullname);
+        my $openstack_image_name = _match_image_name($vcl_image_name);
 
-       if($image_name  =~ m/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})-v/g )
-       {
-                $image_name = $1;
-                notify($ERRORS{'OK'}, 0, "Acquire the Image ID: $image_name");
+       if($openstack_image_name  =~ m/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})-v/g ) {
+                $openstack_image_name = $1;
+                notify($ERRORS{'OK'}, 0, "Acquire the OpenStack image name: 
$openstack_image_name");
         }
         else {
-                notify($ERRORS{'DEBUG'}, 0, "Fail to acquire the Image ID: 
$image_name");
+                notify($ERRORS{'DEBUG'}, 0, "Fail to acquire the OpenStack 
image name for $vcl_image_name");
                 return 0;
         }
 
-       my $describe_images = "nova image-list | grep $image_name";
-       my $describe_images_output = `$describe_images`;
+       my $list_openstack_image = "nova image-list | grep 
$openstack_image_name";
+       my $list_openstack_image_output = `$list_openstack_image`;
 
-       notify($ERRORS{'OK'}, 0, "The describe_image output: 
$describe_images_output");
+       notify($ERRORS{'OK'}, 0, "The describe_image output: 
$list_openstack_image_output");
 
-       if ($describe_images_output =~ /$image_name/) {
-               notify($ERRORS{'OK'}, 0, "The Image $image_name exists");
+       if ($list_openstack_image_output =~ /$openstack_image_name/) {
+               notify($ERRORS{'OK'}, 0, "The openstack image for 
$vcl_image_name exists");
                return 1;
        }
        else
        {
-               notify($ERRORS{'WARNING'}, 0, "The Image $image_name does NOT 
exists");
+               notify($ERRORS{'WARNING'}, 0, "The openstack image for 
$vcl_image_name does NOT exists");
                return 0;
        }
 
@@ -619,14 +627,14 @@ sub does_image_exist {
 
 sub get_image_size {
        my $self = shift;
-       if (ref($self) !~ /open/i) {
-               notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
+       if (ref($self) !~ /openstack/i) {
+               notify($ERRORS{'CRITICAL'}, 0, "get_image_size subroutine was 
called as a function, it must be called as a class method");
                return 0;
        }
  
         notify($ERRORS{'OK'}, 0, "No image size information in Openstack");
 
-       return;
+       return 0;
 } ## end sub get_image_size
 
 #/////////////////////////////////////////////////////////////////////////////
@@ -642,6 +650,8 @@ example: openstack.conf
 "os_username" => "admin",
 "os_password" => "adminpassword",
 "os_auth_url" => "http://openstack_nova_url:5000/v2.0/";,
+"vcl_windows_key" => "vcl_windows_key",
+"vcl_linux_key" => "vcl_linux_key",
 
 
 =cut
@@ -650,8 +660,8 @@ sub _set_openstack_user_conf {
 
        my $self = shift;
         notify($ERRORS{'OK'}, 0, "********* Set OpenStack User 
Configuration******************");
-       my $computer_shortname   = $self->data->get_computer_short_name;
-        notify($ERRORS{'OK'}, 0,  "computer_shortname: $computer_shortname");
+       my $computer_name   = $self->data->get_computer_short_name;
+        notify($ERRORS{'OK'}, 0,  "computer_name: $computer_name");
        # User's environment file
        my $user_config_file = '/etc/vcl/openstack/openstack.conf';
         notify($ERRORS{'OK'}, 0,  "loading $user_config_file");
@@ -665,12 +675,16 @@ sub _set_openstack_user_conf {
         my $os_tenant_name = $self->{config}->{os_tenant_name};
         my $os_username = $self->{config}->{os_username};
         my $os_password = $self->{config}->{os_password};
+        my $vcl_windows_key = $self->{config}->{vcl_windows_key};
+        my $vcl_linux_key = $self->{config}->{vcl_linux_key};
 
        # Set Environment File
        $ENV{'OS_AUTH_URL'} = $os_auth_url;
        $ENV{'OS_TENANT_NAME'} = $os_tenant_name;
        $ENV{'OS_USERNAME'} = $os_username;
        $ENV{'OS_PASSWORD'} = $os_password;
+       $ENV{'VCL_WINDOWS_KEY'} = $vcl_windows_key;
+       $ENV{'VCL_LINUX_KEY'} = $vcl_linux_key;
 
         return 1;
 }# _set_openstack_user_conf close
@@ -715,38 +729,71 @@ sub _match_image_name {
         my $openstack_image_name = $selected_rows[0]{openstack_name};
         my $vcl_imagename  = $selected_rows[0]{vcl_name};
 
-        notify($ERRORS{'OK'}, 0, "new image name (openstack_image_name) 
=$openstack_image_name");
-        notify($ERRORS{'OK'}, 0, "new image name (vcl_image_name) 
=$vcl_imagename");
-       
+        notify($ERRORS{'OK'}, 0, "The OpenStack image name 
$openstack_image_name is matched to $vcl_imagename");
+
        return $openstack_image_name;
+}
+
+sub _get_flavor_type {
+       my $openstack_image_name = shift;
+       my $image_size;
+       my $openstack_image_info = "qemu-img info 
/var/lib/glance/images/$openstack_image_name";
+       my $openstack_image_info_output = `$openstack_image_info`;
+        if($openstack_image_info_output =~ m/virtual size:(\s\d{1,4})G/g)
+        {
+                $image_size = $1;
+                notify($ERRORS{'OK'}, 0, "The disk size for 
$openstack_image_name  is $image_size G");
+        } else {
+                notify($ERRORS{'WARNING'}, 0, "Unable to find 
$openstack_image_name in /var/lib/glance/images");
+                return 0;
+        }
+
+       # Change the disk size based on the OpenStack flavor info
+       my @flavor_disk_sizes = (1, 20, 40, 80, 160);
+       my $flavor_type;
+
+       if($image_size < $flavor_disk_sizes[0]){
+                       $flavor_type = 1;
+       } 
+       elsif($flavor_disk_sizes[0] <= $image_size && $image_size < 
$flavor_disk_sizes[1]) {
+                       $flavor_type = 2;
+       } 
+       elsif($flavor_disk_sizes[1] <= $image_size && $image_size < 
$flavor_disk_sizes[2]) {
+               $flavor_type = 3;
+       } 
+       elsif($flavor_disk_sizes[2] <= $image_size && $image_size < 
$flavor_disk_sizes[3]) {
+               $flavor_type = 4;
+       } 
+       elsif($flavor_disk_sizes[3] <= $image_size && $image_size < 
$flavor_disk_sizes[4]) {
+               $flavor_type = 5;
+       } 
+       else {
+               notify($ERRORS{'OK'}, 0, "No available OpenStack flavor for  
$openstack_image_name");
+               return 0;
+       }
+
+       notify($ERRORS{'OK'}, 0, "The flavor type for  $openstack_image_name is 
$flavor_type");
+       return $flavor_type;
 
-}# _match_image_name close
+}
 
 sub _terminate_instances {
        my $self = shift;
 
-       my $computer_shortname = $self->data->get_computer_short_name;
-       my $instance_private_ip = 
$self->data->get_computer_private_ip_address();
+       my $computer_name = $self->data->get_computer_short_name;
        my $instance_id = $self->_get_instance_id;
        $self->_delete_computer_mapping;
-       my $terminate_instances;
-       my $run_terminate_instances;
-       my $nova_db_sync;
-       my $run_nova_db_sync;
 
        if ($instance_id) {
                notify($ERRORS{'OK'}, 0, "Terminate the existing instance");
-               $terminate_instances = "nova delete $instance_id";
-               $run_terminate_instances = `$terminate_instances`;
-               notify($ERRORS{'OK'}, 0, "The nova delete : 
$run_terminate_instances is terminated");
-               # nova.conf, set force_dhcp_release=true
+               my $terminate_instances = "nova delete $instance_id";
+               my $run_terminate_instances = `$terminate_instances`;
+               notify($ERRORS{'OK'}, 0, "The nova delete : $instance_id is 
terminated");
+               # nova.conf, set force_dhcp_release=true 
                sleep 30; # wait for completely removing from nova list
-               #$nova_db_sync = "nova-manage db sync";
-               #$run_nova_db_sync = `$nova_db_sync`;
-               # Check if terminate instance is okay
        }
        else {
-               notify($ERRORS{'OK'}, 0, "No instance found for 
$computer_shortname");
+               notify($ERRORS{'OK'}, 0, "No instance found for 
$computer_name");
        }
 
        return 1;
@@ -755,24 +802,43 @@ sub _terminate_instances {
 sub _run_instances {
        my $self = shift;
        
-       my $flavor_type = '3';
-       my $key_name = 'newkey';
-       #my $key_name = 'OpenStack_Root';
-       #my $key_name = 'vclkey';
+
        my $image_full_name = $self->data->get_image_name;
-       my $computer_shortname  = $self->data->get_computer_short_name;
+       my $computer_name  = $self->data->get_computer_short_name;
 
         my $image_name = _match_image_name($image_full_name);
        if($image_name  =~ m/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})-v/g )
        {
                 $image_name = $1;
-                notify($ERRORS{'OK'}, 0, "Acquire the Image ID: $image_name");
+                notify($ERRORS{'OK'}, 0, "Acquire the openstack image name: 
$image_name");
         }
         else {
-                notify($ERRORS{'DEBUG'}, 0, "Fail to acquire the Image ID: 
$image_name");
+                notify($ERRORS{'DEBUG'}, 0, "Failed to acquire the openstack 
image name: $image_name");
                 return 0;
         }
-       my $run_instance = "nova boot --flavor $flavor_type --image $image_name 
--key_name $key_name $computer_shortname";
+
+        my $flavor_type = _get_flavor_type($image_name);
+       if(!$flavor_type) {
+                notify($ERRORS{'DEBUG'}, 0, "Fail to acquire openstack flavor 
type for $image_name");
+                return 0;
+       }
+
+       my $openstack_key;
+        my $image_os_type  = $self->data->get_image_os_type;
+       if ($image_os_type eq 'linux') {
+               $openstack_key =  $ENV{'VCL_LINUX_KEY'};        
+               notify($ERRORS{'OK'}, 0, "VCL Linux key is $openstack_key");
+       } 
+       elsif ($image_os_type eq 'windows') {
+               $openstack_key =  $ENV{'VCL_WINDOWS_KEY'};      
+               notify($ERRORS{'OK'}, 0, "VCL Windows key is $openstack_key");
+       }
+       else {
+               notify($ERRORS{'OK'}, 0, "No available openstack keys for 
$image_full_name");
+               return;
+       }
+
+       my $run_instance = "nova boot --flavor $flavor_type --image $image_name 
--key_name $openstack_key $computer_name";
        notify($ERRORS{'OK'}, 0, "The run_instance: $run_instance\n");
        
        my $run_instance_output = `$run_instance`;
@@ -786,17 +852,16 @@ sub _run_instances {
                notify($ERRORS{'OK'}, 0, "The indstance_id: $instance_id\n");
        }
        else {
-               notify($ERRORS{'OK'}, 0, "Fail to run the instance");
+               notify($ERRORS{'OK'}, 0, "Failed to run the instance");
                return 0;
        }
 
-       $insert_success = $self->_insert_instance_id($instance_id);
-
-       if (!$insert_success) {
+       if (!$self->_insert_instance_id($instance_id)) {
+               notify($ERRORS{'OK'}, 0, "Failed to insert the instance id : 
$instance_id");
                return 0;
        }
 
-       return $instance_id;
+       return 1;
 }
 
 sub _insert_instance_id {
@@ -844,6 +909,7 @@ sub _get_instance_id {
        my @selected_rows = database_select($select_statement);
 
        if (scalar @selected_rows == 0) {
+               notify($ERRORS{'WARNING'}, 0, "Unable to find the instance id");
                return 0;
        }
 
@@ -861,7 +927,7 @@ sub _delete_computer_mapping {
        DELETE FROM
        openstackComputerMap
        WHERE
-       computerid='$computer_id'
+       computerid = '$computer_id'
        ";
 
        notify($ERRORS{'OK'}, 0, "$delete_statement");
@@ -886,36 +952,34 @@ sub _update_private_ip {
        my $main_loop = 60;
        my $private_ip;
        my $describe_instance_output;
-       my $computer_shortname  = $self->data->get_computer_short_name;
+        my $computer_id = $self->data->get_computer_id;
+       my $computer_name  = $self->data->get_computer_short_name;
        my $describe_instance = "nova list |grep  $instance_id";
        notify($ERRORS{'OK'}, 0, "Describe Instance: $describe_instance");
+       notify($ERRORS{'OK'}, 0, "Computer ID: $computer_id");
 
        # Find the correct instance among running instances using the private IP
        while($main_loop > 0 && !defined($private_ip))
        {
-               notify($ERRORS{'OK'}, 0, "Try to fetch the Private IP on 
Computer $computer_shortname: Number $main_loop");     
+               notify($ERRORS{'OK'}, 0, "Try to fetch the Private IP on 
Computer $computer_name: Number $main_loop");  
                $describe_instance_output = `$describe_instance`;
                notify($ERRORS{'OK'}, 0, "Describe Instance: 
$describe_instance_output");
 
-               if($describe_instance_output =~ m/(10.25.6.\d{1,3})/g)
+               if($describe_instance_output =~ 
m/((10|192|172).(\d{1,3}|68|16).(\d{1,3}).(\d{1,3}))/g) 
                {
-                       $private_ip = $&;
-                       notify($ERRORS{'OK'}, 0, "The instance private IP on 
Computer $computer_shortname: $private_ip");
+                       $private_ip = $1;
+                       notify($ERRORS{'OK'}, 0, "The instance private IP on 
Computer $computer_name: $private_ip");
                        if (defined($private_ip) && $private_ip ne "") {
-                               notify($ERRORS{'OK'}, 0, "Removing old hosts 
entry");
-                               my $sedoutput = `sed -i 
"/.*\\b$computer_shortname\$/d" /etc/hosts`;
-                               notify($ERRORS{'DEBUG'}, 0, $sedoutput);
-                               `echo -e "$private_ip\t$computer_shortname" >> 
/etc/hosts`;
-                               my $new_private_ip = 
$self->data->set_computer_private_ip_address($private_ip);
+                               my $new_private_ip = 
update_computer_private_ip_address($computer_id, $private_ip);
                                if(!$new_private_ip) {
-                                       notify($ERRORS{'OK'}, 0, "The 
$private_ip on Computer $computer_shortname is NOT updated");
+                                       notify($ERRORS{'OK'}, 0, "The 
$private_ip on Computer $computer_name is NOT updated");
                                        return 0;
                                }
                                goto EXIT_WHILELOOP;
                        }
                }
                else {
-                               notify($ERRORS{'DEBUG'}, 0, "Private IP for 
$computer_shortname is not determined");
+                               notify($ERRORS{'DEBUG'}, 0, "Private IP for 
$computer_name is not determined");
                }
 
                sleep 20;


Reply via email to