941,497 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1068
  • Perl RSS
Jun 29th, 2009
0

Shutting down VMs on a host in order

Expand Post »
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

Perl Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caffine4lovers is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: array problem - help!
Next Thread in Perl Forum Timeline: pearl Newbie





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC