« blib/lib/App/Netdisco/Worker/Plugin/Macsuck/Nodes.pm
↑ Summary
blib/lib/App/Netdisco/DB/ResultSet/Device.pm »
| 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 | |||
| 38 | # DBICx::Sugar caches a schema under the name asked for, so a worker that | ||
| 39 | # asks for 'netdisco' before undef gets two connections and can wait on its | ||
| 40 | # own locks (#1782). Backend only: in the web a tenant changes every URL. | ||
| 41 | var(tenant => 'netdisco') unless vars->{'tenant'}; | ||
| 42 | |||
| 43 | $job->device( get_device($job->device) ) | ||
| 44 | unless scalar grep {$job->action eq $_} @{ setting('job_targets_prefix') }; | ||
| 45 | $self->load_workers(); | ||
| 46 | |||
| 47 | # clean up and finalise job status when we exit | ||
| 48 | my $statusguard = guard { | ||
| 49 | if (var('live_python')) { | ||
| 50 | try { App::Netdisco::Transport::Python->runner->finish }; | ||
| 51 | try { App::Netdisco::Transport::Python->runner->kill_kill }; | ||
| 52 | try { unlink App::Netdisco::Transport::Python->context->filename }; | ||
| 53 | } | ||
| 54 | $job->finalise_status; | ||
| 55 | }; | ||
| 56 | |||
| 57 | my @newdeviceauthconf = (); | ||
| 58 | my @deviceauthconf = @{ dclone (setting('device_auth') || []) }; | ||
| 59 | |||
| 60 | # reduce device_auth by only/no and device_auth_tag_hint | ||
| 61 | if (ref $job->device) { | ||
| 62 | foreach my $stanza (@deviceauthconf) { | ||
| 63 | my $no = (exists $stanza->{no} ? $stanza->{no} : undef); | ||
| 64 | my $only = (exists $stanza->{only} ? $stanza->{only} : undef); | ||
| 65 | |||
| 66 | next if $no and acl_matches($job->device, $no); | ||
| 67 | next if $only and not acl_matches_only($job->device, $only); | ||
| 68 | |||
| 69 | push @newdeviceauthconf, dclone $stanza; | ||
| 70 | } | ||
| 71 | |||
| 72 | # per-device action but no device creds available | ||
| 73 | return $job->add_status( Status->defer('deferred job with no device creds') ) | ||
| 74 | if 0 == scalar @newdeviceauthconf && $self->transport_required; | ||
| 75 | |||
| 76 | # if there is a device_auth_tag_hint then promote it to the front | ||
| 77 | if (my $tag_hint = setting('device_auth_tag_hint')) { | ||
| 78 | my @hint_matches = grep { $_->{tag} and $_->{tag} eq $tag_hint } @newdeviceauthconf; | ||
| 79 | my @not_matches = grep { ! ($_->{tag} and $_->{tag} eq $tag_hint) } @newdeviceauthconf; | ||
| 80 | @newdeviceauthconf = (@hint_matches, @not_matches) if scalar @hint_matches; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | # back up and restore device_auth | ||
| 85 | my $deviceauthguard = guard { set(device_auth => \@deviceauthconf) }; | ||
| 86 | set(device_auth => \@newdeviceauthconf); | ||
| 87 | |||
| 88 | my $runner = sub { | ||
| 89 | my ($self, $job) = @_; | ||
| 90 | # roll everything back if we're testing | ||
| 91 | my $txn_guard = $ENV{ND2_DB_ROLLBACK} | ||
| 92 | ? schema('netdisco')->storage->txn_scope_guard : undef; | ||
| 93 | |||
| 94 | # run check phase and if there are workers then one MUST be successful | ||
| 95 | $self->run_workers('workers_check'); | ||
| 96 | |||
| 97 | # run other phases | ||
| 98 | if ($job->check_passed or $ENV{ND2_WORKER_ROLL_CALL}) { | ||
| 99 | $self->run_workers("workers_${_}") for qw/early main user store late/; | ||
| 100 | } | ||
| 101 | }; | ||
| 102 | |||
| 103 | my $maxtime = ((defined setting($job->action .'_timeout')) | ||
| 104 | ? setting($job->action .'_timeout') : setting('workers')->{'timeout'}); | ||
| 105 | |||
| 106 | # add some slack to timeout if the device is new and needs auth walkthrough | ||
| 107 | $maxtime += (40 * scalar @newdeviceauthconf) if ref $job->device and not $job->device->in_storage; | ||
| 108 | |||
| 109 | if ($maxtime) { | ||
| 110 | debug sprintf '%s: running with timeout %ss', $job->action, $maxtime; | ||
| 111 | if (timeout_call($maxtime, $runner, ($self, $job))) { | ||
| 112 | debug sprintf '%s: timed out!', $job->action; | ||
| 113 | $job->add_status( Status->error("job timed out after $maxtime sec") ); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | debug sprintf '%s: running with no timeout', $job->action; | ||
| 118 | $runner->($self, $job); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub run_workers { | ||
| 123 | my $self = shift; | ||
| 124 | my $job = $self->job or die error 'no job in worker job slot'; | ||
| 125 | |||
| 126 | my $set = shift | ||
| 127 | or return $job->add_status( Status->error('missing set param') ); | ||
| 128 | return unless ref [] eq ref $self->$set and 0 < scalar @{ $self->$set }; | ||
| 129 | |||
| 130 | (my $phase = $set) =~ s/^workers_//; | ||
| 131 | $job->enter_phase($phase); | ||
| 132 | |||
| 133 | foreach my $worker (@{ $self->$set }) { | ||
| 134 | try { $job->add_status( $worker->($job) ) } | ||
| 135 | catch { | ||
| 136 | # stringify: DBIx::Class dies with an object, and jq_complete stores | ||
| 137 | # the job's log only when it is a plain string | ||
| 138 | my $error = "$_"; | ||
| 139 | debug "-> $error" if $error; | ||
| 140 | $job->add_status( Status->error($error) ); | ||
| 141 | }; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | true; |