pigz now uses less CPU, improved logging, unused code removed
This commit is contained in:
parent
6d03fce86c
commit
080dd58916
|
@ -1,4 +1,4 @@
|
|||
vn-vmware (1.0.6) stable; urgency=low
|
||||
vn-vmware (1.0.7) stable; urgency=low
|
||||
|
||||
* Initial Release.
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ Vcs-Git: https://git.verdnatura.es/vn-vmware
|
|||
|
||||
Package: vn-vmware
|
||||
Architecture: all
|
||||
Depends: perl, pigz
|
||||
Depends: perl, pigz, libsys-cpu-perl
|
||||
Section: misc
|
||||
Priority: optional
|
||||
Description: Maintenance scripts for VMWare
|
||||
|
|
154
vn-vmware.pl
154
vn-vmware.pl
|
@ -8,15 +8,12 @@ use VMware::VIRuntime;
|
|||
use VMware::VICredStore;
|
||||
use VMware::VILib;
|
||||
use VMware::VIExt;
|
||||
use Date::Parse;
|
||||
use Date::Format;
|
||||
use Time::Piece;
|
||||
use Time::Seconds;
|
||||
use File::Path;
|
||||
use Net::SMTP;
|
||||
use Term::ANSIColor;
|
||||
use Net::OpenSSH;
|
||||
use XML::LibXML;
|
||||
use Sys::CPU;
|
||||
use Term::ANSIColor;
|
||||
|
||||
use constant false => 0;
|
||||
use constant true => 1;
|
||||
|
@ -182,7 +179,7 @@ sub log_to_file {
|
|||
my ($message) = @_;
|
||||
my $time = Time::Piece->new;
|
||||
my $time_mark = $time->strftime('%Y-%m-%d %H:%M:%S');
|
||||
print $log_fh "$time_mark > $message";
|
||||
print $log_fh "$time_mark $message";
|
||||
}
|
||||
sub log_message {
|
||||
my ($message) = @_;
|
||||
|
@ -413,7 +410,7 @@ sub backup_machine() {
|
|||
my $vdm = Vim::get_view(mo_ref => $service_content->virtualDiskManager);
|
||||
my $devices = $vm->config->hardware->device;
|
||||
|
||||
log_message "Removing snapshots.";
|
||||
log_message "Consolidating and removing existing snapshots.";
|
||||
$vm->RemoveAllSnapshots();
|
||||
|
||||
log_message "Creating backup snapshot.";
|
||||
|
@ -439,11 +436,15 @@ sub backup_machine() {
|
|||
}
|
||||
}
|
||||
|
||||
log_message "Removing snapshots.";
|
||||
log_message "Consolidating and removing snapshots.";
|
||||
$vm->RemoveAllSnapshots();
|
||||
|
||||
log_message "Compressing to TAR file: $tar_file";
|
||||
my $tar_status = system("tar -I pigz -cf $tar_file -C $local_tmp_dir .");
|
||||
my $gzip_cpus = int(Sys::CPU::cpu_count()) - 1;
|
||||
my $tar_command = "tar -I \"pigz -p $gzip_cpus\" -cf $tar_file -C $local_tmp_dir .";
|
||||
|
||||
log_message "Compressing with Gzip (using $gzip_cpus CPUs) to TAR file: $tar_file";
|
||||
log_message $tar_command;
|
||||
my $tar_status = system($tar_command);
|
||||
|
||||
unless ($tar_status == 0) {
|
||||
die "An error occurred when trying to compress the file.";
|
||||
|
@ -533,8 +534,6 @@ sub clone_machine {
|
|||
log_message "Cloning '$vm_name' to '$dst_name'.";
|
||||
log_message "Doing some previous checkings.";
|
||||
|
||||
check_datastore();
|
||||
|
||||
my $original_vm = Vim::find_entity_view(
|
||||
view_type => 'VirtualMachine',
|
||||
filter => {'name' => $dst_name}
|
||||
|
@ -717,73 +716,12 @@ sub clone_machine {
|
|||
sub snapshot_machine() {
|
||||
log_message "Creating snapshot of '$vm_name' with CBT $cbt.";
|
||||
|
||||
if ($cbt) {
|
||||
if ($vm->capability->changeTrackingSupported) {
|
||||
setCBT(true);
|
||||
} else {
|
||||
die "CBT not supported.";
|
||||
}
|
||||
|
||||
my $vm_backup = Vim::find_entity_view(
|
||||
view_type => 'VirtualMachine',
|
||||
filter => {'name' => $dst_name}
|
||||
);
|
||||
my $devices = $vm_backup->config->hardware->device;
|
||||
my $vm_dst_datastore;
|
||||
|
||||
foreach (@$devices) {
|
||||
if ($_->isa('VirtualDisk')) {
|
||||
my $label = $_->deviceInfo->label;
|
||||
my $diskName = $_->backing->fileName;
|
||||
my $disk_string .= "\t" . $label . " = " . $diskName . "\n";
|
||||
my @sp = split(/\s+/,$diskName);
|
||||
my @sp1 = split(/[\/]/,$sp[1]);
|
||||
$vm_dst_datastore = $sp1[0];
|
||||
}
|
||||
}
|
||||
|
||||
# Obtains the ESXI host where machine is located
|
||||
|
||||
my $vm_ds = Vim::get_views(mo_ref_array => $vm->get_property('datastore'));
|
||||
$vm_datastore = join(',', map($_->get_property('name'), @{$vm_ds}));
|
||||
|
||||
my $vm_folder = "/vmfs/volumes/$vm_datastore/$vm_name";
|
||||
|
||||
my $command = "cd $vm_folder && ".q[ls -lhr *-0*-ctk.vmdk|head -n1 |awk '{print $9}' |cut -c].(length($vm_name)+2)."-".(length($vm_name)+7);
|
||||
my $snap_file = execute_ssh_command($command);
|
||||
|
||||
if (length($snap_file) > 0) {
|
||||
$command = "cd $vm_folder && ".q[ls -lhr *-0*-delta.vmdk|head -n1 |awk '{print $5}' | sed '$s/...$//'];
|
||||
my $snp_size = execute_ssh_command($command);
|
||||
|
||||
if (($snp_size + 0) >= $cbt_size) {
|
||||
set_power_state($vm_backup, "poweredOff");
|
||||
|
||||
$command = "scp -i /etc/ssh/ssh_host_dsa_key $vm_folder/$vm_name.vmdk root@"."$dst_host:/vmfs/volumes/$dst_datastore/$vm_dst_datastore";
|
||||
execute_ssh_command($command);
|
||||
|
||||
create_snapshot($vm);
|
||||
create_snapshot($vm_backup);
|
||||
|
||||
my $vmdk_file = "$vm_name-".substr($snap_file,0,length($snap_file)-1)."*.vmdk";
|
||||
log_message "Copying snapshot $vmdk_file is less or equal than $cbt_size with a size of ".length($snap_file).".";
|
||||
$command = "scp -i /etc/ssh/ssh_host_dsa_key $vm_folder/$vmdk_file root@"."$dst_host:/vmfs/volumes/$dst_datastore/$vm_dst_datastore";
|
||||
execute_ssh_command($command);
|
||||
|
||||
# Deletes all snapshots
|
||||
|
||||
$vm->RemoveAllSnapshots();
|
||||
create_snapshot($vm);
|
||||
} else {
|
||||
log_message "No snapshot is made since the size of $vm_name-$snap_file-delta.vmdk is less than $cbt_size.";
|
||||
}
|
||||
} else {
|
||||
log_message "Creating first snapshot";
|
||||
create_snapshot($vm);
|
||||
}
|
||||
} else {
|
||||
create_snapshot($vm);
|
||||
}
|
||||
$vm->CreateSnapshot(
|
||||
name => $snapshot_name,
|
||||
description => $snapshot_desc,
|
||||
memory => 0,
|
||||
quiesce => 0
|
||||
);
|
||||
|
||||
log_message "Snapshot of '$vm_name' successfully created.";
|
||||
}
|
||||
|
@ -855,44 +793,6 @@ sub open_machine() {
|
|||
log_message "Found machine '$vm_name' at host $remote_host at datastore '$vm_datastore'.";
|
||||
}
|
||||
|
||||
sub setCBT {
|
||||
my ($enable) = @_;
|
||||
if ($vm->config->changeTrackingEnabled eq $enable) {
|
||||
return;
|
||||
}
|
||||
eval {
|
||||
log_message "Switching CBT to $enable.";
|
||||
my $spec = Vim::VirtualMachineConfigSpec->new(changeTrackingEnabled => $enable);
|
||||
my $task = $vm->ReconfigVM_Task(spec => $spec);
|
||||
};
|
||||
$cbt = $enable;
|
||||
}
|
||||
|
||||
sub create_snapshot {
|
||||
my ($vm_snapshot) = @_;
|
||||
|
||||
my $spec = VirtualMachineConfigSpec->new(changeTrackingEnabled => $cbt);
|
||||
$vm_snapshot->ReconfigVM_Task(spec => $spec);
|
||||
|
||||
$vm_snapshot->CreateSnapshot(
|
||||
name => $snapshot_name,
|
||||
description => $snapshot_desc,
|
||||
memory => 0,
|
||||
quiesce => 0
|
||||
);
|
||||
}
|
||||
|
||||
sub execute_ssh_command {
|
||||
my ($command) = @_;
|
||||
my $prompt_end = '/\w+[\$\%\#\>]\s{0,1}$/o';
|
||||
my $ssh = Net::OpenSSH->new($remote_host) or die "Cannot connect to $dst_host via SSH.";
|
||||
log_message "SSH: $remote_host: $command";
|
||||
my $result = $ssh->capture($command);
|
||||
log_message "SSH: Result: $result";
|
||||
$ssh->system('exit');
|
||||
return substr($result, 0, 200);
|
||||
}
|
||||
|
||||
sub set_power_state {
|
||||
my ($vm_power, $state) = @_;
|
||||
|
||||
|
@ -912,23 +812,3 @@ sub set_power_state {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
sub check_datastore {
|
||||
my $host_view = $vm->summary->runtime->host;
|
||||
|
||||
if (defined ($dst_host)) {
|
||||
$host_view = Vim::find_entity_view(
|
||||
view_type => 'HostSystem',
|
||||
filter => {name => $dst_host}
|
||||
);
|
||||
}
|
||||
|
||||
foreach my $moref ( @{ $host_view->datastore } ) {
|
||||
my $ds_view = Vim::get_view (mo_ref => $moref);
|
||||
if ($ds_view->name eq $dst_datastore) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
die "Datastore '$dst_datastore' does not exist.";
|
||||
}
|
||||
|
|
Reference in New Issue