Here is a quick and dirty first pass at a fencing agent for VMware's
Virtual Center using the new VI Perl API. The name fence_vi3 comes from
Virtual Infrastructure 3, VMware's name for the current suite of
enterprise stuff.
This is based on the fence_vixel agent, it seems to work for me. It
allows me to fence a guest in an ESX cluster running HA & DRS where your
guest may move around for various reasons.
Enjoy,
-Andrew
--
Andrew A. Neuschwander, RHCE
Linux Systems Administrator
Numerical Terradynamic Simulation Group
College of Forestry and Conservation
The University of Montana
http://www.ntsg.umt.edu
#!/usr/bin/perl
###############################################################################
###############################################################################
##
## Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
## Copyright (C) 2004 Red Hat, Inc. All rights reserved.
##
## This copyrighted material is made available to anyone wishing to use,
## modify, copy, or redistribute it subject to the terms and conditions
## of the GNU General Public License v.2.
##
###############################################################################
###############################################################################
use Getopt::Std;
use VMware::VIM2Runtime;
# Get the program name from $0 and strip directory names
$_=$0;
s/.*\///;
my $pname = $_;
# WARNING!! Do not add code bewteen "#BEGIN_VERSION_GENERATION" and
# "#END_VERSION_GENERATION" It is generated by the Makefile
#BEGIN_VERSION_GENERATION
$FENCE_RELEASE_NAME="2.0.64";
$REDHAT_COPYRIGHT=("Copyright (C) Red Hat, Inc. 2004 All rights reserved.");
$BUILD_DATE="(built Thu Jun 28 07:25:05 EDT 2007)";
#END_VERSION_GENERATION
sub usage
{
print "Usage:\n\n";
print "$pname [options]\n\n";
print "Options:\n";
print " -a <ip> IP address or hostname of VirtualCenter server\n";
print " -h Usage\n";
print " -n <name> Virtual machine name to reset\n";
print " -l <string> Login name for VirtualCenter
server\n";
print " -p <string> Password for login\n";
print " -V version\n\n";
exit 0;
}
sub fail
{
($msg) = @_;
print $msg."\n" unless defined $opt_q;
$t->close if defined $t;
exit 1;
}
sub fail_usage
{
($msg) = @_;
print STDERR $msg."\n" if $msg;
print STDERR "Please use '-h' for usage.\n";
exit 1;
}
sub version
{
print "$pname $FENCE_RELEASE_NAME $BUILD_DATE\n";
print "$REDHAT_COPYRIGHT\n" if ( $REDHAT_COPYRIGHT );
exit 0;
}
if (@ARGV > 0) {
getopts("a:hn:l:p:V") || fail_usage ;
usage if defined $opt_h;
version if defined $opt_V;
fail_usage "Unknown parameter." if (@ARGV > 0);
fail_usage "No '-a' flag specified." unless defined $opt_a;
fail_usage "No '-l' flag specified." unless defined $opt_l;
fail_usage "No '-p' flag specified." unless defined $opt_p;
fail_usage "No '-n' flag specified." unless defined $opt_n;
} else {
get_options_stdin();
fail "failed: no IP/host address for the VirtualCenter." unless defined
$opt_a;
fail "failed: no login name provided." unless defined $opt_l;
fail "failed: no password provided." unless defined $opt_p;
fail "failed: no VM name specified." unless defined $opt_n;
}
#
# Set up and log in
#
Vim::login(service_url=>"https://$opt_a/sdk/vimService", user_name=>"$opt_l",
password=>"$opt_p");
#
# Do the command
#
my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter =>
{name => $opt_n});
if (!$vm_view)
{
fail "Virtual Machine name not found.";
}
$vm_view->ResetVM();
Vim::logout();
print "success: VM: $opt_n reset.\n";
exit 0;
sub get_options_stdin
{
my $opt;
my $line = 0;
while( defined($in = <>) )
{
$_ = $in;
chomp;
# strip leading and trailing whitespace
s/^\s*//;
s/\s*$//;
# skip comments
next if /^#/;
$line+=1;
$opt=$_;
next unless $opt;
($name,$val)=split /\s*=\s*/, $opt;
if ( $name eq "" ) {
print("parse error: illegal name in option $line\n");
exit 2;
}
# DO NOTHING -- this field is used by fenced
elsif ($name eq "agent" ) { }
elsif ($name eq "login" ) {
$opt_l = $val;
}
elsif ($name eq "ipaddr" )
{
$opt_a = $val;
}
elsif ($name eq "name" ) { }
elsif ($name eq "passwd" )
{
$opt_p = $val;
}
elsif ($name eq "port" )
{
$opt_n = $val;
}
}
}