1package App::Netdisco::Worker::Plugin::Hook::Exec;
2
3use Dancer ':syntax';
4use App::Netdisco::Worker::Plugin;
5use aliased 'App::Netdisco::Worker::Status';
6
7use MIME::Base64 'decode_base64';
8use Command::Runner;
9use Template;
10
11register_worker({ phase => 'main' }, sub {
12  my ($job, $workerconf) = @_;
13  my $extra = from_json( decode_base64( $job->extra || '' ) );
14
15  my $event_data  = $extra->{'event_data'};
16  my $action_conf = $extra->{'action_conf'};
17
18  return Status->error('missing cmd parameter to exec Hook')
19    if !defined $action_conf->{'cmd'};
20
21  my $tt = Template->new({ ENCODING => 'utf8' });
22  my ($orig_cmd, $cmd) = ($action_conf->{'cmd'}, undef);
23  $action_conf->{'cmd_is_template'} ||= 1
24    if !exists $action_conf->{'cmd_is_template'};
25  if ($action_conf->{'cmd_is_template'}) {
26      if (ref $orig_cmd) {
27          foreach my $part (@$orig_cmd) {
28              my $tmp_part = undef;
29              $tt->process(\$part, $event_data, \$tmp_part);
30              push @$cmd, $tmp_part;
31          }
32      }
33      else {
34          $tt->process(\$orig_cmd, $event_data, \$cmd);
35      }
36  }
37  $cmd ||= $orig_cmd;
38
39  my $result = Command::Runner->new(
40    command => $cmd,
41    timeout => ($action_conf->{'timeout'} || 60),
42    env => {
43      %ENV,
44      ND_EVENT => $action_conf->{'event'},
45      ND_DEVICE_IP => $event_data->{'ip'},
46    },
47  )->run();
48
49  $result->{cmd} = $cmd;
50  $job->subaction(to_json($result));
51
52  if ($action_conf->{'ignore_failure'} or not $result->{'result'}) {
53    return Status->done(sprintf 'Exec Hook: exit status %s', $result->{'result'});
54  }
55  else {
56    return Status->error(sprintf 'Exec Hook: exit status %s', $result->{'result'});
57  }
58});
59
60true;
61