Advertisement
Guest User

Carter Shanklin

a guest
Jul 7th, 2009
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.95 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # Mostly based on code from William Lam
  3. # http://engineering.ucsb.edu/~duonglt/vmware
  4.  
  5. use strict;
  6. use warnings;
  7.  
  8. use VMware::VIRuntime;
  9.  
  10. my %opts = (
  11.     operation => {
  12.     type => "=s",
  13.         help => "Operation to perform [poweroff|suspend]",
  14.         required => 1,
  15.         },
  16.     hostlist => {
  17.     type => "=s",
  18.     help => "Path to file containing list of hosts, one per line, for ordered shutdown.",
  19.     required => 1,
  20.     },
  21. );
  22.  
  23. Opts::add_options(%opts);
  24. Opts::parse();
  25. Opts::validate();
  26.  
  27. Util::connect();
  28.  
  29. my ($host, $vm_name, $vm_view, $host_view, $hostlist, $operation, $task_ref);
  30.  
  31. $operation = Opts::get_option("operation");
  32. $hostlist = Opts::get_option("hostlist");
  33.  
  34. open(HOSTLIST, $hostlist) or die "Failed to open file, '$hostlist'";
  35. my @host_list = <HOSTLIST>;
  36.  
  37. foreach $host( @host_list ) {
  38.     chomp($host);
  39.  
  40.     $host_view = Vim::find_entity_view(
  41.         view_type => "HostSystem",
  42.         filter    => { 'name' => $host }
  43.     );
  44.  
  45.     # Find all VMs for this host.
  46.     my @vms = map { Vim::get_view(mo_ref => $_) } @{$host_view->vm};
  47.  
  48.     # Perform some action on all these VMs.
  49.     foreach $vm_view (@vms) {
  50.         if($operation eq 'suspend') {
  51.             print "Trying to suspend " . $vm_view->name . "\n";
  52.             eval {
  53.                     $task_ref = $vm_view->SuspendVM_Task();
  54.                 my $msg = "\tSuccessfully suspended " . $vm_name . "\n";
  55.                 &getStatus($task_ref,$msg);
  56.                 };
  57.             if($@) { print "Error: " . $@ . "\n"; }
  58.         } elsif($operation eq 'poweroff') {
  59.             print "Trying to poweroff " . $vm_view->name . "\n";
  60.             eval {
  61.                 $task_ref = $vm_view->PowerOffVM_Task();
  62.                             my $msg = "\tSuccessfully poweredoff " . $vm_name . "\n";
  63.                 &getStatus($task_ref,$msg);
  64.                     };
  65.             if($@) { print "Error: " . $@ . "\n"; }
  66.         } else {
  67.             die "Invalid operation!\n";
  68.         }
  69.     }
  70.  
  71.     # Now power the host off.
  72.     eval {
  73.             #$task_ref = $host_view->ShutdownHost_Task();
  74.         #my $msg = "\tSuccessfully shut down " . $host_view->name . "\n";
  75.         #&getStatus($task_ref,$msg);
  76.         };
  77.     if($@) { print "Error: " . $@ . "\n"; }
  78. }
  79.  
  80. Util::disconnect();
  81.  
  82. sub getStatus {
  83.         my ($taskRef,$message) = @_;
  84.  
  85.         my $task_view = Vim::get_view(mo_ref => $taskRef);
  86.         my $taskinfo = $task_view->info->state->val;
  87.         my $continue = 1;
  88.         while ($continue) {
  89.                 my $info = $task_view->info;
  90.                 if ($info->state->val eq 'success') {
  91.                         print $message;
  92.                         $continue = 0;
  93.                 } elsif ($info->state->val eq 'error') {
  94.                         my $soap_fault = SoapFault->new;
  95.                         $soap_fault->name($info->error->fault);
  96.                         $soap_fault->detail($info->error->fault);
  97.                         $soap_fault->fault_string($info->error->localizedMessage);
  98.                         die "$soap_fault\n";
  99.                 }
  100.                 sleep 5;
  101.                 $task_view->ViewBase::update_view_data();
  102.         }
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement