From 53ac31e3c39d269ba5e995f265e863b33c439758 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Mon, 24 Feb 2020 11:58:29 +0100 Subject: [PATCH] CLI: add rollback option (#2103) --- lemonldap-ng-common/scripts/lemonldap-ng-cli | 7 ++++- .../lib/Lemonldap/NG/Manager/Cli.pm | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lemonldap-ng-common/scripts/lemonldap-ng-cli b/lemonldap-ng-common/scripts/lemonldap-ng-cli index 8d92655ba..448c20a03 100755 --- a/lemonldap-ng-common/scripts/lemonldap-ng-cli +++ b/lemonldap-ng-common/scripts/lemonldap-ng-cli @@ -22,7 +22,7 @@ for ( my $i = 0 ; $i < @ARGV ; $i++ ) { $action ||= "help"; -if ( $action =~ /^(?:[gs]et|(?:add|del)Key|save|restore)$/ ) { +if ( $action =~ /^(?:[gs]et|(?:add|del)Key|save|restore|rollback)$/ ) { eval { require Lemonldap::NG::Manager::Cli; }; die "Manager libraries not available, aborting ($@)" if ($@); Lemonldap::NG::Manager::Cli->run(@ARGV); @@ -50,6 +50,7 @@ Available actions: - save : export configuration to STDOUT - restore - : import configuration from STDIN - restore : import configuration from file + - rollback : restore previous configuration Options: - yes <0|1> : accept confirmation prompt automatically @@ -90,6 +91,10 @@ Restore configuration # OR $ lemonldap-ng-cli restore - mgr->confAcc->lastCfg; + my $previousCfg = $lastCfg - 1; + my $conf = + $self->mgr->confAcc->getConf( { cfgNum => $previousCfg, raw => 1 } ) + or die $Lemonldap::NG::Common::Conf::msg; + + $conf->{cfgNum} = $lastCfg; + $conf->{cfgAuthor} = scalar( getpwuid $< ) . '(command-line-interface)'; + chomp $conf->{cfgAuthor}; + $conf->{cfgAuthorIP} = '127.0.0.1'; + $conf->{cfgDate} = time; + $conf->{cfgVersion} = $Lemonldap::NG::Manager::VERSION; + $conf->{cfgLog} = $self->log // "Rolled back configuration $lastCfg"; + + my $s = $self->mgr->confAcc->saveConf($conf); + if ( $s > 0 ) { + $self->logger->info("CLI: Configuration $lastCfg has been rolled back"); + print STDERR "Configuration $lastCfg has been rolled back\n"; + } + else { + $self->logger->error("CLI: Failed to rollback configuration $lastCfg"); + print STDERR "Failed to rollback configuration $lastCfg\n"; + } +} + sub _getKey { my ( $self, $key ) = @_; my $sep = $self->sep; @@ -355,7 +382,7 @@ sub run { } $self->cfgNum( $self->lastCfg ) unless ( $self->cfgNum ); my $action = shift; - unless ( $action =~ /^(?:get|set|addKey|delKey|save|restore)$/ ) { + unless ( $action =~ /^(?:get|set|addKey|delKey|save|restore|rollback)$/ ) { die "Unknown action $action. Only get, set, addKey or delKey allowed"; }