« blib/lib/App/Netdisco/Web/Plugin/AdminTask/NodeMonitor.pm
↑ Summary
blib/lib/App/Netdisco/Web/Metrics.pm »
| 1 | package App::Netdisco::Worker::Plugin::Hook::Exec; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use MIME::Base64 'decode_base64'; | ||
| 8 | use Command::Runner; | ||
| 9 | use Template; | ||
| 10 | |||
| 11 | # a string cmd goes through the shell: only ip and ndo may be substituted | ||
| 12 | sub render_shell_cmd { | ||
| 13 | my ($orig_cmd, $event_data) = @_; | ||
| 14 | my $tt = Template->new({ ENCODING => 'utf8', STRICT => 1 }); | ||
| 15 | my $shell_data = { map {( $_ => $event_data->{$_} )} qw/ndo ip/ }; | ||
| 16 | |||
| 17 | my $cmd = undef; | ||
| 18 | return ($cmd, undef) if $tt->process(\$orig_cmd, $shell_data, \$cmd); | ||
| 19 | return (undef, sprintf '%s; a string cmd may use only [%% ip %%] and [%% ndo %%], ' | ||
| 20 | .'write cmd as a list to pass other event fields', $tt->error); | ||
| 21 | } | ||
| 22 | |||
| 23 | register_worker({ phase => 'main' }, sub { | ||
| 24 | my ($job, $workerconf) = @_; | ||
| 25 | my $extra = from_json( decode_base64( $job->extra || '' ) ); | ||
| 26 | |||
| 27 | my $event_data = { ('ndo' => $ENV{NETDISCO_DO}), %{ $extra->{'event_data'} || {} } }; | ||
| 28 | my $action_conf = $extra->{'action_conf'}; | ||
| 29 | |||
| 30 | return Status->error('missing cmd parameter to exec Hook') | ||
| 31 | if !defined $action_conf->{'cmd'}; | ||
| 32 | |||
| 33 | my $tt = Template->new({ ENCODING => 'utf8' }); | ||
| 34 | my ($orig_cmd, $cmd) = ($action_conf->{'cmd'}, undef); | ||
| 35 | $action_conf->{'cmd_is_template'} ||= 1 | ||
| 36 | if !exists $action_conf->{'cmd_is_template'}; | ||
| 37 | if ($action_conf->{'cmd_is_template'}) { | ||
| 38 | if (ref $orig_cmd) { | ||
| 39 | foreach my $part (@$orig_cmd) { | ||
| 40 | my $tmp_part = undef; | ||
| 41 | $tt->process(\$part, $event_data, \$tmp_part); | ||
| 42 | push @$cmd, $tmp_part; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | my $error = undef; | ||
| 47 | ($cmd, $error) = render_shell_cmd($orig_cmd, $event_data); | ||
| 48 | return Status->error("exec Hook: $error") if defined $error; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | $cmd ||= $orig_cmd; | ||
| 52 | |||
| 53 | my $result = Command::Runner->new( | ||
| 54 | command => $cmd, | ||
| 55 | timeout => ($action_conf->{'timeout'} || 60), | ||
| 56 | env => { | ||
| 57 | %ENV, | ||
| 58 | ND_EVENT => $action_conf->{'event'}, | ||
| 59 | ND_DEVICE_IP => $event_data->{'ip'}, | ||
| 60 | }, | ||
| 61 | )->run(); | ||
| 62 | |||
| 63 | $result->{cmd} = $cmd; | ||
| 64 | $job->subaction(to_json($result)); | ||
| 65 | |||
| 66 | if ($action_conf->{'ignore_failure'} or not $result->{'result'}) { | ||
| 67 | return Status->done(sprintf 'Exec Hook: exit status %s', $result->{'result'}); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | return Status->error(sprintf 'Exec Hook: exit status %s', $result->{'result'}); | ||
| 71 | } | ||
| 72 | }); | ||
| 73 | |||
| 74 | true; |