package RDA::Macro::Remote; # $Id: Remote.pm,v 1.7 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Remote.pm,v 1.7 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Remote - Class Used for Remote Operation Macros =head1 SYNOPSIS require RDA::Macro::Remote; =head1 DESCRIPTION The objects of the C class are used to interface with 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.7 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_fct = ( 'endSteps' => [\&_m_end_steps, 'N'], 'genRemoteSetup' => [\&_m_gen_setup, 'T'], 'getRemoteSetup' => [\&_m_get_setup, 'T'], 'getStep' => [\&_m_get_step, 'T'], 'initSteps' => [\&_m_init_steps, 'N'], 'setStep' => [\&_m_set_step, 'T'], 'transfer' => [\&_m_transfer, '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::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< '_agt' > Reference to the agent object =item B< '_rlg' > Remote log file path =item B< '_rfh' > Remote log file handler =item B< '_stp' > Step hash =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Create the macro object $slf = bless {_agt => $agt}, $class; # Register the macros $agt->register($slf, [keys(%tb_fct)]); # Return the object reference $slf; } =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 REMOTE MACROS =head2 S This macro collects all the step changes accumulated in the remote log file, updates settings appropriately, and removes the remote log file. This is not performed inside a thread. It returns 1 on successful completion. Otherwise, it returns 0. =cut sub _m_end_steps { my ($slf, $ctx) = @_; # Check if it can be done return 0 if $ctx->in_thread || !exists($slf->{'_rlg'}); # Close the remote log file if (exists($slf->{'_rfh'})) { $slf->{'_rfh'}->close; delete($slf->{'_rfh'}); } # Review all steps present in the remote log file $slf->_load_steps($slf->{'_agt'}, $slf->{'_rlg'}); # Remove the remote log file 1 while unlink($slf->{'_rlg'}); delete($slf->{'_rlg'}); # Indicate the successful completion 1; } # Load steps from the remote log file sub _load_steps { my ($slf, $agt, $pth) = @_; my ($key, $nod, $rec, $stp, $val, @tbl, %stp); if (open(REM, "<$pth")) { # Read the remote log file while () { ($nod, $stp, @tbl) = split(/\|/); pop(@tbl); $stp{$nod} = $stp; foreach my $itm (@tbl) { ($key, $val) = split(/=/, $itm, 2); if (exists($tb_set{$key})) { $rec = $tb_set{$key}; $agt->set_setting("REMOTE_$nod$key", $val, $rec->[0], $rec->[1]); next; } $key = "REMOTE_$nod$key" if $key =~ m/^_/; $agt->set_setting($key, $val); } $slf->{'_stp'}->{$nod} = $stp; } close(REM); # Save the last steps foreach $nod (keys(%stp)) { $agt->set_setting("REMOTE_$nod\_STEP", $stp{$nod}, 'T', 'Remote node execution step'); } } } =head2 S This macro locally generates the setup file for the specified remote node. It returns the local name on successful completion. Otherwise, it returns an undefined value. =cut sub _m_gen_setup { my $slf = shift; my $ctx = shift; my $nod = shift; my ($fil, $key, $sep, $val); # Determine the local setup file name $fil = File::Spec->catfile($slf->{'_agt'}->get_info('ctl'), 'l'.$nod.'_'.$slf->{'_agt'}->get_info('nam').'.cfg'); # Generate the setup file return undef unless open(CFG, ">$fil"); $sep = '-' x 78; print CFG "#$sep\n# Data Collection Overview\n#$sep\n# S900INI=skip\n\n", "#$sep\n# S900INI: Transferred parameters\n#$sep\n"; while (($key, $val) = splice(@_, 0, 2)) { print CFG "#T.$key\n$key=$val\n"; } close(CFG); # Indicate a successful completion $fil; } =head2 S This macro returns the name of the remote node setup file. When the flag is set, it returns the local name. The two names differ by their first letter to detect shared installation. =cut sub _m_get_setup { my ($slf, $ctx, $nod, $flg) = @_; ($flg ? 'l' : 'r').$nod.'_'.$slf->{'_agt'}->get_info('nam').'.cfg'; } =head2 S This macro returns the current step for the specified node. Otherwise, it returns the default value when the node step is not yet defined. =cut sub _m_get_step { my ($slf, $ctx, $nod, $val) = @_; # Get the step information when not yet initialized _m_init_steps($slf, $ctx) unless exists($slf->{'_rfh'}); # Get the step information $val = $slf->{'_stp'}->{$nod} if exists($slf->{'_stp'}->{$nod}); $val; } =head2 S This macro gets the steps from the settings. Unless the flag is set, it recovers steps from an aborted session and opens the remote log file. It creates the remote directory if it does not already exist. It returns 1 on successful completion. Otherwise, it returns 0. You should run this macro before the first C or C, and especially before starting any thread. =cut sub _m_init_steps { my ($slf, $ctx, $flg) = @_; my ($agt, $pth, $rfh); # Check if it can be done return 0 if exists($slf->{'_rlg'}); # Create the remote directory when needed $agt = $slf->{'_agt'}; $pth = File::Spec->catdir($agt->get_setting('RPT_DIRECTORY'), 'remote'); die "RDA-00511: Cannot create the remote directory $pth:\n $!\n" unless -d $pth || mkdir($pth, 0770); # Recover steps present in an existing remote log file $slf->{'_rlg'} = $pth = File::Spec->catfile($pth, $agt->get_info('nam').'.log'); $slf->_load_steps($agt, $pth) unless $flg; # Extract steps from the settings $slf->{'_stp'} = {}; foreach my $key ($agt->grep_setting('^REMOTE_.*_STEP$')) { $slf->{'_stp'}->{$1} = $agt->get_setting("REMOTE_$1\_STEP") if $key =~ m/^REMOTE_(.*)_STEP$/; } # Open the remote log file unless ($flg) { $rfh = IO::File->new; open($rfh, ">>$pth") || die "RDA-00512: Cannot create the remote log file $pth:\n $!\n"; $slf->{'_rfh'} = $rfh; } # Indicate the successful completion 1; } =head2 S This macro sets the current step for the specified node and saves it in the remote log file. You can specify additional setting directives as extra arguments in a 'key=value' format. =cut sub _m_set_step { my ($slf, $ctx, $nod, $stp, @arg) = @_; my ($agt, $key, $val); # Get the step information when not yet initialized _m_init_steps($slf, $ctx) unless exists($slf->{'_rfh'}); # Save the step information in the remote log file print {$slf->{'_rfh'}} join('|', $nod, $stp, @arg), "|\n"; # Also apply the setting directives in the local context $agt = $slf->{'_agt'}; foreach my $itm (@arg) { ($key, $val) = split(/=/, $itm, 2); $key = "REMOTE_$nod$key" if $key =~ m/^_/; $agt->set_setting($key, $val); } # Set the step information $slf->{'_stp'}->{$nod} = $stp; } =head2 S This macro moves a file between directories. When the flag is set, it copies the file. It returns 1 on success or 0 on failure. =cut sub _m_transfer { my ($slf, $ctx, $ldr, $lnm, $rdr, $rnm, $flg) = @_; my ($src, $dst); return -1 unless $ldr && $lnm; $src = _gen_path($ldr, $lnm); return 0 unless -e $src; $rdr = $ldr unless defined($rdr); return 0 unless -d $rdr || mkdir($rdr,0770); $dst = _gen_path($rdr, defined($rnm) ? $rnm : $lnm); $flg ? copy($src, $dst) : move($src, $dst); } # --- Local Internal functions ------------------------------------------------ sub _gen_path { my ($dir, $fil) = @_; (!defined($fil)) ? $dir : ($dir eq '.') ? $fil : File::Spec->catfile($dir, $fil); } 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