2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-22 11:42:05 +00:00
Bin Xu eea661e405 merge from master to 2.13 branch for 2.13.9 release. (1) (#4525)
* fix issueNode range not specified, see man page for syntax. return with no output when site.master is not set #4299

* Fix issue 4246, record rflash process in log file

* dodiscovery: better disksize - ordered by major,minor and no `\n` (#4219)

* better disksize: ordered by major,minor and no `\n`

* nodediscover:`disksize` sent correctly: no need to `split`,`join`

* general sort with explicit key columns, fix search pattern

* dodiscovery: Fix bugs

* dodiscovery: Fix ShellCheck SC2007,SC2004

* dodiscovery: simpler kcmdline parsing, disksize as IEC binary prefix

* Add test case - switch_to_dns_forward_mode

* Modify timeout of login by curl command for OpenBMC

* Add makegocons command

This patch enable goconserver service and manage the node sessions
with a new `makegocons` command.

Implement: #4215

* Refine the Cumulus Linux Section of the Doc (#4249)

* No content in the switches subdirectory in docs, remove

* Change the Network topic to Networking

* Refine and reformat the Cumulus Linux documentation

* Modify the script to build xCAT-genesis-base package (#4292)

* Verify hash ID

* change log messages

* Timeout the ntpd process if ntpd service is not running on ntpserver (#4321)

* enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP (#3995)

* enhance nodediscovery process, if only 1 mac and have \*NOIP\* append, don't write mac table and don't generate dhcp lease entry

* Enhance PR 3995: enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP

* modified depending on comments

* natural_sort_cmp: recursion to iterative implementation (#4314)

* natural_sort_cmp: recursion to iterative implementation

* natural_sort_cmp: correct prototype with parameters, as per `man perlfunc`

* Task 3339, rspconfig ip/netmask/gateway/vlan support

*   * Minor enhance on xcatperftest to put all logs into one file
  * Fix a bug in simulatorctl.sh, and it cause the script cannot be found

* modified depending on comments

* QA list for makehosts (#4284)

* QA list for makehosts

* Fix some bus in makegocons

* Revise test case - switch_to_dns_forward_mode

* Enhancements after the review

* Use IO::Socket to check BMC console port

* Revise test case - switch_to_dns_forward_mode

* modified depending on comments

* Add messages to inform the user of the general action started via xCAT for flashing firmware

* Fix bug, anything in the functional array is the one that's really active, priority will not be 0 if there is pending firmware

* Fix 4338, remove all of  in child process

* fix issue #4354 :The XCATTEST_CN in xcattest can not detect HCP as config file (#4355)

* Fix #4330, close the socket

* Add image name to /opt/xcat/xcatinfo on compute node (#4359)

* Integrate congo console from goconserver with rcons

Enhance the original implement of rcons to support goconserver.
`rcons` will help the user choose from one of the console backend
service based on the console server status of management node.

Implement #4216

* Fix 4363 - discovery broken (#4364)

* do not set exit_code to 1 when the clock is not synced before the timeout (#4362)

* Fix check for MTM values with spaces

* modified depending on comments

* rspconfig admin_password for OpenBMC

* Fix merging in xdcpmerge.sh (#4328)

* Fixes in xdcpmerge.sh

Two fixes:
1. The grep pattern when finding duplicate usernames is missing ":" at the end. So, for example user "test" would also match "test2, etc.". Adding the ":" delimiter fixes the issue.
2. Another issue happens when the file to be merged is a superset of the files on the nodes. For example, if a new user is added and entire passwd file (that is otherwise identical) is sent to be merged. In this case, the $filebackup.nodups file, i.e. the original file with duplicates removed, becomes empty and the condition "if [ -s "$filebackup.nodups" ]" does not execute. Then the merged file ends up being original file with the merge file fully appended, clearly not what was intended.

This is solved by changing the condition to check for file existence "-a" rather then for size. Additionally, I also turn the logic around so that the duplicates are removed from the merge file and then added to the original file. I think this makes logic a bit cleaner and also ensures that existing entries are not reordered or changed in any way.

* Streamlining previous commit

Adjustment to previous commit, streamlining and simplifying logic. Once $mergefile.nodups is created, just concatenate it the original file.

* Update to xdcpmerge

No need to copy $filebackup to $curfile, they are the same.

* Modify for debug conveniently

* add new cases and delete outdated test cases

* Modify genesis build script for centos x86_64

* Add space between at and the time

* Add a print out of the firmware levels for the various UT cases

* Add unit test cases for rspconfig

* rspconfig fix for set hostname

* Enhance the testing case for rspconfig setting hostname

* modified depending on comments

* modified depending on comments

* Add %pretrans script in <lua>. Handle directory to symlink change properly. See comment #3 of https://bugs.launchpad.net/rpm/+bug/633636

* Make xCAT-genesis-base confliects with early version of xCAT-genesis-scripts

* rspconfig dump to allow admins capture logs

* Adding comment

* Improve the error message when BMC does not return a dump ID

* Improve some messages and add timestamp for downloaded dump file

* Improve the message to help Admin figure out where the file is missing

* Leave a log file there when xCAT upgrade in case to debug issue while upgrading (#4389)

* Listen on 0.0.0.0 instead of the hostname

This patch modify the configuration of `makegocons` and `rcons`
for goconserver.

`cat /etc/goconserver/server.conf`
```
global:
  host: 0.0.0.0
  ssl_key_file: /etc/xcat/cert/server-key.pem
  ssl_cert_file: /etc/xcat/cert/server-cert.pem
  ssl_ca_cert_file: /etc/xcat/cert/ca.pem
  logfile: /var/log/goconserver/server.log
api:
  port: 12429
console:
  port: 12430
```

* Support hostname=* for openbmc

* Relay action and snmp configuration support for Coral PDU

* ddns.pm: specify the "directory" option for DNS slaves too (cf. bug #4392)

* Fix issue 4361, modify some sendmsg to message

* 1. add "makeconserver -d" to "rmdef -C", 2. add "makeconserver -C|--cleanup" to remove entries for removed nodes

* When there is a problem with the login, do not hide the message on debug mode. BMCReady does not make sense if the admin does not know how to find that state

* Change function from login_logout_request to login_request, not doing any logout here

* Check that RC is 200 to prevent unknown issues, handle the response generically

* OpenBMC rspconfig dump timeout fixes

* Fix issue 4408, modify error for rspconfig dump

* Clear all BMC Dump logs when BMC firmware flash

* modified depending on comments

* More modifications for pr 4386, to deal with the conflicts

* To handle one case which have 2 implementations, which one is for specific platform, on is for all platforms

* return when current status is RSPCONFIG_DUMP_DOWNLOAD_REQUEST

* fix issue 4417, delete 'clear next_status'

* fix issue 4353: rspconfig needs to support multiple IPs on the BMC and ignore ZeroConfigIPs

* Wait 15 seconds after OpenBMC interface with vlan tag to be activated

* Fix issue #4397: rspconfig <> hostname=xxx show error message when there is multiple network in bmc

* Some sentence modify for makeconservercf -C|--cleanup

* OpenBMC rspconfig dump better dump file name formatting

* Removed the --check and --ipsource option with PR 4258, update the man page

* Improve the message on the HTTP response

* modified depending on comments

* Only handle 404 and 504 in the login request code, defer the rest to deal_with_response()

* rflash stream support

* 1. configure ip/netmask/gateway only on the NIC whose IP match node BMC attribute, 2. add some information for LinkLocal address

* Fix confignetwork bond nic_type detection with multiple bonds

* Modified configonie --ntp command (#4436)

* Add man page for makegocons

This is the guide about how to make goconserver as
a replacement for conserver to help slove the issues reported
for conserver, like: #4043, #3543. For openbmc, the solution of
goconserver is much light-weighted than the conserver which could
help save the system resource. In addition, sshpass is not needed
for openbmc with goconserver.

Implement: #4337

* Add another key for node_info in order not to after the content of $node_info{$node}{bmc}

* enhance rflash stream

* makedhcp does not work well when all service nodes not running dhcp but disjointdhcps=1 (#4426) (#4440)

- if all service nodes not running dhcp, to treat it as disjointdhcps=0
- nodeset will send request to MN by default even if disjointdhcps=1
- Move out of the dhcp service checking from opts pre-check, and do it just before real makedhcp handling.

* rspconfig configure bmc vlan will hung because of PR 4383

* OpenBMC rspconfig dump enhancements

* Changes due to review comments

* Print debug message before login attempt

* Add warning when xCAT throttles SSL connections

* Display first [openbmc_debug] when entering openbmc.pm

* modified error msg

* Make sure credential files have a trailing newline (#4442)

* modified depending on comments

* Fix the typo in the man page of makegocons

* Update the print out based on the review comment, should not use  since the regular expression is removed

* Modify the nodeset disjoint test case accordingly for #4426

* Use short hostname in rcons for goconserver

As the certificate of xcat is signed with short hostname, this
commit force to use the short hostname in  the environment variable for
`congo console`.

* Fix issue 3497, make sense for reventlog msg

* Give summary after flash active when no debugmode

* Fix the issue that the IP configuration will fail if bmc attribute is a hostname

* enhance genimage for sles12sp2 (#4450)

* Add dhcp-client-identifier to lease block (#4429)

Machines that use Infiniband for PXE booting need to have the
dhcp-client-identifier set in the lease block.
Without it, they will not get the lease from the server.

* Support multiple bonds on bring-up

* modified depending on comments

* fix the check for rc to 1 on error cases

* modify response for bmcdiscover when error

* Ignore syslog error in monitorctrl when setNodeStatusAttributes (#4459)

* fix issue https://github.com/xcat2/xcat-core/issues/4411 (#4462)

* fix issue Compute nodes fail to get provisioned #4411: covert imgsrv and xcatmaster to their ip addresses in case the hostname cannot be resolved inside initrd for diskless

* More strict check to tell if it is a chroot env to avoid modify DB (#4463)

when genimage for SN image (#4365)

* issues for install license file on accton switches (#4460)

* Add test cases for rflash regular usage against openbmc

* modify depending on xuwei's comment

* add 2 more cases for option d

* enhance rflash upload message

* Do not display message for clearing dumps when only PNOR

* Display hostname even if multiple IP addresses

* modified depending on comments

* polished message

* Modify the default consoleondemand based on the global setting

This commit fix the bug that consoleondemand works incorrectly.

* modify depending on comments

* modified depending on comments

* enhance rflash error messages

* Modify documenation for servicenode attributes

* build rst file from Schema.pm by db2man

* change status back to starts

* modified depending on comments

* rm openbmcevents

* Usage and man page update for rspconfig dump

* let rflash error message flexible

* Add support for the "file -> (noderange) file" syntax in synclist with ServiceNodes (#4445)

* Add support for "file -  (noderange) file" in synclist when using
hierarchical mode. Fixes #4425

  This patch ensures that:
  1. the synclist is correctly parsed when running on a Service Node
  2. all files are synchronized to SNs in hierarchical mode

* Better test condition for #4425, addresses issue in
https://github.com/xcat2/xcat-core/pull/4445#issuecomment-349472901

* Fix issue 4477, if has node-<mac> will not create node-<mtms> for the same node

* fix issue updatenode -f loses directories when copying files to SN #4456 (#4494)

* comment from ErTao

* Crude attempt at including external configuration files in named.conf

* Fixes after the review

* Fix issue 4490, record any error when rflash active process

* add -d usage and manpage

* updatenode -F not work in hierachy env as the user name is FQDN of MN (#4484)

* updatenode -F not work in hierachy env (#4455)
 - add trace when -V is enabled
 - get the DSH_FROM_USERID from updatenode client

*  - when 'updatenode -F' need to push SN first, using root as non-root does not have permission write to 'SNsyncfiledir'
 - move the set DSH_FROM_USERID code out of the loop, and also cover remote client case.

* fix issue for command rspconfig hostname=*

* enhance rflash

* Adjust the server used for kernel/initrd and imgurl for petitboot (#4416)
 - URL for kernel/initrd, get the value from below value tftpserver -> xcatmaster -> myipfn
 - URL for image, get the value from below value nfsserver -> tftpserver -> xcatmaster -> myipfn

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot (#4428)

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot

* Get NODE from mypostscripts

* Improve the output message for reventlog, use a global variable to set PolicyFile Path

* If debug_msg is not provided, use an empty string

* Check for LinkLocal as well as 169.254 IP address

* Fix issue 4507, add parameter check for rspconfig admin_passwd

* record more information when rflash upload error

* Fix the error when using array ref in updatenode with old version perl, it is introduced by PR#4484 (#4518)

* Do not restart conserver if goconserver was started

If goconserver was enabled, do not start conserver when restart
xcatd on service node.

* remove the /etc/localtime before copy timezone file

* Use CONGO_CLIENT_TYPE to tell goconserver the source of client (#4501)

goconserver could send back message based on the client type
this commit set CONGO_CLIENT_TYPE to xcat to make the message
from goconserver more friendly.

* add rflash -d doc

* only ignore 169.254.x.x for OpenBMC

* Fix issue 4513, print out better error msg for reventlog -s

* Modify or add openbmc test cases or bundle

* add test cases for updatenode -f/F in hierarchy environment, covers issues #4456,#4455 and PR #4425 (#4500)
2017-12-14 05:03:34 -06:00

1692 lines
57 KiB
Perl
Executable File

#!/usr/bin/env perl
## IBM(c) 20013 EPL license http://www.eclipse.org/legal/epl-v10.html
#
# This plugin is used to handle the sequencial discovery. During the discovery,
# the nodes should be powered on one by one, sequencial discovery plugin will
# discover the nodes one by one and define them to xCAT DB.
# For the new discovered node but NOT handled by xCAT plugin,
# it will be recorded to discoverydata table.
#
package xCAT_plugin::seqdiscovery;
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}
use strict;
use Getopt::Long;
use XML::Simple;
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
use lib "$::XCATROOT/lib/perl";
use xCAT::NodeRange;
use xCAT::Table;
use xCAT::NetworkUtils;
use xCAT::MsgUtils;
use xCAT::Utils;
use xCAT::DiscoveryUtils;
use xCAT::NodeRange qw/noderange/;
require xCAT::data::ibmhwtypes;
use Time::HiRes qw(gettimeofday sleep);
sub handled_commands {
return {
findme => 'seqdiscovery',
nodediscoverstart => 'seqdiscovery',
nodediscoverstop => 'seqdiscovery',
nodediscoverls => 'seqdiscovery',
nodediscoverstatus => 'seqdiscovery',
nodediscoverdef => 'seqdiscovery',
}
}
=head3 findme
Handle the request form node to map and define the request to a node
=cut
sub findme {
my $request = shift;
my $callback = shift;
my $subreq = shift;
my @SEQdiscover = xCAT::TableUtils->get_site_attribute("__SEQDiscover");
my @PCMdiscover = xCAT::TableUtils->get_site_attribute("__PCMDiscover");
my @ZVMDiscover = xCAT::TableUtils->get_site_attribute("__ZVMDiscover");
if (defined($request->{discoverymethod}) and defined($request->{discoverymethod}->[0]) and ($request->{discoverymethod}->[0] ne 'undef')) {
# The findme request had been processed by other module, just return
return;
}
unless ($SEQdiscover[0]) {
return;
}
# Get the parameters for the sequential discovery
my %param;
my @params = split(',', $SEQdiscover[0]);
foreach (@params) {
my ($name, $value) = split('=', $_);
$param{$name} = $value;
}
my $mac;
my $ip = $request->{'_xcat_clientip'};
if (defined $request->{nodetype} and $request->{nodetype}->[0] eq 'virtual') {
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Error: virtual machines is not supported");
return;
}
my $arptable;
if (-x "/usr/sbin/arp") {
$arptable = `/usr/sbin/arp -n`;
}
else {
$arptable = `/sbin/arp -n`;
}
my @arpents = split /\n/, $arptable;
foreach (@arpents) {
if (m/^($ip)\s+\S+\s+(\S+)\s/) {
$mac = $2;
last;
}
}
unless ($mac) {
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Error: Could not find mac of the $ip");
return;
}
# check whether the mac could map to a node
my $mactab = xCAT::Table->new('mac');
unless ($mactab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: mac.");
}
my $node;
my @macs = $mactab->getAllAttribs('node', 'mac');
# for each entry: 34:40:b5:be:db:b0!*NOIP*|34:40:b5:be:db:b0!*NOIP*
foreach my $macnode (@macs) {
my @macents = split('\|', $macnode->{'mac'});
foreach my $macent (@macents) {
my ($usedmac) = split('!', $macent);
if ($usedmac =~ /$mac/i) {
$node = $macnode->{'node'};
last;
}
}
}
my @allnodes;
unless ($node) {
# get a free node
@allnodes = getfreenodes($param{'noderange'}, "all");
if (@allnodes) {
$node = $allnodes[0];
}
}
my $bmc_node = undef;
my @bmc_nodes = ();
if ($request->{'mtm'}->[0] and $request->{'serial'}->[0]) {
my $mtms = $request->{'mtm'}->[0] . "*" . $request->{'serial'}->[0];
my $tmp_nodes = $::XCATVPDHASH{$mtms};
foreach (@$tmp_nodes) {
if ($::XCATMPHASH{$_}) {
push @bmc_nodes, $_;
}
}
}
if ($request->{'bmcmac'}->[0]) {
my $bmcmac = lc($request->{'bmcmac'}->[0]);
$bmcmac =~ s/\://g;
my $tmp_node = "node-$bmcmac";
push @bmc_nodes, $tmp_node if ($::XCATMPHASH{$tmp_node});
}
$bmc_node = join(",", @bmc_nodes);
if ($node) {
my $skiphostip;
my $skipbmcip;
my $bmcname;
# check the host ip and bmc
my $hosttab = xCAT::Table->new('hosts');
unless ($hosttab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: hosts.");
}
my $hostip = getpredefips([$node], "host");
foreach (keys %$hostip) {
if ($hostip->{$_} eq "$node") {
$skiphostip = 1;
}
}
my $ipmitab = xCAT::Table->new('ipmi');
unless ($ipmitab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: ipmi.");
}
# check the bmc definition in the ipmi table
my $ipmient = $ipmitab->getNodeAttribs($node, ['bmc']);
if (defined($ipmient->{'bmc'})) {
$bmcname = $ipmient->{'bmc'};
if ($bmcname =~ /\d+\.\d+\.\d+\.\d+/) {
$skipbmcip = 1;
} else {
my $bmcip = getpredefips([$node], "bmc");
foreach (keys %$bmcip) {
if ($bmcip->{$_} eq $bmcname) {
$skipbmcip = 1;
}
}
}
}
# set the host ip if the node does not have
unless ($skiphostip) {
my $hostip = getfreeips($param{'hostiprange'}, \@allnodes, "host");
unless ($hostip) {
nodediscoverstop($callback, undef, "host ips");
return;
}
$hosttab->setNodeAttribs($node, { ip => $hostip });
$hosttab->commit();
}
# set the bmc ip if the node does not have
unless ($skipbmcip) {
unless ($bmcname) {
# set the default bmc name
$bmcname = $node . "-bmc";
}
my $bmcip = getfreeips($param{'bmciprange'}, \@allnodes, "bmc");
unless ($bmcip) {
nodediscoverstop($callback, undef, "bmc ips");
return;
}
# for auto created bmc, just add it to hosts.otherinterfaces instead of adding a new bmc node
my $otherif = $hosttab->getNodeAttribs($node, ['otherinterfaces']);
my $updateotherif;
if ($otherif && defined($otherif->{'otherinterfaces'})) {
$updateotherif .= ",$bmcname:$bmcip";
} else {
$updateotherif = "$bmcname:$bmcip";
}
$hosttab->setNodeAttribs($node, { otherinterfaces => $updateotherif });
$hosttab->commit();
# set the bmc to the ipmi table
$ipmitab->setNodeAttribs($node, { bmc => $bmcname });
$ipmitab->commit();
}
# update the host ip pair to /etc/hosts, it's necessary for discovered and makedhcp commands
my @newhosts = ($node, $bmcname);
if (@newhosts) {
my $req;
$req->{command} = ['makehosts'];
$req->{node} = \@newhosts;
$subreq->($req);
# run makedns only when -n flag was specified with nodediscoverstart,
# dns=yes in site.__SEQDiscover
if (defined($param{'dns'}) && ($param{'dns'} eq 'yes'))
{
my $req;
$req->{command} = ['makedns'];
$req->{node} = \@newhosts;
$subreq->($req);
}
}
# set the specific attributes from parameters
my $updateparams;
my %setpos;
if (defined($param{'rack'})) {
$setpos{'rack'} = $param{'rack'};
}
if (defined($param{'chassis'})) {
$setpos{'chassis'} = $param{'chassis'};
}
if (defined($param{'height'})) {
$setpos{'height'} = $param{'height'};
}
if (defined($param{'unit'})) {
$setpos{'u'} = $param{'unit'};
if (defined($param{'height'})) {
$param{'unit'} += $param{'height'};
} else {
$param{'unit'} += 1;
}
$updateparams = 1;
}
if (keys %setpos) {
my $postab = xCAT::Table->new('nodepos');
unless ($postab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: nodepos.");
}
$postab->setNodeAttribs($node, \%setpos);
$postab->close();
}
if ($updateparams) {
my $textparam;
foreach my $name (keys %param) {
$textparam .= "$name=$param{$name},";
}
$textparam =~ s/,\z//;
# Update the discovery parameters to the site.__SEQDiscover which will be used by nodediscoverls/status/stop and findme,
my $sitetab = xCAT::Table->new("site");
$sitetab->setAttribs({ "key" => "__SEQDiscover" }, { "value" => "$textparam" });
$sitetab->close();
}
#set the groups for the node
my $nltab = xCAT::Table->new('nodelist');
unless ($nltab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: nodelist.");
}
if (defined($param{'groups'})) {
$nltab->setNodeAttribs($node, { groups => $param{'groups'} });
} else {
# just set the groups attribute when there was no groups value was set
my $nlent = $nltab->getNodeAttribs($node, ['groups']);
if (!$nlent || !$nlent->{'groups'}) {
$nltab->setNodeAttribs($node, { groups => "all" });
}
}
# update node groups with pre-defined groups
if (defined($param{'mtm'})) {
my @list = ();
my $tmp_group = xCAT::data::ibmhwtypes::parse_group($param{'mtm'});
if (defined($tmp_group)) {
xCAT::TableUtils->updatenodegroups($node, $nltab, $tmp_group);
}
}
# set the mgt for the node
my $hmtab = xCAT::Table->new('nodehm');
unless ($hmtab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: nodehm.");
}
my $hment = $hmtab->getNodeAttribs($node, ['mgt']);
if (!$hment || !$hment->{'mgt'}) {
$hmtab->setNodeAttribs($node, { mgt => "ipmi" });
}
my $chaintab = xCAT::Table->new('chain');
# Could not open chain table, but do not fail the discovery process
if (!$chaintab) {
xCAT::MsgUtils->message("S", "Error: could not open chain table");
} else {
my $chainent = $chaintab->getNodeAttribs($node, ['chain']);
my $nodechain = '';
my $origchain = '';
if (defined($chainent->{'chain'})) {
$nodechain = $chainent->{'chain'};
$origchain = $nodechain;
}
# If the bmciprange=xxx is specified with nodediscoverstart command,
# add the runcmd=bmcsetup at the beginning of the chain attribute
# the skipbmcsetup could be used to skip the bmcsetup for the node
if ($param{'bmciprange'} && !$param{'skipbmcsetup'}) {
if (!$nodechain) {
$nodechain = "runcmd=bmcsetup";
} else {
# do not add duplicate runcmd=bmcsetup in the chain attribute
if ($nodechain !~ /runcmd=bmcsetup/) {
$nodechain = "runcmd=bmcsetup," . $nodechain;
}
}
} # end if $param{'bmciprange'}
# Remove the runcmd=bmcsetup from chain if skipbmcsetup is specified
# this is useful for predefined configuration, or attributes inherit from groups
if ($param{'skipbmcsetup'}) {
if ($nodechain =~ /runcmd=bmcsetup/) {
$nodechain =~ s/runcmd=bmcsetup,//;
$nodechain =~ s/runcmd=bmcsetup//;
}
}
# If the osimage=xxx is specified with nodediscoverstart command,
# append the osimage=xxx at the end of the chain attribute
if ($param{'osimage'}) {
if (!$nodechain) {
$nodechain = "osimage=$param{'osimage'}";
} else {
# do not add multiple osimage=xxx in the chain attribute
# replace the old one with the new one
if ($nodechain !~ /osimage=/) {
$nodechain = $nodechain . ",osimage=$param{'osimage'}";
} else {
$nodechain =~ s/osimage=\w+/osimage=$param{'osimage'}/;
}
}
} # end if $param{'osimage'}
# Update the table only when the chain attribute is changed
if ($nodechain ne $origchain) {
$chaintab->setNodeAttribs($node, { chain => $nodechain });
}
$chaintab->close();
}
# call the discovered command to update the discovery request to a node
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Found node: $node");
$request->{discoverymethod} = ['sequential'];
my $req = {%$request};
$req->{command} = ['discovered'];
$req->{noderange} = [$node];
$req->{bmc_node} = [$bmc_node];
$req->{updateswitch} = ['yes'];
$subreq->($req);
if (defined($req->{error})) {
$request->{error}->[0] = '1';
$request->{error_msg}->[0] = $req->{error_msg}->[0];
}
%{$req} = (); #Clear req structure, it's done..
undef $mactab;
} else {
nodediscoverstop($callback, undef, "node names");
xCAT::MsgUtils->message("S", "xcat.discovery.seqdiscovery: ($request->{_xcat_clientmac}->[0]) Warning: Could not find any nodes using sequential-based discovery");
return;
}
xCAT::MsgUtils->message("S", "Sequential Discovery: Done");
}
=head3 displayver
Display the version information
=cut
sub displayver {
my $callback = shift;
my $version = xCAT::Utils->Version();
my $rsp;
push @{ $rsp->{data} }, $version;
xCAT::MsgUtils->message("I", $rsp, $callback);
}
=head3 nodediscoverstart
Initiate the sequencial discovery process
=cut
sub nodediscoverstart {
my $callback = shift;
my $args = shift;
my $usage = sub {
my $cb = shift;
my $msg = shift;
my $rsp;
if ($msg) {
push @{ $rsp->{data} }, $msg;
xCAT::MsgUtils->message("E", $rsp, $cb, 1);
}
my $usageinfo = "nodediscoverstart: Start a discovery process: Sequential, Profile or z/VM.
Usage:
Common:
nodediscoverstart [-h|--help|-v|--version|-V|--verbose]
Sequential Discovery:
nodediscoverstart noderange=<noderange> [hostiprange=<hostiprange>] [bmciprange=<bmciprange>] [groups=<groups>] [rack=<rack>] [chassis=<chassis>] [height=<height>] [unit=<unit>] [osimage=<osimagename>] [-n|--dns] [-s|--skipbmcsetup] [-V|--verbose]
Profile Discovery:
nodediscoverstart networkprofile=<networkprofile> imageprofile=<imageprofile> hostnameformat=<hostnameformat> [hardwareprofile=<hardwareprofile>] [groups=<groups>] [rack=<rack>] [chassis=<chassis>] [height=<height>] [unit=<unit>] [rank=rank-num]
z/VM Discovery:
nodediscoverstart zvmhost=<noderange> [defineto=both] [groups=<groups>] [ipfilter=<filter>] [openstackoperands=<operands>] [useridfilter=<filter>]
nodediscoverstart zvmhost=<noderange> defineto=xcatonly [groups=<groups>] [ipfilter=<filter>] [nodenameformat=<nodenameformat>] [useridfilter=<filter>]
nodediscoverstart zvmhost=<noderange> defineto=openstackonly [openstackoperands=<operands>]";
$rsp = ();
push @{ $rsp->{data} }, $usageinfo;
xCAT::MsgUtils->message("I", $rsp, $cb);
};
# valid attributes for seqdiscovery
my %validargs = (
'noderange' => 1,
'hostiprange' => 1,
'bmciprange' => 1,
'groups' => 1,
'rack' => 1,
'chassis' => 1,
'height' => 1,
'unit' => 1,
'osimage' => 1,
);
if ($args) {
@ARGV = @$args;
}
my ($help, $ver);
if (!GetOptions(
'h|help' => \$help,
'V|verbose' => \$::VERBOSE,
'-n|dns' => \$::DNS,
'-s|skipbmcsetup' => \$::SKIPBMCSETUP,
'v|version' => \$ver)) {
$usage->($callback);
return;
}
if ($help) {
$usage->($callback);
return;
}
if ($ver) {
&displayver($callback);
return;
}
my %orgargs;
foreach (@ARGV) {
my ($name, $value) = split('=', $_);
$orgargs{$name} = $value;
}
# Check the zvmhost= has been specified which is the flag that this is for z/VM discovery.
# Otherwise, fall thru to handle either sequential or profile discovery.
if ( defined( $orgargs{zvmhost} ) ) {
return;
}
# Check the noderange= has been specified which is the flag that this is for sequential discovery
# Otherwise try to check the whether the networkprofile || hardwareprofile || imageprofile
# has been passed, if yes, return to profile discovery
unless (defined($orgargs{noderange})) {
if (defined($orgargs{networkprofile}) || defined($orgargs{hostnameformat}) || defined($orgargs{imageprofile})) {
# just return that make profile-based discovery to handle it
return;
} else {
$usage->($callback, "For sequential discovery, the \'noderange\' option must be specified.");
$usage->($callback, "For z/VM discovery, the \'zvmhost\' option must be specified.");
return;
}
}
my %param; # The valid parameters
my $textparam; # The valid parameters in 'name=value,name=value...' format
# Check the validate of parameters
foreach my $name (keys %orgargs) {
unless (defined($validargs{$name})) {
$usage->($callback, "Invalid arguement \"$name\".");
return;
}
unless (defined($orgargs{$name})) {
$usage->($callback, "The parameter \"$name\" need a value.");
return;
}
# keep the valid parameters
$param{$name} = $orgargs{$name};
$textparam .= $name . '=' . $param{$name} . ',';
}
# If the -n flag is specified,
# add setupdns=yes into site.__SEQDiscover
if ($::DNS)
{
$textparam .= "dns=yes,";
}
# If the -s flag is specified,
# add skipbmcsetup=yes into site.__SEQDiscover
if ($::SKIPBMCSETUP)
{
$textparam .= "skipbmcsetup=yes,";
}
$textparam =~ s/,\z//;
# Check the running of profile-based discovery
my @PCMdiscover = xCAT::TableUtils->get_site_attribute("__PCMDiscover");
if ($PCMdiscover[0]) {
my $rsp;
push @{ $rsp->{data} }, "Sequentail Discovery cannot be run together with Profile-based discovery";
xCAT::MsgUtils->message("E", $rsp, $callback, 1);
return;
}
# Check the running of sequential discovery
my @SEQdiscover = xCAT::TableUtils->get_site_attribute("__SEQDiscover");
if ($SEQdiscover[0]) {
my $rsp;
push @{ $rsp->{data} }, "Sequentail Discovery is running. If you want to rerun the discovery, stop the running discovery first.";
xCAT::MsgUtils->message("E", $rsp, $callback, 1);
return;
}
# Check that the dynamic range in the dhcpd.conf has been set correctly
# search all the network in the networks table that make sure the dynamic range for the deployment network has been set
# Set the discovery parameters to the site.__SEQDiscover which will be used by nodediscoverls/status/stop and findme,
my $sitetab = xCAT::Table->new("site");
$sitetab->setAttribs({ "key" => "__SEQDiscover" }, { "value" => "$textparam" });
$sitetab->close();
# Clean the entries which discovery method is 'sequential' from the discoverydata table
my $distab = xCAT::Table->new("discoverydata");
$distab->delEntries({ method => 'sequential' });
$distab->commit();
# Calculate the available node name and IPs
my @freenodes = getfreenodes($param{'noderange'}, "all");
my @freehostips = getfreeips($param{'hostiprange'}, \@freenodes, "host", "all");
my @freebmcips = getfreeips($param{'bmciprange'}, \@freenodes, "bmc", "all");
#xCAT::MsgUtils->message("S", "Sequential Discovery: Start");
my $rsp;
push @{ $rsp->{data} }, "Sequential Discovery: Started:";
push @{ $rsp->{data} }, " Number of free node names: " . ($#freenodes + 1);
if ($param{'hostiprange'}) {
if (@freehostips) {
push @{ $rsp->{data} }, " Number of free host ips: " . ($#freehostips + 1);
} else {
push @{ $rsp->{data} }, " No free host ips.";
}
}
if ($param{'bmciprange'}) {
if (@freebmcips) {
push @{ $rsp->{data} }, " Number of free bmc ips: " . ($#freebmcips + 1);
} else {
push @{ $rsp->{data} }, " No free bmc ips.";
}
}
xCAT::MsgUtils->message("I", $rsp, $callback);
if ($::VERBOSE) {
# dispaly the free nodes
# get predefined host ip
my %prehostips;
my $prehosts = getpredefips(\@freenodes, "host"); #pre{ip} = nodename
foreach (keys %$prehosts) {
$prehostips{ $prehosts->{$_} } = $_; #pre{nodename} = ip
}
# get predefined bmc ip
my %prebmcips;
my $prebmcs = getpredefips(\@freenodes, "bmc"); #pre{ip} = bmcname
foreach (keys %$prebmcs) {
$prebmcips{ $prebmcs->{$_} } = $_; #pre{bmcname} = ip
}
# get the bmc of nodes
my $ipmitab = xCAT::Table->new('ipmi');
my $ipmient;
if ($ipmitab) {
$ipmient = $ipmitab->getNodesAttribs(\@freenodes, ['bmc']);
}
my $vrsp;
push @{ $vrsp->{data} }, "\n====================Free Nodes===================";
push @{ $vrsp->{data} }, sprintf("%-20s%-20s%-20s", "NODE", "HOST IP", "BMC IP");
my $index = 0;
foreach (@freenodes) {
my $hostip;
my $bmcip;
# if predefined, use it; otherwise pop out one from the free ip list
if (defined($prehostips{$_})) {
$hostip = $prehostips{$_};
} else {
while (($hostip = shift @freehostips)) {
if (!defined($prehosts->{$hostip})) { last; }
}
unless ($hostip) {
$hostip = "--no free--";
}
}
# if predefined, use it; otherwise pop out one from the free ip list
my $bmcname;
if (defined($ipmient->{$_}->[0]->{'bmc'})) {
$bmcname = $ipmient->{$_}->[0]->{'bmc'};
if ($bmcname =~ /\d+\.\d+\.\d+\.\d+/) {
$bmcip = $bmcname;
} elsif (defined($prebmcips{$bmcname})) {
$bmcip = $prebmcips{$bmcname};
}
}
unless ($bmcip) {
while (($bmcip = shift @freebmcips)) {
if (!defined($prebmcs->{$bmcip})) { last; }
}
unless ($bmcip) {
$bmcip = "--no free--";
}
}
push @{ $vrsp->{data} }, sprintf("%-20s%-20s%-20s", $_, $hostip, $bmcip);
$index++;
}
xCAT::MsgUtils->message("I", $vrsp, $callback);
}
}
=head3 nodediscoverstop
Stop the sequencial discovery process
=cut
sub nodediscoverstop {
my $callback = shift;
my $args = shift;
my $auto = shift;
my $usage = sub {
my $cb = shift;
my $msg = shift;
my $rsp;
if ($msg) {
push @{ $rsp->{data} }, $msg;
xCAT::MsgUtils->message("E", $rsp, $cb, 1);
}
my $usageinfo = "nodediscoverstop: Stop the running discovery: Profile, Sequential and z/VM.
Usage:
Common:
nodediscoverstop [-h|--help|-v|--version]
z/VM discovery:
nodediscoverstop [-z|--zvmhost <noderange>]";
$rsp = ();
push @{ $rsp->{data} }, $usageinfo;
xCAT::MsgUtils->message("I", $rsp, $cb);
};
if ($args) {
@ARGV = @$args;
}
my ($help, $ver);
if (!GetOptions(
'h|help' => \$help,
'V|verbose' => \$::VERBOSE,
'v|version' => \$ver)) {
$usage->($callback);
return;
}
if ($help) {
$usage->($callback);
return;
}
if ($ver) {
&displayver($callback);
return;
}
# Check the running of sequential discovery
my @SEQDiscover = xCAT::TableUtils->get_site_attribute("__SEQDiscover");
my @PCMDiscover = xCAT::TableUtils->get_site_attribute("__PCMDiscover");
my @ZVMDiscover = xCAT::TableUtils->get_site_attribute("__ZVMDiscover");
if ($PCMDiscover[0] or $ZVMDiscover[0]) {
# return directly that the other discovery will handle the stop function.
return;
} elsif (!$SEQDiscover[0]) {
# Neither of profile nor sequential was running
my $rsp;
push @{ $rsp->{data} }, "Sequential Discovery is stopped.";
push @{ $rsp->{data} }, "Profile Discovery is stopped.";
push @{$rsp->{data}}, "z/VM Discovery is stopped.";
xCAT::MsgUtils->message("E", $rsp, $callback, 1);
return;
}
my $DBname = xCAT::Utils->get_DBName; # support for DB2
# Go through discoverydata table and display the sequential discovery entries
my $distab = xCAT::Table->new('discoverydata');
unless ($distab) {
my $rsp;
push @{ $rsp->{data} }, "Discovery Error: Could not open table: discoverydata.";
xCAT::MsgUtils->message("E", $rsp, $callback);
return;
}
my @disdata;
if ($DBname =~ /^DB2/) {
@disdata = $distab->getAllAttribsWhere("\"method\" = 'sequential'", 'node', 'mtm', 'serial');
} else {
@disdata = $distab->getAllAttribsWhere("method='sequential'", 'node', 'mtm', 'serial');
}
my @discoverednodes;
foreach (@disdata) {
push @discoverednodes, sprintf(" %-20s%-10s%-10s", $_->{'node'}, $_->{'mtm'}, substr($_->{'serial'}, 0, 8),);
}
my $rsp;
push @{ $rsp->{data} }, "Discovered " . ($#discoverednodes + 1) . " nodes.";
if (@discoverednodes) {
push @{ $rsp->{data} }, sprintf(" %-20s%-10s%-10s", 'NODE', 'MTM', 'SERIAL');
foreach (@discoverednodes) {
push @{ $rsp->{data} }, "$_";
}
}
xCAT::MsgUtils->message("I", $rsp, $callback);
if ($auto) {
xCAT::MsgUtils->message("S", "Sequential Discovery: Auto Stopped because all $auto in the specified range have been assigned to discovered nodes. Run \'nodediscoverls -t seq\' to display the discovery result.");
} else {
xCAT::MsgUtils->message("S", "Sequential Discovery: Stoped.");
}
# Remove the site.__SEQDiscover
my $sitetab = xCAT::Table->new("site");
$sitetab->delEntries({ key => '__SEQDiscover' });
$sitetab->commit();
}
=head3 nodediscoverls
Display the discovered nodes. This supports sequential and z/VM and partially Profile discovery.
=cut
sub nodediscoverls {
my $callback = shift;
my $args = shift;
my $usage = sub {
my $cb = shift;
my $msg = shift;
my $rsp;
if ($msg) {
push @{ $rsp->{data} }, $msg;
xCAT::MsgUtils->message("E", $rsp, $cb, 1);
}
my $usageinfo = "nodediscoverls: list the discovered nodes.
Usage:
Common:
nodediscoverls
nodediscoverls [-h|--help|-v|--version]
nodediscoverls [-t seq|profile|switch|blade|manual|mtms|undef|zvm|all] [-l]
nodediscoverls [-u uuid] [-l]
z/VM:
nodediscoverls [-t zvm][-z|--zvmhost <noderange>] [-l]";
$rsp = ();
push @{ $rsp->{data} }, $usageinfo;
xCAT::MsgUtils->message("I", $rsp, $cb);
};
if ($args) {
@ARGV = @$args;
}
my ($type, $uuid, $long, $help, $ver, $zvmHost );
if (!GetOptions(
't=s' => \$type,
'u=s' => \$uuid,
'l' => \$long,
'h|help' => \$help,
'V|verbose' => \$::VERBOSE,
'v|version' => \$ver,
'z|zvmhost=s' => \$zvmHost )) {
$usage->($callback);
return;
}
if ($help) {
$usage->($callback);
return;
}
if ($ver) {
&displayver($callback);
return;
}
# If the type is specified, display the corresponding type of nodes
my ( @SEQDiscover, @ZVMDiscover );
if ($type) {
if ($type !~ /^(seq|profile|switch|blade|manual|mtms|undef|zvm|all)$/) {
$usage->($callback, "The discovery type \'$type\' is not supported.");
return;
}
} elsif ($uuid) {
} else {
# Check the running of sequential discovery
@SEQDiscover = xCAT::TableUtils->get_site_attribute("__SEQDiscover");
@ZVMDiscover = xCAT::TableUtils->get_site_attribute("__ZVMDiscover");
my @PCMDiscover = xCAT::TableUtils->get_site_attribute("__PCMDiscover");
if ($SEQDiscover[0]) {
$type = "seq";
} elsif ($PCMDiscover[0]) {
#return directly if profile discovery is running.
return;
} elsif ( $ZVMDiscover[0] ) {
# zvmdiscovery handles requests for a running z/VM discovery.
return;
} else {
# no type, no seq and no profile, then just display all
$type = "all";
}
}
# If a zvmHost was specified then let zvmdiscovery handle it.
# Specifying '-u' will keep processing within seqdiscovery.
if ( !$uuid && ( $zvmHost || ( $type && $type eq 'zvm' )) ) {
# zvmdiscovery handles request specific to z/VM.
return;
}
my $DBname = xCAT::Utils->get_DBName; # support for DB2
# Go through discoverydata table and display the discovery entries
my $distab = xCAT::Table->new('discoverydata');
unless ($distab) {
my $rsp;
push @{ $rsp->{data} }, "Discovery Error: Could not open table: discoverydata.";
xCAT::MsgUtils->message("E", $rsp, $callback);
return;
}
my @disdata;
my @disattrs;
if ($long) {
@disattrs = ('uuid', 'node', 'method', 'discoverytime', 'arch', 'cpucount', 'cputype', 'memory', 'mtm', 'serial', 'nicdriver', 'nicipv4', 'nichwaddr', 'nicpci', 'nicloc', 'niconboard', 'nicfirm', 'switchname', 'switchaddr', 'switchdesc', 'switchport');
} else {
@disattrs = ('uuid', 'node', 'method', 'mtm', 'serial');
}
if ($type) {
if ($type eq "all") {
@disdata = $distab->getAllAttribs(@disattrs);
} else {
$type = "sequential" if ($type =~ /^seq/);
if ($DBname =~ /^DB2/) {
@disdata = $distab->getAllAttribsWhere("\"method\" = '$type'", @disattrs);
} else {
@disdata = $distab->getAllAttribsWhere("method='$type'", @disattrs);
}
}
} elsif ($uuid) {
if ($DBname =~ /^DB2/) {
@disdata = $distab->getAllAttribsWhere("\"uuid\" = '$uuid'", @disattrs);
} else {
@disdata = $distab->getAllAttribsWhere("uuid='$uuid'", @disattrs);
}
}
my $discoverednum = $#disdata + 1;
my @discoverednodes;
foreach my $ent (@disdata) {
if ($long) {
foreach my $attr (@disattrs) {
if ($attr eq "uuid") {
push @discoverednodes, "Object uuid: $ent->{$attr}";
} elsif (defined($ent->{$attr})) {
push @discoverednodes, " $attr=$ent->{$attr}";
}
}
} else {
$ent->{'node'} = 'undef' unless ($ent->{'node'});
$ent->{'method'} = 'undef' unless ($ent->{'method'});
push @discoverednodes, sprintf(" %-40s%-20s%-15s%-10s %-20s", $ent->{'uuid'}, $ent->{'node'}, $ent->{'method'}, $ent->{'mtm'}, substr($ent->{'serial'}, 0, 19));
}
}
my $rsp;
if (($SEQDiscover[0] && $type eq "sequential" ) or
( $type && $type eq "all" )) {
push @{ $rsp->{data} }, "Discovered $discoverednum node.";
}
if (@discoverednodes) {
unless ($long) {
push @{ $rsp->{data} }, sprintf(" %-40s%-20s%-15s%-10s %-20s", 'UUID', 'NODE', 'METHOD', 'MTM', 'SERIAL');
}
foreach (@discoverednodes) {
push @{ $rsp->{data} }, "$_";
}
}
xCAT::MsgUtils->message("I", $rsp, $callback);
}
=head3 nodediscoverstatus
Display the discovery status
=cut
sub nodediscoverstatus {
my $callback = shift;
my $args = shift;
my $usage = sub {
my $cb = shift;
my $msg = shift;
my $rsp;
if ($msg) {
push @{ $rsp->{data} }, $msg;
xCAT::MsgUtils->message("E", $rsp, $cb, 1);
}
my $usageinfo = "nodediscoverstatus: Display the discovery process status.
Usage:
Common:
nodediscoverstatus [-h|--help|-v|--version]
z/VM
nodediscoverstatus [-z|--zvmhost <noderange>]";
$rsp = ();
push @{ $rsp->{data} }, $usageinfo;
xCAT::MsgUtils->message("I", $rsp, $cb);
};
if ($args) {
@ARGV = @$args;
}
my ($type, $uuid, $long, $help, $ver);
if (!GetOptions(
'h|help' => \$help,
'V|verbose' => \$::VERBOSE,
'v|version' => \$ver)) {
$usage->($callback);
return;
}
if ($help) {
$usage->($callback);
return;
}
if ($ver) {
&displayver($callback);
return;
}
# Check the running of sequential discovery
my @SEQDiscover = xCAT::TableUtils->get_site_attribute("__SEQDiscover");
my @PCMDiscover = xCAT::TableUtils->get_site_attribute("__PCMDiscover");
my @ZVMDiscover = xCAT::TableUtils->get_site_attribute("__ZVMDiscover");
if ($SEQDiscover[0]) {
my $rsp;
push @{ $rsp->{data} }, "Sequential discovery is running.";
push @{ $rsp->{data} }, " The parameters used for discovery: " . $SEQDiscover[0];
xCAT::MsgUtils->message("I", $rsp, $callback);
} elsif ($PCMDiscover[0]) {
my $rsp;
push @{ $rsp->{data} }, "Node discovery for all nodes using profiles is running";
xCAT::MsgUtils->message("I", $rsp, $callback);
} elsif ( $ZVMDiscover[0] ) {
# z/VM discovery is a more complex response so we let its handler return the response.
} else {
my $rsp;
push @{ $rsp->{data} }, "Sequential Discovery is stopped.";
push @{ $rsp->{data} }, "Profile Discovery is stopped.";
push @{$rsp->{data}}, "z/VM Discovery is stopped.";
xCAT::MsgUtils->message("I", $rsp, $callback);
}
}
=head3 nodediscoverdef
Define the undefined entry from the discoverydata table to a specific node
Or clean the discoverydata table
=cut
sub nodediscoverdef {
my $callback = shift;
my $subreq = shift;
my $args = shift;
my @inputZvmHosts; # Input list of z/VM host nodes to stop
my $zvmHost; # Small scope variable to temporarily hold a z/VM host node value
# The subroutine used to display the usage message
my $usage = sub {
my $cb = shift;
my $msg = shift;
my $rsp;
if ($msg) {
push @{ $rsp->{data} }, $msg;
xCAT::MsgUtils->message("E", $rsp, $cb, 1);
}
my $usageinfo = "nodediscoverdef: Define the undefined discovery request, or clean the discovery entries in the discoverydata table (Which can be displayed by nodediscoverls command).
Usage:
Common:
nodediscoverdef -u uuid -n node
nodediscoverdef -r -u uuid
nodediscoverdef -r -t {seq|profile|switch|blade|manual|undef|zvm|all}
nodediscoverdef [-h|--help|-v|--version]
z/VM:
nodediscoverdef -r -t zvm [-z|--zvmhost noderange]";
$rsp = ();
push @{ $rsp->{data} }, $usageinfo;
xCAT::MsgUtils->message("I", $rsp, $cb);
};
# Parse arguments
if ($args) {
@ARGV = @$args;
}
my ($type, $uuid, $node, $remove, $help, $ver);
if (!GetOptions(
'u=s' => \$uuid,
'n=s' => \$node,
't=s' => \$type,
'r' => \$remove,
'h|help' => \$help,
'V|verbose' => \$::VERBOSE,
'v|version' => \$ver,
'z|zvmhost=s' => \$zvmHost )) {
$usage->($callback);
return;
}
if ($help) {
$usage->($callback);
return;
}
if ($ver) {
&displayver($callback);
return;
}
my $DBname = xCAT::Utils->get_DBName; # support for DB2
# Put any specified zvmhosts into an array for later use.
if ( $zvmHost ) {
$type = 'zvm' if ( !$type );
if ( $type ne 'zvm' ) {
xCAT::MsgUtils->message("E", {data=>["Discovery Error: Type must be 'zvm' when '-z' or '--zvmhost' is specified."]}, $callback);
return;
}
if ( index( $zvmHost, ',' ) != -1 ) {
# Must have specified multiple host node names
my @hosts = split( /,/, $zvmHost );
foreach $zvmHost ( @hosts ) {
if ( !$zvmHost ) {
# Tolerate zvmhost value beginning with a comma.
# It is wrong but not worth an error message.
next;
}
push( @inputZvmHosts, $zvmHost );
}
} else {
push( @inputZvmHosts, $zvmHost );
}
}
# open the discoverydata table for the subsequent using
my $distab = xCAT::Table->new("discoverydata");
unless ($distab) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: discoverydata.");
return;
}
if ($remove) {
# handle the -r to remove the entries from discoverydata table
if (!($uuid || $type) || $node) {
$usage->($callback);
return;
}
if ($uuid && $type) {
$usage->($callback);
return;
}
if ($uuid) {
# handle the -r -u <uuid>
my @disdata;
if ($DBname =~ /^DB2/) {
@disdata = $distab->getAllAttribsWhere("\"uuid\" = '$uuid'", 'method');
} else {
@disdata = $distab->getAllAttribsWhere("uuid='$uuid'", 'method');
}
unless (@disdata) {
xCAT::MsgUtils->message("E", { data => ["Cannot find discovery entry with uuid equals [$uuid]."] }, $callback);
return;
}
$distab->delEntries({ uuid => $uuid });
$distab->commit();
} elsif ($type) {
# handle the -r -t <...>
if ($type !~ /^(seq|profile|switch|blade|manual|undef|zvm|all)$/) {
$usage->($callback, "The discovery type \'$type\' is not supported.");
return;
}
if ($type eq "all") {
# remove all the entries from discoverydata table
# there's no subroutine to remove all the entries from a table, so just make code to work around
# get all the entries first
my @disdata = $distab->getAllAttribs('uuid', 'method');
my %methodlist;
foreach my $ent (@disdata) {
if ($ent->{'method'}) {
# if the entry has 'method' att set, classify them and remove at once
$methodlist{ $ent->{'method'} } = 1;
} else {
# if 'method' is not set, remove the entry directly
$distab->delEntries({ uuid => $ent->{'uuid'} });
}
}
# remove entries which have method att been set
foreach (keys %methodlist) {
$distab->delEntries({ method => $_ });
}
$distab->commit();
} else {
# remove the specific type of discovery entries
if ($type =~ /^seq/) {
$type = "sequential";
}
# If a noderange of z/VM hosts was specified then delete
# all z/VM discovered systems for those hosts. 'zvm' type
# must be used when z/VM hosts are specified.
if ( @inputZvmHosts ) {
my %keyhash;
$keyhash{'method'} = 'zvm';
foreach $zvmHost ( @inputZvmHosts ) {
$keyhash{'otherdata'} = "zvmhost." . $zvmHost;
$distab->delEntries( \%keyhash );
}
$distab->commit();
} else {
# Otherwise, Delete all systems discovered using the specified method.
$distab->delEntries({ method => $type });
$distab->commit();
}
}
}
xCAT::MsgUtils->message("I", { data => ["Removing discovery entries finished."] }, $callback);
} elsif ($uuid) {
# define the undefined entry to a node
if (!$node) {
$usage->($callback);
return;
}
# make sure the node is valid.
my @validnode = noderange($node);
if ($#validnode != 0) {
xCAT::MsgUtils->message("E", { data => ["The node [$node] should be a valid xCAT node."] }, $callback);
return;
}
$node = $validnode[0];
# to define the a request to a node, reuse the 'discovered' command to update the node
# so the procedure will be that regenerate the request base on the attributes which stored in the discoverydata table
# get all the attributes for the entry from the discoverydata table
my @disattrs = ('uuid', 'node', 'method', 'discoverytime', 'arch', 'cpucount', 'cputype', 'memory', 'mtm', 'serial', 'nicdriver', 'nicipv4', 'nichwaddr', 'nicpci', 'nicloc', 'niconboard', 'nicfirm', 'switchname', 'switchaddr', 'switchdesc', 'switchport', 'otherdata');
my @disdata;
if ($DBname =~ /^DB2/) {
@disdata = $distab->getAllAttribsWhere("\"uuid\" = '$uuid'", @disattrs);
} else {
@disdata = $distab->getAllAttribsWhere("uuid='$uuid'", @disattrs);
}
unless (@disdata) {
xCAT::MsgUtils->message("E", { data => ["Cannot find discovery entry with uuid equals $uuid"] }, $callback);
return;
}
# generate the request which is used to define the node
my $request;
my $ent = @disdata[0];
my $interfaces;
my $otherdata;
foreach my $key (keys %$ent) {
if ($key =~ /(nicdriver|nicfirm|nicipv4|nichwaddr|nicpci|nicloc|niconboard|switchaddr|switchname|switchport)/) {
# these entries are formatted as: eth0!xxx,eth1!xxx. split it and generate the request as eth0 {...}, eth1 {...}
my @ifs = split(/,/, $ent->{$key});
foreach (@ifs) {
my ($if, $value) = split('!', $_);
my $origname = $key;
if ($key eq "nicdriver") {
$origname = "driver";
} elsif ($key eq "nicfirm") {
$origname = "firmdesc";
} elsif ($key eq "nicipv4") {
$origname = "ip4address";
} elsif ($key eq "nichwaddr") {
$origname = "hwaddr";
} elsif ($key eq "nicpci") {
$origname = "pcidev";
} elsif ($key eq "nicloc") {
$origname = "location";
} elsif ($key eq "niconboard") {
$origname = "onboardeth";
}
push @{ $interfaces->{$if}->{$origname} }, $value;
}
} elsif ($key eq "otherdata") {
# this entry is just keep as is, so translate to hash is enough
$otherdata = eval { XMLin($ent->{$key}, SuppressEmpty => undef, ForceArray => 1) };
} elsif ($key eq "switchdesc") {
# just ingore the switchdesc, since it include ','
} else {
# for general attrs which just have one first level
$request->{$key} = [ $ent->{$key} ];
}
}
# add the interface part to the request hash
if ($interfaces) {
foreach (keys %$interfaces) {
$interfaces->{$_}->{'devname'} = [$_];
push @{ $request->{nic} }, $interfaces->{$_};
}
}
# add the untouched part to the request hash
if ($otherdata) {
foreach (keys %$otherdata) {
$request->{$_} = $otherdata->{$_};
}
}
# call the 'discovered' command to update the request to a node
$request->{command} = ['discovered'];
$request->{node} = [$node];
$request->{discoverymethod} = ['manual'];
$request->{updateswitch} = ['yes'];
my $rsp = $subreq->($request);
if (defined($rsp->{errorcode}->[0])) {
xCAT::MsgUtils->message("E", $rsp, $callback);
} else {
xCAT::MsgUtils->message("I", { data => ["Defined [$uuid] to node $node."] }, $callback);
}
} else {
$usage->($callback);
return;
}
}
sub process_request {
my $request = shift;
my $callback = shift;
my $subreq = shift;
my $command = $request->{command}->[0];
my $args = $request->{arg};
if ($command eq "findme") {
findme($request, $callback, $subreq);
} elsif ($command eq "nodediscoverstart") {
nodediscoverstart($callback, $args);
} elsif ($command eq "nodediscoverstop") {
nodediscoverstop($callback, $args);
} elsif ($command eq "nodediscoverls") {
nodediscoverls($callback, $args);
} elsif ($command eq "nodediscoverstatus") {
nodediscoverstatus($callback, $args);
} elsif ($command eq "nodediscoverdef") {
nodediscoverdef($callback, $subreq, $args);
}
}
=head3 getfreenodes
Get the free nodes base on the user specified noderange and defined nodes
arg1 - the noderange
arg2 - "all': return all the free nodes; otherwise just return one.
=cut
sub getfreenodes () {
my $noderange = shift;
my $all = shift;
my @freenodes;
# get all the nodes from noderange
my @nodes = noderange($noderange, 0);
# get all nodes from nodelist and mac table
my $nltb = xCAT::Table->new('nodelist');
unless ($nltb) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: nodelist.");
return;
}
my $mactb = xCAT::Table->new('mac');
unless ($mactb) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: mac.");
return;
}
# if mac address has been set, the node is not free
my $nlent = $nltb->getNodesAttribs(\@nodes, ['groups']);
my $macent = $mactb->getNodesAttribs(\@nodes, ['mac']);
foreach my $node (@nodes) {
if ($nlent->{$node}->[0]) {
unless ($macent->{$node}->[0] && $macent->{$node}->[0]->{'mac'}) {
push @freenodes, $node;
unless ($all) { last; }
}
} else {
push @freenodes, $node;
unless ($all) { last; }
}
}
unless (@freenodes) {
return;
}
if ($all) {
return @freenodes;
} else {
return $freenodes[0];
}
}
=head3 getpredefips
Get the ips which have been predefined to host or bmc
arg1 - a refenrece to the array of nodes
arg2 - type: host, bmc
return: hash {ip} = node
=cut
sub getpredefips {
my $freenode = shift;
my $type = shift; # type: host, bmc
my $hoststb = xCAT::Table->new('hosts');
unless ($hoststb) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: hosts.");
}
my %predefips; # to have the ip which prefined to the nodes
if ($type eq "bmc") {
# Find the bmc name from the ipmi table.
# if ipmi.bmc is an IP address, that means this is the IP of bmc
my @freebmc;
my %node2bmc; # $node2bmc{$node} = $bmc;
my $ipmitab = xCAT::Table->new('ipmi');
if ($ipmitab) {
my $ipmient = $ipmitab->getNodesAttribs($freenode, ['bmc']);
foreach (@$freenode) {
if (defined($ipmient->{$_}->[0]->{'bmc'})) {
if ($ipmient->{$_}->[0]->{'bmc'} =~ /\d+\.\d+\.\d+\.\d+/) {
$predefips{ $ipmient->{$_}->[0]->{'bmc'} } = $_ . "-bmc";
} else {
push @freebmc, $ipmient->{$_}->[0]->{'bmc'};
$node2bmc{$_} = $ipmient->{$_}->[0]->{'bmc'};
}
}
}
}
# check the system resolution first, then host.ip, then host.otherinterfaces
my $freenodeent = $hoststb->getNodesAttribs(\@freebmc, ['ip']);
foreach (@freebmc) {
my $nodeip = xCAT::NetworkUtils->getipaddr($_);
if ($nodeip) {
# handle the bmc which could be resolved to an IP by system
$predefips{$nodeip} = $_;
} else {
# handle the bmc which IP was defined in the hosts.ip
if (defined($freenodeent->{$_}->[0]) && $freenodeent->{$_}->[0]->{'ip'}) {
$predefips{ $freenodeent->{$_}->[0]->{'ip'} } = $_;
}
}
}
# handle the bmcs which bmc has been set in the hosts.otherinterfaces
$freenodeent = $hoststb->getNodesAttribs($freenode, ['otherinterfaces']);
foreach (@$freenode) {
if (defined($node2bmc{$_})) {
# for bmc node, search the hosts.otherinterface to see whether there's perdefined ip for bmc
my $bmcip = getbmcip_otherinterfaces($_, $node2bmc{$_}, $freenodeent->{$_}->[0]->{'otherinterfaces'});
if ($bmcip) {
$predefips{$bmcip} = $node2bmc{$_};
}
}
}
} elsif ($type eq "host") {
# get the predefined node which ip has been set in the hosts.ip
my $freenodeent = $hoststb->getNodesAttribs($freenode, ['ip']);
foreach (@$freenode) {
my $nodeip = xCAT::NetworkUtils->getipaddr($_);
if ($nodeip) {
$predefips{$nodeip} = $_;
} else {
if (defined($freenodeent->{$_}->[0]) && $freenodeent->{$_}->[0]->{'ip'}) {
$predefips{ $freenodeent->{$_}->[0]->{'ip'} } = $_;
}
}
}
}
return \%predefips;
}
=head3 getfreeips
Get the free ips base on the user specified ip range
arg1 - the ip range. Two format are suported: 192.168.1.1-192.168.2.50; 192.168.[1-2].[10-100]
arg2 - all the free nodes
arg3 - type: host, bmc
arg4 - "all': return free ips for all the free nodes; otherwise just return the first one.
return: array of all free ips or one ip base on the arg4
=cut
sub getfreeips {
my $iprange = shift;
my $freenode = shift;
my $type = shift; # type: host, bmc
my $all = shift;
my @freeips;
my %predefips; # to have the ip which prefined to the nodes
my $hoststb = xCAT::Table->new('hosts');
unless ($hoststb) {
xCAT::MsgUtils->message("S", "Discovery Error: Could not open table: hosts.");
}
my @freebmc;
my %usedips = ();
if ($type eq "bmc") {
# get the host ip for all predefind nodes from $freenode
%predefips = %{ getpredefips($freenode, "bmc") };
# get all the used ips, the predefined ip should be ignored
my @hostsent = $hoststb->getAllNodeAttribs([ 'node', 'ip', 'otherinterfaces' ]);
# Find the bmc name from the ipmi table.
# if ipmi.bmc is an IP address, that means this is the IP of bmc
my %node2bmc; # $node2bmc{$node} = $bmc;
my $ipmitab = xCAT::Table->new('ipmi');
if ($ipmitab) {
my @ipmients = $ipmitab->getAllNodeAttribs([ 'node', 'bmc' ]);
foreach my $ipmient (@ipmients) {
if (defined($ipmient->{'bmc'})) {
if ($ipmient->{'bmc'} =~ /\d+\.\d+\.\d+\.\d+/) {
unless ($predefips{ $ipmient->{'bmc'} }) {
$usedips{ $ipmient->{'bmc'} } = 1;
}
} else {
$node2bmc{ $ipmient->{'node'} } = $ipmient->{'bmc'};
}
}
}
}
foreach my $host (@hostsent) {
# handle the case that bmc has an entry in the hosts table
my $nodeip = xCAT::NetworkUtils->getipaddr($host->{'node'});
if ($nodeip) {
unless ($predefips{$nodeip}) {
$usedips{$nodeip} = 1;
}
} else {
if (defined($host->{'ip'}) && !$predefips{ $host->{'ip'} }) {
$usedips{ $host->{'ip'} } = 1;
}
}
# handle the case that the bmc<->ip mapping is specified in hosts.otherinterfaces
if (defined($node2bmc{ $host->{'node'} })) {
my $bmcip = xCAT::NetworkUtils->getipaddr($node2bmc{ $host->{'node'} });
if ($bmcip) {
unless ($predefips{$bmcip}) {
$usedips{$bmcip} = 1;
}
} else {
if (defined($host->{'otherinterfaces'})) {
my $bmcip = getbmcip_otherinterfaces($host->{'node'}, $node2bmc{ $host->{'node'} }, $host->{'otherinterfaces'});
unless ($predefips{$bmcip}) {
$usedips{$bmcip} = 1;
}
}
}
}
}
} elsif ($type eq "host") {
# get the bmc ip for all predefind nodes from $freenode
%predefips = %{ getpredefips($freenode, "host") };
# get all the used ips, the predefined ip should be ignored
my @hostsent = $hoststb->getAllNodeAttribs([ 'node', 'ip' ]);
foreach my $host (@hostsent) {
my $nodeip = xCAT::NetworkUtils->getipaddr($host->{'node'});
if ($nodeip) {
unless ($predefips{$nodeip}) {
$usedips{$nodeip} = 1;
}
} else {
if (defined($host->{'ip'}) && !$predefips{ $host->{'ip'} }) {
$usedips{ $host->{'ip'} } = 1;
}
}
}
}
# to calculate the free IP. free ip = 'ip in the range' - 'used ip'
if ($iprange =~ /(\d+\.\d+\.\d+\.\d+)-(\d+\.\d+\.\d+\.\d+)/) {
# if ip range format is 192.168.1.0-192.168.1.200
my ($startip, $endip) = ($1, $2);
my $startnum = xCAT::NetworkUtils->ip_to_int($startip);
my $endnum = xCAT::NetworkUtils->ip_to_int($endip);
while ($startnum <= $endnum) {
my $ip = xCAT::NetworkUtils->int_to_ip($startnum);
unless ($usedips{$ip}) {
push @freeips, $ip;
unless ($all) { last; }
}
$startnum++;
}
} elsif ($iprange) {
# use the noderange to expand the range
my @ips = noderange($iprange, 0);
foreach my $ip (@ips) {
unless ($usedips{$ip}) {
push @freeips, $ip;
unless ($all) { last; }
}
}
} else {
# only find the ip which mapping to the node
}
unless (@freeips) {
return;
}
if ($all) {
return @freeips;
} else {
return $freeips[0];
}
}
=head3 getbmcip_otherinterfaces
Parse the value in the hosts.otherinterfaces
arg1 - node
arg2 - bmc name
arg3 - value in the hosts.otherinterfaces
return: the ip of the node <node>-bmc
=cut
sub getbmcip_otherinterfaces
{
my $node = shift;
my $bmc = shift;
my $otherinterfaces = shift;
my @itf_pairs = split(/,/, $otherinterfaces);
foreach (@itf_pairs)
{
my ($itf, $ip);
if ($_ =~ /!/) {
($itf, $ip) = split(/!/, $_);
} else {
($itf, $ip) = split(/:/, $_);
}
if ($itf =~ /^-/)
{
$itf = $node . $itf;
}
if ($itf eq $bmc) {
return xCAT::NetworkUtils->getipaddr($ip);
}
}
return;
}
1;