# # Copyright (c) 2017 NetApp, Inc., All Rights Reserved. # Any use, modification, or distribution is prohibited # without prior written consent from NetApp, Inc. # package NACL::Service::SMAgent; use strict; use warnings; use NATE::Inc qw(find_file); use parent qw(NATE::Service); sub init { my ($self, %opts) = @_; $self->SUPER::init(%opts); my $structure = { start_agent => { callback => \&start_agent, api_settings => {client_sync => 1}, }, stop_agent => { callback => \&stop_agent, api_settings => {client_sync => 1}, }, }; $self->api_register_bulk(namespace => "NACL::Service::SMAgent", structure => $structure); } sub start_agent { my ($self, %args) = @_; my $path = delete($args{SMAGENT_VERSION}) // "/x/eng/rlse/xplat/xplat_mainN"; $path =~ s/\/$//; my $executable = "$path/btrfs/smagent_btrfs"; if ($self->{_smagent_process} and $self->{_smagent_process}->alive()) { $self->log("Already started"); } else { #tail -f the log my $logpath = $args{logpath} // '/btrfs/SmaBtrfs.log'; $self->log("Starting smagent log tail '$logpath'"); my $tail_proc = $self->process_start( codespec => "tail", args => ['--lines=0', '-f', $logpath], runid => "smagent_log_tail"); $self->{_smagent_log_process} = $tail_proc; # if the executable is a relative path, resolve it to an absolute path # based on the workspace that is under test. my $executable = find_file($executable); $self->log("Starting agent '$executable'"); my $process = $self->process_start(codespec => $executable, runid => 'smagent', username => '', #force to run as stafproc user, likely root on_termination => [\&_on_terminate, $self]); $self->{_smagent_process} = $process; } return; } sub _on_terminate { my ($process_obj, $self) = @_; $self->log("The smagent process terminated, it could " . "have been because someone stopped it, or it " . "crashed, use the \$process_obj object to figure " . "out what caused it"); } sub stop_agent { my $self = shift; if ($self->{_smagent_process} and $self->{_smagent_process}->alive()) { $self->log("Stopping Agent"); $self->process_stop(child => $self->{_smagent_process}, hard => 1); } else { $self->log("Can't stop agent if it is not running"); } if ($self->{_smagent_log_process} and $self->{_smagent_log_process}->alive()) { $self->log("Stopping Agent log tail"); $self->process_stop(child => $self->{_smagent_log_process}, hard => 1); } return; } 1;