Skip to content

Commit 9d64476

Browse files
authored
Merge pull request #1202 from metacpan/oalders/cloud-config
Move Elasticsearch server setup to config files
2 parents fe5bdc4 + 3423628 commit 9d64476

File tree

26 files changed

+333
-407
lines changed

26 files changed

+333
-407
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
command: |
2222
git clone https://github.com/metacpan/metacpan-docker.git
2323
cd metacpan-docker
24+
git switch oalders/cloud-config
2425
name: metacpan-docker checkout
2526
- checkout:
2627
path: metacpan-docker/src/metacpan-api

etc/metacpan.pl

Lines changed: 0 additions & 36 deletions
This file was deleted.

etc/metacpan_testing.pl

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/Catalyst/Plugin/Session/Store/ElasticSearch.pm

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use Moose;
66
extends 'Catalyst::Plugin::Session::Store';
77
use MooseX::Types::ElasticSearch qw( ES );
88

9+
use MetaCPAN::Server::Config ();
10+
911
has _session_es => (
10-
required => 1,
11-
is => 'ro',
12-
coerce => 1,
13-
isa => ES,
14-
default => sub { shift->_session_plugin_config->{servers} || ':9200' }
12+
is => 'ro',
13+
lazy => 1,
14+
coerce => 1,
15+
isa => ES,
16+
default =>
17+
sub { MetaCPAN::Server::Config::config()->{elasticsearch_servers} },
1518
);
1619
has _session_es_index => (
1720
required => 1,

lib/MetaCPAN/Role/HasConfig.pm

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package MetaCPAN::Role::HasConfig;
22

33
use Moose::Role;
44

5-
use Config::ZOMG ();
6-
use FindBin ();
5+
use MetaCPAN::Server::Config ();
76
use MetaCPAN::Types::TypeTiny qw( HashRef );
87
use MetaCPAN::Util qw( checkout_root );
98

@@ -20,29 +19,8 @@ has _config => (
2019
);
2120

2221
sub _build_config {
23-
my $self = shift;
24-
my $config = $self->_zomg("$FindBin::RealBin/..");
25-
return $config if $config;
26-
27-
$config = $self->_zomg( checkout_root() );
28-
29-
return $config if $config;
30-
31-
die "Couldn't find config file in $FindBin::RealBin/.. or "
32-
. checkout_root();
33-
}
34-
35-
sub _zomg {
3622
my $self = shift;
37-
my $path = shift;
38-
39-
my $config = Config::ZOMG->new(
40-
local_suffix => $ENV{HARNESS_ACTIVE} ? 'testing' : 'local',
41-
name => 'metacpan_server',
42-
path => $path,
43-
);
44-
45-
return $config->open;
23+
return MetaCPAN::Server::Config::config();
4624
}
4725

4826
1;

lib/MetaCPAN/Role/Logger.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package MetaCPAN::Role::Logger;
22

33
use v5.10;
44
use Moose::Role;
5+
56
use Log::Contextual qw( set_logger );
67
use Log::Log4perl ':easy';
78
use MetaCPAN::Types::TypeTiny qw( Logger Str );
@@ -45,8 +46,7 @@ sub set_logger_once {
4546
return;
4647
}
4748

48-
# XXX NOT A MOOSE BUILDER
49-
# XXX This doesn't belong here.
49+
# Not actually a Moose builder, so we should probably rename it.
5050
sub _build_logger {
5151
my ($config) = @_;
5252
my $log = Log::Log4perl->get_logger( $ARGV[0]

lib/MetaCPAN/Role/Script.pm

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package MetaCPAN::Role::Script;
22

33
use Moose::Role;
44

5+
use Carp ();
56
use ElasticSearchX::Model::Document::Types qw( ES );
67
use File::Path ();
78
use IO::Prompt::Tiny qw( prompt );
@@ -12,8 +13,6 @@ use MetaCPAN::Util qw( checkout_root );
1213
use Mojo::Server ();
1314
use Term::ANSIColor qw( colored );
1415

15-
use Carp ();
16-
1716
with( 'MetaCPAN::Role::HasConfig', 'MetaCPAN::Role::Fastly',
1817
'MetaCPAN::Role::Logger' );
1918

@@ -74,21 +73,24 @@ has es => (
7473
is => 'ro',
7574
isa => ES,
7675
required => 1,
76+
init_arg => 'elasticsearch_servers',
7777
coerce => 1,
7878
documentation => 'Elasticsearch http connection string',
7979
);
8080

8181
has model => (
82-
is => 'ro',
83-
lazy => 1,
84-
builder => '_build_model',
85-
traits => ['NoGetopt'],
82+
is => 'ro',
83+
init_arg => undef,
84+
lazy => 1,
85+
builder => '_build_model',
86+
traits => ['NoGetopt'],
8687
);
8788

8889
has index => (
8990
reader => '_index',
9091
is => 'ro',
9192
isa => Str,
93+
lazy => 1,
9294
default => 'cpan',
9395
documentation =>
9496
'Index to use, defaults to "cpan" (when used: also export ES_SCRIPT_INDEX)',
@@ -98,13 +100,15 @@ has cluster_info => (
98100
isa => HashRef,
99101
traits => ['Hash'],
100102
is => 'rw',
103+
lazy => 1,
101104
default => sub { {} },
102105
);
103106

104107
has indices_info => (
105108
isa => HashRef,
106109
traits => ['Hash'],
107110
is => 'rw',
111+
lazy => 1,
108112
default => sub { {} },
109113
);
110114

@@ -118,7 +122,9 @@ has aliases_info => (
118122
has port => (
119123
isa => Int,
120124
is => 'ro',
121-
required => 1,
125+
required => 0,
126+
lazy => 1,
127+
default => sub {5000},
122128
documentation => 'Port for the proxy, defaults to 5000',
123129
);
124130

@@ -156,11 +162,11 @@ sub BUILDARGS {
156162
my ( $self, @args ) = @_;
157163
my %args = @args == 1 ? %{ $args[0] } : @args;
158164

159-
if ( exists $args{'index'} ) {
165+
if ( exists $args{index} ) {
160166
die
161167
"when setting --index, please export ES_SCRIPT_INDEX to the same value\n"
162-
unless $ENV{'ES_SCRIPT_INDEX'}
163-
and $args{'index'} eq $ENV{'ES_SCRIPT_INDEX'};
168+
unless $ENV{ES_SCRIPT_INDEX}
169+
and $args{index} eq $ENV{ES_SCRIPT_INDEX};
164170
}
165171

166172
return \%args;
@@ -183,11 +189,7 @@ sub handle_error {
183189
sub print_error {
184190
my ( $self, $error ) = @_;
185191

186-
# Always log.
187192
log_error {$error};
188-
189-
# Display Error in red
190-
print colored( ['bold red'], "*** ERROR ***: $error" ), "\n";
191193
}
192194

193195
sub index {
@@ -265,10 +267,7 @@ sub remote {
265267
sub run { }
266268
before run => sub {
267269
my $self = shift;
268-
269270
$self->set_logger_once;
270-
271-
#Dlog_debug {"Connected to $_"} $self->remote;
272271
};
273272

274273
sub _get_indices_info {
@@ -410,7 +409,7 @@ sub are_you_sure {
410409
}
411410
}
412411
else {
413-
print colored( ['bold yellow'], "*** Warning ***: $msg" ) . "\n";
412+
log_info {"*** Warning ***: $msg"};
414413
$iconfirmed = 1;
415414
}
416415

lib/MetaCPAN/Script/Mapping.pm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ sub delete_all {
254254
|| $runtime_environment eq 'testing';
255255

256256
if ($is_development) {
257-
$self->are_you_sure("ALL Indices will be deleted !!!");
258-
259257
foreach my $name ( keys %{ $self->indices_info } ) {
260258
$self->_delete_index($name);
261259
}
@@ -489,7 +487,7 @@ sub show_info {
489487
'indices_info' => \%{ $self->indices_info },
490488
'aliases_info' => \%{ $self->aliases_info }
491489
};
492-
print JSON->new->utf8->pretty->encode($info_rs);
490+
log_info { JSON->new->utf8->pretty->encode($info_rs) };
493491
}
494492

495493
sub _build_mapping {
@@ -538,7 +536,6 @@ sub _build_mapping {
538536
sub _build_aliases {
539537
my $self = $_[0];
540538
return { 'cpan' => $self->cpan_index };
541-
542539
}
543540

544541
sub deploy_mapping {

lib/MetaCPAN/Script/Runner.pm

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package MetaCPAN::Script::Runner;
33
use strict;
44
use warnings;
55

6-
use Config::ZOMG ();
7-
use File::Path ();
8-
use Hash::Merge::Simple qw( merge );
9-
use Module::Pluggable search_path => ['MetaCPAN::Script'];
6+
use File::Path ();
7+
use MetaCPAN::Server::Config ();
8+
use Module::Pluggable search_path => ['MetaCPAN::Script']; # plugins()
109
use Module::Runtime ();
1110
use Term::ANSIColor qw( colored );
1211
use Try::Tiny qw( catch try );
@@ -21,7 +20,7 @@ sub run {
2120
die "Usage: metacpan [command] [args]" unless ($class);
2221
Module::Runtime::require_module( $plugins{$class} );
2322

24-
my $config = build_config();
23+
my $config = MetaCPAN::Server::Config::config();
2524

2625
foreach my $logger ( @{ $config->{logger} || [] } ) {
2726
my $path = $logger->{filename} or next;
@@ -61,9 +60,11 @@ sub run {
6160
}
6261

6362
# Display Exception Message in red
64-
print colored( ['bold red'],
65-
"*** EXCEPTION [ $EXIT_CODE ] ***: " . $ex->{'message'} ),
66-
"\n";
63+
unless ( $ENV{HARNESS_ACTIVE} ) {
64+
print colored( ['bold red'],
65+
"*** exception [ $EXIT_CODE ] ***: " . $ex->{'message'} ),
66+
"\n";
67+
}
6768
};
6869

6970
unless ( defined $ex ) {
@@ -75,21 +76,6 @@ sub run {
7576
return ( $EXIT_CODE == 0 );
7677
}
7778

78-
sub build_config {
79-
my $config = Config::ZOMG->new(
80-
name => 'metacpan',
81-
path => 'etc'
82-
)->load;
83-
if ( $ENV{HARNESS_ACTIVE} ) {
84-
my $tconf = Config::ZOMG->new(
85-
name => 'metacpan',
86-
file => 'etc/metacpan_testing.pl'
87-
)->load;
88-
$config = merge $config, $tconf;
89-
}
90-
return $config;
91-
}
92-
9379
# AnyEvent::Run calls the main method
9480
*main = \&run;
9581

lib/MetaCPAN/Script/Snapshot.pm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use Cpanel::JSON::XS qw( decode_json encode_json );
77
use DateTime ();
88
use DateTime::Format::ISO8601 ();
99
use HTTP::Tiny ();
10-
use Log::Contextual qw( :log :dlog );
10+
use Log::Contextual qw( :log );
11+
use MetaCPAN::Server::Config ();
1112
use MetaCPAN::Types::TypeTiny qw( ArrayRef Bool Str );
1213
use Moose;
1314
use Sys::Hostname qw( hostname );
@@ -79,9 +80,10 @@ has snap_name => (
7980
);
8081

8182
has host => (
82-
is => 'ro',
83-
isa => Str,
84-
default => 'http://localhost:9200',
83+
is => 'ro',
84+
isa => Str,
85+
default =>
86+
sub { MetaCPAN::Server::Config::config()->{elasticsearch_servers} },
8587
documentation => 'ES host, defaults to: http://localhost:9200',
8688
);
8789

0 commit comments

Comments
 (0)