Refactor hostportgroup management code in esx plugin
Have esxi support --addvlan parameter
This commit is contained in:
		@@ -2087,11 +2087,16 @@ sub chhypervisor {
 | 
			
		||||
    my $maintenance;
 | 
			
		||||
	my $online;
 | 
			
		||||
    my $stat;
 | 
			
		||||
    my $vlanaddspec;
 | 
			
		||||
    my $vlanremspec;
 | 
			
		||||
    require Getopt::Long;
 | 
			
		||||
    GetOptions(
 | 
			
		||||
        'maintenance|m' => \$maintenance,
 | 
			
		||||
        'online|o' => \$online,
 | 
			
		||||
        'show|s' => \$stat,
 | 
			
		||||
        'show|s' => \$stat,
 | 
			
		||||
        'addvlan=s' => \$vlanaddspec,
 | 
			
		||||
        'removevlan=s' => \$vlanremspec,
 | 
			
		||||
        );
 | 
			
		||||
    my $hyp = $args{hyp};
 | 
			
		||||
    $hyphash{$hyp}->{hostview} = get_hostview(hypname=>$hyp,conn=>$hyphash{$hyp}->{conn}); #,properties=>['config','configManager']); 
 | 
			
		||||
@@ -2119,8 +2124,9 @@ sub chhypervisor {
 | 
			
		||||
                        xCAT::SvrUtils::sendmsg("hypervisor online", $output_handler,$hyp);
 | 
			
		||||
                }
 | 
			
		||||
        }
 | 
			
		||||
    } elsif ($vlanaddspec) {
 | 
			
		||||
        fixup_hostportgroup($vlanaddspec, $hyp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
  
 | 
			
		||||
@@ -3736,6 +3742,62 @@ sub scan_cluster_networks {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub fixup_hostportgroup {
 | 
			
		||||
    my $vlanspec = shift;
 | 
			
		||||
    my $hyp = shift;
 | 
			
		||||
    my %args = @_;
 | 
			
		||||
    my $hostview = $hyphash{$hyp}->{hostview};
 | 
			
		||||
    my $hypconn = $hyphash{$hyp}->{conn}; #this function can't work in clustered mode anyway, so this is appropriote.
 | 
			
		||||
    my $vldata = $vlanspec;
 | 
			
		||||
 | 
			
		||||
    my $switchname = get_default_switch_for_hypervisor($hyp);
 | 
			
		||||
    my $pgname;
 | 
			
		||||
    $vldata =~ s/=.*//; #TODO specify nic model with <blah>=model
 | 
			
		||||
    if ($vldata =~ /:/) { #The config specifies a particular path in some way
 | 
			
		||||
        $vldata =~ s/(.*)://;
 | 
			
		||||
        $switchname = get_switchname_for_portdesc($hyp,$1);
 | 
			
		||||
        $pgname=$switchname."-".$vldata;
 | 
			
		||||
    } else { #Use the default vswitch per table config to connect this through, use the same name we did before to maintain compatibility
 | 
			
		||||
        $pgname=$vldata;
 | 
			
		||||
    }
 | 
			
		||||
    my $netsys;
 | 
			
		||||
    $hyphash{$hyp}->{pgnames}->{$vlanspec}=$pgname;
 | 
			
		||||
    my $policy = HostNetworkPolicy->new();
 | 
			
		||||
    unless ($hyphash{$hyp}->{nets}->{$pgname}) {
 | 
			
		||||
        my $vlanid;
 | 
			
		||||
        if ($vldata =~ /trunk/) {
 | 
			
		||||
            $vlanid=4095;
 | 
			
		||||
        } elsif ($vldata =~ /vl(an)?(\d+)$/) {
 | 
			
		||||
            $vlanid=$2;
 | 
			
		||||
        } else {
 | 
			
		||||
            $vlanid = 0;
 | 
			
		||||
        }
 | 
			
		||||
        my $hostgroupdef = HostPortGroupSpec->new(
 | 
			
		||||
            name =>$pgname,
 | 
			
		||||
            vlanId=>$vlanid,
 | 
			
		||||
            policy=>$policy,
 | 
			
		||||
            vswitchName=>$switchname
 | 
			
		||||
            );
 | 
			
		||||
        unless ($netsys) {
 | 
			
		||||
            $netsys = $hyphash{$hyp}->{conn}->get_view(mo_ref=>$hostview->configManager->networkSystem);
 | 
			
		||||
        }
 | 
			
		||||
        $netsys->AddPortGroup(portgrp=>$hostgroupdef);
 | 
			
		||||
        #$hyphash{$hyp}->{nets}->{$netname}=1;
 | 
			
		||||
        while ((not defined $hyphash{$hyp}->{nets}->{$pgname}) and sleep 1) { #we will only sleep if we know something will be waiting for
 | 
			
		||||
            $hostview->update_view_data(); #pull in changes induced by previous activity
 | 
			
		||||
            if (defined $hostview->{network}) { #We load the new object references
 | 
			
		||||
                foreach (@{$hostview->network}) {
 | 
			
		||||
                    my $nvw = $hypconn->get_view(mo_ref=>$_);
 | 
			
		||||
                    if (defined $nvw->name) {
 | 
			
		||||
                        $hyphash{$hyp}->{nets}->{$nvw->name}=$_;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub validate_network_prereqs {
 | 
			
		||||
    my $nodes = shift;
 | 
			
		||||
    my $hyp  = shift;
 | 
			
		||||
@@ -3761,53 +3823,7 @@ sub validate_network_prereqs {
 | 
			
		||||
    foreach $node (@$nodes) {
 | 
			
		||||
        my @networks = split /,/,$tablecfg{vm}->{$node}->[0]->{nics};
 | 
			
		||||
        foreach (@networks) {
 | 
			
		||||
            my $switchname = get_default_switch_for_hypervisor($hyp); 
 | 
			
		||||
            my $tabval=$_;
 | 
			
		||||
            my $pgname;
 | 
			
		||||
            s/=.*//; #TODO specify nic model with <blah>=model
 | 
			
		||||
            if (/:/) { #The config specifies a particular path in some way
 | 
			
		||||
                s/(.*)://;
 | 
			
		||||
                $switchname = get_switchname_for_portdesc($hyp,$1);
 | 
			
		||||
                $pgname=$switchname."-".$_;
 | 
			
		||||
            } else { #Use the default vswitch per table config to connect this through, use the same name we did before to maintain compatibility
 | 
			
		||||
                $pgname=$_;
 | 
			
		||||
            }
 | 
			
		||||
            my $netname = $_;
 | 
			
		||||
            my $netsys;
 | 
			
		||||
            $hyphash{$hyp}->{pgnames}->{$tabval}=$pgname;
 | 
			
		||||
            my $policy = HostNetworkPolicy->new();
 | 
			
		||||
            unless ($hyphash{$hyp}->{nets}->{$pgname}) {
 | 
			
		||||
                my $vlanid;
 | 
			
		||||
                if ($netname =~ /trunk/) {
 | 
			
		||||
                    $vlanid=4095;
 | 
			
		||||
                } elsif ($netname =~ /vl(an)?(\d+)$/) {
 | 
			
		||||
                    $vlanid=$2;
 | 
			
		||||
                } else {
 | 
			
		||||
                    $vlanid = 0;
 | 
			
		||||
                }
 | 
			
		||||
                my $hostgroupdef = HostPortGroupSpec->new(
 | 
			
		||||
                    name =>$pgname,
 | 
			
		||||
                    vlanId=>$vlanid,
 | 
			
		||||
                    policy=>$policy,
 | 
			
		||||
                    vswitchName=>$switchname
 | 
			
		||||
                    );
 | 
			
		||||
                unless ($netsys) {
 | 
			
		||||
                    $netsys = $hyphash{$hyp}->{conn}->get_view(mo_ref=>$hostview->configManager->networkSystem);
 | 
			
		||||
                }
 | 
			
		||||
                $netsys->AddPortGroup(portgrp=>$hostgroupdef);
 | 
			
		||||
                #$hyphash{$hyp}->{nets}->{$netname}=1;
 | 
			
		||||
                while ((not defined $hyphash{$hyp}->{nets}->{$pgname}) and sleep 1) { #we will only sleep if we know something will be waiting for
 | 
			
		||||
                    $hostview->update_view_data(); #pull in changes induced by previous activity
 | 
			
		||||
                    if (defined $hostview->{network}) { #We load the new object references
 | 
			
		||||
                        foreach (@{$hostview->network}) {
 | 
			
		||||
                            my $nvw = $hypconn->get_view(mo_ref=>$_);
 | 
			
		||||
                            if (defined $nvw->name) {
 | 
			
		||||
                                $hyphash{$hyp}->{nets}->{$nvw->name}=$_;
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                } #end while loop
 | 
			
		||||
            }
 | 
			
		||||
            fixup_hostportgroup($_, $hyp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return 1;
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,7 @@ sub handled_commands {
 | 
			
		||||
    return {
 | 
			
		||||
        mkstorage => "storage:type",
 | 
			
		||||
        rmstorage => "storage:type",
 | 
			
		||||
        lspool => "storage:type",
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -86,6 +87,25 @@ sub mkstorage {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub hashifyoutput {
 | 
			
		||||
    my @svcoutput = @_;
 | 
			
		||||
    my $hdr = shift @svcoutput;
 | 
			
		||||
    my @columns = split /:/, $hdr;
 | 
			
		||||
    my @ret;
 | 
			
		||||
    foreach my $line (@svcoutput) {
 | 
			
		||||
        my $index = 0;
 | 
			
		||||
        my %record = ();
 | 
			
		||||
        my $keyname;
 | 
			
		||||
        foreach my $datum (split /:/, $line) {
 | 
			
		||||
            $keyname = $columns[$index];
 | 
			
		||||
            $record{$keyname} = $datum;
 | 
			
		||||
            $index += 1;
 | 
			
		||||
        }
 | 
			
		||||
        push @ret,\%record;
 | 
			
		||||
    }
 | 
			
		||||
    pop @ret; # discard data from prompt
 | 
			
		||||
    return @ret;
 | 
			
		||||
}
 | 
			
		||||
sub bindhosts {
 | 
			
		||||
    my $nodes = shift;
 | 
			
		||||
    my $lun = shift;
 | 
			
		||||
@@ -283,10 +303,24 @@ sub process_request {
 | 
			
		||||
    $dorequest = shift;
 | 
			
		||||
    if ($request->{command}->[0] eq 'mkstorage') {
 | 
			
		||||
        mkstorage($request);
 | 
			
		||||
    } elsif ($request->{command}->[0] eq 'lspool') {
 | 
			
		||||
        lsmdiskgrp($request);
 | 
			
		||||
    }
 | 
			
		||||
    foreach (values %controllersessions) {
 | 
			
		||||
        $_->close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sub lsmdiskgrp {
 | 
			
		||||
    my $req = shift;
 | 
			
		||||
    foreach my $node (@{$req->{node}}) {
 | 
			
		||||
        my $session = establish_session(controller=>$node);
 | 
			
		||||
        my @pools = hashifyoutput($session->cmd("lsmdiskgrp -delim :"));
 | 
			
		||||
        foreach my $pool (@pools) {
 | 
			
		||||
            sendmsg($pool->{name}. " available capacity: ".$pool->{free_capacity},$callback,$node);
 | 
			
		||||
            sendmsg($pool->{name}. " total capacity: ".$pool->{capacity},$callback,$node);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
1;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user