From 01d795c38184db34d273e9296e4fe3a6f58815c0 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Wed, 31 Jan 2018 14:54:31 +0000 Subject: [PATCH] #368 ND2_DB_ROLLBACK environment variable to roll back job updates to DB [skip ci] --- Changes | 1 + bin/netdisco-do | 11 ++++++++--- lib/App/Netdisco/Worker/Runner.pm | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index ef70e8a9..cb325a8b 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ * #228 timeout setting (default 10min) for backend jobs * #341 timeout setting for all actions ("_timeout") + * #368 ND2_DB_ROLLBACK environment variable to roll back job updates to DB [BUG FIXES] diff --git a/bin/netdisco-do b/bin/netdisco-do index 08d82bd2..7c363d33 100755 --- a/bin/netdisco-do +++ b/bin/netdisco-do @@ -50,7 +50,7 @@ use App::Netdisco::Util::Device 'get_device'; use Getopt::Long; Getopt::Long::Configure ("bundling"); -my ($device, $port, $extra, $debug); +my ($device, $port, $extra, $debug, $rollback); my ($infotrace, $snmptrace, $sqltrace) = (0, 0, 0); my $result = GetOptions( @@ -58,6 +58,7 @@ my $result = GetOptions( 'port|p=s' => \$port, 'extra|e=s' => \$extra, 'debug|D' => \$debug, + 'rollback|R' => \$rollback, 'infotrace|I+' => \$infotrace, 'snmptrace|S+' => \$snmptrace, 'sqltrace|Q+' => \$sqltrace, @@ -74,6 +75,7 @@ $CONFIG->{log} = ($debug ? 'debug' : 'info'); $ENV{INFO_TRACE} ||= $infotrace; $ENV{SNMP_TRACE} ||= $snmptrace; $ENV{DBIC_TRACE} ||= $sqltrace; +$ENV{ND2_DB_ROLLBACK} ||= $rollback; # reconfigure logging to force console output Dancer::Logger->init('console', $CONFIG); @@ -160,7 +162,7 @@ netdisco-do - Run any Netdisco job from the command-line. =head1 SYNOPSIS - ~/bin/netdisco-do [-DISQ] [-d [-p ] [-e ]] + ~/bin/netdisco-do [-DISQR] [-d [-p ] [-e ]] =head1 DESCRIPTION @@ -346,7 +348,10 @@ Set the PoE on/off status on a device port. Requires the C<-d> parameter Will dump the loaded and parsed configuration for the application. Pass a specific configuration setting name to the C<-e> parameter to dump only that. -=head1 DEBUG LEVELS +=head1 DEBUG OPTIONS + +The flag "C<-R>" will cause any changes to the database to be rolled back +at the end of the action. The flags "C<-DISQ>" can be specified, multiple times, and enable the following items in order: diff --git a/lib/App/Netdisco/Worker/Runner.pm b/lib/App/Netdisco/Worker/Runner.pm index bf604a15..f4fad1e4 100644 --- a/lib/App/Netdisco/Worker/Runner.pm +++ b/lib/App/Netdisco/Worker/Runner.pm @@ -1,6 +1,7 @@ package App::Netdisco::Worker::Runner; use Dancer qw/:moose :syntax/; +use Dancer::Plugin::DBIC 'schema'; use App::Netdisco::Util::Device 'get_device'; use App::Netdisco::Util::Permission qw/check_acl_no check_acl_only/; use aliased 'App::Netdisco::Worker::Status'; @@ -59,6 +60,9 @@ sub run { my $runner = sub { my ($self, $job) = @_; + # roll everything back if we're testing + my $txn_guard = $ENV{ND2_DB_ROLLBACK} + ? schema('netdisco')->storage->txn_scope_guard : undef; # run check phase and if there are workers then one MUST be successful $self->run_workers('workers_check');