| 1 | package App::Netdisco::Worker::Runner; | ||
| 2 | |||
| 3 | use Dancer qw/:moose :syntax/; | ||
| 4 | use Dancer::Plugin::DBIC 'schema'; | ||
| 5 | |||
| 6 | use App::Netdisco::Util::CustomFields; | ||
| 7 | use App::Netdisco::Transport::Python (); | ||
| 8 | use App::Netdisco::Util::Device 'get_device'; | ||
| 9 | use App::Netdisco::Util::SNMPText 'reset_snmp_charsets_cache'; | ||
| 10 | use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/; | ||
| 11 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 12 | |||
| 13 | use Try::Tiny; | ||
| 14 | use Time::HiRes (); | ||
| 15 | use Module::Load (); | ||
| 16 | use Scope::Guard 'guard'; | ||
| 17 | use Storable 'dclone'; | ||
| 18 | use Sys::SigAction 'timeout_call'; | ||
| 19 | |||
| 20 | use Moo::Role; | ||
| 21 | use namespace::clean; | ||
| 22 | |||
| 23 | with 'App::Netdisco::Worker::Loader'; | ||
| 24 | has 'job' => ( is => 'rw' ); | ||
| 25 | |||
| 26 | # mixin code to run workers loaded via plugins | ||
| 27 | sub run { | ||
| 28 | my ($self, $job) = @_; | ||
| 29 | |||
| 30 | die 'cannot reuse a worker' if $self->job; | ||
| 31 | die 'bad job to run()' | ||
| 32 | unless ref $job eq 'App::Netdisco::Backend::Job'; | ||
| 33 | |||
| 34 | $job->apply_config_overrides; | ||
| 35 | $self->job($job); | ||
| 36 | reset_snmp_charsets_cache(); | ||
| 37 | $job->device( get_device($job->device) ) | ||
| 38 | unless scalar grep {$job->action eq $_} @{ setting('job_targets_prefix') }; | ||
| 39 | $self->load_workers(); | ||
| 40 | |||
| 41 | # clean up and finalise job status when we exit | ||
| 42 | my $statusguard = guard { | ||
| 43 | if (var('live_python')) { | ||
| 44 | try { App::Netdisco::Transport::Python->runner->finish }; | ||
| 45 | try { App::Netdisco::Transport::Python->runner->kill_kill }; | ||
| 46 | try { unlink App::Netdisco::Transport::Python->context->filename }; | ||
| 47 | } | ||
| 48 | $job->finalise_status; | ||
| 49 | }; | ||
| 50 | |||
| 51 | my @newdeviceauthconf = (); | ||
| 52 | my @deviceauthconf = @{ dclone (setting('device_auth') || []) }; | ||
| 53 | |||
| 54 | # reduce device_auth by only/no and device_auth_tag_hint | ||
| 55 | if (ref $job->device) { | ||
| 56 | foreach my $stanza (@deviceauthconf) { | ||
| 57 | my $no = (exists $stanza->{no} ? $stanza->{no} : undef); | ||
| 58 | my $only = (exists $stanza->{only} ? $stanza->{only} : undef); | ||
| 59 | |||
| 60 | next if $no and acl_matches($job->device, $no); | ||
| 61 | next if $only and not acl_matches_only($job->device, $only); | ||
| 62 | |||
| 63 | push @newdeviceauthconf, dclone $stanza; | ||
| 64 | } | ||
| 65 | |||
| 66 | # per-device action but no device creds available | ||
| 67 | return $job->add_status( Status->defer('deferred job with no device creds') ) | ||
| 68 | if 0 == scalar @newdeviceauthconf && $self->transport_required; | ||
| 69 | |||
| 70 | # if there is a device_auth_tag_hint then promote it to the front | ||
| 71 | if (my $tag_hint = setting('device_auth_tag_hint')) { | ||
| 72 | my @hint_matches = grep { $_->{tag} and $_->{tag} eq $tag_hint } @newdeviceauthconf; | ||
| 73 | my @not_matches = grep { ! ($_->{tag} and $_->{tag} eq $tag_hint) } @newdeviceauthconf; | ||
| 74 | @newdeviceauthconf = (@hint_matches, @not_matches) if scalar @hint_matches; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | # back up and restore device_auth | ||
| 79 | my $deviceauthguard = guard { set(device_auth => \@deviceauthconf) }; | ||
| 80 | set(device_auth => \@newdeviceauthconf); | ||
| 81 | |||
| 82 | my $runner = sub { | ||
| 83 | my ($self, $job) = @_; | ||
| 84 | # roll everything back if we're testing | ||
| 85 | my $txn_guard = $ENV{ND2_DB_ROLLBACK} | ||
| 86 | ? schema('netdisco')->storage->txn_scope_guard : undef; | ||
| 87 | |||
| 88 | # run check phase and if there are workers then one MUST be successful | ||
| 89 | $self->run_workers('workers_check'); | ||
| 90 | |||
| 91 | # run other phases | ||
| 92 | if ($job->check_passed or $ENV{ND2_WORKER_ROLL_CALL}) { | ||
| 93 | $self->run_workers("workers_${_}") for qw/early main user store late/; | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | |||
| 97 | my $maxtime = ((defined setting($job->action .'_timeout')) | ||
| 98 | ? setting($job->action .'_timeout') : setting('workers')->{'timeout'}); | ||
| 99 | |||
| 100 | # add some slack to timeout if the device is new and needs auth walkthrough | ||
| 101 | $maxtime += (40 * scalar @newdeviceauthconf) if ref $job->device and not $job->device->in_storage; | ||
| 102 | |||
| 103 | if ($maxtime) { | ||
| 104 | debug sprintf '%s: running with timeout %ss', $job->action, $maxtime; | ||
| 105 | if (timeout_call($maxtime, $runner, ($self, $job))) { | ||
| 106 | debug sprintf '%s: timed out!', $job->action; | ||
| 107 | $job->add_status( Status->error("job timed out after $maxtime sec") ); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | debug sprintf '%s: running with no timeout', $job->action; | ||
| 112 | $runner->($self, $job); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub run_workers { | ||
| 117 | my $self = shift; | ||
| 118 | my $job = $self->job or die error 'no job in worker job slot'; | ||
| 119 | |||
| 120 | my $set = shift | ||
| 121 | or return $job->add_status( Status->error('missing set param') ); | ||
| 122 | return unless ref [] eq ref $self->$set and 0 < scalar @{ $self->$set }; | ||
| 123 | |||
| 124 | (my $phase = $set) =~ s/^workers_//; | ||
| 125 | $job->enter_phase($phase); | ||
| 126 | |||
| 127 | foreach my $worker (@{ $self->$set }) { | ||
| 128 | try { $job->add_status( $worker->($job) ) } | ||
| 129 | catch { | ||
| 130 | # stringify: DBIx::Class dies with an object, and jq_complete stores | ||
| 131 | # the job's log only when it is a plain string | ||
| 132 | my $error = "$_"; | ||
| 133 | debug "-> $error" if $error; | ||
| 134 | $job->add_status( Status->error($error) ); | ||
| 135 | }; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | true; |