rename Core to Worker and move other packages around

This commit is contained in:
Oliver Gorwits
2017-09-03 18:45:00 +01:00
parent 4def0af0b0
commit 8eaa33770c
11 changed files with 71 additions and 67 deletions

View File

@@ -101,7 +101,7 @@ unless ($action) {
use NetAddr::IP qw/:rfc3021 :lower/; use NetAddr::IP qw/:rfc3021 :lower/;
use Dancer ':script'; use Dancer ':script';
use App::Netdisco::Core::Transport::SNMP; use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Device use App::Netdisco::Util::Device
qw/get_device delete_device renumber_device/; qw/get_device delete_device renumber_device/;
@@ -145,7 +145,7 @@ unless ($action) {
$extra = $class; $extra = $class;
undef $class; undef $class;
} }
my $i = App::Netdisco::Core::Transport::SNMP->reader_for($device, $class); my $i = App::Netdisco::Transport::SNMP->reader_for($device, $class);
Data::Printer::p($i->$extra); Data::Printer::p($i->$extra);
return ('done', sprintf "Showed %s response from %s.", $extra, $device->ip); return ('done', sprintf "Showed %s response from %s.", $extra, $device->ip);
} }

View File

@@ -6,15 +6,15 @@ use warnings;
use Module::Load (); use Module::Load ();
use Dancer ':syntax'; use Dancer ':syntax';
# load core worker plugins for our workers # load worker plugins for our workers
# NOTE: this package is loaded for all actions whether backend or netdisco-do # NOTE: this package is loaded for all actions whether backend or netdisco-do
sub load_core_plugins { sub load_worker_plugins {
my $plugin_list = shift; my $plugin_list = shift;
foreach my $plugin (@$plugin_list) { foreach my $plugin (@$plugin_list) {
$plugin =~ s/^X::/+App::NetdiscoX::Core::Plugin::/; $plugin =~ s/^X::/+App::NetdiscoX::Worker::Plugin::/;
$plugin = 'App::Netdisco::Core::Plugin::'. $plugin $plugin = 'App::Netdisco::Worker::Plugin::'. $plugin
if $plugin !~ m/^\+/; if $plugin !~ m/^\+/;
$plugin =~ s/^\+//; $plugin =~ s/^\+//;
@@ -23,7 +23,7 @@ sub load_core_plugins {
} }
} }
load_core_plugins( setting('extra_core_plugins') || [] ); load_worker_plugins( setting('extra_worker_plugins') || [] );
load_core_plugins( setting('core_plugins') || [] ); load_worker_plugins( setting('worker_plugins') || [] );
true; true;

View File

@@ -1,6 +1,6 @@
package App::Netdisco::Backend::Worker::Interactive::DeviceActions; package App::Netdisco::Backend::Worker::Interactive::DeviceActions;
use App::Netdisco::Core::Transport::SNMP; use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Backend::Util ':all'; use App::Netdisco::Backend::Util ':all';
@@ -22,7 +22,7 @@ sub _set_device_generic {
$data ||= ''; $data ||= '';
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) my $info = App::Netdisco::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to update $slot"); or return job_defer("Failed to connect to device [$ip] to update $slot");
my $method = 'set_'. $slot; my $method = 'set_'. $slot;

View File

@@ -1,7 +1,7 @@
package App::Netdisco::Backend::Worker::Interactive::PortActions; package App::Netdisco::Backend::Worker::Interactive::PortActions;
use App::Netdisco::Util::Port ':all'; use App::Netdisco::Util::Port ':all';
use App::Netdisco::Core::Transport::SNMP; use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Backend::Util ':all'; use App::Netdisco::Backend::Util ':all';
@@ -74,7 +74,7 @@ sub _set_port_generic {
if ($device->vendor ne 'netdisco') { if ($device->vendor ne 'netdisco') {
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) my $info = App::Netdisco::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to control port"); or return job_defer("Failed to connect to device [$ip] to control port");
my $iid = get_iid($info, $port) my $iid = get_iid($info, $port)
@@ -127,7 +127,7 @@ sub power {
$data = 'false' if $data =~ m/^(off|no|down)$/; $data = 'false' if $data =~ m/^(off|no|down)$/;
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip) my $info = App::Netdisco::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to control power"); or return job_defer("Failed to connect to device [$ip] to control power");
my $powerid = get_powerid($info, $port) my $powerid = get_powerid($info, $port)

View File

@@ -2,7 +2,7 @@ package App::Netdisco::Backend::Worker::Poller::Common;
use Dancer qw/:moose :syntax :script/; use Dancer qw/:moose :syntax :script/;
use App::Netdisco::Core::Transport::SNMP; use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Backend::Util ':all'; use App::Netdisco::Backend::Util ':all';
use App::Netdisco::JobQueue qw/jq_queued jq_insert/; use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
@@ -63,7 +63,7 @@ sub _single_body {
return job_defer("$job_type deferred: $host is not ${job_type}able"); return job_defer("$job_type deferred: $host is not ${job_type}able");
} }
my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device); my $snmp = App::Netdisco::Transport::SNMP->reader_for($device);
if (!defined $snmp) { if (!defined $snmp) {
return job_defer("$job_type failed: could not SNMP connect to $host"); return job_defer("$job_type failed: could not SNMP connect to $host");
} }

View File

@@ -2,7 +2,7 @@ package App::Netdisco::Backend::Worker::Poller::Device;
use Dancer qw/:moose :syntax :script/; use Dancer qw/:moose :syntax :script/;
use App::Netdisco::Core::Transport::SNMP; use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Device qw/get_device is_discoverable_now/; use App::Netdisco::Util::Device qw/get_device is_discoverable_now/;
use App::Netdisco::Core::Discover ':all'; use App::Netdisco::Core::Discover ':all';
use App::Netdisco::Backend::Util ':all'; use App::Netdisco::Backend::Util ':all';
@@ -59,7 +59,7 @@ sub discover {
return job_defer("discover deferred: $host is not discoverable"); return job_defer("discover deferred: $host is not discoverable");
} }
my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device); my $snmp = App::Netdisco::Transport::SNMP->reader_for($device);
if (!defined $snmp) { if (!defined $snmp) {
return job_defer("discover failed: could not SNMP connect to $host"); return job_defer("discover failed: could not SNMP connect to $host");
} }

