package RDA::Macro::Unix::Remote; # $Id: Remote.pm,v 1.19 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Unix/Remote.pm,v 1.19 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Unix::Remote - Class Used for UNIX Remote Operation Macros =head1 SYNOPSIS require RDA::Macro::Unix::Remote; =head1 DESCRIPTION The objects of the C class are used to interface with UNIX remote operation macros. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Copy; use File::Spec; use IO::File; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_cmd = ( rcp => '-p', rsh => '', scp => '-BCpq -o ConnectTimeout=30', ssh => '-Cnq -o ConnectTimeout=30', SCP => '-BCpq', SSH => '-Cnq', ); my %tb_fct = ( 'get' => [\&_m_get, 'N'], 'getRemoteLines' => [\&_m_get_lines, 'L'], 'getRemoteTimeout' => [\&_m_get_timeout, 'N'], 'initRemote' => [\&_m_init_remote, 'N'], 'isRemote' => [\&_m_is_remote, 'N'], 'mget' => [\&_m_mget, 'N'], 'mput' => [\&_m_mput, 'N'], 'put' => [\&_m_put, 'N'], 'rda' => [\&_m_rda, 'N'], 'rexec' => [\&_m_rexec, 'N'], 'setRemoteTimeout' => [\&_m_set_timeout, 'N'], 'setRemoteTrace' => [\&_m_set_trace, 'N'], ); my %tb_set = ( '_STEP' => ['T', 'Remote node execution step'], '_STORAGE' => ['T', 'Remote node storage type'], ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Unix::Remote-Enew($agt)> The object constructor. It takes the agent object reference as an argument. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item B< 'lim' > Execution time limit (in seconds) =item B< 'trc' > Remote execution trace flag =item B< '_agt' > Reference to the agent object =item B< '_err' > Last command error lines =item B< '_not' > Statistics note =item B< '_out' > Number of operating system requests timed out =item B< '_req' > Number of operating system requests =item B< '_ssh' > Authentication agent indicator =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Abort if not executed in a Unix environment return unless $agt->is_unix; # Create the macro object $slf = bless {_agt => $agt}, $class; # Setup some parameters by default $slf->{'lim'} = _chk_alarm($agt->get_setting('RDA_TIMEOUT', 30)); $slf->{'trc'} = $agt->get_setting('REMOTE_TRACE', 0); $slf->{'_err'} = []; $slf->{'_req'} = $slf->{'_out'} = 0; $slf->{'_ssh'} = 0; # Register the macros $agt->register($slf, [keys(%tb_fct)], qw(end stat)); # Return the object reference $slf; } =head2 S<$h-Eclr_stats($name)> This method resets the statistics and clears corresponding module settings. =cut sub clr_stats { my ($slf, $nam) = @_; my $agt = $slf->{'_agt'}; $slf->{'_not'} = ''; $slf->{'_req'} = $slf->{'_out'} = 0; } =head2 S<$h-Eend> This method terminates the remote operations. =cut sub end { my ($slf) = @_; # Kill the authentication agent if ($slf->{'_ssh'} > 0) { `ssh-agent -k`; $slf->{'_ssh'} = 0; } } =head2 S<$h-Eget_stats($name)> This method reports the library statistics in the specified module. =cut sub get_stats { my ($slf, $nam) = @_; if ($slf->{'_req'}) { my $agt = $slf->{'_agt'}; # Indicate the current timeout when there is no other note $slf->{'_not'} = 'Command execution limited to '.$slf->{'lim'}.'s' unless $slf->{'_not'} || $slf->{'lim'} <= 0; $agt->log('s', $nam, 'Unix/Remote', $VERSION, $slf->{'_req'}, 'N/A', $slf->{'_out'}, 'N/A', $slf->{'_not'}); # Get other macro library contributions $slf->{'_req'} += $agt->get_setting("OS_STAT_$nam\_REQ", 0); $slf->{'_out'} += $agt->get_setting("OS_STAT_$nam\_OUT", 0); # Generate the module statistics $agt->set_setting("OS_STAT_$nam\_REQ", $slf->{'_req'}, 'N', 'Number of Operating System requests'); $agt->set_setting("OS_STAT_$nam\_OUT", $slf->{'_out'}, 'N', 'Number of Operating System requests timed out'); $agt->set_setting("OS_STAT_$nam\_NOTE", $slf->{'_not'}, 'T', 'Operating System statistics note'); } } =head2 S<$h-Erun($name,$arg,$ctx)> This method executes the macro with the specified argument list in a given context. =cut sub run { my ($slf, $nam, $arg, $ctx) = @_; my ($fct, $ret, $typ); $fct = $tb_fct{$nam}; $typ = $fct->[1]; # Treat an array context return RDA::Value::new_list(&{$fct->[0]}($slf, $ctx, $arg->eval_as_array)) if $typ eq 'L'; # Treat a scalar context $ret = &{$fct->[0]}($slf, $ctx, $arg->eval_as_array); $typ = 'U' unless defined($ret); return RDA::Value->new($typ, $ret); } =head1 UNIX REMOTE MACROS =head2 S This macro gets a single file from a remote node. By default, the same directory and file name are assumed for the local destination. However, it takes remote relative paths from the home directory, local paths from RDA_HOME. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_get { my ($slf, $ctx, $nod, $rdr, $rnm, $ldr, $lnm) = @_; my ($agt, $dst, $src, $tgt, $usr); return -1 unless $nod && $rdr && $rnm; # Determine the target system $agt = $slf->{'_agt'}; $tgt = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost'); # Determine the file locations $dst = _gen_path(defined($ldr) ? $ldr : $rdr, $lnm); $src = $tgt.':'._gen_path($rdr, $rnm); $usr = $agt->get_setting("REMOTE_$nod\_USER"); $usr = $agt->get_setting("REMOTE_USER") unless defined($usr); $src = "$usr\@$src" if defined($usr); # Execute the remote request $slf->_do_scp($agt, $nod, '', $dst, $src); } =head2 S This macro returns the lines stored during the last command execution. =cut sub _m_get_lines { @{shift->{'_err'}}; } =head2 S This macro returns the current duration of the timeout for executing remote commands. When this mechanism is disabled, it returns 0. =cut sub _m_get_timeout { shift->{'lim'}; } =head2 S This macro forces the initialization of the authentication agent. It returns the authentication agent status. =cut sub _m_init_remote { my ($slf) = @_; $slf->{'_ssh'} = _set_agent($slf->{'_agt'}) unless $slf->{'_ssh'}; $slf->{'_ssh'}; } =head2 S This macro indicates if the specified node is a remote one. =cut sub _m_is_remote { my ($slf, $ctx, $nod) = @_; my ($agt); $agt = $slf->{'_agt'}; _is_remote($agt, $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost')); } sub _is_remote { my ($agt, $nod) = @_; my ($nam, $dom); $nam = $agt->get_setting('MACHINE_NAME'); $dom = $agt->get_setting('DOMAIN_NAME'); $nod ne 'localhost' && $nod ne $nam && $nod ne "$nam.$dom"; } =head2 S This macro gets one or more files from a remote node. The name may contain shell meta characters. By default, a same directory name is assumed for the local destination. However, it takes remote relative paths from the home directory, and local paths from RDA_HOME. If the flag is set, it recursively copies entire directories. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_mget { my ($slf, $ctx, $nod, $flg, $rdr, $re, $ldr) = @_; my ($agt, $dst, $src, $usr); return 0 unless $nod && $rdr; # Determine the file locations $agt = $slf->{'_agt'}; $dst = defined($ldr) ? $ldr : $rdr; $src = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost').":$rdr"; $usr = $agt->get_setting("REMOTE_$nod\_USER"); $usr = $agt->get_setting("REMOTE_USER") unless defined($usr); $src = "$usr\@$src" if defined($usr); $src .= "/$re" if defined($re); # Execute the remote request $slf->_do_scp($agt, $nod, $flg ? '-r' : '', $dst, $src); } =head2 S This macro puts one or more files into a remote node. You can use a regular expression to select the files inside the local directory. By default, it assumes the same directory name for the remote destination. However, it takes remote relative paths from the home directory, and local paths from RDA_HOME. If the flag is set, it recursively copies entire directories. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_mput { my ($slf, $ctx, $nod, $flg, $ldr, $re, $rdr) = @_; my ($agt, $dst, $src, $usr, @src); return -1 unless $nod && $ldr; # Determine the file locations $agt = $slf->{'_agt'}; $dst = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost').':'; $usr = $agt->get_setting("REMOTE_$nod\_USER"); $usr = $agt->get_setting("REMOTE_USER") unless defined($usr); $dst = "$usr\@$dst" if defined($usr); $dst .= defined($rdr) ? $rdr : $ldr; # Determine the list of the files to copy if ($re) { $re = qr/$re/i; if (opendir(DIR, $ldr)) { @src = grep {$_ =~ $re} readdir(DIR); closedir(DIR) } return -2 unless scalar(@src); @src = join(' ', map {File::Spec->catfile($ldr, $_)} @src) unless $ldr eq '.'; $src = join(' ', @src); } else { $src = $ldr; } # Execute the remote request $slf->_do_scp($agt, $nod, $flg ? '-r' : '', $dst, $src); } =head2 S This macro puts a single file into a remote node. By default, it assumes the same directory and file name for the remote destination. However, it takes remote relative paths from the home directory, and local paths from RDA_HOME. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_put { my ($slf, $ctx, $nod, $ldr, $lnm, $rdr, $rnm) = @_; my ($agt, $dst, $src, $usr); return -1 unless $nod && $ldr && $lnm; # Determine the file locations $agt = $slf->{'_agt'}; $dst = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost').':'; $usr = $agt->get_setting("REMOTE_$nod\_USER"); $usr = $agt->get_setting("REMOTE_USER") unless defined($usr); $dst = "$usr\@$dst" if defined($usr); $dst .= _gen_path(($rdr) ? $rdr : $ldr, $rnm); $src = File::Spec->catfile($ldr, $lnm); # Execute the remote request $slf->_do_scp($agt, $nod, '', $dst, $src); } =head2 S This macro executes RDA with the specified options on a remote node. It treats requests differently, depending on whether they are on local or remote systems. When the flag is set, it captures all output lines. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_rda { my ($slf, $ctx, $nod, $opt, $flg) = @_; my ($agt, $cmd, $tgt); return -1 unless $nod && defined($opt); # Determine the target system $agt = $slf->{'_agt'}; $tgt = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost'); # Execute the RDA command if (_is_remote($agt, $tgt)) { # Determine the command $cmd = File::Spec->catfile($agt->get_setting("REMOTE_$nod\_HOME"), $agt->get_setting("REMOTE_$nod\_RDA_COMMAND") || $agt->get_setting('REMOTE_RDA_COMMAND', 'rda.sh')); $cmd .= ' '.$opt if $opt; $cmd .= ' 2>&1'; # Execute the remote request $slf->_do_ssh($agt, $nod, $tgt, $cmd, $flg); } else { # Determine the command $cmd = $agt->get_setting('RDA_SELF'); $cmd .= ' '.$opt if $opt; $cmd .= ' 2>&1'; # Execute the local request $slf->_do_exec($agt, $nod, $cmd, $flg); } } =head2 S This macro executes the specified command on a remote node. There is no attempt to treat the request differently if the remote node is the local node. When the flag is set, it captures all output lines. It returns the command exit code when the request is executed. Otherwise, it returns -1. =cut sub _m_rexec { my ($slf, $ctx, $nod, $cmd, $flg) = @_; my ($agt, $tgt); return -1 unless $nod && $cmd; # Determine the remote system $agt = $slf->{'_agt'}; $tgt = $agt->get_setting("REMOTE_$nod\_HOSTNAME", 'localhost'); # Execute the remote request $slf->_do_ssh($agt, $nod, $tgt, $cmd, $flg); } =head2 S This macro sets the timeout for executing remote commands, specified in seconds, only if the value is strictly positive. Otherwise, it disables the timeout mechanism. It is also disabled if the C function is not implemented. It returns the effective value. =cut sub _m_set_timeout { my ($slf, $ctx, $val) = @_; $slf->{'lim'} = _chk_alarm($val); } =head2 S This macro sets the remote trace level: =over 8 =item C< 0 > No trace =item C< 1 > Trace the remote command execution =back The level is unchanged if the new level is not defined. It returns the previous level. =cut sub _m_set_trace { my ($slf, $ctx, $lvl) = @_; my $old; $old = $slf->{'trc'}; $slf->{'trc'} = $lvl if defined($lvl); $old; } # --- Alarm functions --------------------------------------------------------- # Check if alarm is implemented sub _chk_alarm { my ($lim) = @_; return 0 unless $lim > 0; eval {alarm(0);}; $@ ? 0 : $lim; } # Get the alarm duration sub _get_alarm { my ($slf, $val) = @_; return $slf->{'lim'} unless defined($val); return 0 unless $slf->{'lim'} > 0 && $val > 0; $val *= $slf->{'lim'}; ($val > 1) ? int($val) : 1; } # --- Local Internal functions ------------------------------------------------ sub _do_exec { my ($slf, $agt, $nod, $pgm, $flg) = @_; my ($err, $pre, $trc); # Execute the remote request $slf->{'_err'} = $err = []; $pre = $agt->get_setting("REMOTE_$nod\_PREFIX", $nod) if ($trc = $slf->{'trc'}); if (open(CMD, "$pgm 2>&1 |")) { while () { s/[\n\r\s]+$//; print "$pre> $_\n" if $trc; push(@$err, $_) if $flg || m/RDA-\d{5}:/; } close(CMD); } # Indicate the command result ++$slf->{'_req'}; return $?; } sub _gen_path { my ($dir, $fil) = @_; (!defined($fil)) ? $dir : ($dir eq '.') ? $fil : File::Spec->catfile($dir, $fil); } # --- SSH Internal functions -------------------------------------------------- sub _do_scp { my ($slf, $agt, $nod, $rec, $dst, $src) = @_; my ($cmd, $opt, $pre, $trc); # Set an authentication agent when appropriate $slf->{'_ssh'} = _set_agent($agt) unless $slf->{'_ssh'}; # Determine the command and its options $cmd = $agt->get_setting("REMOTE_$nod\_SCP_COMMAND") || $agt->get_setting('REMOTE_SCP_COMMAND', 'scp'); $opt = $agt->get_setting("REMOTE_$nod\_SCP_OPTIONS"); $opt = $agt->get_setting('REMOTE_SCP_OPTIONS', $tb_cmd{'scp'}) unless defined($opt); # Execute the remote request $slf->{'_err'} = []; $pre = $agt->get_setting("REMOTE_$nod\_PREFIX", $nod) if ($trc = $slf->{'trc'}); if (open(CMD, "$cmd $opt $rec $src $dst 2>&1 |")) { while () { print "$pre> $_" if $trc; } close(CMD); } # Indicate the command result ++$slf->{'_req'}; return $?; } sub _do_ssh { my ($slf, $agt, $nod, $tgt, $pgm, $flg) = @_; my ($cmd, $err, $opt, $pre, $trc, $usr); # Set an authentication agent when appropriate $slf->{'_ssh'} = _set_agent($agt) unless $slf->{'_ssh'}; # Determine the command and its options $cmd = $agt->get_setting("REMOTE_$nod\_SSH_COMMAND") || $agt->get_setting('REMOTE_SSH_COMMAND', 'ssh'); $opt = $agt->get_setting("REMOTE_$nod\_SSH_OPTIONS"); $opt = $agt->get_setting('REMOTE_SSH_OPTIONS', $tb_cmd{'ssh'}) unless defined($opt); $usr = $agt->get_setting("REMOTE_$nod\_USER"); $usr = $agt->get_setting("REMOTE_USER") unless defined($usr); $tgt = "-l $usr $tgt" if defined($usr); # Execute the remote request $slf->{'_err'} = $err = []; $pre = $agt->get_setting("REMOTE_$nod\_PREFIX", $nod) if ($trc = $slf->{'trc'}); if (open(CMD, "$cmd $opt $tgt $pgm 2>&1 |")) { while () { s/[\n\r\s]+$//; print "$pre> $_\n" if $trc; push(@$err, $_) if $flg || m/RDA-\d{5}:/; } close(CMD); } # Indicate the command result ++$slf->{'_req'}; return $?; } # Start an authentication agent when required sub _set_agent { my ($agt) = @_; my ($alt, $flg, $key, $opt, $ssh, $val); # Identify the command to be used unless (defined($flg = $agt->get_setting('REMOTE_SSH_AGENT'))) { $alt = $flg = 0; $ssh = !$agt->get_setting('REMOTE_NO_SSH'); # Determine the remote shell command unless ($agt->get_setting('REMOTE_SSH_COMMAND')) { if ($ssh && ($val = _find_cmd($agt, 'ssh'))) { $flg = 1; $opt = $tb_cmd{'ssh'}; $key = eval "`$val $opt -o bad_option=bad_value localhost date 2>&1`"; $opt = $tb_cmd{'SSH'} if ($alt = ($key =~ m/ConnectTimeout/)); $agt->set_setting('REMOTE_SSH_COMMAND', $val, 'F', 'SSH command'); $agt->set_setting('REMOTE_SSH_OPTIONS', $opt, 'T', 'SSH options'); } elsif (($val = _find_cmd($agt, 'rsh')) || ($val = _find_cmd($agt, 'remsh'))) { $opt = $tb_cmd{'rsh'}; $agt->set_setting('REMOTE_SSH_COMMAND', $val, 'F', 'RSH command'); $agt->set_setting('REMOTE_SSH_OPTIONS', $opt, 'T', 'RSH options'); } else { die "RDA-00513: Missing remote shell command\n"; } } # Determine the remote file copy command unless ($agt->get_setting('REMOTE_SCP_COMMAND')) { if ($ssh && ($val = _find_cmd($agt, 'scp'))) { $flg = 1; $opt = $tb_cmd{$alt ? 'SCP' : 'scp'}; $agt->set_setting('REMOTE_SCP_COMMAND', $val, 'F', 'SCP command'); $agt->set_setting('REMOTE_SCP_OPTIONS', $opt, 'T', 'SCP options'); } elsif ($val = _find_cmd($agt, 'rcp')) { $opt = $tb_cmd{'rcp'}; $agt->set_setting('REMOTE_SCP_COMMAND', $val, 'F', 'RCP command'); $agt->set_setting('REMOTE_SCP_OPTIONS', $opt, 'T', 'RCP options'); } else { die "RDA-00514: Missing remote file copy command\n"; } } # Determine the authentication agent requirement $agt->set_setting('REMOTE_SSH_AGENT', $flg, 'B', 'Authentication agent required ?'); } # Check if an authentication agent must be started return -1 if !$flg || exists($ENV{'SSH_AUTH_SOCK'}) || exists($ENV{'SSH_AGENT_PID'}); # Create an authentication agent foreach my $lin (`ssh-agent -s 2>/dev/null`) { next unless $lin =~ m/ export /; ($key, $val) = split(/[=;]/, $lin, 3); $ENV{$key} = $val; } return -1 if $?; # Add RSA or DSA identities to the authentication agent `ssh-add 2>/dev/null`; 1; } sub _find_cmd { my ($agt, $cmd) = @_; my ($fil, @tbl); foreach my $dir (split(/:/, $ENV{'PATH'})) { if (opendir(DIR, $dir)) { @tbl = grep {m#^$cmd$#} readdir(DIR); # closedir(DIR); foreach my $nam (@tbl) { $fil = File::Spec->catfile($dir, $nam); return $agt->quote($fil) if -f $fil || -l $fil; } } } undef; } 1; __END__ =head1 SEE ALSO See also L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut