diff --git a/bin/snapshot.pl b/bin/snapshot.pl index 2fa1d30..3b97419 100644 --- a/bin/snapshot.pl +++ b/bin/snapshot.pl @@ -8,27 +8,42 @@ use HTTP::Tiny (); use Getopt::Long; use Sys::Hostname qw< hostname >; +use Try::Tiny; use MetaCPAN::Logger qw< :log :dlog >; use MetaCPAN::ES; -use MetaCPAN::Ingest qw< are_you_sure >; +use MetaCPAN::Ingest qw< + are_you_sure + config + is_dev +>; # setup -my $hostname = hostname(); -my $mode = $hostname =~ /dev/ ? 'testing' : 'production'; +my $config = config(); +my $mode = is_dev() ? 'testing' : 'production'; my $bucket = "mc-${mode}-backups"; # So we don't break production my $repository_name = 'our_backups'; -#my $es = MetaCPAN::ES->new( type => "distribution" ); -#my $bulk = $es->bulk(); +my $aws_key = $config->{es_aws_s3_access_key}; +die "es_aws_s3_access_key not in config" unless $aws_key; + +my $aws_secret = $config->{es_aws_s3_secret}; +die "es_aws_s3_secret not in config" unless $aws_secret; + +my $http_client = HTTP::Tiny->new( + default_headers => { 'Accept' => 'application/json' }, + timeout => 120, # list can be slow +); # args my ( $date_format, $indices, $list, $purge_old, $restore, $setup, $snap, $snap_name, $snap_stub ); -my $host = MetaCPAN::Server::Config::config()->{elasticsearch_servers}; + +my $host = $config->{es_node}; + GetOptions( "list" => \$list, "date_format=s" => \$date_format, @@ -45,20 +60,7 @@ # Note: can take wild cards https://www.elastic.co/guide/en/elasticsearch/reference/2.4/multi-index.html $indices //= '*'; -my $config = {}; ## TODO ( use MetaCPAN::Server::Config (); ??? ) - -my $aws_key = $config->{es_aws_s3_access_key}; -my $aws_secret = $config->{es_aws_s3_secret}; - -my $http_client = HTTP::Tiny->new( - default_headers => { 'Accept' => 'application/json' }, - timeout => 120, # list can be slow -); - # run -die "es_aws_s3_access_key not in config" unless $aws_key; -die "es_aws_s3_secret not in config" unless $aws_secret; - run_list_snaps() if $list; run_setup() if $setup; run_snapshot() if $snap; @@ -205,7 +207,7 @@ ( $method, $path, $data ) Dlog_error {"Error response: $_"} $resp_json; } catch { - log_error { 'Error msg: ' . $response->{content} } + log_error { 'Error msg: ' . $response->{content} }; } return 0; } diff --git a/cpanfile b/cpanfile index 0497c40..1e77067 100644 --- a/cpanfile +++ b/cpanfile @@ -6,6 +6,7 @@ requires 'CPAN::DistnameInfo', '0.12'; requires 'Cpanel::JSON::XS'; requires 'Data::Printer'; requires 'DateTime'; +requires 'DateTime::Format::ISO8601'; requires 'Data::Printer'; requires 'DBI'; requires 'Digest::SHA'; @@ -37,7 +38,7 @@ requires 'XML::Simple'; requires 'MetaCPAN::Common', git => 'https://github.com/metacpan/MetaCPAN-Common', - ref => '48274b9cb890d7f76a8ba6e2fce78348ca1165ca'; + ref => '0e5c2852178a47e98db291f613da2b96aca85f64'; on test => sub { requires 'Code::TidyAll', '>= 0.74'; diff --git a/lib/MetaCPAN/Ingest.pm b/lib/MetaCPAN/Ingest.pm index 2c605d9..50b6fb4 100644 --- a/lib/MetaCPAN/Ingest.pm +++ b/lib/MetaCPAN/Ingest.pm @@ -32,6 +32,7 @@ use Sub::Exporter -setup => { fix_version handle_error home + is_dev minion numify_version read_00whois @@ -55,7 +56,7 @@ my $config //= do { }; $config->init_logger; -sub config () {$config} +sub config () { $config->config(); } sub are_you_sure ( $msg, $force=0 ) { return 1 if $force; @@ -187,6 +188,11 @@ sub home () { return $stdout; } +# TODO: there must be a better way +sub is_dev () { + return $ENV{PLACK_ENV} =~ /dev/; +} + sub minion () { require 'Mojo::Server'; return Mojo::Server->new->build_app('MetaCPAN::API')->minion;