From d4a8e6eb074c31a7111011de51a326ab3d5c5bb7 Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 17 Aug 2018 09:46:04 +0200 Subject: [PATCH] Bugs fixed --- debian/changelog | 2 +- vn-vmware.pl | 94 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/debian/changelog b/debian/changelog index 362db82..432f057 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -vn-vmware (1.0.4) stable; urgency=low +vn-vmware (1.0.5) stable; urgency=low * Initial Release. diff --git a/vn-vmware.pl b/vn-vmware.pl index 1d35c1c..5a8ca86 100755 --- a/vn-vmware.pl +++ b/vn-vmware.pl @@ -197,7 +197,7 @@ sub log_error { my ($error) = @_; if ($error->isa('SOAP::Fault')) { - $error = $error->faultcode.": ".$error->faultString; + $error = $error->faultcode.": ".$error->faultString."\n"; } print color('red'); @@ -250,6 +250,9 @@ sub main { log_message "Connected to $vcenter_host."; eval { + unless ($operation) { + die "Operation not defined."; + } given ($operation) { when ('backup-job') { backup_job(); @@ -385,7 +388,7 @@ sub backup_machine() { my $service_content = Vim::get_service_content(); my $file_manager = Vim::get_view(mo_ref => $service_content->fileManager); my $dc = Vim::find_entity_view( - view_type => "Datacenter", + view_type => 'Datacenter', filter => {'name' => $config{datacenter}} ); @@ -410,7 +413,10 @@ sub backup_machine() { my $vdm = Vim::get_view(mo_ref => $service_content->virtualDiskManager); my $devices = $vm->config->hardware->device; + log_message "Removing snapshots."; $vm->RemoveAllSnapshots(); + + log_message "Creating backup snapshot."; $vm->CreateSnapshot( name => "backup", description => "Scheduled backup", @@ -433,6 +439,7 @@ sub backup_machine() { } } + log_message "Removing snapshots."; $vm->RemoveAllSnapshots(); log_message "Compressing to TAR file: $tar_file"; @@ -446,6 +453,8 @@ sub backup_machine() { if ($err) { log_message "An error ocurred during '$vm_name' backup, aborting."; + + log_message "Removing snapshots."; $vm->RemoveAllSnapshots(); } @@ -522,6 +531,7 @@ sub rotate_backup() { sub clone_machine { log_message "Cloning '$vm_name' to '$dst_name'."; + log_message "Doing some previous checkings."; check_datastore(); @@ -532,8 +542,9 @@ sub clone_machine { if ($original_vm) { if ($overwrite) { - set_power_state($original_vm, "poweredOff"); - sleep(20); + set_power_state($original_vm, 'poweredOff'); + + log_message "Deleting machine '$dst_name'."; $original_vm->Destroy(); } else { die "Machine with same name exists."; @@ -556,14 +567,48 @@ sub clone_machine { } } + log_message "Defining clone specifications."; + + # Hostname + + my $extra_conf = OptionValue->new( + key => 'guestinfo.hostname', + value => $dst_name + ); + + # Operating system + + my $guest_id = $vm->guest->guestId; + $guest_id = $guest_id ? $guest_id : undef; + + # CPU & memory + + my $cpu; + my $mem_res; + my $sl = SharesLevel->new('normal'); + my $sh = SharesInfo->new(level => $sl, shares => 1); + + if (defined($cpu_reservation)) { + $cpu = ResourceAllocationInfo->new( + reservation => $cpu_reservation, + limit => -1, + shares => $sh + ); + } + if (defined($mem_reservation)) { + $mem_res = ResourceAllocationInfo->new( + reservation => $mem_reservation, + limit => -1, + shares => $sh + ); + } + + # Datastore + my $relocate_spec; - my $mac_type = 'Generated'; my $host_view = $vm->summary->runtime->host; - if (defined($mac)) { - $mac_type = 'Manual'; - } - if (defined ($dst_host)) { + if (defined($dst_host)) { $host_view = Vim::find_entity_view( view_type => 'HostSystem', filter => {'name' => $dst_host} @@ -589,6 +634,14 @@ sub clone_machine { host => $host_view ); } + + # Network card + + my $mac_type = 'Generated'; + + if (defined($mac)) { + $mac_type = 'Manual'; + } my $vnic_device; my $devices = $vm->config->hardware->device; @@ -603,7 +656,6 @@ sub clone_machine { my $curr_mac = $vnic_device->macAddress; my $network = Vim::get_view(mo_ref => $vnic_device->backing->network, properties => ['name']); - my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit'); my $backing_info = VirtualEthernetCardNetworkBackingInfo->new(deviceName => $network->{'name'}); my $nic_type = ref($vnic_device); @@ -619,6 +671,7 @@ sub clone_machine { die "Unable to retrieve NIC type."; } + my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit'); my $new_network_device = $nic_type->new( key => $vnic_device->key, unitNumber => $vnic_device->unitNumber, @@ -627,24 +680,12 @@ sub clone_machine { addressType => $mac_type, macAddress => $mac ); - - my $sl = SharesLevel->new('normal'); - my $sh = SharesInfo->new(level => $sl, shares => 1); - - my $mem_res = ResourceAllocationInfo->new( - reservation => $mem_reservation, - limit => -1, - shares => $sh - ); - my $cpu = ResourceAllocationInfo->new( - reservation => $cpu_reservation, - limit => -1, - shares => $sh + my $vm_dev_spec = VirtualDeviceConfigSpec->new( + operation => $config_spec_operation, + device => $new_network_device ); - my $guest_id = ($vm->guest->guestId =~ m/^other/) ? "debian6_64Guest" : $vm->guest->guestId; - my $vm_dev_spec = VirtualDeviceConfigSpec->new(device => $new_network_device, operation => $config_spec_operation); - my $extra_conf = OptionValue->new(key => "guestinfo.hostname", value => $dst_name); + # Gathering specifications and cloning my $change_spec = VirtualMachineConfigSpec->new( deviceChange => [$vm_dev_spec], @@ -663,6 +704,7 @@ sub clone_machine { config => $change_spec ); + log_message "Cloning machine."; $vm->CloneVM( folder => $vm->parent, name => $dst_name,