From e86d5442422b9fbe533ee61a382b721bef98aebf Mon Sep 17 00:00:00 2001 From: chenglch Date: Wed, 28 Feb 2018 14:46:50 +0800 Subject: [PATCH] Add makegocons -C|--cleanup to remove entries that do not exist `rmdef` command do not help clean up the recousrces that related to the node when removing the node. this is a technical debt. This patch is only a work around to add -C|--cleanup option to help remove the entries in goconserver. --- .../admin-guides/references/man1/rmdef.1.rst | 2 +- .../references/man8/makegocons.8.rst | 8 ++ xCAT-client/pods/man1/rmdef.1.pod | 2 +- xCAT-client/pods/man8/makegocons.8.pod | 5 ++ xCAT-server/lib/perl/xCAT/Goconserver.pm | 78 ++++++++++++++++++- xCAT-server/lib/xcat/plugins/DBobjectdefs.pm | 6 ++ xCAT-server/lib/xcat/plugins/goconserver.pm | 61 +++------------ 7 files changed, 110 insertions(+), 52 deletions(-) diff --git a/docs/source/guides/admin-guides/references/man1/rmdef.1.rst b/docs/source/guides/admin-guides/references/man1/rmdef.1.rst index 221319ff5..e2190cf6b 100644 --- a/docs/source/guides/admin-guides/references/man1/rmdef.1.rst +++ b/docs/source/guides/admin-guides/references/man1/rmdef.1.rst @@ -90,7 +90,7 @@ OPTIONS \ **-C|-**\ **-cleanup**\ - Perform additional cleanup by running \ **nodeset offline**\ and \ **makeconservercf -d**\ on the objects specified in the \ *noderange*\ . + Perform additional cleanup by running \ **nodeset offline**\, \ **makeconservercf -d**\ and \ **makegocons --cleanup**\ on the objects specified in the \ *noderange*\ . diff --git a/docs/source/guides/admin-guides/references/man8/makegocons.8.rst b/docs/source/guides/admin-guides/references/man8/makegocons.8.rst index 6ceff8ae6..b9ba5e686 100644 --- a/docs/source/guides/admin-guides/references/man8/makegocons.8.rst +++ b/docs/source/guides/admin-guides/references/man8/makegocons.8.rst @@ -21,6 +21,8 @@ SYNOPSIS \ **makegocons**\ [\ **-V|-**\ **-verbose**\ ] [\ **-d|-**\ **-delete**\ ] [\ **-q|-**\ **-query**\ ] [\ *noderange*\ ] +\ **makegocons**\ [\ **-V|-**\ **-verbose**\ ] [\ **-C|-**\ **-cleanup**\ ] + *********** DESCRIPTION @@ -70,6 +72,12 @@ OPTIONS +\ **-C|-**\ **-cleanup**\ + + Remove the entries for the nodes whose definitions have been removed from xCAT db. + + + \ **-q|-**\ **-query**\ List the console connection of the nodes. If noderange is not specified, all of the console nodes will be displayed. diff --git a/xCAT-client/pods/man1/rmdef.1.pod b/xCAT-client/pods/man1/rmdef.1.pod index 38b67b077..347cf18a1 100644 --- a/xCAT-client/pods/man1/rmdef.1.pod +++ b/xCAT-client/pods/man1/rmdef.1.pod @@ -58,7 +58,7 @@ A set of comma delimited object types. =item B<-C|--cleanup> -Perform additional cleanup by running B and B on the objects specified in the I. +Perform additional cleanup by running B, B and B on the objects specified in the I. =item B<-V|--verbose> diff --git a/xCAT-client/pods/man8/makegocons.8.pod b/xCAT-client/pods/man8/makegocons.8.pod index ca17bb122..9d69e3d3d 100644 --- a/xCAT-client/pods/man8/makegocons.8.pod +++ b/xCAT-client/pods/man8/makegocons.8.pod @@ -6,6 +6,7 @@ B - Register or unregister the node in the goconserver service B [B<-V|--verbose>] [B<-d|--delete>] [B<-q|--query>] [I] +B [B<-V|--verbose>] [B<-C|--cleanup>] =head1 DESCRIPTION @@ -48,6 +49,10 @@ B goconserver only support the systemd based systems. It has been integra Delete rather than add or refresh the nodes specified as a noderange. +=item B<-C|--cleanup> + +Remove the entries for the nodes whose definitions have been removed from xCAT db. + =item B<-q|--query> List the console connection of the nodes. If noderange is not specified, all of the console nodes will be displayed. diff --git a/xCAT-server/lib/perl/xCAT/Goconserver.pm b/xCAT-server/lib/perl/xCAT/Goconserver.pm index 0e3906def..211808a2f 100644 --- a/xCAT-server/lib/perl/xCAT/Goconserver.pm +++ b/xCAT-server/lib/perl/xCAT/Goconserver.pm @@ -22,6 +22,7 @@ use IO::Socket::SSL qw( SSL_VERIFY_PEER ); my $go_api_port = 12429; my $go_cons_port = 12430; my $bmc_cons_port = "2200"; +my $isSN = xCAT::Utils->isServiceNode(); use constant CONSOLE_LOG_DIR => "/var/log/consoles"; use constant PRINT_FORMAT => "%-32s %-32s %-64s"; @@ -58,7 +59,7 @@ sub http_request { } sub gen_request_data { - my ($cons_map, $siteondemand, $isSN, $callback) = @_; + my ($cons_map, $siteondemand, $callback) = @_; my (@openbmc_nodes, $data); while (my ($k, $v) = each %{$cons_map}) { my $ondemand; @@ -397,6 +398,81 @@ sub list_nodes { return 0; } +sub cleanup_nodes { + my $callback = shift; + my @hostinfo = xCAT::NetworkUtils->determinehostname(); + my $host = $hostinfo[-1]; + my $api_url = "https://$host:". get_api_port(); + my $rsp; + my $response = http_request("GET", "$api_url/nodes"); + if (!defined($response)) { + if ($callback) { + $rsp->{data}->[0] = "Failed to send list request."; + xCAT::MsgUtils->message("E", $rsp, $callback); + } else { + xCAT::MsgUtils->message("S", "Failed to send list request."); + } + return 1; + } + if (!$response->{nodes}) { + return 0; + } + my %delete_map; + my %cons_map = get_cons_map(undef); + foreach my $node (@{$response->{nodes}}) { + # not in xcatdb but exist in goconserver + $delete_map{$node->{name}} = 1 if !exists($cons_map{$node->{name}}); + } + return delete_nodes($api_url, \%delete_map, 1, $callback); +} + +sub get_cons_map { + my $req = shift; + my %iphash = (); + my %cons_map; + my $hmtab = xCAT::Table->new('nodehm'); + my @cons_nodes; + my @hostinfo = xCAT::NetworkUtils->determinehostname(); + foreach (@hostinfo) { + $iphash{$_} = 1; + } + if (defined($req) && (($req->{node} and @{$req->{node}} > 0) or $req->{noderange}->[0])) { + # Note: do not consider terminal server currently + @cons_nodes = $hmtab->getNodesAttribs($req->{node}, [ 'node', 'cons', 'serialport', 'mgt', 'conserver', 'consoleondemand' ]); + # Adjust the data structure to make the result consistent with the getAllNodeAttribs() call we make if a noderange was not specified + my @tmpcons_nodes; + foreach my $ent (@cons_nodes) + { + foreach my $nodeent (keys %$ent) + { + push @tmpcons_nodes, $ent->{$nodeent}->[0]; + } + } + @cons_nodes = @tmpcons_nodes + } else { + @cons_nodes = $hmtab->getAllNodeAttribs([ 'cons', 'serialport', 'mgt', 'conserver', 'consoleondemand' ]); + } + $hmtab->close(); + my $rsp; + + foreach (@cons_nodes) { + if ($_->{cons} or defined($_->{'serialport'})) { + unless ($_->{cons}) { $_->{cons} = $_->{mgt}; } #populate with fallback + if ($isSN && $_->{conserver} && exists($iphash{ $_->{conserver} }) || !$isSN) { + $cons_map{ $_->{node} } = $_; # also put the ref to the entry in a hash for quick look up + } else { + $rsp->{data}->[0] = $_->{node} .": ignore, the host for conserver could not be determined."; + xCAT::MsgUtils->message("I", $rsp, $::callback); + } + } else { + $rsp->{data}->[0] = $_->{node} .": ignore, cons attribute or serialport attribute is not specified."; + xCAT::MsgUtils->message("I", $rsp, $::callback); + } + } + return %cons_map; +} + + #------------------------------------------------------------------------------- =head3 is_xcat_conf_ready diff --git a/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm b/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm index 0ca5df5c5..221c12819 100755 --- a/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm +++ b/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm @@ -4423,6 +4423,12 @@ sub defrm command => ['makeconservercf'], node => [@allnodes], arg => ['-d'],}, $doreq, 0, 1); + if (-x "/usr/bin/goconserver") { + require xCAT::Goconserver; + if (xCAT::Goconserver::is_goconserver_running()) { + xCAT::Goconserver::cleanup_nodes(undef); + } + } } } diff --git a/xCAT-server/lib/xcat/plugins/goconserver.pm b/xCAT-server/lib/xcat/plugins/goconserver.pm index b3487e81a..a4c1e8ea2 100644 --- a/xCAT-server/lib/xcat/plugins/goconserver.pm +++ b/xCAT-server/lib/xcat/plugins/goconserver.pm @@ -20,6 +20,7 @@ my $host; my $usage_string =" makegocons [-V|--verbose] [-d|--delete] noderange -h|--help Display this usage statement. -v|--version Display the version number. + -C|--cleanup Remove the entries for the nodes whose definitions have been removed from xCAT db. -q|--query [noderange] Display the console connection status."; my $version_string = xCAT::Utils->Version(); @@ -138,50 +139,6 @@ sub process_request { } } -sub get_cons_map { - my ($req, $iphashref) = @_; - my %cons_map; - my %iphash = %{$iphashref}; - my $hmtab = xCAT::Table->new('nodehm'); - my @cons_nodes; - - if (($req->{node} and @{$req->{node}} > 0) or $req->{noderange}->[0]) { - # Note: do not consider terminal server currently - @cons_nodes = $hmtab->getNodesAttribs($req->{node}, [ 'node', 'cons', 'serialport', 'mgt', 'conserver', 'consoleondemand' ]); - # Adjust the data structure to make the result consistent with the getAllNodeAttribs() call we make if a noderange was not specified - my @tmpcons_nodes; - foreach my $ent (@cons_nodes) - { - foreach my $nodeent (keys %$ent) - { - push @tmpcons_nodes, $ent->{$nodeent}->[0]; - } - } - @cons_nodes = @tmpcons_nodes - - } else { - @cons_nodes = $hmtab->getAllNodeAttribs([ 'cons', 'serialport', 'mgt', 'conserver', 'consoleondemand' ]); - } - $hmtab->close(); - my $rsp; - - foreach (@cons_nodes) { - if ($_->{cons} or defined($_->{'serialport'})) { - unless ($_->{cons}) { $_->{cons} = $_->{mgt}; } #populate with fallback - if ($isSN && $_->{conserver} && exists($iphash{ $_->{conserver} }) || !$isSN) { - $cons_map{ $_->{node} } = $_; # also put the ref to the entry in a hash for quick look up - } else { - $rsp->{data}->[0] = $_->{node} .": ignore, the host for conserver could not be determined."; - xCAT::MsgUtils->message("I", $rsp, $::callback); - } - } else { - $rsp->{data}->[0] = $_->{node} .": ignore, cons attribute or serialport attribute is not specified."; - xCAT::MsgUtils->message("I", $rsp, $::callback); - } - } - return %cons_map; -} - sub start_goconserver { my ($rsp, $running, $ready, $ret); unless (-x "/usr/bin/goconserver") { @@ -233,18 +190,24 @@ sub makegocons { } @ARGV = @exargs; $Getopt::Long::ignorecase = 0; - my ($delmode, $querymode); + my ($delmode, $querymode, $cleanupmode); GetOptions('d|delete' => \$delmode, 'q|query' => \$querymode, + 'C|cleanup' => \$cleanupmode, ); my $svboot = 0; if (exists($req->{svboot})) { $svboot = 1; } - my %iphash = (); - foreach (@$hostinfo) { $iphash{$_} = 1; } - my %cons_map = get_cons_map($req, \%iphash); + if ($cleanupmode) { + if (exists($req->{_allnodes}) && $req->{_allnodes}->[0] != 1) { + xCAT::SvrUtils::sendmsg([ 1, "Can not specify noderange together with -C|--cleanup." ], $::callback); + return 1; + } + return xCAT::Goconserver::cleanup_nodes($::callback); + } + my %cons_map = xCAT::Goconserver::get_cons_map($req); if (! %cons_map) { xCAT::SvrUtils::sendmsg([ 1, "Could not get any console request entry" ], $::callback); return 1; @@ -271,7 +234,7 @@ sub makegocons { } } my (@nodes); - my $data = xCAT::Goconserver::gen_request_data(\%cons_map, $siteondemand, $isSN, $::callback); + my $data = xCAT::Goconserver::gen_request_data(\%cons_map, $siteondemand, $::callback); if (! $data) { xCAT::SvrUtils::sendmsg([ 1, "Could not generate the request data" ], $::callback); return 1;