View File

@@ -1,6 +1,6 @@
=head1 NAME =head1 NAME
App::Netdisco::Manual::WritingCoreWorkers - Developer Documentation on Core Plugins App::Netdisco::Manual::WritingWorkers - Developer Documentation on Worker Plugins
=head1 Introduction =head1 Introduction
@@ -17,7 +17,8 @@ Workers can be restricted to certain vendor platforms using familiar ACL
syntax. They are also attached to specific actions in Netdisco's backend syntax. They are also attached to specific actions in Netdisco's backend
operation (discover, macsuck, etc). operation (discover, macsuck, etc).
See L<App::Netdisco::Core::Plugin> for more information about core plugins. See L<App::Netdisco::Worker::Plugin> for more information about worker
plugins.
=head1 Developing Workers =head1 Developing Workers
@@ -25,14 +26,14 @@ A worker is Perl code which is run. Therefore it can do anything you like, but
typically it will make a connection to a device, gather some data, and store typically it will make a connection to a device, gather some data, and store
it in Netdisco's database. it in Netdisco's database.
App::Netdisco plugins must load the L<App::Netdisco::Core::Plugin> module. App::Netdisco plugins must load the L<App::Netdisco::Worker::Plugin> module.
This exports a helper subroutine to register the worker. Here's the This exports a helper subroutine to register the worker. Here's the
boilerplate code for our example plugin module: boilerplate code for our example plugin module:
package App::Netdisco::Core::Plugin::Discover::Wireless::UniFi; package App::Netdisco::Worker::Plugin::Discover::Wireless::UniFi;
use Dancer ':syntax'; use Dancer ':syntax';
use App::Netdisco::Core::Plugin; use App::Netdisco::Worker::Plugin;
# worker registration code goes here, ** see below ** # worker registration code goes here, ** see below **
@@ -40,14 +41,14 @@ boilerplate code for our example plugin module:
=head1 Registering a Worker =head1 Registering a Worker
Use the C<register_core_worker> helper from L<App::Netdisco::Core::Plugin> to Use the C<register_worker> helper from L<App::Netdisco::Worker::Plugin> to
register a worker: register a worker:
register_core_worker( \%workerconf, $coderef ); register_worker( \%workerconf, $coderef );
For example: For example:
register_core_worker({ register_worker({
driver => 'unifiapi', driver => 'unifiapi',
}, sub { "worker code here" }); }, sub { "worker code here" });
@@ -68,11 +69,13 @@ to declare the worker (documented below).
The package name used where the worker is declared is significant. Let's look The package name used where the worker is declared is significant. Let's look
at the boilerplate example again: at the boilerplate example again:
package App::Netdisco::Core::Plugin::Discover::Wireless::UniFi; package App::Netdisco::Worker::Plugin::Discover::Wireless::UniFi;
Workers registered in this package will be run during the I<discover> backend The package name B<must> contain C<Plugin::> and the namespace component after
action (that is, during a C<discover> job). You can replace C<Discover> with that becomes the action. For example workers registered in the above package
other actions such as C<Macsuck>, C<Arpnip>, C<Expire>, and C<Nbtstat>. will be run during the I<discover> backend action (that is, during a
C<discover> job). You can replace C<Discover> with other actions such as
C<Macsuck>, C<Arpnip>, C<Expire>, and C<Nbtstat>, or create your own.
The component after the action is known as the I<phase> (C<Wireless> in this The component after the action is known as the I<phase> (C<Wireless> in this
example), and is the way to override a Netdisco built-in worker, by using the example), and is the way to override a Netdisco built-in worker, by using the
@@ -110,8 +113,8 @@ run for every device and phase (such as during Expire jobs).
When multiple workers are registered for the same phase, they will all be run. When multiple workers are registered for the same phase, they will all be run.
However there is a special "I<primary>" slot for each phase in which only one However there is a special "I<primary>" slot for each phase in which only one
worker (the first that succeeds) is used. Most of Netdisco's core worker code worker (the first that succeeds) is used. Most of Netdisco's built-in worker
is registered in this way, so to override it you can use the same package code is registered in this way, so to override it you can use the same package
namespace and set C<primary> to be C<true>. namespace and set C<primary> to be C<true>.
=back =back
@@ -121,10 +124,10 @@ namespace and set C<primary> to be C<true>.
Workers are configured as an ordered list. They are grouped by C<action> and Workers are configured as an ordered list. They are grouped by C<action> and
C<phase> (as in Package Naming Convention, above). C<phase> (as in Package Naming Convention, above).
Workers defined in C<extra_core_plugins> are run before those in Workers defined in C<extra_worker_plugins> are run before those in
C<core_plugins> so you have an opportunity to override core workers by adding C<worker_plugins> so you have an opportunity to override built-in workers by
them to C<extra_core_plugins> and setting C<primary> to C<true> in the worker adding them to C<extra_worker_plugins> and setting C<primary> to C<true> in
configuration. the worker configuration.
The return code of the worker is significant for those configured with The return code of the worker is significant for those configured with
C<primary> as C<true>: when the worker returns true, no other C<primary> hooks C<primary> as C<true>: when the worker returns true, no other C<primary> hooks
@@ -147,7 +150,7 @@ See the documentation for each transport to find out how to access it:
=item * =item *
L<App::Netdisco::Core::Transport::SNMP> L<App::Netdisco::Transport::SNMP>
=back =back
@@ -170,8 +173,8 @@ do really early bootstrapping work.
=item C<phase> =item C<phase>
The next level down from C<action> for grouping workers. Phases have arbitrary The next level down from C<action> for grouping workers. Phases have arbitrary
names and are visited in the order defined in the C<extra_core_plugins> names and are visited in the order defined in the C<extra_worker_plugins>
setting list, followed by the C<core_plugins> setting list. Workers are setting list, followed by the C<worker_plugins> setting list. Workers are
usually registered at this level. usually registered at this level.
=item C<worker> =item C<worker>

