App-nrun

 view release on metacpan or  search on metacpan

lib/NRun/Util.pm  view on Meta::CPAN

    my @_arr = @_;

    return keys %{ { map { $_ => 1 } @_arr } } ;
}

###
# resolve a target definition.
#
# a target definition may be an alias defined in the
# configuration file, a file name containing the target
# hosts or simply the hostname.
#
# $_tgt   - the target to be resolved
# $_alias - the alias definitions
# $_seen  - hash ref with a key for every target already seen
# <- the resolved target hostnames
sub resolve_target {

    my $_tgt   = shift;
    my $_alias = shift;
    my $_seen  = shift;

    $_seen = {} if not defined($_seen);
    if (defined($_seen->{$_tgt})) {

        return;
    }
    $_seen->{$_tgt} = 1;

    my @targets;

    foreach my $token (split(/[ ,]/, $_tgt)) {

        if (defined($_alias) and defined($_alias->{$token})) {
    
            foreach my $tgt (@{$_alias->{$token}}) {
    
                push(@targets, resolve_target($tgt, $_alias, $_seen));
            }
        } elsif (-e $token or $token eq "-") {
    
            foreach my $tgt (read_hosts($token)) {
    
                push(@targets, resolve_target($tgt, $_alias, $_seen));
            }
        } else {
    
            push(@targets, $token);
        }
    }

    return @targets;
}

###
# return the users home directory.
#
# <- the current users home directory
sub home {

    my $home = (getpwuid(getuid()))[7];
}

###
# read a file containing hostnames.
#
# $_file - the file containing the hostnames, one per line
# <- an array containing all hostnames
sub read_hosts {

    my $_file = shift;

    my $hosts = {};

    open(HOSTS, "<$_file") or die("Cannot open $_file: $!");
    foreach my $host (<HOSTS>) {

        chomp($host);
        $host =~ s/^\s+//;
        $host =~ s/\s+$//;

        if (not $host =~ /^ *$/) {

            $hosts->{$host} = 1;
        }
    }

    return keys(%$hosts);
}

##
# read the configuration files.
#
# $_files - the files to be read (values in last file will overwrite values in first file)
sub read_config_files {

    my $_files = shift;

    my $config = {};

    foreach my $file (@$_files) {
  
        if (-e $file) {
  
            my $options = { %{LoadFile($file)} };
            my $aliases = merge($config->{alias}, $options->{alias});

            my $args_nrun  = merge($config->{nrun}, $options->{nrun});
            my $args_ncopy = merge($config->{ncopy}, $options->{ncopy});

            $config = merge($config, $options);

            $config->{alias} = $aliases; 
            $config->{nrun}  = $args_nrun; 
            $config->{ncopy} = $args_ncopy; 
        }
    }

    return $config;
}



( run in 2.189 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )