« blib/lib/App/Netdisco/Util/Device.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/ASA.pm »
| 1 | package App::Netdisco::Worker::Plugin::PythonShim; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | |||
| 6 | use App::Netdisco::Transport::Python (); | ||
| 7 | |||
| 8 | sub import { | ||
| 9 | my ($pkg, $action) = @_; | ||
| 10 | return unless $action; | ||
| 11 | _find_python_worklets($action, $_) | ||
| 12 | for qw/python_worker_plugins extra_python_worker_plugins/; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub _find_python_worklets { | ||
| 16 | my ($action, $setting) = @_; | ||
| 17 | my $config = setting($setting); | ||
| 18 | return unless $config and ref [] eq ref $config; | ||
| 19 | |||
| 20 | foreach my $entry (@{ $config }) { | ||
| 21 | my $worklet = undef; | ||
| 22 | if (ref {} eq ref $entry) { | ||
| 23 | $worklet = (keys %$entry)[0]; | ||
| 24 | } | ||
| 25 | else { | ||
| 26 | $worklet = $entry; | ||
| 27 | } | ||
| 28 | next unless $worklet and $worklet =~ m/^\Q${action}\E\./; | ||
| 29 | my @parts = split /\./, $worklet; | ||
| 30 | |||
| 31 | my %base = ( | ||
| 32 | action => shift @parts, | ||
| 33 | pyworklet => ($setting =~ m/extra/ ? setting('extra_python_worker_package_namespace') : 'netdisco') | ||
| 34 | ); | ||
| 35 | my %phases = (map {$_ => ''} qw(check early main user store late)); | ||
| 36 | |||
| 37 | while (my $phase = shift @parts) { | ||
| 38 | if (exists $phases{$phase}) { | ||
| 39 | $base{phase} = $phase; | ||
| 40 | last; | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | push @{ $base{namespace} }, $phase; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | if (scalar @parts and exists setting('driver_priority')->{$parts[0]}) { | ||
| 48 | $base{driver} = shift @parts; | ||
| 49 | } | ||
| 50 | |||
| 51 | $base{platform} = [ @parts ] if scalar @parts; | ||
| 52 | |||
| 53 | if (ref {} eq ref $entry) { | ||
| 54 | my $rhs = (values %$entry)[0]; | ||
| 55 | if (ref [] eq ref $rhs) { | ||
| 56 | foreach my $driver (@{ $rhs }) { | ||
| 57 | _register_python_worklet({ %base, driver => $driver }); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | _register_python_worklet({ %base, %$rhs }); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | _register_python_worklet({ %base }); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _register_python_worklet { | ||
| 71 | my $workerconf = shift; | ||
| 72 | $workerconf->{pyworklet} .= _build_pyworklet(%$workerconf); | ||
| 73 | $workerconf->{full_namespace} = join '::', @{ $workerconf->{namespace} } | ||
| 74 | if exists $workerconf->{namespace}; | ||
| 75 | $workerconf->{namespace} = $workerconf->{namespace}->[0] | ||
| 76 | if exists $workerconf->{namespace}; | ||
| 77 | |||
| 78 | $ENV{ND2_LOG_PLUGINS} && | ||
| 79 | debug sprintf 'loading python worklet a:%s s:%s p:%s d:%s/p:%s', | ||
| 80 | (exists $workerconf->{action} ? ($workerconf->{action} || '?') : '-'), | ||
| 81 | (exists $workerconf->{namespace} ? ($workerconf->{namespace} || '?') : '-'), | ||
| 82 | (exists $workerconf->{phase} ? ($workerconf->{phase} || '?') : '-'), | ||
| 83 | (exists $workerconf->{driver} ? ($workerconf->{driver} || '?') : '-'), | ||
| 84 | (exists $workerconf->{priority} ? ($workerconf->{priority} || '?') : '-'); | ||
| 85 | |||
| 86 | register_worker($workerconf, sub { App::Netdisco::Transport::Python->py_worklet(@_) }); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _build_pyworklet { | ||
| 90 | my %base = @_; | ||
| 91 | return join '.', '', 'worklet', | ||
| 92 | $base{action}, | ||
| 93 | (exists $base{namespace} ? @{ $base{namespace} } : ()), | ||
| 94 | $base{phase}, | ||
| 95 | (exists $base{driver} ? $base{driver} : ()), | ||
| 96 | (exists $base{platform} ? @{ $base{platform} } : ()); | ||
| 97 | } | ||
| 98 | |||
| 99 | true; |