Shutting down VMs on a host in order

Reply

Join Date: Jun 2009
Posts: 1
Reputation: caffine4lovers is an unknown quantity at this point 
Solved Threads: 0
caffine4lovers caffine4lovers is offline Offline
Newbie Poster

Shutting down VMs on a host in order

 
0
  #1
Jun 29th, 2009
Hi I am new to perl programming, and have hit a bit of a snag. I have been editing the ghettoshutdown script for my own devices. What it does so far is shutdown or suspend all virtual machines on a host (using vmware for virtualization), but it does not shut them down in a specific order (which is the end goal). Is there anyway to do this in perl, possibly using an external file. Any help would be greatly appreciated.

The code thus far is shown below

  1. use strict;
  2. use warnings;
  3. use Getopt::Long;
  4. use VMware::VIRuntime;
  5. use VMware::VILib;
  6.  
  7. ####################
  8. # GLOBAL VARIABLES
  9. ####################
  10. my $host_type;
  11. my $host_view;
  12. my $ups_vm_name;
  13. my $content;
  14. my $sleep_duration;
  15. my $final_host_wait = 30;
  16. my $ENABLE_HARD_POWER_OFF = 1;
  17. my $ITER_TO_WAIT_SHUTDOWN = 20;
  18. my $MAX_ITERATION = 25;
  19.  
  20. ############################
  21. # ENABLE_HARD_POWER_OFF 0=no 1=yes
  22. # ITER_TO_WAIT_SHUTDOWN = iteration ( * 3 seconds ) to wait before HARD_POWER_OFF
  23. # MAX_ITERATION = iteration ( * 3 seconds ) to wait for each vm before beginning host shutdown
  24. ############################
  25.  
  26. my %opts = (
  27.  
  28. sleep => {
  29. type => "=i",
  30. help => "The amount of time (secs) to wait after a guestOS shutdown (default 15 secs)",
  31. required => 0,
  32. },
  33. );
  34.  
  35. # validate options, and connect to the server
  36. Opts::add_options(%opts);
  37.  
  38. # validate options, and connect to the server
  39. Opts::parse();
  40. Opts::validate();
  41. Util::connect();
  42.  
  43. if (Opts::option_is_set ('ups_vm') ) {
  44. $ups_vm_name = Opts::get_option('ups_vm');
  45. }
  46.  
  47. if (Opts::option_is_set ('sleep') ) {
  48. $sleep_duration = Opts::get_option('sleep');
  49. } else { $sleep_duration = 15; }
  50.  
  51.  
  52. ############################
  53. # PARSE COMMANDLINE OPTIONS
  54. #############################
  55. $content = Vim::get_service_content();
  56. $host_type = $content->about->apiType;
  57. if($host_type eq 'HostAgent') {
  58. $host_view = Vim::find_entity_views(view_type => 'HostSystem');
  59. if (!$host_view) {
  60. print "ESX/ESXi host was not found\n";
  61. } else {
  62. shutdownVMs($host_view);
  63. }
  64. }
  65.  
  66. Util::disconnect();
  67.  
  68. ### HELPER FUNCTIONS ###
  69.  
  70. sub shutdownVMs {
  71. my $poweroff = "POWEROFF";
  72. my $suspend = "SUSPEND";
  73.  
  74. print "Type SUSPEND to suspend the VMs or POWEROFF to power off the VMs: ";
  75. chomp (my $powerchoice = <>);
  76.  
  77. if($powerchoice eq $suspend) {
  78.  
  79. my ($host) = @_;
  80. my $found_vima;
  81. foreach(@$host) {
  82. print giveMeDate('MDYHMS')," -- Found ESX/ESXi host: ",$_->name,"!\n";
  83. print "\t",giveMeDate('MDYHMS')," -- Begin suspend process ...\n";
  84. my $vm_views = Vim::get_views (mo_ref_array => $_->vm);
  85.  
  86. my $found_vima = 0;
  87. foreach my $vm (@$vm_views) {
  88. my $vm_name = $vm->summary->config->name;
  89. next if(!defined($vm_name));
  90.  
  91. if($vm_name) {
  92. if($vm->runtime->powerState->val eq 'poweredOn') {
  93. my $START_ITERATION = 0;
  94. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," Suspending initiated.\n";
  95. eval { $vm->SuspendVM();
  96. }
  97. }
  98. elsif($vm->runtime->powerState->val eq 'poweredOff') {
  99. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already off.\n";
  100. }
  101. elsif($vm->runtime->powerState->val eq 'suspended') {
  102. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already suspended.\n";
  103. }
  104. } else {
  105. $found_vima = 1;
  106. }
  107. }
  108. }
  109. }
  110.  
  111.  
  112.  
  113.  
  114. elsif($powerchoice eq $poweroff) {
  115. my ($host) = @_;
  116. my $found_vima;
  117. foreach(@$host) {
  118. print giveMeDate('MDYHMS')," -- Found ESX/ESXi host: ",$_->name,"!\n";
  119. print "\t",giveMeDate('MDYHMS')," -- Begin shutdown process ...\n";
  120. my $vm_views = Vim::get_views (mo_ref_array => $_->vm);
  121.  
  122. my $found_vima = 0;
  123. foreach my $vm (@$vm_views) {
  124. my $vm_name = $vm->summary->config->name;
  125. next if(!defined($vm_name));
  126.  
  127. if($vm_name) {
  128. if($vm->runtime->powerState->val eq 'poweredOn') {
  129. my $START_ITERATION = 0;
  130. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," Powering off initiated.\n";
  131. eval { $vm->ShutdownGuest();
  132. }
  133. }
  134. elsif($vm->runtime->powerState->val eq 'poweredOff') {
  135. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already off.\n";
  136. }
  137. elsif($vm->runtime->powerState->val eq 'suspended') {
  138. print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already suspended.\n";
  139. }
  140. } else {
  141. $found_vima = 1;
  142. }
  143. }
  144. }
  145. }
  146. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum


Views: 642 | Replies: 0
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC