summaryrefslogtreecommitdiffstats
path: root/agents/vmware/fence_vmware_helper.pl
blob: a0b5cea8c41f1b6b886830e09bfe36d8b6760c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/perl

use strict;
use warnings;

my $ME = $0;

END {
  defined fileno STDOUT or return;
  close STDOUT and return;
  warn "$ME: failed to close standard output: $!\n";
  $? ||= 1;
}

my ($RELEASE_VERSION, $REDHAT_COPYRIGHT, $BUILD_DATE);

#BEGIN_VERSION_GENERATION
$RELEASE_VERSION="";
$REDHAT_COPYRIGHT="";
$BUILD_DATE="";
#END_VERSION_GENERATION

#### FUNCTIONS #####
# Show error message
sub show_error {
  print STDERR @_;
}

sub my_exit {
  my ($exit_code)=@_;

  # Disconnect from server
  Util::disconnect();

  exit $exit_code;
}

# Convert one field (string) to format acceptable by DSV. This
# means replace any : with \: and \ with \\.
sub convert_field_to_dsv {
  my ($input_line)=@_;

  $input_line =~ s/([\\:])/\\$1/g;
  return $input_line
}

#### Global variables #####
# Aditional options
my %opts = (
   'operation' => {
      type => "=s",
      help => "The operation to perform (on,off,list,status). "
             . "Operations on/off/status require name of the virtual machine",
      default => "list",
      required => 0,
   },
   'vmname' => {
      type => "=s",
      help => "The name of the virtual machine",
      required => 0,
   },
   'datacenter' => {
      type => "=s",
      help => "The name of the datacenter",
      required => 0,
   }
);

#################
##### MAIN ######
#################

# Conditional use of VIRuntime
eval "use VMware::VIRuntime;";

if ($@) {
  show_error "Please install VI Perl API package to use this tool!\nPerl error: $@";
  exit 1;
}

# Parse options
Opts::add_options(%opts);
Opts::parse();
Opts::validate();

if (!(Opts::get_option('operation')=~/^(on|off|list|status)$/i)) {
  show_error "Operation should be on, off, list or status!\n";
  exit 2;
}

my $operation=lc(Opts::get_option('operation'));

if (($operation ne 'list') && (!defined Opts::get_option('vmname'))) {
  show_error "Operation on, off, status require vmname parameter!\n";
  exit 2;
}


# Try connect to machine
eval {
  Util::connect();
};

if ($@) {
  show_error "Cannot connect to server!\nVMware error:".$@;
  exit 3;
}

my ($datacenter, $datacenter_view, $vm_views,$vm);
# We are connected to machine

# If user want's datacenter, we must first find datacenter
my %filter=(view_type => 'VirtualMachine');

if( defined (Opts::get_option('datacenter')) ) {
  $datacenter = Opts::get_option('datacenter');
  $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter',
                                            filter => { name => $datacenter });
  if (!$datacenter_view) {
    show_error "Cannot find datacenter ".$datacenter."!\n";

    my_exit 4;
  }

  $filter{'begin_entity'}=$datacenter_view;
}

if ($operation ne 'list') {
  $filter{'filter'}= {"config.name" => Opts::get_option('vmname')};
}

$vm_views = Vim::find_entity_views(%filter);

my $found=0;

# Traverse all found vm
foreach $vm(@$vm_views) {
  if (($operation eq 'list') or ($operation eq 'status')) {
    if (!$vm->summary->config->template) {
      print convert_field_to_dsv($vm->name).":".
            convert_field_to_dsv($vm->summary->config->vmPathName).":".
            convert_field_to_dsv($vm->runtime->powerState->val).":".
            convert_field_to_dsv($vm->runtime->connectionState->val)."\n";
    }
  } elsif ($operation eq 'on') {
    eval {
      $vm->PowerOnVM();
    };

    if ($@) {
      # If error is SoapFault with InvalidPowerState, user maybe use some auto power on tool.
      # This is not error, warning is enought.
      if (ref($@) eq 'SoapFault') {
        if (ref($@->detail) eq 'InvalidPowerState') {
          show_error "Warning: Cannot power on vm (somebody done it before???) ".Opts::get_option('vmname').
                     "!\nVMware error:".$@."\n";
        }
      } else {
        # Some other more serious problem
        show_error "Cannot power on vm ".Opts::get_option('vmname')."!\nVMware error:".$@."\n";
        my_exit 6;
      }
    }
  } elsif ($operation eq 'off') {
    eval {
      $vm->PowerOffVM();
    };

    if ($@) {
      # If error is SoapFault with InvalidPowerState, user maybe use some auto power off tool.
      # This is not error, warning is enought.
      if (ref($@) eq 'SoapFault') {
        if (ref($@->detail) eq 'InvalidPowerState') {
          show_error "Warning: Cannot power off vm (somebody done it before???) ".Opts::get_option('vmname').
                     "!\nVMware error:".$@."\n";
        }
      } else {
        # Some other more serious problem
        show_error "Cannot power off vm ".Opts::get_option('vmname')."!\nVMware error:".$@."\n";
        my_exit 6;
      }
    }
  } else {
    show_error "Operation should be on, off or list!\n";
    my_exit 2;
  }
  $found++;
}

if ((!$found) && ($operation ne 'list')) {
  show_error "Cannot find vm ".Opts::get_option('vmname')."!\n";
  my_exit 5;
}

# Should be 0 -> success all, or 6 in case of error
my_exit 0;

__END__

=head1 NAME

fence_vmware_helper - Perform list of virtual machines and
               poweron, poweroff  of operations on virtual machines.

=head1 SYNOPSIS

 fence_vmware_helper --operation <on|off|list|status> [options]

=head1 DESCRIPTION

This VI Perl command-line utility provides an interface for
seven common provisioning operations on one or more virtual
machines: powering on, powering off and listing virtual mode.

=head1 OPTIONS

=head2 GENERAL OPTIONS

=over

=item B<operation>

Operation to be performed.  One of the following:

  <on> (power on one or more virtual machines),
  <off> (power off one  or more virtual machines),
  <list> (list virtual machines and their status)
  <status> (same as list, but show only machines with vmname)

=item B<vmname>

Optional. Name of the virtual machine on which the
operation is to be performed.

=item B<datacenter>

Optional. Name of the  datacenter for the virtual machine(s).
Operations will be performed on all the virtual machines under the given datacenter.

=back

=head1 EXAMPLES

Power on a virtual machine

   fence_vmware_helper --username administrator --password administrator --operation on
                --vmname rhel --server win1

   fence_vmware_helper --username administrator --password administrator --operation on
                --vmname rhel --server win1 --datacenter Datacenter

Power off a virtual machine

   fence_vmware_helper --username administrator --password administrator --operation off
                --vmname rhel --server win1

   perl fence_vmware_helper --username administrator --password administrator --operation off
                --vmname rhel --server win1 --datacenter Datacenter

List of virtual machines

   fence_vmware_helper --username administrator --password administrator --server win1

   fence_vmware_helper --username administrator --password administrator --server win1
                --operation list

Get status of virtual machine

   fence_vmware_helper --username administrator --password administrator --server win1
	    --vmname rhel --operation status

=head1 SUPPORTED PLATFORMS

All operations supported on ESX 3.0.1

All operations supported on Virtual Center 2.0.1