Rex
view release on metacpan or search on metacpan
lib/Rex/CLI.pm view on Meta::CPAN
my $host_eval = eval { `$opts{'z'}`; };
if ( $host_eval =~ m/\S/xms ) {
$::FORCE_SERVER = join( " ", split /\n|,|;/, $host_eval );
}
else {
Rex::Logger::info("You must give a valid command.");
}
}
if ( $opts{'o'} ) {
Rex::Output->require;
Rex::Output->get( $opts{'o'} );
}
handle_lock_file($::rexfile);
Rex::Config->set_environment( $opts{"E"} ) if ( $opts{"E"} );
if ( $opts{'g'} || $opts{'G'} ) {
#$::FORCE_SERVER = "\0" . $opts{'g'};
$opts{'g'} ||= $opts{'G'};
if ( ref $opts{'g'} ne "ARRAY" ) {
$::FORCE_SERVER = [ $opts{'g'} ];
}
else {
$::FORCE_SERVER = $opts{'g'};
}
}
load_server_ini_file($::rexfile);
load_rexfile($::rexfile);
#### check if some parameters should be overwritten from the command line
CHECK_OVERWRITE: {
my $pass_auth = 0;
if ( $opts{'u'} ) {
Rex::Commands::user( $opts{'u'} );
for my $task ( Rex::TaskList->create()->get_tasks ) {
Rex::TaskList->create()->get_task($task)->set_user( $opts{'u'} );
}
}
if ( $opts{'p'} ) {
Rex::Commands::password( $opts{'p'} );
unless ( $opts{'P'} ) {
$pass_auth = 1;
}
for my $task ( Rex::TaskList->create()->get_tasks ) {
Rex::TaskList->create()->get_task($task)->set_password( $opts{'p'} );
}
}
if ( $opts{'P'} ) {
Rex::Commands::private_key( $opts{'P'} );
for my $task ( Rex::TaskList->create()->get_tasks ) {
Rex::TaskList->create()
->get_task($task)
->set_auth( "private_key", $opts{'P'} );
}
}
if ( $opts{'K'} ) {
Rex::Commands::public_key( $opts{'K'} );
for my $task ( Rex::TaskList->create()->get_tasks ) {
Rex::TaskList->create()
->get_task($task)
->set_auth( "public_key", $opts{'K'} );
}
}
if ($pass_auth) {
pass_auth;
}
}
Rex::Logger::debug("Initializing Logger from parameters found in $::rexfile");
if ( $opts{'T'} && $opts{'m'} ) {
# create machine readable tasklist
my @tasks = Rex::TaskList->create()->get_tasks;
for my $task (@tasks) {
my $desc = Rex::TaskList->create()->get_desc($task);
$desc =~ s/'/\\'/gms;
print "'$task'" . " = '$desc'\n";
}
}
elsif ( $opts{'T'} && $opts{'y'} ) {
my @tasks = Rex::TaskList->create()->get_tasks;
my @envs = Rex::Commands->get_environments();
my %groups = Rex::Group->get_groups;
my %real_groups;
for my $group ( keys %groups ) {
my @servers = map { $_->get_servers }
Rex::Group->get_group_object($group)->get_servers;
$real_groups{$group} = \@servers;
}
print YAML::Dump(
{
tasks => \@tasks,
envs => \@envs,
groups => \%real_groups,
}
);
}
elsif ( $opts{'T'} ) {
_handle_T(%opts);
Rex::global_sudo(0);
Rex::Logger::debug("Removing lockfile") if ( !exists $opts{'F'} );
CORE::unlink("$::rexfile.lock") if ( !exists $opts{'F'} );
CORE::exit 0;
}
# turn sudo on with cli option s is used
if ( exists $opts{'s'} ) {
sudo("on");
}
if ( exists $opts{'S'} ) {
sudo_password( $opts{'S'} );
}
if ( exists $opts{'t'} ) {
parallelism( $opts{'t'} );
}
if ( $opts{'e'} ) {
Rex::Logger::debug("Executing command line code");
Rex::Logger::debug( "\t" . $opts{'e'} );
# execute the given code
my $code = "sub { \n";
$code .= $opts{'e'} . "\n";
$code .= "}";
$code = eval($code);
if ($EVAL_ERROR) {
Rex::Logger::info( "Error in eval line: $EVAL_ERROR\n", "warn" );
exit 1;
}
if ( exists $opts{'t'} ) {
parallelism( $opts{'t'} );
}
my $pass_auth = 0;
if ( $opts{'u'} ) {
Rex::Commands::user( $opts{'u'} );
}
if ( $opts{'p'} ) {
Rex::Commands::password( $opts{'p'} );
unless ( $opts{'P'} ) {
$pass_auth = 1;
}
}
if ( $opts{'P'} ) {
Rex::Commands::private_key( $opts{'P'} );
}
if ( $opts{'K'} ) {
Rex::Commands::public_key( $opts{'K'} );
}
if ($pass_auth) {
pass_auth;
}
my @params = ();
if ( $opts{'H'} ) {
push @params, split( /\s+/, $opts{'H'} );
}
push @params, $code;
push @params, "eval-line-desc";
push @params, {};
Rex::TaskList->create()->create_task( "eval-line", @params );
Rex::Commands::do_task("eval-line");
exit_rex();
}
elsif ( $opts{'M'} ) {
Rex::Logger::debug( "Loading Rex-Module: " . $opts{'M'} );
my $mod = $opts{'M'};
$mod =~ s{::}{/}g;
$mod .= ".pm";
require $mod;
}
my $run_list = Rex::RunList->instance;
if ( $opts{'b'} ) {
my $batch = $opts{'b'};
Rex::Logger::debug("Running batch: $batch");
$run_list->add_task($_) for Rex::Batch->get_batch($batch);
}
$run_list->parse_opts(@ARGV);
eval { $run_list->run_tasks };
if ($EVAL_ERROR) {
# this is always the child
Rex::Logger::info( "Error running task/batch: $EVAL_ERROR", "warn" );
CORE::exit(0);
}
exit_rex();
}
sub _print_color {
my ( $msg, $color ) = @_;
$color = 'green' if !defined($color);
if ($no_color) {
print $msg;
}
else {
print colored( [$color], $msg );
( run in 1.236 second using v1.01-cache-2.11-cpan-524268b4103 )