| 1 |
package Lemonldap::NG::Manager::Plugin; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Mouse; |
| 5 |
use Lemonldap::NG::Common::UserAgent; |
| 6 |
use Lemonldap::NG::Common::Conf::Constants; |
| 7 |
use URI::URL; |
| 8 |
|
| 9 |
our $VERSION = '2.18.0'; |
| 10 |
|
| 11 |
extends 'Lemonldap::NG::Common::Module'; |
| 12 |
|
| 13 |
has _confAcc => ( |
| 14 |
is => 'rw', |
| 15 |
lazy => 1, |
| 16 |
default => sub { return $_[0]->p->{_confAcc} }, |
| 17 |
); |
| 18 |
|
| 19 |
has ua => ( |
| 20 |
is => 'rw', |
| 21 |
lazy => 1, |
| 22 |
builder => sub { |
| 23 |
Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); |
| 24 |
} |
| 25 |
); |
| 26 |
|
| 27 |
# Icon from https://www.w3schools.com/icons/bootstrap_icons_glyphicons.asp |
| 28 |
use constant icon => 'wrench'; |
| 29 |
|
| 30 |
sub sendError { |
| 31 |
my $self = shift; |
| 32 |
return $self->p->sendError(@_); |
| 33 |
} |
| 34 |
|
| 35 |
sub sendJSONresponse { |
| 36 |
my $self = shift; |
| 37 |
return $self->p->sendJSONresponse(@_); |
| 38 |
} |
| 39 |
|
| 40 |
sub addRoute { |
| 41 |
my ( $self, $word, $subName, $methods, $transform ) = @_; |
| 42 |
$transform //= sub { |
| 43 |
my ($sub) = @_; |
| 44 |
if ( ref $sub ) { |
| 45 |
return sub { |
| 46 |
shift; |
| 47 |
return $sub->( $self, @_ ); |
| 48 |
} |
| 49 |
} |
| 50 |
else { |
| 51 |
return sub { |
| 52 |
shift; |
| 53 |
return $self->$sub(@_); |
| 54 |
} |
| 55 |
} |
| 56 |
}; |
| 57 |
$self->p->addRoute( $word, $subName, $methods, $transform ); |
| 58 |
return $self; |
| 59 |
} |
| 60 |
|
| 61 |
sub loadTemplate { |
| 62 |
my $self = shift; |
| 63 |
return $self->p->loadTemplate(@_); |
| 64 |
} |
| 65 |
|
| 66 |
## @method private applyConf() |
| 67 |
# Try to inform other servers declared in `reloadUrls` that a new |
| 68 |
# configuration is available. |
| 69 |
# |
| 70 |
#@return reload status as boolean |
| 71 |
sub applyConf { |
| 72 |
my ( $self, $newConf ) = @_; |
| 73 |
return |
| 74 |
unless $self->p->api->tsv->{msgBrokerWriter} |
| 75 |
and ref( $self->p->api->tsv->{msgBrokerWriter} ) =~ /NoBroker$/; |
| 76 |
|
| 77 |
my $status; |
| 78 |
|
| 79 |
# 1 Apply conf locally |
| 80 |
$self->p->api->checkConf(); |
| 81 |
|
| 82 |
# Get apply section values |
| 83 |
my %reloadUrls = |
| 84 |
%{ $self->confAcc->getLocalConf( APPLYSECTION, undef, 0 ) }; |
| 85 |
if ( !%reloadUrls && $newConf->{reloadUrls} ) { |
| 86 |
%reloadUrls = %{ $newConf->{reloadUrls} }; |
| 87 |
} |
| 88 |
return {} unless (%reloadUrls); |
| 89 |
|
| 90 |
$self->ua->timeout( $newConf->{reloadTimeout} ); |
| 91 |
|
| 92 |
# Parse apply values |
| 93 |
while ( my ( $host, $request ) = each %reloadUrls ) { |
| 94 |
my $r = HTTP::Request->new( 'GET', "http://$host$request" ); |
| 95 |
$self->logger->debug("Sending reload request to $host"); |
| 96 |
if ( $request =~ /^https?:\/\/[^\/]+.*$/ ) { |
| 97 |
my $url = URI::URL->new($request); |
| 98 |
my $targetUrl = $url->scheme . "://" . $host; |
| 99 |
$targetUrl .= ":" . $url->port if defined( $url->port ); |
| 100 |
$targetUrl .= $url->full_path; |
| 101 |
$r = |
| 102 |
HTTP::Request->new( 'GET', $targetUrl, |
| 103 |
HTTP::Headers->new( Host => $url->host ) ); |
| 104 |
if ( defined $url->userinfo |
| 105 |
&& $url->userinfo =~ /^([^:]+):(.*)$/ ) |
| 106 |
{ |
| 107 |
$r->authorization_basic( $1, $2 ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
my $response = $self->ua->request($r); |
| 112 |
if ( $response->code != 200 ) { |
| 113 |
$status->{$host} = |
| 114 |
"Error " . $response->code . " (" . $response->message . ")"; |
| 115 |
$self->logger->error( "Apply configuration for $host: error " |
| 116 |
. $response->code . " (" |
| 117 |
. $response->message |
| 118 |
. ")" ); |
| 119 |
} |
| 120 |
else { |
| 121 |
$status->{$host} = "OK"; |
| 122 |
$self->logger->notice("Apply configuration for $host: ok"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $status; |
| 127 |
} |
| 128 |
|
| 129 |
1; |