Move applyConf in Manager::Plugin (#2245)

This commit is contained in:
Maxime Besson 2020-12-28 17:46:38 +01:00
parent 13b1097304
commit 279ea39e94
3 changed files with 74 additions and 65 deletions

View File

@ -30,6 +30,8 @@ use constant defaultRoute => 'api.html';
sub init {
my ( $self, $conf ) = @_;
$self->ua( Lemonldap::NG::Common::UserAgent->new($conf) );
# HTML template
$self->addRoute( 'api.html', undef, ['GET'] )

View File

@ -30,8 +30,6 @@ our $VERSION = '2.0.10';
use constant defaultRoute => 'manager.html';
has ua => ( is => 'rw' );
sub init {
my ( $self, $conf ) = @_;
$self->ua( Lemonldap::NG::Common::UserAgent->new($conf) );
@ -306,12 +304,10 @@ sub prx {
# IV. Upload methods #
######################
# In this section, 4 methods:
# In this section, 3 methods:
# - getConfByNum: override SUPER method to be able to use Zero
# - newConf()
# - newConf(), load a new configuration and invokes reloadUrls
# - newRawConf(): restore a saved conf
# - applyConf(): called by the 2 previous to inform other servers that a new
# configuration is available
sub getConfByNum {
my ( $self, $cfgNum, @args ) = @_;
@ -479,65 +475,6 @@ sub newRawConf {
return $self->sendJSONresponse( $req, $res );
}
## @method private applyConf()
# Try to inform other servers declared in `reloadUrls` that a new
# configuration is available.
#
#@return reload status as boolean
sub applyConf {
my ( $self, $newConf ) = @_;
my $status;
# 1 Apply conf locally
$self->p->api->checkConf();
# Get apply section values
my %reloadUrls =
%{ $self->confAcc->getLocalConf( APPLYSECTION, undef, 0 ) };
if ( !%reloadUrls && $newConf->{reloadUrls} ) {
%reloadUrls = %{ $newConf->{reloadUrls} };
}
return {} unless (%reloadUrls);
$self->ua->timeout( $newConf->{reloadTimeout} );
# Parse apply values
while ( my ( $host, $request ) = each %reloadUrls ) {
my $r = HTTP::Request->new( 'GET', "http://$host$request" );
$self->logger->debug("Sending reload request to $host");
if ( $request =~ /^https?:\/\/[^\/]+.*$/ ) {
my $url = URI::URL->new($request);
my $targetUrl = $url->scheme . "://" . $host;
$targetUrl .= ":" . $url->port if defined( $url->port );
$targetUrl .= $url->full_path;
$r =
HTTP::Request->new( 'GET', $targetUrl,
HTTP::Headers->new( Host => $url->host ) );
if ( defined $url->userinfo
&& $url->userinfo =~ /^([^:]+):(.*)$/ )
{
$r->authorization_basic( $1, $2 );
}
}
my $response = $self->ua->request($r);
if ( $response->code != 200 ) {
$status->{$host} =
"Error " . $response->code . " (" . $response->message . ")";
$self->logger->error( "Apply configuration for $host: error "
. $response->code . " ("
. $response->message
. ")" );
}
else {
$status->{$host} = "OK";
$self->logger->notice("Apply configuration for $host: ok");
}
}
return $status;
}
sub diff {
my ( $self, $req, @path ) = @_;
return $self->sendError( $req, 'to many arguments in path info', 400 )

View File

@ -2,6 +2,9 @@ package Lemonldap::NG::Manager::Plugin;
use strict;
use Mouse;
use Lemonldap::NG::Common::UserAgent;
use Lemonldap::NG::Common::Conf::Constants;
use URI::URL;
our $VERSION = '2.0.10';
@ -13,6 +16,14 @@ has _confAcc => (
default => sub { return $_[0]->p->{_confAcc} },
);
has ua => (
is => 'rw',
lazy => 1,
builder => sub {
Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
}
);
sub sendError {
my $self = shift;
return $self->p->sendError(@_);
@ -49,4 +60,63 @@ sub loadTemplate {
return $self->p->loadTemplate(@_);
}
## @method private applyConf()
# Try to inform other servers declared in `reloadUrls` that a new
# configuration is available.
#
#@return reload status as boolean
sub applyConf {
my ( $self, $newConf ) = @_;
my $status;
# 1 Apply conf locally
$self->p->api->checkConf();
# Get apply section values
my %reloadUrls =
%{ $self->confAcc->getLocalConf( APPLYSECTION, undef, 0 ) };
if ( !%reloadUrls && $newConf->{reloadUrls} ) {
%reloadUrls = %{ $newConf->{reloadUrls} };
}
return {} unless (%reloadUrls);
$self->ua->timeout( $newConf->{reloadTimeout} );
# Parse apply values
while ( my ( $host, $request ) = each %reloadUrls ) {
my $r = HTTP::Request->new( 'GET', "http://$host$request" );
$self->logger->debug("Sending reload request to $host");
if ( $request =~ /^https?:\/\/[^\/]+.*$/ ) {
my $url = URI::URL->new($request);
my $targetUrl = $url->scheme . "://" . $host;
$targetUrl .= ":" . $url->port if defined( $url->port );
$targetUrl .= $url->full_path;
$r =
HTTP::Request->new( 'GET', $targetUrl,
HTTP::Headers->new( Host => $url->host ) );
if ( defined $url->userinfo
&& $url->userinfo =~ /^([^:]+):(.*)$/ )
{
$r->authorization_basic( $1, $2 );
}
}
my $response = $self->ua->request($r);
if ( $response->code != 200 ) {
$status->{$host} =
"Error " . $response->code . " (" . $response->message . ")";
$self->logger->error( "Apply configuration for $host: error "
. $response->code . " ("
. $response->message
. ")" );
}
else {
$status->{$host} = "OK";
$self->logger->notice("Apply configuration for $host: ok");
}
}
return $status;
}
1;