blib/lib/App/Netdisco/Worker/Plugin.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 79 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 259 CC 4 SCAR 21.5
CC
4
Cov
0%
_find_matchaction
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 8.5
1 package App::Netdisco::Worker::Plugin;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin;
5  
6 use App::Netdisco::Util::Permission qw/acl_matches acl_matches_only/;
7 use aliased 'App::Netdisco::Worker::Status';
8  
9 use Term::ANSIColor qw(:constants :constants256);
10 use Scope::Guard 'guard';
11 use Storable 'dclone';
12  
13 register 'register_worker' => sub {
14 my ($self, $first, $second) = plugin_args(@_);
15  
16 my $workerconf = (ref $first eq 'HASH' ? $first : {});
17 my $code = (ref $first eq 'CODE' ? $first : $second);
18 return error "bad param to register_worker"
19 unless ((ref sub {} eq ref $code) and (ref {} eq ref $workerconf));
20  
21 my $package = (caller)[0];
22 ($workerconf->{package} = $package) =~ s/^App::Netdisco::Worker::Plugin:://;
23 if ($package =~ m/Plugin::(\w+)(?:::(\w+))?/) {
24 $workerconf->{action} ||= lc($1);
25 $workerconf->{namespace} ||= lc($2) if $2;
26 }
27 return error "failed to parse action in '$package'"
28 unless $workerconf->{action};
29  
30 $workerconf->{title} ||= '';
31 $workerconf->{phase} ||= 'user';
32 $workerconf->{namespace} ||= '_base_';
33 $workerconf->{priority} ||= (exists $workerconf->{driver}
34 ? (setting('driver_priority')->{$workerconf->{driver}} || 0) : 0);
35  
36 my $worker = sub {
37 my $job = shift or die 'missing job param';
38 # use DDP; p $workerconf;
39  
40 debug YELLOW, "\N{RIGHTWARDS BLACK ARROW} worker ", GREY10, $workerconf->{package},
41 ($workerconf->{pyworklet} ? (' '. $workerconf->{pyworklet}) : ''),
42 GREY10, ' p', MAGENTA, $workerconf->{priority},
43 ($workerconf->{title} ? (GREY10, ' "', BRIGHT_BLUE, $workerconf->{title}, GREY10, '"') : ''),
44 RESET;
45  
46 if ($job->is_cancelled) {
47 return $job->add_status( Status->info('skip: job is cancelled') );
48 }
49  
50 if ($job->is_offline
51 and $workerconf->{phase} eq 'main'
52 and $workerconf->{priority} > 0
53 and $workerconf->{priority} < setting('driver_priority')->{'direct'}) {
54  
55 return $job->add_status( Status->info('skip: networked worker but job is running offline') );
56 }
57  
58 # support part-actions via action::namespace
59 if ($job->only_namespace and $workerconf->{phase} ne 'check') {
60 # skip namespaces not the requested ::namespace
61 if (not ($workerconf->{namespace} eq lc( $job->only_namespace )
62 # apart from discover::properties which needs to run, so that's early
63 # phase for unknown devices, but not ::hooks/early (if implemented)
64 or (($job->only_namespace ne 'hooks') and ($workerconf->{phase} eq 'early')
65 and ($job->device and not $job->device->in_storage)) )) {
66  
67 return;
68 }
69 }
70  
71 my @newuserconf = ();
72 my @userconf = @{ dclone (setting('device_auth') || []) };
73  
74 # worker might be vendor/platform specific
75 if (ref $job->device) {
76 my $no = (exists $workerconf->{no} ? $workerconf->{no} : undef);
77 my $only = (exists $workerconf->{only} ? $workerconf->{only} : undef);
78  
79 return $job->add_status( Status->info('skip: acls restricted') )
80 if ($no and acl_matches($job->device, $no))
81 or ($only and not acl_matches_only($job->device, $only));
82  
83 # reduce device_auth by driver and action filters
84 foreach my $stanza (@userconf) {
85 next if exists $stanza->{driver} and exists $workerconf->{driver}
86 and (($stanza->{driver} || '') ne ($workerconf->{driver} || ''));
87  
88 # filter here rather than in Runner as runner does not know namespace
89 next if exists $stanza->{action}
90 and not _find_matchaction($workerconf, lc($stanza->{action}));
91  
92 push @newuserconf, dclone $stanza;
93 }
94  
95 # per-device action but no device creds available
96 return $job->add_status( Status->info('skip: driver or action not applicable') )
97 if 0 == scalar @newuserconf
98 and $workerconf->{priority} > 0
99 and $workerconf->{priority} < setting('driver_priority')->{'direct'};
100 }
101  
102 # back up and restore device_auth
103 my $guard = guard { set(device_auth => \@userconf) };
104 set(device_auth => \@newuserconf);
105 # use DDP; p @newuserconf;
106  
107 # run worker
108 if ($ENV{ND2_WORKER_ROLL_CALL}) {
109 return Status->info('-');
110 }
111 else {
112 $code->($job, $workerconf);
113 }
114 };
115  
116 # store the built worker as Worker::Loader will build the dispatch order
117 push @{ vars->{'workers'}->{$workerconf->{action}}
118 ->{$workerconf->{phase}}
119 ->{$workerconf->{namespace}}
120 ->{$workerconf->{priority}} }, $worker;
121 };
122  
123 sub _find_matchaction {
124 my ($conf, $action) = @_;
125 return true if !defined $action;
126 $action = [$action] if ref [] ne ref $action;
127  
128 foreach my $f (@$action) {
129 return true if
130 $f eq $conf->{action} or $f eq "$conf->{action}::$conf->{namespace}";
131 }
132 return false;
133 }
134  
135 register_plugin;
136 true;
137  
138 =head1 NAME
139  
140 App::Netdisco::Worker::Plugin - Netdisco Workers
141  
142 =head1 Introduction
143  
144 L<App::Netdisco>'s plugin system allows users to write I<workers> to gather
145 information from network devices using different I<transports> and store
146 results in the database.
147  
148 For example, transports might be SNMP, SSH, or HTTPS. Workers might be
149 combining those transports with application protocols such as SNMP, NETCONF
150 (OpenConfig with XML), RESTCONF (OpenConfig with JSON), eAPI, or even CLI
151 scraping. The combination of transport and protocol is known as a I<driver>.
152  
153 Workers can be restricted to certain vendor platforms using familiar ACL
154 syntax. They are also attached to specific actions in Netdisco's backend
155 operation (discover, macsuck, etc).
156  
157 See L<https://github.com/netdisco/netdisco/wiki/Backend-Plugins> for details.
158  
159 =cut
160