Skip to content

Commit

Permalink
node-control: implement install/update cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 2, 2024
1 parent 6252775 commit 6b7a52a
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ sub _omd_install_step2 {
print "*** installing $version\n";
_set_job_started($c, 'installing', $peer->{'key'});

my $tversion = $version;
$tversion =~ s/omd-//gmx;
if(grep(/^$tversion/mx, @{$facts->{'omd_versions'}})) {
printf("*** omd %s already installed\n", $version);
_set_job_done($c, 'installing', $peer->{'key'});
return 1;
}

if(!$config->{'cmd_'.$facts->{'ansible_facts'}->{'ansible_pkg_mgr'}.'_pkg_install'}) {
return _set_job_errored($c, 'installing', $peer->{'key'}, "package manager ".$facts->{'ansible_facts'}->{'ansible_pkg_mgr'}." not supported");
}
Expand Down Expand Up @@ -566,6 +574,12 @@ sub _omd_update_step2 {

_set_job_started($c, 'updating', $peer->{'key'});

if($facts->{'omd_version'} eq $version) {
printf("*** already at omd %s\n", $version);
_set_job_done($c, 'updating', $peer->{'key'});
return 1;
}

if($config->{'hook_update_pre_local'}) {
print "*** hook_update_pre_local:\n";
my($rc, $out) = _local_run_hook($c, $config->{'hook_update_pre_local'}, $env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ The nodecontrol command can start node control commands.
available commands are:
- -l|list list available backends.
- facts <backendid|all> [-w|--worker=<nr>] update facts for given backend.
- runtime <backendid|all> [-w|--worker=<nr>] update runtime data for given backend.
- setversion <version> set new default omd version
- install <backendid|all> install default omd version for given backend.
- update <backendid|all> update default omd version for given backend.
- cleanup <backendid|all> cleanup unused omd versions for given backend.
- -l|list list available backends.
- facts <backendid|all> [-w|--worker=<nr>] update facts for given backend.
- runtime <backendid|all> [-w|--worker=<nr>] update runtime data for given backend.
- setversion <version> set new default omd version
- install <backendid|all> [--version=<version] install default omd version for given backend.
- update <backendid|all> [--version=<version] update default omd version for given backend.
- cleanup <backendid|all> cleanup unused omd versions for given backend.
=back
Expand All @@ -42,6 +42,7 @@ use Getopt::Long ();

use Thruk::Utils ();
use Thruk::Utils::CLI ();
use Thruk::Utils::External ();
use Thruk::Utils::Log qw/:all/;

##############################################
Expand Down Expand Up @@ -79,6 +80,7 @@ sub cmd {
# parse options
my $opt = {
'worker' => $config->{'parallel_tasks'} // 3,
'version' => $config->{'version'},
'mode_list' => 0,
};
Getopt::Long::Configure('no_ignore_case');
Expand All @@ -87,6 +89,7 @@ sub cmd {
Getopt::Long::GetOptionsFromArray($commandoptions,
"w|worker=i" => \$opt->{'worker'},
"l|list" => \$opt->{'mode_list'},
"version=s" => \$opt->{'version'},
) or do {
return(Thruk::Utils::CLI::get_submodule_help(__PACKAGE__));
};
Expand All @@ -107,6 +110,12 @@ sub cmd {
elsif($mode eq 'cleanup') {
return(_action_cleanup($c, $opt, $commandoptions));
}
elsif($mode eq 'install') {
return(_action_install($c, $opt, $commandoptions, $config));
}
elsif($mode eq 'update') {
return(_action_update($c, $opt, $commandoptions, $config));
}

$c->stats->profile(end => "_cmd_nc()");
return(Thruk::Utils::CLI::get_submodule_help(__PACKAGE__));
Expand Down Expand Up @@ -191,6 +200,82 @@ sub _action_facts {
return("", 0);
}

##############################################
sub _action_install {
my($c, $opt, $commandoptions, $config) = @_;

my $version = $opt->{'version'} || $config->{'omd_default_version'};
my $errors = 0;
my $peers = _get_selected_peers($c, $commandoptions);
for my $peer_key (@{$peers}) {
my $peer = $c->db->get_peer_by_key($peer_key);
local $ENV{'THRUK_LOG_PREFIX'} = sprintf("[%s] ", $peer->{'name'});
_debug("start installing...\n");
my($job) = Thruk::NodeControl::Utils::omd_install($c, $peer, $version);
if(!$job) {
_error("failed to start install");
$errors++;
next;
}
my $jobdata = Thruk::Utils::External::wait_for_peer_job($c, $peer, $job, 3, 1800);
if(!$jobdata) {
_error("failed to install");
$errors++;
next;
}
if($jobdata->{'rc'} ne '0') {
_error("failed to install\n");
_error("%s\n", $jobdata->{'stdout'}) if $jobdata->{'stdout'};
_error("%s\n", $jobdata->{'stderr'}) if $jobdata->{'stderr'};
$errors++;
next;
}
_info("%s\n", $jobdata->{'stdout'}) if $jobdata->{'stdout'};
_info("%s\n", $jobdata->{'stderr'}) if $jobdata->{'stderr'};
_info("%s install sucessfully: OK\n", $peer->{'name'});
}
$c->stats->profile(end => "_cmd_nc()");
return("", $errors > 0 ? 1 : 0);
}

##############################################
sub _action_update {
my($c, $opt, $commandoptions, $config) = @_;

my $version = $opt->{'version'} || $config->{'omd_default_version'};
my $errors = 0;
my $peers = _get_selected_peers($c, $commandoptions);
for my $peer_key (@{$peers}) {
my $peer = $c->db->get_peer_by_key($peer_key);
local $ENV{'THRUK_LOG_PREFIX'} = sprintf("[%s] ", $peer->{'name'});
_debug("start update...\n");
my($job) = Thruk::NodeControl::Utils::omd_update($c, $peer, $version);
if(!$job) {
_error("failed to start update");
$errors++;
next;
}
my $jobdata = Thruk::Utils::External::wait_for_peer_job($c, $peer, $job, 3, 1800);
if(!$jobdata) {
_error("failed to update");
$errors++;
next;
}
if($jobdata->{'rc'} ne '0') {
_error("failed to update\n");
_error("%s\n", $jobdata->{'stdout'}) if $jobdata->{'stdout'};
_error("%s\n", $jobdata->{'stderr'}) if $jobdata->{'stderr'};
$errors++;
next;
}
_info("%s\n", $jobdata->{'stdout'}) if $jobdata->{'stdout'};
_info("%s\n", $jobdata->{'stderr'}) if $jobdata->{'stderr'};
_info("%s update sucessfully: OK\n", $peer->{'name'});
}
$c->stats->profile(end => "_cmd_nc()");
return("", $errors > 0 ? 1 : 0);
}

##############################################
sub _action_cleanup {
my($c, $opt, $commandoptions) = @_;
Expand Down
20 changes: 19 additions & 1 deletion t/scenarios/lmd_federation_multitier_e2e/t/local/node-control.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strict;
use Test::More;

BEGIN {
plan tests => 55;
plan tests => 71;

use lib('t');
require TestUtils;
Expand Down Expand Up @@ -62,3 +62,21 @@ TestUtils::test_command({
cmd => '/usr/bin/env thruk nc cleanup tier2a',
errlike => ['/tier2a cleanup sucessfully/'],
});

TestUtils::test_command({
cmd => '/usr/bin/env thruk nc install tier2c',
errlike => ['/already installed/', '/tier2c install sucessfully/'],
});

# run update to test version
TestUtils::test_command({
cmd => '/usr/bin/env thruk nc update tier2c',
errlike => ['/updating demo on tier2c/', '/tier2c update sucessfully/', '/OMD_SITE=demo/', '/OMD_UPDATE=/'],
});

# and back...
my $omd_version = `omd version -b`; chomp($omd_version);
TestUtils::test_command({
cmd => '/usr/bin/env thruk nc update tier2c --version='.$omd_version,
errlike => ['/updating demo on tier2c/', '/tier2c update sucessfully/', '/OMD_SITE=demo/', '/OMD_UPDATE=/'],
});

0 comments on commit 6b7a52a

Please sign in to comment.