From feee4a2d8a4ab886dbf3af4ee36bbc63d5a476b3 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Thu, 11 Sep 2008 18:30:46 +0200 Subject: [PATCH] Added options to isolate stdout or stderr only. Added documentation. --- deploy | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 9 deletions(-) diff --git a/deploy b/deploy index acc00a4..41ec7f6 100755 --- a/deploy +++ b/deploy @@ -31,6 +31,8 @@ GetOptions( password|pass|p:s prompt|P! script|s=s + stderr|E! + stdout|O! username|user|u=s ) ); @@ -41,6 +43,10 @@ pod2usage(-verbose => 99, -sections => 'USAGE|EXAMPLES|OPTIONS') if $config{help}; pod2usage(-verbose => 2) if $config{man}; +pod2usage(-verbose => 99, -sections => 'USAGE', + message => 'Only one allowed between --stdout and --stderr') + if $config{stdout} && $config{stderr}; + # Script implementation here my @hostnames = @ARGV; @ARGV = (); @@ -79,14 +85,23 @@ sub operate_on_host { $|++; print "$remote "; my ($out, $err, $exit) = $ssh->cmd($remote); - print "exit = $exit\n"; - for ([STDOUT => $out], [STDERR => $err]) { - my ($type, $val) = @$_; - next unless defined $val; - $val =~ s{\s+\z}{}mxs; - $val =~ s{^}{$type }gmxs; - print $val, "\n\n"; - } ## end for ([STDOUT => $out], ... + + if ($config{stdout} && defined $out) { + print {*STDOUT} $out; + } + elsif ($config{stderr} && defined $err) { + print {*STDOUT} $err; + } + else { + print "exit = $exit\n"; + for ([STDOUT => $out], [STDERR => $err]) { + my ($type, $val) = @$_; + next unless defined $val; + $val =~ s{\s+\z}{}mxs; + $val =~ s{^}{$type }gmxs; + print $val, "\n\n"; + } ## end for ([STDOUT => $out], ... + } return; } ## end sub operate_on_host @@ -150,7 +165,8 @@ See version at beginning of script, variable $VERSION, or call deploy [--debug|-D] [--dir|--directory|-d ] [--password|--pass|-p] [--prompt|-P] - [--script|-s ] [--username|--user|-u] + [--script|-s ] [--stderr|-E] [--stdout|-O] + [--username|--user|-u] =head1 EXAMPLES @@ -192,6 +208,70 @@ just hit enter when requested for a password, or you can pass C<-p> without a password on the command line (you can actually pass every password you can think of, it will be ignored). +=head2 Output Format + +The normal output format is geared at easing parsing by other programs. It +is compound of the following parts: + +=over + +=item * + +a single line specifing the hostname/ip, with the following format: + + *** OPERATING ON *** + +=item * + +a single line reporting the exit code from the remote process, with the +following format: + + exit = + +=item * + +0 or more lines starting with C (note the space); + +=item * + +0 or more lines starting with C (note the space). + +=back + +If any of L or L are present, then the relevant +channel is printed on STDOUT immediately after the first line of the +format above, unchanged. + +=head2 Example Runs + +Suppose to have the following script F to deploy: + + #!/bin/bash + + echo 'Hi there!' + ls baz + echo 'How are you all?!?' + +If you don't provide any of L or L, you will have +something like this: + + *** OPERATING ON foo.example.com *** + /tmp/our-deploy/bar.sh exit = 0 + STDOUT Hi there! + STDOUT How are you all?!? + STDERR ls: baz: No such file or directory + +If you pass L<--stderr> you will get: + + *** OPERATING ON foo.example.com *** + ls: baz: No such file or directory + +If you pass L<--stdout> you will get: + + *** OPERATING ON foo.example.com *** + Hi there! + How are you all?!? + =head1 OPTIONS =over @@ -260,6 +340,16 @@ script will be sanitised (only alphanumeric, C<_>, C<.> and C<-> will be retained), so be careful if you have to look for the uploaded script later. +=item --stderr | -E + +select only the STDERR channel from the responses got via SSH. This +option cannot be used with L. + +=item --stdout | -O + +select only the STDOUT channel from the responses got via SSH. This +option cannot be used with L. + =item --usage print a concise usage line and exit. -- 2.11.4.GIT