Author: arkurth
Date: Wed Apr  4 19:18:45 2012
New Revision: 1309553

URL: http://svn.apache.org/viewvc?rev=1309553&view=rev
Log:
VCL-450
Updated VMware.pm::prepare_vmx allowing it to utilize the 
vmprofile.eth0generated and vmprofile.eth1generated values. If set to 1, the 
interfaces added to the VM are configured with auto-generated MAC addresses. 
Also added ability to add additional interfaces if virtualswitch2 or 
virtualswitch3 are configured.

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

Modified: 
incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/VMware.pm
URL: 
http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/VMware.pm?rev=1309553&r1=1309552&r2=1309553&view=diff
==============================================================================
--- 
incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/VMware.pm 
(original)
+++ 
incubator/vcl/trunk/managementnode/lib/VCL/Module/Provisioning/VMware/VMware.pm 
Wed Apr  4 19:18:45 2012
@@ -269,9 +269,12 @@ sub initialize {
        my $vmhost_os_module_package = 
$vmhost_data->get_image_os_module_perl_package();
        my $vmhost_lastcheck_time = 
$vmhost_data->get_computer_lastcheck_time(0);
        my $vmhost_computer_id = $self->data->get_vmhost_computer_id();
+       my $vmprofile_name = $self->data->get_vmhost_profile_name();
        
        my $vmware_api;
        
+       notify($ERRORS{'DEBUG'}, 0, "VM profile assigned to 
$vmhost_computer_name: $vmprofile_name");
+       
        # Create an API object which will be used to control the VM (register, 
power on, etc.)
        if (($vmware_api = $self->get_vmhost_api_object($VSPHERE_SDK_PACKAGE)) 
&& !$vmware_api->is_restricted()) {
                notify($ERRORS{'DEBUG'}, 0, "vSphere SDK object will be used to 
control the VM host $vmhost_computer_name and the VM: $vm_computer_name");
@@ -1657,10 +1660,12 @@ sub prepare_vmx {
        my $image_name                = $self->data->get_image_name() || return;
        my $vm_ram                    = $self->get_vm_ram() || return;
        my $vm_ethernet_adapter_type  = $self->get_vm_ethernet_adapter_type() 
|| return;
-       my $vm_eth0_mac               = 
$self->data->get_computer_eth0_mac_address() || return;
-       my $vm_eth1_mac               = 
$self->data->get_computer_eth1_mac_address() || return; 
-       my $virtual_switch_0          = 
$self->data->get_vmhost_profile_virtualswitch0() || return;
-       my $virtual_switch_1          = 
$self->data->get_vmhost_profile_virtualswitch1() || return;
+       my $vm_eth0_generated         = 
$self->data->get_vmhost_profile_eth0generated(0);
+       my $vm_eth1_generated         = 
$self->data->get_vmhost_profile_eth1generated(0);
+       my $virtual_switch_0          = 
$self->data->get_vmhost_profile_virtualswitch0(0) || '';
+       my $virtual_switch_1          = 
$self->data->get_vmhost_profile_virtualswitch1(0) || '';
+       my $virtual_switch_2          = 
$self->data->get_vmhost_profile_virtualswitch2(0) || '';
+       my $virtual_switch_3          = 
$self->data->get_vmhost_profile_virtualswitch3(0) || '';
        my $vm_disk_adapter_type      = $self->get_vm_disk_adapter_type() || 
return;
        my $vm_hardware_version       = 
$self->get_vm_virtual_hardware_version() || return;
        my $is_vm_dedicated           = $self->is_vm_dedicated();
@@ -1719,20 +1724,6 @@ sub prepare_vmx {
                
                "displayName" => "$display_name",
                
-               "ethernet0.connectionType" => "custom",
-               "ethernet0.address" => "$vm_eth0_mac",
-               "ethernet0.addressType" => "static",
-               "ethernet0.present" => "TRUE",
-               "ethernet0.virtualDev" => "$vm_ethernet_adapter_type",
-               "ethernet0.$network_parameter" => "$virtual_switch_0",
-               
-               "ethernet1.connectionType" => "custom",
-               "ethernet1.address" => "$vm_eth1_mac",
-               "ethernet1.addressType" => "static",
-               "ethernet1.present" => "TRUE",
-               "ethernet1.virtualDev" => "$vm_ethernet_adapter_type",
-               "ethernet1.$network_parameter" => "$virtual_switch_1",
-               
                "floppy0.present" => "FALSE",
                
                "guestOS" => "$guest_os",
@@ -1829,6 +1820,41 @@ sub prepare_vmx {
                ));
        }
        
+       # Add ethernet adapter definitions to the hash
+       my $interface_index = 0;
+       
+       if ($virtual_switch_0) {
+               if ($vm_eth0_generated) {
+                       %vmx_parameters = (%vmx_parameters, 
%{$self->get_generated_ethernet_vmx_definition($interface_index, 
$virtual_switch_0)});
+               }
+               else {
+                       my $vm_eth0_mac = 
$self->data->get_computer_eth0_mac_address();
+                       %vmx_parameters = (%vmx_parameters, 
%{$self->get_static_ethernet_vmx_definition($interface_index, 
$virtual_switch_0, $vm_eth0_mac)});
+               }
+               $interface_index++;
+       }
+       
+       if ($virtual_switch_1) {
+               if ($vm_eth1_generated) {
+                       %vmx_parameters = (%vmx_parameters, 
%{$self->get_generated_ethernet_vmx_definition($interface_index, 
$virtual_switch_1)});
+               }
+               else {
+                       my $vm_eth1_mac = 
$self->data->get_computer_eth1_mac_address();
+                       %vmx_parameters = (%vmx_parameters, 
%{$self->get_static_ethernet_vmx_definition($interface_index, 
$virtual_switch_1, $vm_eth1_mac)});
+               }
+               $interface_index++;
+       }
+       
+       # If vmprofile.virtualswitch2 or vmprofile.virtualswitch3 are defined 
in the database, add autogenerated interfaces
+       if ($virtual_switch_2) {
+               %vmx_parameters = (%vmx_parameters, 
%{$self->get_generated_ethernet_vmx_definition($interface_index, 
$virtual_switch_2)});
+               $interface_index++;
+       }
+       if ($virtual_switch_3) {
+               %vmx_parameters = (%vmx_parameters, 
%{$self->get_generated_ethernet_vmx_definition($interface_index, 
$virtual_switch_3)});
+               $interface_index++;
+       }
+       
        # Add additional Ethernet interfaces if the image project name is not 
vcl
        if ($image_project !~ /^vcl$/i && $self->api->can('get_network_names')) 
{
                notify($ERRORS{'DEBUG'}, 0, "image project is: $image_project, 
checking if additional network adapters should be configured");
@@ -1839,21 +1865,10 @@ sub prepare_vmx {
                
                # Check each network name
                # Begin the index at 2 for additional interfaces added because 
ethernet0 and ethernet1 have already been added
-               my $interface_index = 2;
                for my $network_name (@network_names) {
-                       # Ignore network names which have already been added
-                       if ($network_name =~ 
/^($virtual_switch_0|$virtual_switch_1)$/) {
-                               notify($ERRORS{'DEBUG'}, 0, "ignoring network 
name because it is already being used for the private or public interface: 
$network_name");
-                               next;
-                       }
-                       elsif ($network_name =~ /$image_project/i || 
$image_project =~ /$network_name/i) {
+                       if ($network_name =~ /$image_project/i || 
$image_project =~ /$network_name/i) {
                                notify($ERRORS{'DEBUG'}, 0, "network name 
($network_name) and image project name ($image_project) intersect, adding 
network interface to VM for network $network_name");
-                               
-                               
$vmx_parameters{"ethernet$interface_index.addressType"} = "generated";
-                               
$vmx_parameters{"ethernet$interface_index.present"} = "TRUE";
-                               
$vmx_parameters{"ethernet$interface_index.virtualDev"} = 
"$vm_ethernet_adapter_type";
-                               
$vmx_parameters{"ethernet$interface_index.$network_parameter"} = 
"$network_name";
-                               
+                               %vmx_parameters = (%vmx_parameters, 
%{$self->get_generated_ethernet_vmx_definition($interface_index, 
$network_name)});
                                $interface_index++;
                        }
                        else {
@@ -1904,6 +1919,100 @@ sub prepare_vmx {
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 get_static_ethernet_vmx_definition
+
+ Parameters  : $interface_index, $network_name, $mac_address
+ Returns     : hash reference
+ Description : Generates a hash containing keys and values which correlate to
+               the vmx file parameters necessary to define an ethernet 
interface
+               with a static MAC address.
+
+=cut
+
+sub get_static_ethernet_vmx_definition {
+       my $self = shift;
+       if (ref($self) !~ /vmware/i) {
+               notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
+               return;
+       }
+       
+       my $interface_index = shift;
+       if (!defined($interface_index)) {
+               notify($ERRORS{'WARNING'}, 0, "interface index argument was not 
supplied");
+               return;
+       }
+       
+       my $network_name = shift;
+       if (!defined($network_name)) {
+               notify($ERRORS{'WARNING'}, 0, "network name argument was not 
supplied");
+               return;
+       }
+       
+       my $mac_address = shift;
+       if (!defined($mac_address)) {
+               notify($ERRORS{'WARNING'}, 0, "MAC address argument was not 
supplied");
+               return;
+       }
+       
+       my $vm_ethernet_adapter_type  = $self->get_vm_ethernet_adapter_type() 
|| return;
+       
+       my %vmx_parameters = (
+               "ethernet$interface_index.present" => "TRUE",
+               "ethernet$interface_index.connectionType" => "custom",
+               "ethernet$interface_index.addressType" => "static",
+               "ethernet$interface_index.address" => "$mac_address",
+               "ethernet$interface_index.virtualDev" => 
"$vm_ethernet_adapter_type",
+               "ethernet$interface_index.networkName" => "$network_name",
+       );
+       
+       return \%vmx_parameters;
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
+=head2 get_generated_ethernet_vmx_definition
+
+ Parameters  : $interface_index, $network_name
+ Returns     : hash reference
+ Description : Generates a hash containing keys and values which correlate to
+               the vmx file parameters necessary to define an ethernet 
interface
+               with an auto-generated MAC address.
+
+=cut
+
+sub get_generated_ethernet_vmx_definition {
+       my $self = shift;
+       if (ref($self) !~ /vmware/i) {
+               notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a 
function, it must be called as a class method");
+               return;
+       }
+       
+       my $interface_index = shift;
+       if (!defined($interface_index)) {
+               notify($ERRORS{'WARNING'}, 0, "interface index argument was not 
supplied");
+               return;
+       }
+       
+       my $network_name = shift;
+       if (!defined($interface_index)) {
+               notify($ERRORS{'WARNING'}, 0, "network name argument was not 
supplied");
+               return;
+       }
+       
+       my $vm_ethernet_adapter_type  = $self->get_vm_ethernet_adapter_type() 
|| return;
+       
+       my %vmx_parameters = (
+               "ethernet$interface_index.addressType" => "generated",
+               "ethernet$interface_index.present" => "TRUE",
+               "ethernet$interface_index.virtualDev" => 
"$vm_ethernet_adapter_type",
+               "ethernet$interface_index.networkName" => "$network_name",
+       );
+       
+       return \%vmx_parameters;
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 =head2 prepare_vmdk
 
  Parameters  : none


Reply via email to