diff --git a/xCAT-client/bin/rcons b/xCAT-client/bin/rcons index 7a432c08c..fd10fa4d4 100755 --- a/xCAT-client/bin/rcons +++ b/xCAT-client/bin/rcons @@ -149,7 +149,8 @@ elif [ $USE_GOCONSERVER == "1" ]; then CONGO_SSL_CERT=$HOME/.xcat/client-cred.pem \ CONGO_SSL_CA_CERT=$HOME/.xcat/ca.pem \ CONGO_PORT=12430 \ - CONGO_CLIENT_TYPE=xcat" + CONGO_CLIENT_TYPE=xcat \ + CONGO_SSL_INSECURE=true" if [ "$CONSERVER" == `hostname` ]; then host=`hostname -s` if [ $? -ne 0 ]; then diff --git a/xCAT-server/lib/perl/xCAT/Goconserver.pm b/xCAT-server/lib/perl/xCAT/Goconserver.pm index f1b5cc0a0..7f281cde4 100644 --- a/xCAT-server/lib/perl/xCAT/Goconserver.pm +++ b/xCAT-server/lib/perl/xCAT/Goconserver.pm @@ -15,8 +15,17 @@ use HTTP::Request; use HTTP::Headers; use LWP; use JSON; +use File::Path; use IO::Socket::SSL qw( SSL_VERIFY_PEER ); +my $go_api_port = 12429; +my $go_cons_port = 12430; + +use constant CONSOLE_LOG_DIR => "/var/log/consoles"; +unless (-d CONSOLE_LOG_DIR) { + mkpath(CONSOLE_LOG_DIR, 0, 0755); +} + sub http_request { my ($method, $url, $data) = @_; my @user = getpwuid($>); @@ -27,6 +36,7 @@ sub http_request { SSL_cert_file => xCAT::Utils->getHomeDir() . "/.xcat/client-cred.pem", SSL_ca_file => xCAT::Utils->getHomeDir() . "/.xcat/ca.pem", SSL_use_cert => 1, + verify_hostname => 0, SSL_verify_mode => SSL_VERIFY_PEER, }, ); my $header = HTTP::Headers->new('Content-Type' => 'application/json'); # $data = encode_json $data if defined($data); @@ -105,4 +115,246 @@ sub create_nodes { return $ret; } +#------------------------------------------------------------------------------- + +=head3 is_xcat_conf_ready + Check if the goconserver configuration file was generated by xcat + + Returns: + 1 - ready + 0 - not ready + Globals: + none + Example: + my $ready=(xCAT::Goconserver::is_xcat_conf_ready() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub is_xcat_conf_ready { + my $file; + open $file, '<', "/etc/goconserver/server.conf"; + my $line = <$file>; + close $file; + if ($line =~ /#generated by xcat/) { + return 1; + } + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 is_goconserver_running + Check if the goconserver service is running + + Returns: + 1 - running + 0 - not running + Globals: + none + Example: + my $running=(xCAT::Goconserver::is_goconserver_running() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub is_goconserver_running { + my $cmd = "ps axf | grep -v grep | grep \/usr\/bin\/goconserver"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + return 0; + } + return 1; +} + +#------------------------------------------------------------------------------- + +=head3 is_conserver_running + Check if the conserver service is running + + Returns: + 1 - running + 0 - not running + Globals: + none + Example: + my $running=(xCAT::Goconserver::is_conserver_running() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub is_conserver_running { + # On ubuntu system 'service conserver status' can not get the correct status of conserver, + # use 'pidof conserver' like what we did in rcons. + my $cmd = "pidof conserver"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC == 0) { + return 1; + } + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 build_conf + generate configuration file for goconserver + + Returns: + none + Globals: + none + Example: + my $running=(xCAT::Goconserver::build_conf() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub build_conf { + # TODO(chenglch): add -f options to support import the configuration file customized by the user, + # it would be friendly to the version control system. + my $config= "#generated by xcat ".xCAT::Utils->Version()."\n". + "global:\n". + " host: 0.0.0.0\n". + " ssl_key_file: /etc/xcat/cert/server-cred.pem\n". + " ssl_cert_file: /etc/xcat/cert/server-cred.pem\n". + " ssl_ca_cert_file: /etc/xcat/cert/ca.pem\n". + " logfile: /var/log/goconserver/server.log # the log for goconserver\n". + "api:\n". + " port: $go_api_port # the port for rest api\n". + "console:\n". + " datadir: /var/lib/goconserver/ # the data file to save the hosts\n". + " port: $go_cons_port # the port for console\n". + " logdir: ".CONSOLE_LOG_DIR." # log files for session nodes\n". + " log_timestamp: true # log the timestamp at the beginning of line\n". + " reconnect_interval: 10 # retry interval in second if console could not be connected\n"; + my $file; + my $ret = open ($file, '>', '/etc/goconserver/server.conf'); + if ($ret == 0) { + xCAT::MsgUtils->message("S", "Could not open file /etc/goconserver/server.conf"); + return 1; + } + print $file $config; + close $file; + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 start_service + start goconserver service + + Returns: + none + Globals: + none + Example: + my $running=(xCAT::Goconserver::start_service() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub start_service { + my $cmd = "service goconserver start"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("S", "Could not start goconserver service."); + return 1; + } + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 stop_service + stop goconserver service + + Returns: + none + Globals: + none + Example: + my $ret=(xCAT::Goconserver::stop_service() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub stop_service { + my $cmd = "service goconserver stop"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("S", "Could not stop goconserver service."); + return 1; + } + return 0; +} + +#------------------------------------------------------------------------------- + +=head3 stop_conserver_service + stop conserver service + + Returns: + none + Globals: + none + Example: + my $ret=(xCAT::Goconserver::stop_conserver_service() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub stop_conserver_service { + my $cmd = "service conserver stop"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("S", "Could not stop conserver service."); + return 1; + } + return 0; +} +#------------------------------------------------------------------------------- + +=head3 restart_service + restart goconserver service + + Returns: + none + Globals: + none + Example: + my $ret=(xCAT::Goconserver::restart_service() + Comments: + none + +=cut + +#------------------------------------------------------------------------------- +sub restart_service { + my $cmd = "service goconserver restart"; + xCAT::Utils->runcmd($cmd, -1); + if ($::RUNCMD_RC != 0) { + xCAT::MsgUtils->message("S", "Could not restart goconserver service."); + return 1; + } + return 0; +} + + +sub get_api_port { + return $go_api_port; +} 1; \ No newline at end of file diff --git a/xCAT-server/lib/xcat/plugins/AAsn.pm b/xCAT-server/lib/xcat/plugins/AAsn.pm index 4bef1e5e0..e941c12fc 100755 --- a/xCAT-server/lib/xcat/plugins/AAsn.pm +++ b/xCAT-server/lib/xcat/plugins/AAsn.pm @@ -152,6 +152,9 @@ sub init_plugin $rc = xCAT::Utils->setupAIXconserver(); } + } elsif ($servicelist->{"conserver"} == 2) + { + &setup_GOCONS(); } if (($servicelist->{"nameserver"} == 1) || ($servicelist->{"nameserver"} == 2)) { @@ -488,15 +491,17 @@ sub setup_CONS { my ($nodename) = @_; my $rc = 0; + my $cmdref; if (-x "/usr/bin/goconserver") { - my $cmd = "ps axf | grep -v grep | grep \/usr\/bin\/goconserver"; - xCAT::Utils->runcmd($cmd, 0); - if ($::RUNCMD_RC == 0) { - xCAT::MsgUtils->message("S", "INFO: goconserver was started, do nothing for conserver."); - return 0; + require xCAT::Goconserver; + if (xCAT::Goconserver::is_goconserver_running()) { + $rc = xCAT::Goconserver::stop_service(); + if($rc) { + xCAT::MsgUtils->message("S", "Error: Failed to stop goconserver service."); + return 1; + } } } - my $cmdref; $cmdref->{command}->[0] = "makeconservercf"; $cmdref->{arg}->[0] = "-l"; $cmdref->{cwd}->[0] = "/opt/xcat/sbin"; @@ -538,6 +543,55 @@ sub setup_CONS #----------------------------------------------------------------------------- +=head3 setup_GOCONS + + Sets up goconserver + +=cut + +#----------------------------------------------------------------------------- +sub setup_GOCONS +{ + my ($running, $ready, $ret); + unless (-x "/usr/bin/goconserver") { + xCAT::MsgUtils->message("S", "Error: goconserver is not installed."); + return 1; + } + require xCAT::Goconserver; + # if goconserver is installed, check the status of conserver service. + if (xCAT::Goconserver::is_conserver_running()) { + xCAT::MsgUtils->message("S", "conserver is started, stopping it."); + $ret = xCAT::Goconserver::stop_conserver_service(); + if ($ret) { + xCAT::MsgUtils->message("S", "Error: failed to stop conserver service."); + return 1; + } + } + $running = xCAT::Goconserver::is_goconserver_running(); + $ready = xCAT::Goconserver::is_xcat_conf_ready(); + if ( $running && $ready ) { + # Already started by xcat + return 0; + } + # user could customize the configuration, do not rewrite the configuration if this file has been + # generated by xcat + if (!$ready) { + $ret = xCAT::Goconserver::build_conf(); + if ($ret) { + xCAT::MsgUtils->message("S", "Error: failed to create configuration file for goconserver."); + return 1; + } + } + $ret = xCAT::Goconserver::restart_service(); + if ($ret) { + xCAT::MsgUtils->message("S", "Error: failed to start goconserver service."); + return 1; + } + return 0; +} + +#----------------------------------------------------------------------------- + =head3 setup_DHCP Sets up DHCP services diff --git a/xCAT-server/lib/xcat/plugins/conserver.pm b/xCAT-server/lib/xcat/plugins/conserver.pm index d54cbe10d..db0473f76 100644 --- a/xCAT-server/lib/xcat/plugins/conserver.pm +++ b/xCAT-server/lib/xcat/plugins/conserver.pm @@ -203,9 +203,8 @@ sub process_request { my $cb = shift; if ($req->{command}->[0] eq "makeconservercf") { if (-x "/usr/bin/goconserver") { - my $cmd = "ps axf | grep -v grep | grep \/usr\/bin\/goconserver"; - xCAT::Utils->runcmd($cmd, 0); - if ($::RUNCMD_RC == 0) { + require xCAT::Goconserver; + if (xCAT::Goconserver::is_goconserver_running()) { my $rsp->{data}->[0] = "goconserver is started, please stop it at first."; xCAT::MsgUtils->message("E", $rsp, $cb); return; diff --git a/xCAT-server/lib/xcat/plugins/goconserver.pm b/xCAT-server/lib/xcat/plugins/goconserver.pm index 0fcf0d141..2cc470b2e 100644 --- a/xCAT-server/lib/xcat/plugins/goconserver.pm +++ b/xCAT-server/lib/xcat/plugins/goconserver.pm @@ -17,8 +17,6 @@ use Data::Dumper; my $isSN; my $host; -my $go_api_port = 12429; -my $go_cons_port = 12430; my $bmc_cons_port = "2200"; my $usage_string =" makegocons [-V|--verbose] [-d|--delete] noderange -h|--help Display this usage statement. @@ -56,8 +54,6 @@ sub preprocess_request { #$Getopt::Long::pass_through=1; if (!GetOptions( - 'c|conserver' => \$::CONSERVER, - 'l|local' => \$::LOCAL, 'h|help' => \$::HELP, 'D|debug' => \$::DEBUG, 'v|version' => \$::VERSION, @@ -75,19 +71,6 @@ sub preprocess_request { $request = {}; return; } - if ($::LOCAL) { - if ($noderange && @$noderange > 0) { - $::callback->({ data => "Invalid option -l or --local when there are nodes specified." }); - $request = {}; - return; - } - } - if ($::CONSERVER && $::LOCAL) { - $::callback->({ data => "Can not specify -l or --local together with -c or --conserver." }); - $request = {}; - return; - } - # get site master my $master = xCAT::TableUtils->get_site_Master(); @@ -122,42 +105,14 @@ sub preprocess_request { push @nodes, $_->{node}; } - #send all nodes to the MN - if (!$isSN && !$::CONSERVER) { #If -c flag is set, do not add the all nodes to the management node - if ($::VERBOSE) { - my $rsp; - $rsp->{data}->[0] = "Setting the nodes into goconserver on the management node"; - xCAT::MsgUtils->message("I", $rsp, $::callback); - } - my $reqcopy = {%$request}; - $reqcopy->{'_xcatdest'} = $master; - $reqcopy->{_xcatpreprocessed}->[0] = 1; - $reqcopy->{'_allnodes'} = $allnodes; # the original command comes with nodes or not - if ($allnodes == 1) { @nodes = (); } - $reqcopy->{node} = \@nodes; - push @requests, $reqcopy; - if ($::LOCAL) { return \@requests; } - } - # send to conserver hosts - foreach my $cons (keys %cons_hash) { - - #print "cons=$cons\n"; - my $doit = 0; - if ($isSN) { - if (exists($iphash{$cons})) { $doit = 1; } - } else { - if (!exists($iphash{$cons}) || $::CONSERVER) { $doit = 1; } - } - - if ($doit) { - my $reqcopy = {%$request}; - $reqcopy->{'_xcatdest'} = $cons; - $reqcopy->{_xcatpreprocessed}->[0] = 1; - $reqcopy->{'_allnodes'} = [$allnodes]; # the original command comes with nodes or not - $reqcopy->{node} = $cons_hash{$cons}{nodes}; - push @requests, $reqcopy; - } #end if + foreach my $host (keys %cons_hash) { + my $reqcopy = {%$request}; + $reqcopy->{'_xcatdest'} = $host; + $reqcopy->{_xcatpreprocessed}->[0] = 1; + $reqcopy->{'_allnodes'} = [$allnodes]; # the original command comes with nodes or not + $reqcopy->{node} = $cons_hash{$host}{nodes}; + push @requests, $reqcopy; } #end foreach if ($::DEBUG) { @@ -308,54 +263,44 @@ sub gen_request_data { return $data; } - sub start_goconserver { - my $rsp; + my ($rsp, $running, $ready, $ret); unless (-x "/usr/bin/goconserver") { $rsp->{data}->[0] = "goconserver is not installed."; xCAT::MsgUtils->message("E", $rsp, $::callback); return 1; } - # As conserver is always installed, we check the existence of goconserver at first. # if goconserver is installed, check the status of conserver service. - my $cmd = "ps axf | grep -v grep | grep \/usr\/sbin\/conserver"; - xCAT::Utils->runcmd($cmd, 0); - if ($::RUNCMD_RC == 0) { + if (xCAT::Goconserver::is_conserver_running()) { $rsp->{data}->[0] = "conserver is started, please stop it at first."; xCAT::MsgUtils->message("E", $rsp, $::callback); return 1; } - $cmd = "ps axf | grep -v grep | grep \/usr\/bin\/goconserver"; - xCAT::Utils->runcmd($cmd, 0); - if ($::RUNCMD_RC != 0) { - my $config= "global:\n". - " host: 0.0.0.0\n". - " ssl_key_file: /etc/xcat/cert/server-key.pem\n". - " ssl_cert_file: /etc/xcat/cert/server-cert.pem\n". - " ssl_ca_cert_file: /etc/xcat/cert/ca.pem\n". - " logfile: /var/log/goconserver/server.log\n". - "api:\n". - " port: $go_api_port\n". - "console:\n". - " port: $go_cons_port\n"; - my $file; - my $ret = open ($file, '>', '/etc/goconserver/server.conf'); - if ($ret == 0) { - $rsp->{data}->[0] = "Could not open file /etc/goconserver/server.conf."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - print $file $config; - close $file; - my $cmd = "service goconserver start"; - xCAT::Utils->runcmd($cmd, 0); - if ($::RUNCMD_RC != 0) { - $rsp->{data}->[0] = "Could not start goconserver service."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - sleep(3); + $running = xCAT::Goconserver::is_goconserver_running(); + $ready = xCAT::Goconserver::is_xcat_conf_ready(); + if ( $running && $ready ) { + # Already started by xcat + return 0; } + # user could customize the configuration, do not rewrite the configuration if this file has been + # generated by xcat + if (!$ready) { + $ret = xCAT::Goconserver::build_conf(); + if ($ret) { + $rsp->{data}->[0] = "Failed to create configuration file for goconserver."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; + } + } + $ret = xCAT::Goconserver::restart_service(); + if ($ret) { + $rsp->{data}->[0] = "Failed to start goconserver service."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; + } + $rsp->{data}->[0] = "Starting goconserver service ..."; + xCAT::MsgUtils->message("I", $rsp, $::callback); + sleep(3); return 0; } @@ -406,7 +351,7 @@ sub makegocons { xCAT::SvrUtils::sendmsg([ 1, "Could not generate the request data" ], $::callback); return 1; } - my $api_url = "https://$host:$go_api_port"; + my $api_url = "https://$host:". xCAT::Goconserver::get_api_port(); $ret = xCAT::Goconserver::delete_nodes($api_url, $data, $delmode, $::callback); if ($delmode) { return $ret;