Rex

 view release on metacpan or  search on metacpan

lib/Rex/CLI.pm  view on Meta::CPAN

  Rex::Logger::debug("Listing Groups");

  my %groups      = Rex::Group->get_groups;
  my @group_names = sort keys %groups;

  return unless @group_names;

  _print_color( "Server Groups\n", "yellow" );
  my $max_group_len = max map { length } @group_names;
  my $fmt           = " %-" . $max_group_len . "s  %s\n";

  for my $group_name (@group_names) {
    my $hosts  = join( ", ", sort @{ $groups{$group_name} } );
    my $output = sprintf $fmt, $group_name, $hosts;
    my $indent = " " x $max_group_len . "   ";
    print wrap( "", $indent, $output );
  }
}

sub summarize {
  my ($signal) = @_;
  my %opts = Rex::Args->getopts;
  return if $opts{'T'};

  my @summary = Rex::TaskList->create()->get_summary();
  return unless @summary; # no tasks ran -- nothing to summarize

  my @failures = grep { $_->{exit_code} != 0 } @summary;

  if ( !@failures ) {
    Rex::Logger::info("All tasks successful on all hosts");
    return;
  }

  Rex::Logger::info( @failures . " out of " . @summary . " task(s) failed:",
    "error" );

  foreach (
    sort {
           ncmp( $a->{task}, $b->{task} )
        || ncmp( $a->{server}, $b->{server} )
    } @failures
    )
  {
    Rex::Logger::info( "\t$_->{task} failed on $_->{server}", "error" );
    if ( $_->{error_message} ) {
      for my $line ( split( $INPUT_RECORD_SEPARATOR, $_->{error_message} ) ) {
        Rex::Logger::info( "\t\t$line", "error" );
      }
    }
  }
}

sub handle_lock_file {
  my $rexfile = shift;

  if ( $OSNAME !~ m/^MSWin/ ) {
    if ( -f "$rexfile.lock" && !exists $opts{'F'} ) {
      Rex::Logger::debug("Found $rexfile.lock");
      my $pid = eval {
        local ( @ARGV, $INPUT_RECORD_SEPARATOR ) = ("$rexfile.lock");
        <>;
      };
      system(
        "ps aux | awk -F' ' ' { print \$2 } ' | grep $pid >/dev/null 2>&1");
      if ( $CHILD_ERROR == 0 ) {
        Rex::Logger::info("Rexfile is in use by $pid.");
        CORE::exit 1;
      }
      else {
        Rex::Logger::debug("Found stale lock file. Removing it.");
        Rex::global_sudo(0);
        CORE::unlink("$rexfile.lock");
      }
    }

    Rex::Logger::debug("Creating lock-file ($rexfile.lock)");
    open( my $f, ">", "$rexfile.lock" ) or die($OS_ERROR);
    print $f $PID;
    close($f);
  }
  else {
    Rex::Logger::debug("Running on windows. Disabled lock file support.");
  }
}

sub load_server_ini_file {
  my $rexfile = shift;

  # load server ini file
  my $env             = environment;
  my $ini_dir         = dirname($rexfile);
  my $server_ini_file = "$ini_dir/server.$env.ini";
  $server_ini_file = "$ini_dir/server.ini" unless -f $server_ini_file;

  if ( -f $server_ini_file && Rex::Group::Lookup::INI->is_loadable ) {
    Rex::Logger::debug("Loading $server_ini_file");
    Rex::Group::Lookup::INI::groups_file($server_ini_file);
  }
}

sub load_rexfile {
  my $rexfile = shift;
  Rex::Logger::debug("Loading $rexfile");

  if ( !-f $rexfile ) {
    if ( !exists $opts{'e'} ) {
      Rex::Logger::info( "No Rexfile found.", "warn" );
      Rex::Logger::info( "Create a file named 'Rexfile' in this directory,",
        "warn" );
      Rex::Logger::info( "or specify the file you want to use with:", "warn" );
      Rex::Logger::info( "   rex -f file_to_use task_to_run",         "warn" );
    }
    return;
  }

  my $rexfile_dir = dirname $rexfile;
  my @new_inc     = Rex::generate_inc($rexfile_dir);
  @INC = @new_inc;

  # load Rexfile



( run in 1.313 second using v1.01-cache-2.11-cpan-364913b4093 )