use vars() cache between phases

This commit is contained in:
Oliver Gorwits
2017-10-07 12:43:50 +01:00
parent 08b34e083d
commit b10908a138
7 changed files with 79 additions and 110 deletions

View File

@@ -13,11 +13,11 @@ register_worker({ phase => 'check' }, sub {
return Status->error('Missing port (-p).') unless defined $job->port; return Status->error('Missing port (-p).') unless defined $job->port;
return Status->error('Missing status (-e).') unless defined $job->subaction; return Status->error('Missing status (-e).') unless defined $job->subaction;
my $port = get_port($job->device, $job->port) vars->{'port'} = get_port($job->device, $job->port)
or return Status->error(sprintf "Unknown port name [%s] on device %s", or return Status->error(sprintf "Unknown port name [%s] on device %s",
$job->port, $job->device); $job->port, $job->device);
my $vlan_reconfig_check = vlan_reconfig_check($port); my $vlan_reconfig_check = vlan_reconfig_check(vars->{'port'});
return Status->error("Cannot alter vlan: $vlan_reconfig_check") return Status->error("Cannot alter vlan: $vlan_reconfig_check")
if $vlan_reconfig_check; if $vlan_reconfig_check;
@@ -27,7 +27,6 @@ register_worker({ phase => 'check' }, sub {
register_worker({ phase => 'main' }, sub { register_worker({ phase => 'main' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $pn) = map {$job->$_} qw/device port/; my ($device, $pn) = map {$job->$_} qw/device port/;
my $port = get_port($device, $pn);
# need to remove "-other" which appears for power/portcontrol # need to remove "-other" which appears for power/portcontrol
(my $sa = $job->subaction) =~ s/-\w+//; (my $sa = $job->subaction) =~ s/-\w+//;
@@ -35,23 +34,23 @@ register_worker({ phase => 'main' }, sub {
if ($sa eq 'bounce') { if ($sa eq 'bounce') {
$job->subaction('down'); $job->subaction('down');
my $status = _action($job, $port); my $status = _action($job);
return $status if $status->not_ok; return $status if $status->not_ok;
$job->subaction('up'); $job->subaction('up');
} }
return _action($job, $port); return _action($job);
}); });
sub _action { sub _action {
my ($job, $port) = @_; my $job = shift;
my ($device, $pn, $data) = map {$job->$_} qw/device port subaction/; my ($device, $pn, $data) = map {$job->$_} qw/device port subaction/;
# snmp connect using rw community # snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update up_admin"); or return Status->defer("failed to connect to $device to update up_admin");
my $iid = get_iid($snmp, $port) my $iid = get_iid($snmp, vars->{'port'})
or return Status->error("Failed to get port ID for [$pn] from $device"); or return Status->error("Failed to get port ID for [$pn] from $device");
my $rv = $snmp->set_i_up_admin($data, $iid); my $rv = $snmp->set_i_up_admin($data, $iid);
@@ -69,7 +68,7 @@ sub _action {
} }
# update netdisco DB # update netdisco DB
$port->update({up_admin => $data}); vars->{'port'}->update({up_admin => $data});
return Status->done("Updated [$pn] up_admin on [$device] to [$data]"); return Status->done("Updated [$pn] up_admin on [$device] to [$data]");
} }

View File

@@ -13,7 +13,7 @@ register_worker({ phase => 'check' }, sub {
return Status->error('Missing port (-p).') unless defined $job->port; return Status->error('Missing port (-p).') unless defined $job->port;
return Status->error('Missing name (-e).') unless defined $job->subaction; return Status->error('Missing name (-e).') unless defined $job->subaction;
my $port = get_port($job->device, $job->port) vars->{'port'} = get_port($job->device, $job->port)
or return Status->error(sprintf "Unknown port name [%s] on device %s", or return Status->error(sprintf "Unknown port name [%s] on device %s",
$job->port, $job->device); $job->port, $job->device);
@@ -28,9 +28,7 @@ register_worker({ phase => 'main' }, sub {
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update alias"); or return Status->defer("failed to connect to $device to update alias");
my $port = get_port($device, $pn); my $iid = get_iid($snmp, vars->{'port'})
my $iid = get_iid($snmp, $port)
or return Status->error("Failed to get port ID for [$pn] from $device"); or return Status->error("Failed to get port ID for [$pn] from $device");
my $rv = $snmp->set_i_alias($data, $iid); my $rv = $snmp->set_i_alias($data, $iid);
@@ -48,7 +46,7 @@ register_worker({ phase => 'main' }, sub {
} }
# update netdisco DB # update netdisco DB
$port->update({name => $data}); vars->{'port'}->update({name => $data});
return Status->done("Updated [$pn] alias on [$device] to [$data]"); return Status->done("Updated [$pn] alias on [$device] to [$data]");
}); });

View File

@@ -13,16 +13,16 @@ register_worker({ phase => 'check' }, sub {
return Status->error('Missing port (-p).') unless defined $job->port; return Status->error('Missing port (-p).') unless defined $job->port;
return Status->error('Missing status (-e).') unless defined $job->subaction; return Status->error('Missing status (-e).') unless defined $job->subaction;
my $port = get_port($job->device, $job->port) vars->{'port'} = get_port($job->device, $job->port)
or return Status->error(sprintf "Unknown port name [%s] on device %s", or return Status->error(sprintf "Unknown port name [%s] on device %s",
$job->port, $job->device); $job->port, $job->device);
my $vlan_reconfig_check = vlan_reconfig_check($port); my $vlan_reconfig_check = vlan_reconfig_check(vars->{'port'});
return Status->error("Cannot alter vlan: $vlan_reconfig_check") return Status->error("Cannot alter vlan: $vlan_reconfig_check")
if $vlan_reconfig_check; if $vlan_reconfig_check;
return Status->error("No PoE service on port [$pn] on device $device") return Status->error("No PoE service on port [$pn] on device $device")
unless $port->power; unless vars->{'port'}->power;
return Status->done('Power is able to run'); return Status->done('Power is able to run');
}); });
@@ -30,7 +30,6 @@ register_worker({ phase => 'check' }, sub {
register_worker({ phase => 'main' }, sub { register_worker({ phase => 'main' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my ($device, $pn) = map {$job->$_} qw/device port/; my ($device, $pn) = map {$job->$_} qw/device port/;
my $port = get_port($device, $pn);
# munge data # munge data
(my $data = $job->subaction) =~ s/-\w+//; # remove -other (my $data = $job->subaction) =~ s/-\w+//; # remove -other
@@ -41,7 +40,7 @@ register_worker({ phase => 'main' }, sub {
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update vlan"); or return Status->defer("failed to connect to $device to update vlan");
my $powerid = get_powerid($snmp, $port) my $powerid = get_powerid($snmp, vars->{'port'})
or return Status->error("failed to get power ID for [$pn] from $device"); or return Status->error("failed to get power ID for [$pn] from $device");
my $rv = $snmp->set_peth_port_admin($data, $powerid); my $rv = $snmp->set_peth_port_admin($data, $powerid);
@@ -59,7 +58,7 @@ register_worker({ phase => 'main' }, sub {
} }
# update netdisco DB # update netdisco DB
$port->power->update({ vars->{'port'}->power->update({
admin => $data, admin => $data,
status => ($data eq 'false' ? 'disabled' : 'searching'), status => ($data eq 'false' ? 'disabled' : 'searching'),
}); });

View File

@@ -13,14 +13,14 @@ register_worker({ phase => 'check' }, sub {
return Status->error('Missing port (-p).') if !defined $pn; return Status->error('Missing port (-p).') if !defined $pn;
return Status->error('Missing vlan (-e).') if !defined $data; return Status->error('Missing vlan (-e).') if !defined $data;
my $port = get_port($device, $pn) vars->{'port'} = get_port($device, $pn)
or return Status->error("Unknown port name [$pn] on device $device"); or return Status->error("Unknown port name [$pn] on device $device");
my $port_reconfig_check = port_reconfig_check($port); my $port_reconfig_check = port_reconfig_check(vars->{'port'});
return Status->error("Cannot alter port: $port_reconfig_check") return Status->error("Cannot alter port: $port_reconfig_check")
if $port_reconfig_check; if $port_reconfig_check;
my $vlan_reconfig_check = vlan_reconfig_check($port); my $vlan_reconfig_check = vlan_reconfig_check(vars->{'port'});
return Status->error("Cannot alter vlan: $vlan_reconfig_check") return Status->error("Cannot alter vlan: $vlan_reconfig_check")
if $vlan_reconfig_check; if $vlan_reconfig_check;

View File

@@ -0,0 +1,61 @@
package App::Netdisco::Worker::Plugin::Vlan::Native;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Port ':all';
register_worker({ phase => 'early', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_;
my ($device, $pn) = map {$job->$_} qw/device port/;
# snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update vlan/pvid");
vars->{'iid'} = get_iid($snmp, vars->{'port'})
or return Status->error("Failed to get port ID for [$pn] from $device");
return Status->noop("Vlan set can continue.");
});
register_worker({ phase => 'main', driver => 'snmp' }, sub {
return unless defined vars->{'iid'};
_action($job, 'pvid');
return _action($job, 'vlan');
}
sub _action {
my ($job, $slot) = @_;
my ($device, $pn, $data) = map {$job->$_} qw/device port extra/;
my $getter = "i_${slot}";
my $setter = "set_i_${slot}";
# snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update $slot");
my $rv = $snmp->$setter($data, vars->{'iid'});
if (!defined $rv) {
return Status->error(sprintf 'Failed to set [%s] %s to [%s] on $device: %s',
$pn, $slot, $data, ($snmp->error || ''));
}
# confirm the set happened
$snmp->clear_cache;
my $state = ($snmp->$getter(vars->{'iid'}) || '');
if (ref {} ne ref $state or $state->{ vars->{'iid'} } ne $data) {
return Status->error("Verify of [$pn] $slot failed on $device");
}
# update netdisco DB
vars->{'port'}->update({$slot => $data});
return Status->done("Updated [$pn] $slot on [$device] to [$data]");
});
true;

View File

@@ -1,44 +0,0 @@
package App::Netdisco::Worker::Plugin::Vlan::Native;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Port ':all';
register_worker({ phase => 'main' }, sub {
my ($job, $workerconf) = @_;
my ($device, $pn, $data) = map {$job->$_} qw/device port extra/;
# snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update vlan");
my $port = get_port($device, $pn)
or return Status->error("Unknown port name [$pn] on device $device");
my $iid = get_iid($snmp, $port)
or return Status->error("Failed to get port ID for [$pn] from $device");
my $rv = $snmp->set_i_vlan($data, $iid);
if (!defined $rv) {
return Status->error(sprintf 'Failed to set [%s] vlan to [%s] on $device: %s',
$pn, $data, ($snmp->error || ''));
}
# confirm the set happened
$snmp->clear_cache;
my $state = ($snmp->i_vlan($iid) || '');
if (ref {} ne ref $state or $state->{$iid} ne $data) {
return Status->error("Verify of [$pn] vlan failed on $device");
}
# update netdisco DB
$port->update({vlan => $data});
return Status->done("Updated [$pn] vlan on [$device] to [$data]");
});
true;

View File

@@ -1,44 +0,0 @@
package App::Netdisco::Worker::Plugin::Vlan::Port;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP;
use App::Netdisco::Util::Port ':all';
register_worker({ phase => 'main' }, sub {
my ($job, $workerconf) = @_;
my ($device, $pn, $data) = map {$job->$_} qw/device port extra/;
# snmp connect using rw community
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
or return Status->defer("failed to connect to $device to update pvid");
my $port = get_port($device, $pn)
or return Status->error("Unknown port name [$pn] on device $device");
my $iid = get_iid($snmp, $port)
or return Status->error("Failed to get port ID for [$pn] from $device");
my $rv = $snmp->set_i_pvid($data, $iid);
if (!defined $rv) {
return Status->error(sprintf 'Failed to set [%s] pvid to [%s] on $device: %s',
$pn, $data, ($snmp->error || ''));
}
# confirm the set happened
$snmp->clear_cache;
my $state = ($snmp->i_pvid($iid) || '');
if (ref {} ne ref $state or $state->{$iid} ne $data) {
return Status->error("Verify of [$pn] pvid failed on $device");
}
# update netdisco DB
$port->update({pvid => $data});
return Status->done("Updated [$pn] pvid on [$device] to [$data]");
});
true;