all the bug fixes and a working plugin!!!!!!!!! :-D

This commit is contained in:
Oliver Gorwits
2017-09-05 22:10:53 +01:00
parent 04896202e0
commit a79cb9a9e4
5 changed files with 56 additions and 10 deletions

View File

@@ -34,7 +34,7 @@ sub _load_web_plugins {
if $plugin !~ m/^\+/;
$plugin =~ s/^\+//;
debug "loading Netdisco plugin $plugin";
$ENV{PLUGIN_LOAD_DEBUG} && debug "loading Netdisco plugin $plugin";
Module::Load::load $plugin;
}
}

View File

@@ -18,7 +18,7 @@ sub load_worker_plugins {
if $plugin !~ m/^\+/;
$plugin =~ s/^\+//;
debug "loading Netdisco plugin $plugin";
$ENV{PLUGIN_LOAD_DEBUG} && debug "loading Netdisco plugin $plugin";
eval { Module::Load::load $plugin };
}
}

View File

@@ -12,16 +12,16 @@ use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/;
set( '_nd2worker_hooks' => [] );
register 'register_worker' => sub {
my ($self, $workerconf, $code) = @_;
my ($self, $workerconf, $code) = plugin_args(@_);
return error "bad param to register_worker"
unless ((ref sub {} eq ref $code) and (ref {} eq ref $workerconf));
# needs to be here for caller() context
my ($package, $action, $phase) = ((caller)[0], undef, undef);
if ($package =~ m/Plugin::(\w+)$/) {
if ($package =~ m/::Plugin::(\w+)$/) {
$action = lc $1;
}
if ($package =~ m/Plugin::(\w+)::(\w+)/) {
elsif ($package =~ m/::Plugin::(\w+)::(\w+)/) {
$action = lc $1; $phase = lc $2;
}
else { return error "worker Package does not match standard naming" }
@@ -63,17 +63,18 @@ register 'register_worker' => sub {
};
my $primary = ($workerconf->{primary} ? '_primary' : '');
my $hook = 'nd2worker_'. $action .'_'. $phase . $primary;
my $hook = 'nd2worker_'. $action .'_'. $workerconf->{phase} . $primary;
my $store = Dancer::Factory::Hook->instance();
if (not $store->hook_is_registered($hook)) {
$store->install_hooks($hook);
# track just the basic phase names which are used
push @{ setting('_nd2worker_hooks') }, $hook
if $phase ne '00init' and 0 == length($primary);
if $workerconf->{phase} ne '00init' and 0 == length($primary);
}
$store->register_hook($hook, $worker);
# D::Factory::Hook::register_hook() does not work?!
hook $hook => $worker;
};
register_plugin;

View File

@@ -0,0 +1,40 @@
package App::Netdisco::Worker::Plugin::Psql;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
register_worker({} => sub {
my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
my $name = ($ENV{NETDISCO_DBNAME} || setting('database')->{name} || 'netdisco');
my $host = setting('database')->{host};
my $user = setting('database')->{user};
my $pass = setting('database')->{pass};
my $portnum = undef;
if ($host and $host =~ m/([^;]+);port=(\d+)/) {
$host = $1;
$portnum = $2;
}
$ENV{PGHOST} = $host if $host;
$ENV{PGPORT} = $portnum if defined $portnum;
$ENV{PGDATABASE} = $name;
$ENV{PGUSER} = $user;
$ENV{PGPASSWORD} = $pass;
$ENV{PGCLIENTENCODING} = 'UTF8';
if ($extra) {
system('psql', '-c', $extra);
}
else {
system('psql');
}
return Status->done('psql session closed.');
});
true;

View File

@@ -62,8 +62,12 @@ sub run {
@{ (setting('_nd2worker_hooks') || []) };
# run 00init primary
$self->run_workers("nd2worker_${action}_00init_primary");
return if $self->jobstat->not_ok;
my $store = Dancer::Factory::Hook->instance();
my $initprimary = "nd2worker_${action}_00init_primary";
if (scalar @{ $store->get_hooks_for($initprimary) }) {
$self->run_workers($initprimary);
return if $self->jobstat->not_ok;
}
# run each 00init worker
$self->run_workers("nd2worker_${action}_00init");
@@ -80,6 +84,7 @@ sub run_workers {
my $hook = shift or return $self->jobstat->error('missing hook param');
my $primary = ($hook =~ m/_primary$/);
my $store = Dancer::Factory::Hook->instance();
# debug "entering hook $hook";
foreach my $worker (@{ $store->get_hooks_for($hook) }) {
try {