| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # shell::command Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Shell::Command; | ||
| 7 | 24 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 24 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 24 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(exec execute) ], | ||
| 16 | attributes => { | ||
| 17 | as_array => [ qw(0|1) ], | ||
| 18 | as_matrix => [ qw(0|1) ], | ||
| 19 | capture_stderr => [ qw(0|1) ], | ||
| 20 | capture_mode => [ qw(0|1) ], | ||
| 21 | capture_system => [ qw(0|1) ], | ||
| 22 | ignore_error => [ qw(0|1) ], | ||
| 23 | use_sudo => [ qw(0|1) ], | ||
| 24 | use_pager => [ qw(0|1) ], | ||
| 25 | use_globbing => [ qw(0|1) ], | ||
| 26 | sudo_args => [ qw(args) ], | ||
| 27 | }, | ||
| 28 | attributes_default => { | ||
| 29 | as_array => 1, | ||
| 30 | as_matrix => 0, | ||
| 31 | capture_stderr => 1, | ||
| 32 | capture_mode => 0, | ||
| 33 | capture_system => 0, | ||
| 34 | ignore_error => 1, | ||
| 35 | use_sudo => 0, | ||
| 36 | use_pager => 0, | ||
| 37 | use_globbing => 0, | ||
| 38 | #sudo_args => '-E', # Keep environment | ||
| 39 | sudo_args => '', # Do not keep env by default. | ||
| 40 | # Not needed anymore cause Metabrik is installed | ||
| 41 | # system-wide. | ||
| 42 | }, | ||
| 43 | commands => { | ||
| 44 | system => [ qw(command args|OPTIONAL) ], | ||
| 45 | sudo_system => [ qw(command args|OPTIONAL) ], | ||
| 46 | system_in_background => [ qw(command args|OPTIONAL) ], | ||
| 47 | sudo_system_in_background => [ qw(command args|OPTIONAL) ], | ||
| 48 | capture => [ qw(command args|OPTIONAL) ], | ||
| 49 | sudo_capture => [ qw(command args|OPTIONAL) ], | ||
| 50 | system_capture => [ qw(command args|OPTIONAL) ], | ||
| 51 | sudo_system_capture => [ qw(command args|OPTIONAL) ], | ||
| 52 | execute => [ qw(command args|OPTIONAL) ], | ||
| 53 | sudo_execute => [ qw(command args|OPTIONAL) ], | ||
| 54 | }, | ||
| 55 | require_binaries => { | ||
| 56 | script => [ ], | ||
| 57 | }, | ||
| 58 | require_modules => { | ||
| 59 | 'IPC::Run3' => [ ], | ||
| 60 | 'Metabrik::System::Os' => [ ], | ||
| 61 | 'Metabrik::System::Process' => [ ], | ||
| 62 | }, | ||
| 63 | need_packages => { | ||
| 64 | ubuntu => [ qw(bsdutils) ], | ||
| 65 | debian => [ qw(bsdutils) ], | ||
| 66 | kali => [ qw(bsdutils) ], | ||
| 67 | }, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub system { | ||
| 72 | my $self = shift; | ||
| 73 | my ($cmd, @args) = @_; | ||
| 74 | |||
| 75 | $self->brik_help_run_undef_arg('system', $cmd) or return; | ||
| 76 | |||
| 77 | # Remove undefined values from arguments | ||
| 78 | my @new; | ||
| 79 | for (@args) { | ||
| 80 | if (defined($_)) { | ||
| 81 | push @new, $_; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | my $command = join(' ', $cmd, @new); | ||
| 86 | my @toks = split(/\s+/, $command); | ||
| 87 | my $bin = $toks[0]; | ||
| 88 | |||
| 89 | my @path = split(':', $ENV{PATH}); | ||
| 90 | if (! -f $bin) { # If file is not directly found | ||
| 91 | for my $path (@path) { | ||
| 92 | if (-f "$path/$bin") { | ||
| 93 | $bin = "$path/$bin"; | ||
| 94 | last; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $toks[0] = $bin; | ||
| 99 | |||
| 100 | if (! -f $bin) { | ||
| 101 | return $self->log->error("system: program [$bin] not found in PATH"); | ||
| 102 | } | ||
| 103 | |||
| 104 | $command = join(' ', @toks); | ||
| 105 | |||
| 106 | # Use sudo only when not root | ||
| 107 | if ($self->use_sudo && $< != 0) { | ||
| 108 | my @sudo = ( "sudo" ); | ||
| 109 | if (! ref($self->sudo_args) && length($self->sudo_args)) { | ||
| 110 | my @args = split(/\s+/, $self->sudo_args); | ||
| 111 | push @sudo, @args; | ||
| 112 | } | ||
| 113 | $command = join(' ', @sudo)." $command"; | ||
| 114 | @toks = ( @sudo, @toks ); | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($self->use_pager) { | ||
| 118 | my $pager = $ENV{PAGER} || 'less'; | ||
| 119 | $command .= " | $pager"; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Also capture output to terminal to a file | ||
| 123 | my $so = Metabrik::System::Os->new_from_brik_init($self) or return; | ||
| 124 | my $output_file = 'capture_system.script'; | ||
| 125 | if ($self->capture_system) { | ||
| 126 | if ($so->is_freebsd) { | ||
| 127 | $command = "script -q $output_file $command"; | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $command = "script -q --command '".$command."' $output_file"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->log->verbose("system: command[$command]"); | ||
| 135 | |||
| 136 | my $r = CORE::system($command); | ||
| 137 | |||
| 138 | $self->log->debug("system: command returned code [$r] with status [$?]"); | ||
| 139 | |||
| 140 | if (! $self->ignore_error && $? != 0) { | ||
| 141 | $self->log->verbose("system: exit code[$?]"); | ||
| 142 | # Failure, we return the program exit code | ||
| 143 | $self->log->debug("system: program exit code [$?]"); | ||
| 144 | return $?; | ||
| 145 | } | ||
| 146 | |||
| 147 | $self->log->debug("system: program exit with success"); | ||
| 148 | |||
| 149 | # Program succeeded, we only return full path to output file, | ||
| 150 | # maybe the caller will have some optimization to not process ths full | ||
| 151 | # file content afterwards. | ||
| 152 | if ($self->capture_system) { | ||
| 153 | my $pwd = defined($self->shell) && $self->shell->full_pwd || '/tmp'; | ||
| 154 | my $homedir = defined($self->global) && $self->global->homedir | ||
| 155 | || defined($ENV{HOME}) && $ENV{HOME} || '/tmp'; | ||
| 156 | $pwd =~ s{^~}{$homedir}; | ||
| 157 | return $pwd.'/'.$output_file; | ||
| 158 | } | ||
| 159 | |||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub sudo_system { | ||
| 164 | my $self = shift; | ||
| 165 | my ($cmd, @args) = @_; | ||
| 166 | |||
| 167 | $self->brik_help_run_undef_arg('sudo_system', $cmd) or return; | ||
| 168 | |||
| 169 | my $prev = $self->use_sudo; | ||
| 170 | $self->use_sudo(1); | ||
| 171 | my $r = $self->system($cmd, @args); | ||
| 172 | $self->use_sudo($prev); | ||
| 173 | |||
| 174 | return $r; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub system_in_background { | ||
| 178 | my $self = shift; | ||
| 179 | my ($cmd, @args) = @_; | ||
| 180 | |||
| 181 | $self->brik_help_run_undef_arg('system_in_background', $cmd) or return; | ||
| 182 | |||
| 183 | my $sp = Metabrik::System::Process->new_from_brik_init($self) or return; | ||
| 184 | $sp->close_output_on_start(1); | ||
| 185 | |||
| 186 | $sp->start(sub { | ||
| 187 | $self->system($cmd, @args); | ||
| 188 | }); | ||
| 189 | |||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub sudo_system_in_background { | ||
| 194 | my $self = shift; | ||
| 195 | my ($cmd, @args) = @_; | ||
| 196 | |||
| 197 | $self->brik_help_run_undef_arg('sudo_system_in_background', $cmd) or return; | ||
| 198 | |||
| 199 | my $sp = Metabrik::System::Process->new_from_brik_init($self) or return; | ||
| 200 | $sp->close_output_on_start(1); | ||
| 201 | |||
| 202 | $sp->start(sub { | ||
| 203 | $self->sudo_system($cmd, @args); | ||
| 204 | }); | ||
| 205 | |||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub capture { | ||
| 210 | my $self = shift; | ||
| 211 | my ($cmd, @args) = @_; | ||
| 212 | |||
| 213 | $self->brik_help_run_undef_arg('capture', $cmd) or return; | ||
| 214 | |||
| 215 | # Remove undefined values from arguments | ||
| 216 | my @new; | ||
| 217 | for (@args) { | ||
| 218 | if (defined($_)) { | ||
| 219 | push @new, $_; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | my $command = join(' ', $cmd, @new); | ||
| 224 | my @toks = split(/\s+/, $command); | ||
| 225 | my $bin = $toks[0]; | ||
| 226 | |||
| 227 | my @path = split(':', $ENV{PATH}); | ||
| 228 | if (! -f $bin) { # If file is not directly found | ||
| 229 | for my $path (@path) { | ||
| 230 | if (-f "$path/$bin") { | ||
| 231 | $bin = "$path/$bin"; | ||
| 232 | last; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | $toks[0] = $bin; | ||
| 237 | |||
| 238 | if (! -f $bin) { | ||
| 239 | return $self->log->error("capture: program [$bin] not found in PATH"); | ||
| 240 | } | ||
| 241 | |||
| 242 | # Perform file globbing, if any | ||
| 243 | if ($self->use_globbing) { | ||
| 244 | my @globbed = (); | ||
| 245 | for (@toks) { | ||
| 246 | push @globbed, glob($_); | ||
| 247 | } | ||
| 248 | @toks = @globbed; | ||
| 249 | } | ||
| 250 | |||
| 251 | $command = join(' ', @toks); | ||
| 252 | |||
| 253 | # Use sudo only when not root | ||
| 254 | if ($self->use_sudo && $< != 0) { | ||
| 255 | my @sudo = ( "sudo" ); | ||
| 256 | if (! ref($self->sudo_args) && length($self->sudo_args)) { | ||
| 257 | my @args = split(/\s+/, $self->sudo_args); | ||
| 258 | push @sudo, @args; | ||
| 259 | } | ||
| 260 | $command = join(' ', @sudo)." $command"; | ||
| 261 | @toks = ( @sudo, @toks ); | ||
| 262 | } | ||
| 263 | |||
| 264 | my $out; | ||
| 265 | my $err; | ||
| 266 | eval { | ||
| 267 | my $cmd = join(' ', @toks); | ||
| 268 | $self->log->verbose("capture: command[$cmd]"); | ||
| 269 | IPC::Run3::run3($cmd, undef, \$out, \$err); | ||
| 270 | }; | ||
| 271 | # Error in executing run3() | ||
| 272 | if ($@) { | ||
| 273 | chomp($@); | ||
| 274 | return $self->log->error("capture: unable to execute command [$command]: $@"); | ||
| 275 | } | ||
| 276 | # Error in command execution | ||
| 277 | elsif ($?) { | ||
| 278 | chomp($err); | ||
| 279 | chomp($out); | ||
| 280 | $err ||= $out; # Sometimes, the error is printed on stdout instead of stderr | ||
| 281 | if ($self->ignore_error) { | ||
| 282 | $self->log->warning("capture: command execution had errors [$command]: $err"); | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | return $self->log->error("capture: command execution failed [$command]: $err"); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | $out ||= 'undef'; | ||
| 290 | $err ||= 'undef'; | ||
| 291 | chomp($out); | ||
| 292 | chomp($err); | ||
| 293 | |||
| 294 | # If we also wanted stderr, we put it at the end of output | ||
| 295 | if ($self->capture_stderr && $err ne 'undef') { | ||
| 296 | $out .= "\n\nSTDERR:\n$err"; | ||
| 297 | } | ||
| 298 | |||
| 299 | # as_matrix has precedence over as_array (because as_array is the default) | ||
| 300 | if (! $self->as_matrix && $self->as_array) { | ||
| 301 | $out = [ split(/\n/, $out) ]; | ||
| 302 | } | ||
| 303 | elsif ($self->as_matrix) { | ||
| 304 | my @matrix = (); | ||
| 305 | for my $this (split(/\n/, $out)) { | ||
| 306 | push @matrix, [ split(/\s+/, $this) ]; | ||
| 307 | } | ||
| 308 | $out = \@matrix; | ||
| 309 | } | ||
| 310 | |||
| 311 | return $out; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub sudo_capture { | ||
| 315 | my $self = shift; | ||
| 316 | my ($cmd, @args) = @_; | ||
| 317 | |||
| 318 | $self->brik_help_run_undef_arg('sudo_capture', $cmd) or return; | ||
| 319 | |||
| 320 | my $prev = $self->use_sudo; | ||
| 321 | $self->use_sudo(1); | ||
| 322 | my $r = $self->capture($cmd, @args); | ||
| 323 | $self->use_sudo($prev); | ||
| 324 | |||
| 325 | return $r; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub system_capture { | ||
| 329 | my $self = shift; | ||
| 330 | my ($cmd, @args) = @_; | ||
| 331 | |||
| 332 | $self->brik_help_run_undef_arg('system_capture', $cmd) or return; | ||
| 333 | |||
| 334 | my $prev = $self->capture_system; | ||
| 335 | $self->capture_system(1); | ||
| 336 | my $r = $self->system($cmd, @args); | ||
| 337 | $self->capture_system($prev); | ||
| 338 | |||
| 339 | return $r; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub sudo_system_capture { | ||
| 343 | my $self = shift; | ||
| 344 | my ($cmd, @args) = @_; | ||
| 345 | |||
| 346 | $self->brik_help_run_undef_arg('sudo_system_capture', $cmd) or return; | ||
| 347 | |||
| 348 | my $prev = $self->capture_system; | ||
| 349 | $self->capture_system(1); | ||
| 350 | my $r = $self->sudo_system($cmd, @args); | ||
| 351 | $self->capture_system($prev); | ||
| 352 | |||
| 353 | return $r; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub execute { | ||
| 357 | my $self = shift; | ||
| 358 | my ($cmd, @args) = @_; | ||
| 359 | |||
| 360 | $self->brik_help_run_undef_arg('execute', $cmd) or return; | ||
| 361 | |||
| 362 | if ($self->capture_system) { | ||
| 363 | return $self->system_capture($cmd, @args); | ||
| 364 | } | ||
| 365 | elsif ($self->capture_mode) { | ||
| 366 | return $self->capture($cmd, @args); | ||
| 367 | } | ||
| 368 | else { # non-capture mode | ||
| 369 | return $self->system($cmd, @args); | ||
| 370 | } | ||
| 371 | |||
| 372 | # Unknown error | ||
| 373 | return; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub sudo_execute { | ||
| 377 | my $self = shift; | ||
| 378 | my ($cmd, @args) = @_; | ||
| 379 | |||
| 380 | $self->brik_help_run_undef_arg('sudo_execute', $cmd) or return; | ||
| 381 | |||
| 382 | if ($self->capture_system) { | ||
| 383 | return $self->sudo_system_capture($cmd, @args); | ||
| 384 | } | ||
| 385 | elsif ($self->capture_mode) { | ||
| 386 | return $self->sudo_capture($cmd, @args); | ||
| 387 | } | ||
| 388 | else { # non-capture mode | ||
| 389 | return $self->sudo_system($cmd, @args); | ||
| 390 | } | ||
| 391 | |||
| 392 | # Unknown error | ||
| 393 | return; | ||
| 394 | } | ||
| 395 | |||
| 396 | 1; | ||
| 397 | |||
| 398 | __END__ | ||