View File

@@ -1,4 +1,4 @@
package App::Netdisco::Core::Transport::SNMP; package App::Netdisco::Transport::SNMP;
use Dancer qw/:syntax :script/; use Dancer qw/:syntax :script/;
use App::Netdisco::Util::SNMP 'build_communities'; use App::Netdisco::Util::SNMP 'build_communities';
@@ -14,14 +14,14 @@ use base 'Dancer::Object::Singleton';
=head1 NAME =head1 NAME
App::Netdisco::Core::Transport::SNMP App::Netdisco::Transport::SNMP
=head1 DESCRIPTION =head1 DESCRIPTION
Singleton for SNMP connections. Returns cached L<SNMP::Info> instance for a Singleton for SNMP connections. Returns cached L<SNMP::Info> instance for a
given device IP, or else undef. All methods are class methods, for example: given device IP, or else undef. All methods are class methods, for example:
App::Netdisco::Core::Transport::SNMP->reader_for( ... ); App::Netdisco::Transport::SNMP->reader_for( ... );
=cut =cut

View File

@@ -1,4 +1,4 @@
package App::Netdisco::Core::Plugin; package App::Netdisco::Worker::Plugin;
use Dancer ':syntax'; use Dancer ':syntax';
use Dancer::Plugin; use Dancer::Plugin;
@@ -9,19 +9,19 @@ use Scope::Guard;
use Try::Tiny; use Try::Tiny;
# track the phases seen so we can recall them in order # track the phases seen so we can recall them in order
set( '_nd2core_hooks' => [] ); set( '_nd2worker_hooks' => [] );
register 'register_core_worker' => sub { register 'register_worker' => sub {
my ($self, $workerconf, $code) = @_; my ($self, $workerconf, $code) = @_;
return error "bad param to register_core_worker" return error "bad param to register_worker"
unless ((ref sub {} eq ref $code) and (ref {} eq ref $workerconf)); unless ((ref sub {} eq ref $code) and (ref {} eq ref $workerconf));
# needs to be here for caller() context # needs to be here for caller() context
my ($package, $action, $phase) = ((caller)[0], undef, undef); my ($package, $action, $phase) = ((caller)[0], undef, undef);
if ($package =~ m/::(Discover|Arpnip|Macsuck|Expire|Nbtstat)$/) { if ($package =~ m/Plugin::(\w+)$/) {
$action = lc $1; $action = lc $1;
} }
if ($package =~ m/::(Discover|Arpnip|Macsuck|Expire|Nbtstat)::(\w+)/) { if ($package =~ m/Plugin::(\w+)::(\w+)/) {
$action = lc $1; $phase = lc $2; $action = lc $1; $phase = lc $2;
} }
else { return error "worker Package does not match standard naming" } else { return error "worker Package does not match standard naming" }
@@ -66,12 +66,12 @@ register 'register_core_worker' => sub {
}; };
my $primary = ($workerconf->{primary} ? '_primary' : ''); my $primary = ($workerconf->{primary} ? '_primary' : '');
my $hook = 'nd2core_'. $action .'_'. $phase . $primary; my $hook = 'nd2worker_'. $action .'_'. $phase . $primary;
if (not Dancer::Factory::Hook->instance->hook_is_registered($hook)) { if (not Dancer::Factory::Hook->instance->hook_is_registered($hook)) {
Dancer::Factory::Hook->instance->install_hooks($hook); Dancer::Factory::Hook->instance->install_hooks($hook);
# track just the basic phase names which are used # track just the basic phase names which are used
push @{ setting('_nd2core_hooks') }, $hook push @{ setting('_nd2worker_hooks') }, $hook
if $phase ne '00init' and 0 == length($primary); if $phase ne '00init' and 0 == length($primary);
} }
@@ -83,7 +83,7 @@ true;
=head1 NAME =head1 NAME
App::Netdisco::Core::Plugin - Netdisco Core Workers App::Netdisco::Worker::Plugin - Netdisco Workers
=head1 Introduction =head1 Introduction
@@ -102,8 +102,8 @@ operation (discover, macsuck, etc).
=head1 Application Configuration =head1 Application Configuration
The C<core_plugins> and C<extra_core_plugins> settings list in YAML format the The C<worker_plugins> and C<extra_worker_plugins> settings list in YAML format
set of Perl module names which are the plugins to be loaded. the set of Perl module names which are the plugins to be loaded.
Any change should go into your local C<deployment.yml> configuration file. If Any change should go into your local C<deployment.yml> configuration file. If
you want to view the default settings, see the C<share/config.yml> file in the you want to view the default settings, see the C<share/config.yml> file in the
@@ -111,36 +111,36 @@ C<App::Netdisco> distribution.
=head1 How to Configure =head1 How to Configure
The C<extra_core_plugins> setting is empty, and used only if you want to add The C<extra_worker_plugins> setting is empty, and used only if you want to add
new plugins but not change the set enabled by default. If you do want to add new plugins but not change the set enabled by default. If you do want to add
to or remove from the default set, then create a version of C<core_plugins> to or remove from the default set, then create a version of C<worker_plugins>
instead. instead.
Netdisco prepends "C<App::Netdisco::Core::Plugin::>" to any entry in the list. Netdisco prepends "C<App::Netdisco::Worker::Plugin::>" to any entry in the
For example, "C<Discover::Wireless::UniFi>" will load the list. For example, "C<Discover::Wireless::UniFi>" will load the
C<App::Netdisco::Core::Plugin::Discover::Wireless::UniFi> package. C<App::Netdisco::Worker::Plugin::Discover::Wireless::UniFi> package.
You can prepend module names with "C<X::>" as shorthand for the "Netdisco You can prepend module names with "C<X::>" as shorthand for the "Netdisco
extension" namespace. For example, "C<X::Macsuck::WirelessNodes::UniFi>" will extension" namespace. For example, "C<X::Macsuck::WirelessNodes::UniFi>" will
load the L<App::NetdiscoX::Core::Plugin::Macsuck::WirelessNodes::UniFi> load the L<App::NetdiscoX::Worker::Plugin::Macsuck::WirelessNodes::UniFi>
module. module.
If an entry in the list starts with a "C<+>" (plus) sign then Netdisco attemps If an entry in the list starts with a "C<+>" (plus) sign then Netdisco attemps
to load the module as-is, without prepending anything to the name. This allows to load the module as-is, without prepending anything to the name. This allows
you to have App::Netdiso Core plugins in other namespaces. you to have App::Netdisco Worker plugins in other namespaces.
Plugin modules can either ship with the App::Netdisco distribution itself, or Plugin modules can either ship with the App::Netdisco distribution itself, or
be installed separately. Perl uses the standard C<@INC> path searching be installed separately. Perl uses the standard C<@INC> path searching
mechanism to load the plugin modules. See the C<include_paths> and mechanism to load the plugin modules. See the C<include_paths> and
C<site_local_files> settings in order to modify C<@INC> for loading local C<site_local_files> settings in order to modify C<@INC> for loading local
plugins. As an example, if your plugin is called plugins. As an example, if your plugin is called
"App::NetdiscoX::Core::Plugin::MyPluginName" then it could live at: "App::NetdiscoX::Worker::Plugin::MyPluginName" then it could live at:
~netdisco/nd-site-local/lib/App/NetdiscoX/Core/Plugin/MyPluginName.pm ~netdisco/nd-site-local/lib/App/NetdiscoX/Worker/Plugin/MyPluginName.pm
The order of the entries is significant, workers being executed in the order The order of the entries is significant, workers being executed in the order
which they appear in C<core_plugins> and C<extra_core_plugins> (although see which they appear in C<worker_plugins> and C<extra_worker_plugins> (although
L<App::Netdisco::Manual::WritingCoreWorkers> for caveats). see L<App::Netdisco::Manual::WritingWorkers> for caveats).
=cut =cut

View File

@@ -1,4 +1,4 @@
package App::Netdisco::Backend::Status; package App::Netdisco::Worker::Status;
use strict; use strict;
use warnings; use warnings;

View File

@@ -258,10 +258,11 @@ job_prio:
# others go into new ::DRIVERNAME # others go into new ::DRIVERNAME
# such as: ::CLI ::eAPI # such as: ::CLI ::eAPI
extra_core_plugins: extra_worker_plugins: []
- Discover::ConfigBackup::CLI # - Discover::ConfigBackup::CLI
core_plugins: # FIXME
worker_plugins:
- Discover::Properties::RFC - Discover::Properties::RFC
- Discover::Interfaces::RFC - Discover::Interfaces::RFC
- Discover::PortPower::RFC - Discover::PortPower::RFC