Skip to content

Commit cdf54f9

Browse files
committed
Added mapping creation script
1 parent d37517d commit cdf54f9

File tree

6 files changed

+96
-15
lines changed

6 files changed

+96
-15
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ EOT
2323

2424
COPY bin bin
2525
COPY lib lib
26+
COPY conf conf
2627
COPY *.conf .
2728

2829
ENV PERL5LIB="/app/local/lib/perl5:/app/lib" PATH="/app/local/bin:${PATH}"

bin/backup.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
);
3434

3535
# setup
36-
my $home = path( home() );
36+
my $home = home();
3737

3838
run_restore() if $restore;
3939
run_purge() if $purge;

bin/cpan_testers.pl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
);
2424

2525
# setup
26-
27-
# XXX fix hardcoded path
2826
my $home = home();
2927

3028
my $db

bin/mapping.pl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use strict;
2+
use warnings;
3+
4+
use Cpanel::JSON::XS qw< decode_json >;
5+
use Getopt::Long;
6+
use MetaCPAN::Mapper;
7+
use MetaCPAN::Ingest qw< home >;
8+
9+
my ( $index, $cmd );
10+
GetOptions(
11+
"index=s" => \$index,
12+
"cmd=s" => \$cmd,
13+
);
14+
die "cmd can only be one of: 'create', 'delete'\n"
15+
unless grep { $cmd eq $_ } qw< create delete >;
16+
17+
# setup
18+
my $type = $index;
19+
20+
my $mapper = MetaCPAN::Mapper->new();
21+
22+
$mapper->index_delete($index)
23+
if $mapper->index_exists($index);
24+
25+
if ( $cmd eq 'create' ) {
26+
my $home = home();
27+
my $map_file = $home->child('conf/es/' . $index . '/mapping.json');
28+
my $mapping = decode_json $map_file->slurp();
29+
30+
$mapper->index_create($index);
31+
$mapper->index_put_mapping($index, $type, $mapping);
32+
}

lib/MetaCPAN/Ingest.pm

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use Cpanel::JSON::XS;
88
use Digest::SHA;
99
use Encode qw< decode_utf8 >;
1010
use IO::Prompt::Tiny qw< prompt >;
11-
use IPC::Run3 ();
11+
use File::Basename ();
12+
use File::Spec ();
1213
use LWP::UserAgent;
1314
use Path::Tiny qw< path >;
1415
use PAUSE::Permissions ();
@@ -183,17 +184,14 @@ sub handle_error ( $exit_code, $error, $die_always ) {
183184
}
184185

185186
sub home () {
186-
IPC::Run3::run3(
187-
[ qw< git rev-parse --show-toplevel > ], # TODO: use alternative persistent path that's accessible from the container
188-
\undef, \my $stdout, \my $stderr
189-
);
190-
191-
die $stderr if ($?);
192-
193-
chomp $stdout;
194-
die "Failed to find git dir: '$stdout'" unless -d $stdout;
195-
196-
return $stdout;
187+
my $dir = Cwd::abs_path( File::Spec->catdir(
188+
File::Basename::dirname(__FILE__),
189+
( File::Spec->updir ) x 2
190+
) );
191+
192+
my $path = path($dir);
193+
die "Failed to find git dir: '$path'" unless $path;
194+
return $path;
197195
}
198196

199197
# TODO: there must be a better way

lib/MetaCPAN/Mapper.pm

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package MetaCPAN::Mapper;
2+
3+
use strict;
4+
use warnings;
5+
use v5.36;
6+
7+
use Path::Tiny qw< path >;
8+
use MetaCPAN::Logger qw< :log :dlog >;
9+
use Search::Elasticsearch;
10+
use MetaCPAN::Ingest qw< config home >;
11+
12+
sub new ( $class, %args ) {
13+
my $node = $args{node};
14+
15+
my $config = config;
16+
$node ||= $config->{es_node};
17+
$node or die "Cannot create an ES instance without a node\n";
18+
19+
return bless {
20+
es => Search::Elasticsearch->new(
21+
client => '2_0::Direct',
22+
nodes => [$node],
23+
),
24+
}, $class;
25+
}
26+
27+
sub index_exists ($self, $index) {
28+
$self->{es}->indices->exists( index => $index );
29+
}
30+
31+
sub index_create ($self, $index) {
32+
$self->{es}->indices->create( index => $index );
33+
}
34+
35+
sub index_delete ($self, $index) {
36+
$self->{es}->indices->delete( index => $index );
37+
}
38+
39+
sub index_put_mapping ($self, $index, $type, $mapping) {
40+
$self->{es}->indices->put_mapping(
41+
index => $index,
42+
type => $type,
43+
body => $mapping,
44+
);
45+
}
46+
47+
sub get_mapping ($self, $index) {
48+
# my $home = home();
49+
# my $file = $dir->child('');
50+
}
51+
52+
1;

0 commit comments

Comments
 (0)