Skip to content

Commit

Permalink
node-control: run cleanup in extra job with separate logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Sep 24, 2024
1 parent 7c31b3f commit f543705
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ sub _node_action {

if($action eq 'cleanup') {
return unless Thruk::Utils::check_csrf($c);
return unless $config->{'pkg_cleanup'};
return($c->render(json => {'success' => 0, 'error' => "cleanup is disabled by config"})) unless $config->{'pkg_cleanup'};
my $job = Thruk::NodeControl::Utils::omd_cleanup($c, $peer);
return($c->render(json => {'success' => 1, job => $job})) if $job;
return($c->render(json => {'success' => 0, 'error' => "failed to start job"}));
}

if($action eq 'omd_install') {
return unless Thruk::Utils::check_csrf($c);
return unless $config->{'pkg_install'};
return($c->render(json => {'success' => 0, 'error' => "pkg installation is disabled by config"})) unless $config->{'pkg_install'};
my $job = Thruk::NodeControl::Utils::omd_install($c, $peer, $config->{'omd_default_version'});
return($c->render(json => {'success' => 1, job => $job})) if $job;
return($c->render(json => {'success' => 0, 'error' => "failed to start job"}));
Expand All @@ -175,15 +175,15 @@ sub _node_action {

if($action eq 'os_update') {
return unless Thruk::Utils::check_csrf($c);
return unless $config->{'os_updates'};
return($c->render(json => {'success' => 0, 'error' => "os updates are disabled by config"})) unless $config->{'os_updates'};
my $job = Thruk::NodeControl::Utils::os_update($c, $peer, $config->{'omd_default_version'});
return($c->render(json => {'success' => 1, job => $job})) if $job;
return($c->render(json => {'success' => 0, 'error' => "failed to start job"}));
}

if($action eq 'os_sec_update') {
return unless Thruk::Utils::check_csrf($c);
return unless $config->{'os_updates'};
return($c->render(json => {'success' => 0, 'error' => "os updates are disabled by config"})) unless $config->{'os_updates'};
my $job = Thruk::NodeControl::Utils::os_sec_update($c, $peer, $config->{'omd_default_version'});
return($c->render(json => {'success' => 1, job => $job})) if $job;
return($c->render(json => {'success' => 0, 'error' => "failed to start job" }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,23 @@ sub omd_cleanup {

my $file = $c->config->{'var_path'}.'/node_control/'.$peer->{'key'}.'.json';
my $f = Thruk::Utils::IO::json_lock_patch($file, { 'cleaning' => 1, 'last_error' => '' }, { pretty => 1, allow_empty => 1 });

# continue in background job
my $job = Thruk::Utils::External::perl($c, {
expr => 'Thruk::NodeControl::Utils::_omd_cleanup_step2($c, "'.$peer->{'key'}.'")',
message => 'running OMD cleanup',
background => 1,
log_archive => $c->config->{'var_path'}.'/node_control/'.$peer->{'key'}.'_cleanup.log',
});
return($job);
}

##########################################################
sub _omd_cleanup_step2 {
my($c, $peerkey) = @_;

my $peer = $c->db->get_peer_by_key($peerkey);
my $file = $c->config->{'var_path'}.'/node_control/'.$peer->{'key'}.'.json';
my $config = config($c);
my $cmd = _cmd_line($config->{'cmd_omd_cleanup'});

Expand All @@ -793,7 +810,7 @@ sub omd_cleanup {
($rc, $job) = _remote_cmd($c, $peer, $cmd, { message => 'Running OMD cleanup' });
};
if($@) {
$f = Thruk::Utils::IO::json_lock_patch($file, { 'cleaning' => 0, 'last_error' => $@ }, { pretty => 1, allow_empty => 1 });
Thruk::Utils::IO::json_lock_patch($file, { 'cleaning' => 0, 'last_error' => $@ }, { pretty => 1, allow_empty => 1 });
return;
}

Expand Down Expand Up @@ -929,6 +946,7 @@ sub config {
'ssh_fallback' => 1,
'os_updates' => 1,
'pkg_install' => 1,
'pkg_cleanup' => 1,
'parallel_tasks' => 3,
'omd_update_script' => abs_path(Thruk::Base::dirname(__FILE__)."/../../../scripts/omd_update.sh"),
'cmd_omd_cleanup' => 'sudo -n omd cleanup',
Expand Down

0 comments on commit f543705

Please sign in to comment.