diff --git a/config.pl b/config.pl index 26bbe5d..68ead19 100644 --- a/config.pl +++ b/config.pl @@ -4,15 +4,6 @@ # The log file path log_file => '/var/log/vn-vmware.log', -# Hostname or IP address of vCenter host -hostname => 'vcenter', - -# The user used to connect to vCenter -user => 'root', - -# Credentials file where user password is stored -credentials_file => '/root/.vmware/credstore/vicredentials.xml', - # The datacenter name datacenter => 'datacenter1', diff --git a/debian/changelog b/debian/changelog index f847020..d7b7436 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -vn-vmware (1.0.25) stable; urgency=low +vn-vmware (1.0.26) stable; urgency=low * Initial Release. diff --git a/vn-vmware.pl b/vn-vmware.pl index bd57e1f..4ee285e 100755 --- a/vn-vmware.pl +++ b/vn-vmware.pl @@ -19,6 +19,39 @@ use List::Util qw(min max); use constant false => 0; use constant true => 1; +# Perl config file + +my %config; +my @config_files = ( + '/etc/vn-vmware/config.my.pl', + '/etc/vn-vmware/config.pl', + 'config.my.pl', + 'config.pl' +); + +foreach my $config_file (@config_files) { + if (-e $config_file) { + %config = do $config_file; + last; + } +} +unless (%config) { + die "Configuration file not found."; +} + +# Vim configuration file + +my @vi_config_files = ( + '/etc/vn-vmware/visdkrc', + 'visdkrc' +); + +foreach my $vi_config_file (@vi_config_files) { + if (-e $vi_config_file) { + Opts::set_option('config', $vi_config_file); + } +} + my %opts = ( 'operation' => { type => "=s", @@ -99,11 +132,6 @@ my %opts = ( help => "Whether to power on machine after operation", default => 1 }, - 'show-logs' => { - type => "", - help => "Whether to redirect logs to stdout", - default => 0 - }, 'snapshot-name' => { type => "=s", help => "Name of the snapshot", @@ -118,9 +146,9 @@ my %opts = ( Opts::add_options(%opts); Opts::parse(); -# FIXME: When enabled, asks for user and password -#Opts::validate(); +Opts::validate(); +my $server = Opts::get_option('server'); my $operation = Opts::get_option('operation'); my $job = Opts::get_option('job'); my $vm_name = Opts::get_option('vm-name'); @@ -138,27 +166,9 @@ my $mem_reservation = Opts::get_option('mem-reservation'); my $cpu_reservation = Opts::get_option('cpu-reservation'); my $overwrite = Opts::option_is_set('overwrite'); my $poweron = Opts::option_is_set('poweron'); -my $show_logs = Opts::option_is_set('show-logs'); my $snapshot_name = Opts::get_option('snapshot-name'); my $snapshot_desc = Opts::get_option('snapshot-desc'); - -my %config; -my @config_files = ( - '/etc/vn-vmware/config.my.pl', - '/etc/vn-vmware/config.pl', - 'config.my.pl', - 'config.pl' -); - -foreach my $config_file (@config_files) { - if (-e $config_file) { - %config = do $config_file; - last; - } -} -unless (%config) { - die "Configuration file not found."; -} +my $verbose = Opts::get_option('verbose'); my $vm; my $remote_host; @@ -176,10 +186,10 @@ sub log_to_file { sub log_message { my ($message) = @_; + Util::trace(1, "$message\n"); + if ($log_fh) { log_to_file "LOG: $message\n"; - } else { - print "$message\n" } } sub log_error { @@ -202,7 +212,7 @@ sub log_error { } } -unless ($show_logs) { +if (exists $config{log_file}) { open($log_fh, '>>', $config{log_file}); $log_fh->autoflush(1); } @@ -219,29 +229,8 @@ if ($log_fh) { } sub main { - my $vcenter_host = $config{hostname}; - my $username = $config{user}; - - VMware::VICredStore::init(filename => $config{credentials_file}); - my $password = VMware::VICredStore::get_password(server => $vcenter_host, username => $username); - my $url = "https://$vcenter_host/sdk/vimService"; - - unless (defined ($password)) { - die "Password not found $vcenter_host."; - } - - eval { - Vim::login( - service_url => $url, - user_name => $username, - password => $password - ); - }; - if ($@) { - die "Cannot connect to $vcenter_host."; - } - - log_message "Connected to $vcenter_host."; + Util::connect(); + log_message "Connected to $server."; my $about = Vim::get_service_content()->about; log_message "Version: $about->{apiType} $about->{version}"; @@ -277,7 +266,7 @@ sub main { migrate_machine(); } when ('test') { - log_message "Test operation, doing nothing."; + test_operation(); } default { die "Unknown operation '$operation'."; @@ -286,7 +275,7 @@ sub main { }; my $err = $@; - Vim::logout(); + Util::disconnect(); if ($err) { die $err; @@ -781,6 +770,13 @@ sub migrate_machine { log_message "Migration of '$vm_name' successfull."; } +sub test_operation() { + if ($vm_name) { + open_machine(); + } + log_message "Test operation, doing nothing."; +} + #--------------------------------- Utils sub open_machine() {