Show
Ignore:
Timestamp:
05/27/08 17:17:07 (6 months ago)
Author:
kindlund
Message:

Added finer-grain .vmx/.cfg configuration editing, as suggested in ticket #148. By using Config::General to handle low-level .vmx/.cfg parsing, we no longer need to worry about how well each VM configuration is formatted, in order for the system properly run.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeyclient/trunk/lib/HoneyClient/Manager/VM.pm

    r1577 r1590  
    465465use Fcntl qw(O_RDONLY); 
    466466 
     467# Make sure Config::General loads. 
     468BEGIN { use_ok('Config::General') or diag("Can't load Config::General package.  Check to make sure the package library is correctly listed within the path."); } 
     469require_ok('Config::General'); 
     470use Config::General; 
     471 
    467472# Make sure VMware::VmPerl loads. 
    468473BEGIN { use_ok('VMware::VmPerl', qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED)) or diag("Can't load VMware::VmPerl package.  Check to make sure the package library is correctly listed within the path."); } 
     
    582587use Tie::File; 
    583588use Fcntl qw(O_RDONLY); 
     589use Config::General; 
    584590 
    585591# Include Thread Libraries 
     
    26132619    $vmSemaphore->down(); 
    26142620 
    2615     # Set the displayName within the config file on disk... 
    2616     if (!tie(@configArray, 'Tie::File', $args{'config'})) { 
     2621    my $config_obj = undef; 
     2622    eval { 
     2623        # Construct a new configuration object. 
     2624        $config_obj = new Config::General($args{'config'}); 
     2625    }; 
     2626    if ($@) { 
    26172627        # Unlock VM early, if failed. 
    26182628        $vmSemaphore->up(); 
     
    26212631                       ->faultstring("Could not set name of VM ($args{'config'})."); 
    26222632    } 
    2623  
    2624     for (@configArray) { 
    2625         s/^displayName =.*$/displayName = "$args{'name'}"/g; 
    2626     } 
    2627     untie @configArray; 
    2628      
     2633    # Parse all the data. 
     2634    my %raw_config = $config_obj->getall; 
     2635 
     2636    # Sort the configuration. 
     2637    my @sorted_keys = (sort keys %raw_config); 
     2638    
     2639    # Adjust the configuration. 
     2640    # Set the displayName within the config file on disk... 
     2641    $raw_config{'displayName'} = $args{'name'}; 
     2642 
     2643    # Save the new configuration. 
     2644    my $OUTPUT_FILE = new IO::File($args{'config'}, "w"); 
     2645    print $OUTPUT_FILE "#!/usr/bin/vmware\n\n"; 
     2646    foreach my $key (@sorted_keys) { 
     2647        print $OUTPUT_FILE $key . " = \"" . $raw_config{$key} . "\"\n"; 
     2648    } 
     2649    $OUTPUT_FILE->close(); 
     2650 
    26292651    # Unlock the VM. 
    26302652    $vmSemaphore->up(); 
     
    37933815    $vmSemaphore->down(); 
    37943816 
    3795     if (!tie(@configArray, 'Tie::File', $args{'config'})) { 
     3817    my $config_obj = undef; 
     3818    eval { 
     3819        # Construct a new configuration object. 
     3820        $config_obj = new Config::General($args{'config'}); 
     3821    }; 
     3822    if ($@) { 
    37963823        # Unlock VM early, if failed. 
    37973824        $vmSemaphore->up(); 
     
    38003827                       ->faultstring("Could not set Master VM ($args{'config'})."); 
    38013828    } 
    3802  
    3803     for (@configArray) { 
    3804         # Make sure the master VM configuration version is "7", since 
    3805         # versions 8 and higher have marked the undoable mode operation 
    3806         # as deprecated, as it's implemented differently. 
    3807         s/^config.version.*$/config.version = "7"/g; 
     3829    # Parse all the data. 
     3830    my %raw_config = $config_obj->getall; 
     3831 
     3832    # Sort the configuration. 
     3833    my @sorted_keys = (sort keys %raw_config); 
     3834     
     3835    # Adjust the configuration. 
     3836    # Make sure the master VM configuration version is "7", since 
     3837    # versions 8 and higher have marked the undoable mode operation 
     3838    # as deprecated, as it's implemented differently. 
     3839    $raw_config{'config.version'} = 7; 
     3840    foreach my $key (@sorted_keys) { 
    38083841        # Switch all virtual disks to undoable mode... 
    3809         s/^(.*)\.mode = "persistent"$/$1\.mode = "undoable"/g; 
    3810         # Make sure all *.vmdk files are specified with absolute paths... 
    3811         s/^(.*)\.fileName = "(.*\/)*(.*\.vmdk)"$/$1\.fileName = \"$srcDir\/$3\"/g; 
    3812     } 
    3813     untie @configArray; 
    3814      
     3842        if ($key =~ /^.*\.mode/) { 
     3843            $raw_config{$key} = "undoable"; 
     3844        } elsif ($key =~ /^.*\.fileName/) { 
     3845            # Make sure all *.vmdk files are specified with absolute paths... 
     3846            $raw_config{$key} =~ s/^(.*\/)*(.*\.vmdk)$/$srcDir\/$2/g; 
     3847        } 
     3848    } 
     3849 
     3850    # Save the new configuration. 
     3851    my $OUTPUT_FILE = new IO::File($args{'config'}, "w"); 
     3852    print $OUTPUT_FILE "#!/usr/bin/vmware\n\n"; 
     3853    foreach my $key (@sorted_keys) { 
     3854        print $OUTPUT_FILE $key . " = \"" . $raw_config{$key} . "\"\n"; 
     3855    } 
     3856    $OUTPUT_FILE->close(); 
     3857 
    38153858    # Unlock the VM. 
    38163859    $vmSemaphore->up(); 
     
    46694712        !exists($args{'snapshot_file'}) || 
    46704713        !defined($args{'snapshot_file'})) { 
    4671         my @configArray = undef; 
    4672         unless (tie(@configArray, 'Tie::File', $args{'config'})) {  
     4714 
     4715        my $config_obj = undef; 
     4716        eval { 
     4717            # Construct a new configuration object. 
     4718            $config_obj = new Config::General($args{'config'}); 
     4719        }; 
     4720        if ($@) { 
    46734721            $LOG->warn("Could not read VM configuration ($args{'config'})."); 
    46744722            die SOAP::Fault->faultcode(__PACKAGE__ . "->revertVM()") 
    46754723                           ->faultstring("Could not read VM configuration ($args{'config'})."); 
    46764724        } 
    4677         for (@configArray) { 
     4725        # Parse all the data. 
     4726        my %raw_config = $config_obj->getall; 
     4727 
     4728        # Sort the configuration. 
     4729        my @sorted_keys = (sort keys %raw_config); 
     4730     
     4731        # Adjust the configuration. 
     4732        foreach my $key (@sorted_keys) { 
    46784733            # Make sure all *.vmdk files are specified with absolute paths 
    46794734            # to a Master VM... 
    4680             if (/^(.*)\.fileName = "(.*\/)*(.*\.vmdk)"$/) { 
    4681                 $masterConfig = "$2$configFile"; 
    4682                 if ((!-d $2) || (dirname("$2/x") eq dirname("$vmDir/x"))) {  
    4683                     $LOG->warn("Could not revert; specified VM is not a quick clone ($2$3)."); 
     4735            if (($key =~ /^.*\.fileName/) && ($raw_config{$key} =~ /^(.*\/)*(.*\.vmdk)$/)) { 
     4736                my $dir  = defined($1) ? $1 : ""; 
     4737                my $file = defined($2) ? $2 : ""; 
     4738                $masterConfig = $dir . $configFile; 
     4739                if ((!-d $dir) || (dirname("$dir/x") eq dirname("$vmDir/x"))) {  
     4740                    $LOG->warn("Could not revert; specified VM is not a quick clone (" . $args{'config'} . ")."); 
    46844741                    die SOAP::Fault->faultcode(__PACKAGE__ . "->revertVM()") 
    4685                                    ->faultstring("Could not revert; specified VM is not a quick clone ($2$3)."); 
     4742                                   ->faultstring("Could not revert; specified VM is not a quick clone (" . $args{'config'} . ")."); 
    46864743                } 
    46874744            } 
    46884745        } 
    4689         untie @configArray; 
    46904746    } 
    46914747 
  • honeyclient/trunk/t/honeyclient_manager_vm.t

    r1499 r1590  
    122122use Fcntl qw(O_RDONLY); 
    123123 
     124# Make sure Config::General loads. 
     125BEGIN { use_ok('Config::General') or diag("Can't load Config::General package.  Check to make sure the package library is correctly listed within the path."); } 
     126require_ok('Config::General'); 
     127use Config::General; 
     128 
    124129# Make sure VMware::VmPerl loads. 
    125130BEGIN { use_ok('VMware::VmPerl', qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED)) or diag("Can't load VMware::VmPerl package.  Check to make sure the package library is correctly listed within the path."); }