Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # vnicmove.pl
- use strict;
- use warnings;
- use LWP;
- use LWP::Simple;
- use Data::Dumper qw(Dumper);
- use lib "/usr/lib/vmware-vcli/apps/";
- use VMware::VIRuntime;
- use AppUtil::HostUtil;
- use lib "JSON-2.90/lib/";
- use JSON;
- my $DEBUG = 1;
- # Opts::parse() appears to break $0
- my $scriptname = $0;
- # Function prototypes
- sub move_vnic ($$);
- # Monkey see, monkey do
- $Util::script_version = "1.0";
- # Read only a task ID from the commandline, and the rest from imgmgr
- my %opts = (
- name => {
- type => "=s",
- help => "Name of the VM to move.",
- required => 1,
- },
- vlan => {
- type => "=i",
- help => "VLAN ID. (e.g. 300)",
- required => 1,
- default => "321",
- },
- );
- Opts::add_options(%opts);
- Opts::parse();
- if ((!defined $opts{'name'}->{'value'}) || (!defined $opts{'vlan'}->{'value'})) {
- print "Usage: $scriptname -name [VM name] -vlan [Numeric VLAN ID]\n";
- exit 1;
- }
- if ($DEBUG) { print Dumper(\%opts); }
- Util::connect();
- move_vnic($opts{'name'}->{'value'}, $opts{'vlan'}->{'value'});
- # Don't disconnect because it deletes our cached credentials
- #Util::disconnect();
- sub move_vnic ($$) {
- my $name = shift;
- my $vlan = shift;
- # Find the dvs portgroup from the VLAN id
- my $my_dvs_pg_view = '';
- my $dvs_pg_views = Vim::find_entity_views (
- view_type => 'DistributedVirtualPortgroup'
- );
- unless (@$dvs_pg_views) {
- Util::trace(0, "\nError updating VM '$name': "
- . "No DVS port groups could be found!\n");
- return;
- }
- foreach my $dvs_pg_view (@$dvs_pg_views) {
- if ($dvs_pg_view->{'config'}->{'defaultPortConfig'}->{'vlan'}->{'vlanId'} == $vlan) {
- if ($my_dvs_pg_view ne '') {
- Util::trace(0, "\nError updating VM '$name': "
- . "VLAN ID $vlan is not unique!\n");
- return;
- }
- $my_dvs_pg_view = $dvs_pg_view;
- }
- }
- if ($my_dvs_pg_view eq '') {
- Util::trace(0, "\nError updating VM '$name': "
- . "VLAN ID $vlan could not be found!\n");
- return;
- }
- if ($DEBUG) { print $my_dvs_pg_view->{'name'} . " ($vlan) => " . $my_dvs_pg_view->{'key'} . "\n"; }
- my $dvs_view = Vim::get_view(mo_ref => $my_dvs_pg_view->{'config'}->{'distributedVirtualSwitch'});
- print "PortGroup is on switch ". $dvs_view->{'summary'}->{'name'} . " with UUID ". $dvs_view->{'uuid'} . "\n";
- # Find the VM by name
- my $vm;
- $vm = Vim::find_entity_view(
- view_type => 'VirtualMachine',
- filter => {'name' => $name}
- );
- if (!$vm) {
- Util::trace(0, "FATAL: Could not find VM named '$name'!\n");
- return;
- } else {
- if ($DEBUG) {
- print "====== Found VM: ";
- print Dumper($vm);
- }
- }
- # Get a handle on the first vNIC on the VM
- my $nic = '';
- my $numdevices = scalar @{$vm->{'config'}->{'hardware'}->{'device'}};
- my $i = 0;
- while (($nic eq '') && ($i < $numdevices)) {
- my $device = $vm->{'config'}->{'hardware'}->{'device'}->[$i];
- if ($device->isa("VirtualEthernetCard")) {
- if ($DEBUG) {
- print "====== Found NIC: ";
- print Dumper($device);
- }
- $nic = $device;
- }
- $i++;
- }
- my $my_dvs_pg_conn = DistributedVirtualSwitchPortConnection->new(
- portgroupKey => $my_dvs_pg_view->{'key'},
- switchUuid => $dvs_view->{'uuid'},
- );
- my $nic_backing = VirtualEthernetCardDistributedVirtualPortBackingInfo->new( port => $my_dvs_pg_conn );
- $nic->{'backing'} = $nic_backing;
- if ($DEBUG) {
- print "################ Data summary\n";
- print "## my_dvs_pg_conn: ";
- print Dumper($my_dvs_pg_conn);
- print "\n";
- print "## nic_backing: ";
- print Dumper($nic_backing);
- print "\n";
- print "## nic: ";
- print Dumper($nic);
- print "\n";
- }
- my $device_spec = VirtualDeviceConfigSpec->new(device => $nic);
- # $device_spec isa VirtualDeviceConfigSpec data object
- # $device_spec.device isa VirtualDevice
- # VirtualEthernetCard is a subclass of VirtualDevice
- # $device_spec.device.backing isa VirtualDeviceBackingInfo
- # $device_spec.device.key (required) isa xsd:int
- #
- if ($DEBUG) {
- print "## device_spec: ";
- print Dumper($device_spec);
- print "\n";
- }
- my @device_spec_array = ();
- if ($device_spec) {
- @device_spec_array = ($device_spec);
- } else {
- Util::trace(0, "\nFATAL: Failed to attach to $my_dvs_pg_view->{'name'}\n");
- return;
- }
- my $vm_reconfig_spec = VirtualMachineConfigSpec->new(deviceChange => \@device_spec_array);
- if ($DEBUG) {
- print "## vm_reconfig_spec: ";
- print Dumper($vm_reconfig_spec);
- print "\n";
- print "################ END\n";
- }
- eval {
- $vm->ReconfigVM( spec => $vm_reconfig_spec );
- Util::trace(0,"\nVirtual machine '" . $vm->name
- . "' is reconfigured successfully.\n");
- };
- if ($@) {
- Util::trace(0, "\nReconfiguration failed: ");
- if (ref($@) eq 'SoapFault') {
- if (ref($@->detail) eq 'TooManyDevices') {
- Util::trace(0, "\nNumber of virtual devices exceeds "
- . "the maximum for a given controller.\n");
- }
- elsif (ref($@->detail) eq 'InvalidDeviceSpec') {
- Util::trace(0, "The Device configuration is not valid\n");
- Util::trace(0, "\nFollowing is the detailed error: \n\n$@");
- }
- elsif (ref($@->detail) eq 'FileAlreadyExists') {
- Util::trace(0, "\nOperation failed because file already exists");
- }
- else {
- Util::trace(0, "\n" . $@ . "\n");
- }
- }
- else {
- Util::trace(0, "\n" . $@ . "\n");
- }
- } #if ($@) // VM reconfiguration failed
- if ($DEBUG) {
- print " ## Final VM: \n";
- print Dumper($vm);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement