-Add runcmd3 for functions that need more granular data from executing code

-Hav ADUtils move away from 'system()' calls that mess up environment
-Set up environment right for ldap toosls to reference specific ldaprc


git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@6157 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
This commit is contained in:
jbjohnso 2010-05-18 19:30:49 +00:00
parent 06b5894b63
commit 535e30ce4a
3 changed files with 74 additions and 9 deletions

View File

@ -22,6 +22,7 @@ use POSIX qw(ceil);
use File::Path;
use Socket;
use strict;
use Symbol;
use warnings "all";
require xCAT::InstUtils;
require xCAT::NetworkUtils;
@ -31,7 +32,7 @@ require xCAT::NodeRange;
require DBI;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(genpassword);
our @EXPORT_OK = qw(genpassword runcmd3);
my $utildata; #data to persist locally
#--------------------------------------------------------------------------------
@ -865,6 +866,64 @@ sub remove_cron_job
return (0, "");
}
#-------------------------------------------------------------------------------
=head3 runcmd3
Run the specified command with optional input and return stderr, stdout, and exit code
Arguments:
command=>[] - Array reference of command to run
input=>[] or string - Data to send to stdin of process like piping input
Returns:
{ exitcode => number, output=> $string, errors => string }
=cut
sub runcmd3 { #a proper runcmd that indpendently returns stdout, stderr, pid and accepts a stdin
my %args = @_;
my @indata;
my $output;
my $errors;
if ($args{input}) {
if (ref $args{input}) { #array ref
@indata = @{$args{input}};
} else { #just a string
@indata=($args{input});
}
}
my @cmd;
if (ref $args{command}) {
@cmd = @{$args{command}};
} else {
@cmd = ($args{command});
}
my $cmdin;
my $cmdout;
my $cmderr = gensym;
my $cmdpid = open3($cmdin,$cmdout,$cmderr,@cmd);
my $cmdsel = IO::Select->new($cmdout,$cmdin);
foreach (@indata) {
print $cmdin $_;
}
my @handles;
while (@handles = $cmdsel->can_read()) {
foreach (@handles) {
my $line;
my $done = sysread $_,$line,180;
if ($done) {
if ($_ eq $cmdout) {
$output .= $line;
} else {
$errors .= $line;
}
} else {
$cmdsel->remove($_);
close($_);
}
}
}
waitpid($cmdpid,0);
my $exitcode = $? >> 8;
return { 'exitcode' => $exitcode, 'output' => $output, 'errors' => $errors }
}
#-------------------------------------------------------------------------------
=head3 runcmd

View File

@ -9,7 +9,7 @@ package xCAT::ADUtils;
use strict;
use MIME::Base64;
use Encode;
use xCAT::Utils qw/genpassword/;
use xCAT::Utils qw/genpassword runcmd3/;
use IPC::Open3;
use IO::Select;
use Symbol qw/gensym/;
@ -456,13 +456,14 @@ sub add_user_account {
$ldif =~ s/##USERSHELL##/$shell/g;
$ldif =~ s/##B64PASSWORD##/$b64password/g;
my $dn = "CN=$fullname,$ou";
my $rc = system("ldapsearch -H ldaps://$directoryserver -b \"$dn\"");
my $retdata = runcmd3(command=>["ldapsearch","-H","ldaps://$directoryserver","-b" ,"$dn"]);
my $rc = $retdata->{exitcode};
if ($rc == 0) {
return {error=>"User already exists"};
} elsif (not $rc==8192) {
return {error=>"Unknown error $rc"};
return {error=>"Unknown error $rc:".$retdata->{errors}};
}
$rc = system("echo '$ldif'|ldapmodify -H ldaps://$directoryserver");
$retdata = runcmd3(input=>$ldif,command=>["ldapmodify","-H","ldaps://$directoryserver"]);
return {password=>$newpassword};
}
=cut
@ -511,7 +512,8 @@ sub add_host_account {
my $b64password = encode_base64($newpassword);
my $ldif;
my $dn = "CN=$nodename,$ou";
my $rc = system("ldapsearch -H ldaps://$directoryserver -b $dn"); #TODO: for mass add, search once, hit that
my $retdata = runcmd3(command=>["ldapsearch","-H","ldaps://$directoryserver","-b","$dn"]); #TODO: for mass add, search once, hit that
my $rc = $retdata->{exitcode};
if ($rc == 0) {
if ($changepassondupe) {
$ldif = $machineldifpasschange;
@ -519,7 +521,7 @@ sub add_host_account {
return {error=>"System already exists"};
}
} elsif (not $rc==8192) {
return {error=>"Unknown error $rc"};
return {error=>"Unknown error $rc: ".$retdata->{errors}};
} else {
$ldif = $machineldiftemplate;
}
@ -528,9 +530,11 @@ sub add_host_account {
$ldif =~ s/##REALMDCS##/$domain_components/g;
$ldif =~ s/##DNSDOMAIN##/$dnsdomain/g;
$ldif =~ s/##NODENAME##/$nodename/g;
$rc = system("echo '$ldif'|ldapmodify -H ldaps://$directoryserver");
$retdata = runcmd3(input=>$ldif,command=>['ldapmodify','-H',"ldaps://$directoryserver"]);
substr $nativenewpassword,0,1,'';
chop($nativenewpassword);
#if ($retdata->{exitcode} != 0) {
#}
return {password=>$nativenewpassword};
}
@ -572,6 +576,7 @@ sub krb_login {
sub find_free_params { #search for things like next available uidNumber
my %args = @_;
my @needed_parms = split /,/,$args{needed_params};

View File

@ -23,7 +23,8 @@ sub handled_commands {
}
sub process_request {
$ENV{LDAPCONF}='/etc/xcat/ad.ldaprc';
$ENV{LDAPRC}='/etc/xcat/ad.ldaprc';
$ENV{HOME}='';
my $request = shift;
my $command = $request->{command}->[0];
$callback = shift;