| 1 | package App::Netdisco::Worker::Actions; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Dancer qw/:moose :syntax/; | ||
| 7 | |||
| 8 | use base 'Exporter'; | ||
| 9 | our @EXPORT = (); | ||
| 10 | our @EXPORT_OK = qw/ | ||
| 11 | supported_actions | ||
| 12 | supported_stages | ||
| 13 | plugin_package | ||
| 14 | /; | ||
| 15 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Netdisco::Worker::Actions | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Introspects the configured worker plugins (perl, and python worklets when | ||
| 24 | enabled) to learn which actions, and which stages of those actions, can be run. | ||
| 25 | Nothing is loaded, only the config is read, so this is cheap and has no side | ||
| 26 | effects. C<netdisco-do --list> prints it. | ||
| 27 | |||
| 28 | =head1 EXPORT_OK | ||
| 29 | |||
| 30 | =head2 supported_actions | ||
| 31 | |||
| 32 | Returns an array reference of distinct lowercase action names. | ||
| 33 | |||
| 34 | =head2 supported_stages | ||
| 35 | |||
| 36 | Returns a hash reference of action name to a sorted array reference of the | ||
| 37 | stages (C<action::stage>) which can be run on their own. Only actions having | ||
| 38 | stages are present. | ||
| 39 | |||
| 40 | =head2 plugin_package | ||
| 41 | |||
| 42 | Returns the full package name for a C<worker_plugins> entry (C<X::Foo> or a | ||
| 43 | leading C<+> are expanded as the loader does). The argument is not modified. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | my @phases = qw/check early main user store late/; | ||
| 48 | |||
| 49 | # shared with Worker::Loader so that --list reports what the loader would run | ||
| 50 | sub plugin_package { | ||
| 51 | my $name = shift; | ||
| 52 | $name =~ s/^X::/+App::NetdiscoX::Worker::Plugin::/; | ||
| 53 | $name = 'App::Netdisco::Worker::Plugin::' . $name if $name !~ m/^\+/; | ||
| 54 | $name =~ s/^\+//; | ||
| 55 | return $name; | ||
| 56 | } | ||
| 57 | |||
| 58 | # yields [ action, stage-or-undef ] for every configured worker | ||
| 59 | sub _configured_workers { | ||
| 60 | my @found; | ||
| 61 | |||
| 62 | my @core_plugins = @{ setting('worker_plugins') || [] }; | ||
| 63 | my @user_plugins = @{ setting('extra_worker_plugins') || [] }; | ||
| 64 | |||
| 65 | foreach my $plugin (@user_plugins, @core_plugins) { | ||
| 66 | my $p = plugin_package($plugin); | ||
| 67 | |||
| 68 | # same parse as register_worker: only the first sub-namespace is the stage | ||
| 69 | if ($p =~ m/::Plugin::([^:]+)(?:::([^:]+))?/i) { | ||
| 70 | next if lc $1 eq 'internal'; | ||
| 71 | push @found, [ lc $1, ($2 ? lc $2 : undef) ]; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | # an action may exist only as a python worklet ('action.phase...'), which | ||
| 76 | # the loader runs, so it has no perl plugin above but must still be supported | ||
| 77 | if (setting('enable_python_worklets')) { | ||
| 78 | foreach my $setting (qw/python_worker_plugins extra_python_worker_plugins/) { | ||
| 79 | my $config = setting($setting); | ||
| 80 | next unless $config and ref [] eq ref $config; | ||
| 81 | |||
| 82 | foreach my $entry (@$config) { | ||
| 83 | my $worklet = (ref {} eq ref $entry ? (keys %$entry)[0] : $entry); | ||
| 84 | next unless $worklet and $worklet =~ m/^([^.]+)\.(?:([^.]+)\.)?/; | ||
| 85 | |||
| 86 | my ($action, $next) = (lc $1, lc($2 || '')); | ||
| 87 | my $stage = (($next and not scalar grep {$_ eq $next} @phases) ? $next : undef); | ||
| 88 | push @found, [ $action, $stage ]; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return @found; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub supported_actions { | ||
| 97 | my @supported; | ||
| 98 | foreach my $worker (_configured_workers()) { | ||
| 99 | my $action = $worker->[0]; | ||
| 100 | push @supported, $action unless scalar grep {$_ eq $action} @supported; | ||
| 101 | } | ||
| 102 | return \@supported; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub supported_stages { | ||
| 106 | my %stages; | ||
| 107 | foreach my $worker (_configured_workers()) { | ||
| 108 | my ($action, $stage) = @$worker; | ||
| 109 | $stages{$action}->{$stage} = 1 if $stage; | ||
| 110 | } | ||
| 111 | |||
| 112 | return { map {($_ => [ sort keys %{ $stages{$_} } ])} keys %stages }; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |