From 46ece568f6102a15be2076cb511699d13dad79d3 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 5 Sep 2017 19:54:57 +0100 Subject: [PATCH] implement runner?! --- lib/App/Netdisco/Worker/Runner.pm | 60 +++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/App/Netdisco/Worker/Runner.pm b/lib/App/Netdisco/Worker/Runner.pm index 1d2a6e72..056a1a0c 100644 --- a/lib/App/Netdisco/Worker/Runner.pm +++ b/lib/App/Netdisco/Worker/Runner.pm @@ -15,6 +15,30 @@ sub run { my ($self, $job) = @_; die 'bad job to run()' unless ref $job eq 'App::Netdisco::Backend::Job'; + my @newuserconf = (); + my @userconf = @{ setting('device_auth') || [] }; + + # reduce device_auth by only/no + if (ref $job->device) { + foreach my $stanza (@userconf) { + my $no = (exists $stanza->{no} ? $stanza->{no} : undef); + my $only = (exists $stanza->{only} ? $stanza->{only} : undef); + + next if $no and check_acl_no($job->device->ip, $no); + next if $only and not check_acl_only($job->device->ip, $only); + + push @newuserconf, $stanza; + } + } + + # per-device action but no device creds available + return Status->error('skipped with no device creds') + if ref $job->device and 0 == scalar @newuserconf; + + # back up and restore device_auth + my $guard = guard { set(device_auth => \@userconf) }; + set(device_auth => \@newuserconf); + my $action = $job->action; my @phase_hooks = grep { m/^nd2worker_${action}_/ } @{ (setting('_nd2worker_hooks') || []) }; @@ -43,20 +67,34 @@ sub _run_first { $store->hook_is_registered($hook) or return Status->error("no such hook: $hook"); - foreach my $worker (@{ $store->get_hooks_for($hook) }) { - my $retval = false; - try { - $retval = $worker->($job); - } - catch { - $retval = Status->error($_); - }; + # track returned status of the workers + my $best = Status->error("no primary worker at $hook was successful"); - $retval ||= Status->done('no status supplied'); - return $retval if $retval->is_ok; + foreach my $worker (@{ $store->get_hooks_for($hook) }) { + try { + my $retval = $worker->($job); + # could die or return undef or a scalar or Status or another class + $best = $retval if ref $retval eq 'App::Netdisco::Worker::Status'; + } + catch { $best->log($_) }; + + last if $best->is_ok; } - return Status->error('no worker was successful'); + return $best; +} + +sub _run_all { + my $hook = shift or return Status->error('missing hook param'); + my $job = shift or return Status->error('missing job param'); + + my $store = Dancer::Factory::Hook->instance(); + $store->hook_is_registered($hook) + or return Status->error("no such hook: $hook"); + + foreach my $worker (@{ $store->get_hooks_for($hook) }) { + try { $worker->($job) }; + } } true;