/usr/share/perl5/Rex/Helper/Run.pm is in rex 1.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::Helper::Run;
use strict;
use warnings;
our $VERSION = '1.6.0'; # VERSION
require Exporter;
use base qw(Exporter);
use vars qw(@EXPORT);
use Net::OpenSSH::ShellQuoter;
use Rex::Interface::File;
use Rex::Interface::Fs;
use Rex::Helper::Path;
use Carp;
require Rex::Commands;
require Rex::Config;
@EXPORT = qw(upload_and_run i_run i_exec i_exec_nohup);
sub upload_and_run {
my ( $template, %option ) = @_;
my $rnd_file = get_tmp_file;
my $fh = Rex::Interface::File->create;
$fh->open( ">", $rnd_file );
$fh->write($template);
$fh->close;
my $fs = Rex::Interface::Fs->create;
$fs->chmod( 755, $rnd_file );
my @argv;
my $command = $rnd_file;
if ( exists $option{with} ) {
$command = Rex::Config->get_executor_for( $option{with} ) . " $command";
}
if ( exists $option{args} ) {
$command .= join( " ", @{ $option{args} } );
}
return i_run("$command 2>&1");
}
# internal run command, doesn't get reported
sub i_run {
my $cmd = shift;
my ( $code, $option );
$option = {};
if ( ref $_[0] eq "CODE" ) {
$code = shift;
}
if ( scalar @_ > 0 ) {
$option = {@_};
}
$option->{valid_retval} ||= [0];
$option->{fail_ok} //= 0;
if ( $option->{no_stderr} ) {
$cmd = "$cmd 2>/dev/null";
}
if ( $option->{stderr_to_stdout} ) {
$cmd = "$cmd 2>&1";
}
if ( ref $option->{valid_retval} ne "ARRAY" ) {
$option->{valid_retval} = [ $option->{valid_retval} ];
}
my $is_no_hup = 0;
my $tmp_output_file = get_tmp_file();
if ( exists $option->{nohup} && $option->{nohup} ) {
$cmd = "nohup $cmd >$tmp_output_file";
delete $option->{nohup};
$is_no_hup = 1;
}
my $path;
if ( !Rex::Config->get_no_path_cleanup() ) {
$path = join( ":", Rex::Config->get_path() );
}
my $exec = Rex::Interface::Exec->create;
my ( $out, $err ) = $exec->exec( $cmd, $path, $option );
my $ret_val = $?;
chomp $out if $out;
chomp $err if $err;
$Rex::Commands::Run::LAST_OUTPUT = [ $out, $err ];
$out ||= "";
$err ||= "";
if ( scalar( grep { $_ == $ret_val } @{ $option->{valid_retval} } ) == 0 ) {
if ( !$option->{fail_ok} ) {
Rex::Logger::debug("Error executing `$cmd`: ");
Rex::Logger::debug("STDOUT:");
Rex::Logger::debug($out);
Rex::Logger::debug("STDERR:");
Rex::Logger::debug($err);
if ($is_no_hup) {
$out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
$Rex::Commands::Run::LAST_OUTPUT = [$out];
$? = $ret_val;
}
confess("Error during `i_run`");
}
}
if ($code) {
return &$code( $out, $err );
}
if (wantarray) {
return split( /\r?\n/, $out );
}
if ($is_no_hup) {
$out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
$Rex::Commands::Run::LAST_OUTPUT = [$out];
$? = $ret_val;
}
return $out;
}
sub i_exec {
my ( $cmd, @args ) = @_;
my $exec = Rex::Interface::Exec->create;
my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
my $_cmd_str = "$cmd " . join( " ", map { $_ = $quoter->quote($_) } @args );
i_run $_cmd_str;
}
sub i_exec_nohup {
my ( $cmd, @args ) = @_;
my $exec = Rex::Interface::Exec->create;
my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
my $_cmd_str = "$cmd " . join( " ", map { $_ = $quoter->quote($_) } @args );
i_run $_cmd_str, nohup => 1;
}
1;
|