Rex-JobControl

 view release on metacpan or  search on metacpan

lib/Rex/JobControl/Mojolicious/Command/jobcontrol.pm  view on Meta::CPAN


    if ( !-d dirname( $self->app->config->{auth}->{passwd} ) ) {
      $self->app->log->info( "Creating passwd path: "
          . dirname( $self->app->config->{auth}->{passwd} ) );
      File::Path::make_path( dirname( $self->app->config->{auth}->{passwd} ) );
      $changed = 1;
    }

    if ( exists $self->app->config->{log}->{audit_log}
      && !-d dirname( $self->app->config->{log}->{audit_log} ) )
    {
      $self->app->log->info( "Creating audit.log path: "
          . dirname( $self->app->config->{log}->{audit_log} ) );
      File::Path::make_path(
        dirname( $self->app->config->{log}->{audit_log} ) );
      $changed = 1;
    }

    if ( exists $self->app->config->{log}->{access_log}
      && !-d dirname( $self->app->config->{log}->{access_log} ) )
    {
      $self->app->log->info( "Creating access.log path: "
          . dirname( $self->app->config->{log}->{access_log} ) );
      File::Path::make_path(
        dirname( $self->app->config->{log}->{access_log} ) );
      $changed = 1;
    }

    if ( !-f $self->app->config->{auth}->{passwd} ) {
      $self->app->log->info(
        "No passwd file found. Creating one with user 'admin' and password 'admin'."
      );
      $self->add_user( "admin", "admin" );
      $changed = 1;
    }

    if ( $changed == 0 ) {
      $self->app->log->info("Everything seems ok.");
    }

  }

  if ( $command eq "adduser" ) {
    my ( $user, $password );

    GetOptionsFromArray \@args,
      'u|user=s'     => sub { $user     = $_[1] },
      'p|password=s' => sub { $password = $_[1] };

    $self->add_user( $user, $password );
  }

  if ( $command eq "deluser" ) {

    my $user;

    GetOptionsFromArray \@args, 'u|user=s' => sub { $user = $_[1] };

    my @lines =
      grep { !m/^$user:/ }
      eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };

    open( my $fh, ">", $self->app->config->{auth}->{passwd} ) or die($!);
    print $fh join( "\n", @lines );
    close($fh);
  }

  if ( $command eq "listuser" ) {
    my @lines =
      eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
    chomp @lines;

    for my $l (@lines) {
      my ( $user, $pass ) = split( /:/, $l );
      print "> $user\n";
    }
  }

}

sub add_user {
  my ( $self, $user, $password ) = @_;

  $self->app->log->debug("Creating new user $user with password $password");

  my $salt = $self->app->config->{auth}->{salt};
  my $cost = $self->app->config->{auth}->{cost};

  my $bcrypt = Digest::Bcrypt->new;
  $bcrypt->salt($salt);
  $bcrypt->cost($cost);
  $bcrypt->add($password);

  my $pw = $bcrypt->hexdigest;

  open( my $fh, ">>", $self->app->config->{auth}->{passwd} ) or die($!);
  print $fh "$user:$pw\n";
  close($fh);

}

sub create_upstart_unit {
  my ($self) = @_;

  my $pid_file = $self->app->config->{hypnotoad}->{pid_file};

  my $whereis_hypnotoad = qx{which hypnotoad};
  chomp $whereis_hypnotoad;

  open( my $fh, ">", "/etc/init/rex-jobcontrol.conf" ) or die($!);
  print $fh qq~# Rex::JobControl

description     "Rex::JobControl upstart job"

start on runlevel [2345]
stop on runlevel [!2345]

pre-start exec $whereis_hypnotoad $0
post-stop exec $whereis_hypnotoad $0 -s
~;
  close($fh);

  open( $fh, ">", "/etc/init/rex-jobcontrol-minion.conf" ) or die($!);
  print $fh qq~# Rex::JobControl Minion

description     "Rex::JobControl Minion upstart job"

start on runlevel [2345]
stop on runlevel [!2345]



( run in 0.621 second using v1.01-cache-2.11-cpan-5a3173703d6 )