Skip to content
Snippets Groups Projects
Select Git revision
  • 93d1c57f5aff972a0160a61d203c3d9fbacc2b2c
  • master default protected
  • diag
  • version1.3-archivematica
  • version1.2-archivematica
  • version1.1-archivematica
  • version1.0-archivematica
  • version1.0-rosetta
8 results

init.pm

Blame
  • init.pm 2.29 KiB
    package SLUB::LZA::TA::Command::init;
    use SLUB::LZA::TA -command;
    use strict;
    use warnings;
    use YAML qw(DumpFile);
    use feature qw(say);
    use IO::Prompt::Tiny qw/prompt/;
    use Crypt::Mode::CBC;
    use Path::Tiny;
    use namespace::autoclean;
    
    # VERSION
    
    # ABSTRACT: init module for ta-tool
    
    sub abstract {"Initialize $0";}
    sub description {"Initialize $0, preparing config file."}
    sub opt_spec {
        return(
            ["help|h" => "this usage screen"],
            ["verbose|v" => "enable verbose output"],
            ["elasticsearchhost|e=s" => "host adress where Archivematica's ElasticSearch runs", {required=>1}],
            ["elasticsearchport|E=s" => "port of Archivematica's ElasticSearch", {default => 9200}],
            ["logdir|l=s" => "logdir where Archivematica stores it server log files", {required=>1}],
            ["httponly" => "with this flag only HTTP (instead HTTPS) is used"],
        );
    }
    sub validate_args {
        my ($self, $opt, $args) = @_;
        # no args allowed but options!
        $self->usage_error("No args allowed") if @$args;
        1;
    }
    
    sub execute {
        my ($self, $opt, $args) = @_;
        my %config;
        $config{elasticsearchhost} = $opt->{elasticsearchhost};
        $config{elasticsearchport} = $opt->{elasticsearchport};
        $config{logdir} = $opt->{logdir};
        $config{httponly} = $opt->{httponly};
        my $in_test = $ENV{TEST_ACTIVE};
        local *IO::Prompt::Tiny::_is_interactive = sub {$in_test}; # fake it for testing
        warn "HINT: The password will stored in config file!";
        my $user = prompt('User:');
    RETRY:
        my $passwd1 = prompt('Password:', -echo => "*");
        my $passwd2 = prompt('Password, again:', -echo => "*");
        if ($passwd1 ne $passwd2) {
            say "you typed different passwords, retry";
            goto RETRY;
        }
        say "user: $user";
        say "pw1: $passwd1";
        say "pw2: $passwd2";
        $config{user} = "$user";
        $config{password} = SLUB::LZA::TA::Crypt::encrypt("$passwd1");
        if (defined $SLUB::LZA::TA::config_file) {
            if (defined $opt->{verbose}) {
                say "store config in $SLUB::LZA::TA::config_file";
            }
            my $file = path($SLUB::LZA::TA::config_file);
            $file->touch();
            $file->chmod("0600");
            my $fh = path($SLUB::LZA::TA::config_file)->filehandle({ exclusive => 0 }, ">");
            YAML::DumpFile($fh, \%config);
            $fh->close;
        }
        return 1;
    }