2007-10-26 22:44:33 +00:00
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
package xCAT_plugin::pxe ;
2009-09-25 20:30:05 +00:00
use Data::Dumper ;
2007-10-26 22:44:33 +00:00
use Sys::Syslog ;
2012-02-27 18:48:33 +00:00
use xCAT::Scope ;
2012-03-16 10:31:33 +00:00
use xCAT::MsgUtils ;
2007-10-26 22:44:33 +00:00
use Socket ;
2008-03-04 19:36:35 +00:00
use File::Copy ;
2010-04-01 14:45:22 +00:00
use File::Path ;
2009-09-25 21:08:41 +00:00
use Getopt::Long ;
2012-08-09 04:07:40 +00:00
require xCAT::Utils ;
require xCAT::TableUtils ;
2013-08-27 16:48:37 +00:00
use xCAT::ServiceNodeUtils ;
2009-09-25 20:46:26 +00:00
my $ addkcmdlinehandled ;
2007-10-26 22:44:33 +00:00
my $ request ;
my $ callback ;
my $ dhcpconf = "/etc/dhcpd.conf" ;
2012-08-09 04:07:40 +00:00
my $ globaltftpdir = xCAT::TableUtils - > getTftpDir ( ) ;
2007-10-26 22:44:33 +00:00
#my $dhcpver = 3;
my % usage = (
2012-12-11 20:59:50 +00:00
"nodeset" = > "Usage: nodeset <noderange> [install|shell|boot|runcmd=bmcsetup|netboot|iscsiboot|osimage[=<imagename>]|statelite|offline]" ,
2007-10-26 22:44:33 +00:00
) ;
sub handled_commands {
return {
nodeset = > "noderes:netboot"
}
}
sub check_dhcp {
2008-05-15 21:16:33 +00:00
return 1 ;
2007-10-26 22:44:33 +00:00
#TODO: omapi magic to do things right
my $ node = shift ;
my $ dhcpfile ;
open ( $ dhcpfile , $ dhcpconf ) ;
while ( <$dhcpfile> ) {
if ( /host $node\b/ ) {
close $ dhcpfile ;
return 1 ;
}
}
close $ dhcpfile ;
return 0 ;
}
2012-03-29 18:14:03 +00:00
sub _slow_get_tftpdir { #make up for paths where tftpdir is not passed in
2012-03-29 22:22:32 +00:00
my $ node = shift ;
2012-03-29 19:12:25 +00:00
my $ nrtab = xCAT::Table - > new ( 'noderes' , - create = > 0 ) ; #in order to detect per-node tftp directories
2012-03-29 18:14:03 +00:00
unless ( $ nrtab ) { return $ globaltftpdir ; }
my $ ent = $ nrtab - > getNodeAttribs ( $ node , [ "tftpdir" ] ) ;
if ( $ ent and $ ent - > { tftpdir } ) {
return $ ent - > { tftpdir } ;
} else {
return $ globaltftpdir ;
}
}
2007-10-26 22:44:33 +00:00
sub getstate {
my $ node = shift ;
2012-02-15 21:04:42 +00:00
my $ tftpdir = shift ;
2012-03-29 18:14:03 +00:00
unless ( $ tftpdir ) { $ tftpdir = _slow_get_tftpdir ( $ node ) ; }
2007-10-26 22:44:33 +00:00
if ( check_dhcp ( $ node ) ) {
2009-09-25 20:30:05 +00:00
if ( - r $ tftpdir . "/pxelinux.cfg/" . $ node ) {
2007-10-26 22:44:33 +00:00
my $ fhand ;
open ( $ fhand , $ tftpdir . "/pxelinux.cfg/" . $ node ) ;
my $ headline = <$fhand> ;
close $ fhand ;
$ headline =~ s/^#// ;
chomp ( $ headline ) ;
return $ headline ;
} else {
return "boot" ;
}
} else {
return "discover" ;
}
}
sub setstate {
= pod
This function will manipulate the pxelinux . cfg structure to match what the noderes / chain tables indicate the node should be booting .
= cut
my $ node = shift ;
2008-07-11 19:02:39 +00:00
my % bphash = % { shift ( ) } ;
my % chainhash = % { shift ( ) } ;
my % machash = % { shift ( ) } ;
2010-07-14 11:51:44 +00:00
my % nthash = % { shift ( ) } ;
2012-02-15 21:04:42 +00:00
my $ tftpdir = shift ;
2013-04-26 14:10:58 +00:00
my % linuximghash = ( ) ;
my $ linuximghashref = shift ;
if ( ref $ linuximghashref ) { % linuximghash = % { $ linuximghashref } ; }
2013-04-25 12:07:48 +00:00
my $ imgaddkcmdline = ( $ linuximghash { 'boottarget' } ) ? undef : $ linuximghash { 'addkcmdline' } ;
2008-07-11 19:02:39 +00:00
my $ kern = $ bphash { $ node } - > [ 0 ] ; #$bptab->getNodeAttribs($node,['kernel','initrd','kcmdline']);
2013-04-25 12:07:48 +00:00
if ( not $ addkcmdlinehandled - > { $ node } and ( $ kern - > { addkcmdline } or ( $ imgaddkcmdline ) ) ) {
2010-03-01 16:32:22 +00:00
#Implement the kcmdline append here for
#most generic, least code duplication
###hack start
# This is my comment. There are many others like it, but this one is mine.
# My comment is my best friend. It is my life. I must master it as I must master my life.
# Without me, my comment is useless. Without my comment, I am useless.
# Jarrod to clean up. It really should be in Table.pm and support
# the new statelite $table notation.
#I dislike spaces, tabs are cleaner, I'm too tired to change all the xCAT code.
#I give in.
2013-04-25 12:07:48 +00:00
my $ kcmdlinehack = ( $ imgaddkcmdline ) ? $ kern - > { addkcmdline } . " " . $ imgaddkcmdline : $ kern - > { addkcmdline } ;
2010-03-01 16:32:22 +00:00
while ( $ kcmdlinehack =~ /#NODEATTRIB:([^:#]+):([^:#]+)#/ ) {
my $ natab = xCAT::Table - > new ( $ 1 ) ;
my $ naent = $ natab - > getNodeAttribs ( $ node , [ $ 2 ] ) ;
my $ naval = $ naent - > { $ 2 } ;
$ kcmdlinehack =~ s/#NODEATTRIB:([^:#]+):([^:#]+)#/$naval/ ;
}
while ( $ kcmdlinehack =~ /#TABLE:([^:#]+):([^:#]+):([^:#]+)#/ ) {
my $ tabname = $ 1 ;
my $ keyname = $ 2 ;
my $ colname = $ 3 ;
if ( $ 2 =~ /THISNODE/ or $ 2 =~ /\$NODE/ ) {
my $ natab = xCAT::Table - > new ( $ tabname ) ;
my $ naent = $ natab - > getNodeAttribs ( $ node , [ $ colname ] ) ;
my $ naval = $ naent - > { $ colname } ;
$ kcmdlinehack =~ s/#TABLE:([^:#]+):([^:#]+):([^:#]+)#/$naval/ ;
} else {
my $ msg = "Table key of $2 not yet supported by boottarget mini-template" ;
2013-10-09 16:28:09 +00:00
$ ::callback - > ( {
2010-03-01 16:32:22 +00:00
error = > [ "$msg" ] ,
errorcode = > [ 1 ]
} ) ;
}
}
#$kern->{kcmdline} .= " ".$kern->{addkcmdline};
2013-04-25 12:07:48 +00:00
2010-03-01 16:32:22 +00:00
$ kern - > { kcmdline } . = " " . $ kcmdlinehack ;
###hack end
2009-09-25 20:46:26 +00:00
}
2009-03-18 14:11:30 +00:00
if ( $ kern - > { kcmdline } =~ /!myipfn!/ ) {
2012-08-09 04:07:40 +00:00
my $ ipfn = xCAT::NetworkUtils - > my_ip_facing ( $ node ) ;
2009-03-18 19:37:54 +00:00
unless ( $ ipfn ) {
2012-08-09 04:07:40 +00:00
my @ myself = xCAT::NetworkUtils - > determinehostname ( ) ;
2009-03-18 19:37:54 +00:00
my $ myname = $ myself [ ( scalar @ myself ) - 1 ] ;
2013-10-09 16:28:09 +00:00
$ ::callback - > (
2009-03-18 19:37:54 +00:00
{
error = > [
"$myname: Unable to determine or reasonably guess the image server for $node"
] ,
errorcode = > [ 1 ]
}
) ;
}
2009-09-25 20:46:26 +00:00
$ kern - > { kcmdline } =~ s/!myipfn!/$ipfn/g ;
2009-03-18 14:11:30 +00:00
}
2007-10-26 22:44:33 +00:00
my $ pcfg ;
2010-04-01 14:45:22 +00:00
unless ( - d $ tftpdir . "/pxelinux.cfg/" ) {
mkpath ( $ tftpdir . "/pxelinux.cfg/" ) ;
}
2009-09-25 20:30:05 +00:00
open ( $ pcfg , '>' , $ tftpdir . "/pxelinux.cfg/" . $ node ) ;
2008-07-11 19:02:39 +00:00
my $ cref = $ chainhash { $ node } - > [ 0 ] ; #$chaintab->getNodeAttribs($node,['currstate']);
2007-10-26 22:44:33 +00:00
if ( $ cref - > { currstate } ) {
print $ pcfg "#" . $ cref - > { currstate } . "\n" ;
}
2009-09-25 20:30:05 +00:00
print $ pcfg "DEFAULT xCAT\n" ;
print $ pcfg "LABEL xCAT\n" ;
2008-07-11 19:02:39 +00:00
if ( $ cref and $ cref - > { currstate } eq "boot" ) {
2009-09-25 20:30:05 +00:00
print $ pcfg "LOCALBOOT 0\n" ;
2007-10-26 22:44:33 +00:00
close ( $ pcfg ) ;
} elsif ( $ kern and $ kern - > { kernel } ) {
2009-09-25 20:30:05 +00:00
if ( $ kern - > { kernel } =~ /!/ ) {
my $ hypervisor ;
my $ kernel ;
( $ kernel , $ hypervisor ) = split /!/ , $ kern - > { kernel } ;
print $ pcfg " KERNEL mboot.c32\n" ;
print $ pcfg " APPEND $hypervisor --- $kernel " . $ kern - > { kcmdline } . " --- " . $ kern - > { initrd } . "\n" ;
2009-02-21 14:25:29 +00:00
} else {
2009-09-25 20:30:05 +00:00
#It's time to set pxelinux for this node to boot the kernel..
print $ pcfg " KERNEL " . $ kern - > { kernel } . "\n" ;
if ( $ kern - > { initrd } or $ kern - > { kcmdline } ) {
print $ pcfg " APPEND " ;
}
if ( $ kern and $ kern - > { initrd } ) {
print $ pcfg "initrd=" . $ kern - > { initrd } . " " ;
}
if ( $ kern and $ kern - > { kcmdline } ) {
print $ pcfg $ kern - > { kcmdline } . "\n" ;
} else {
print $ pcfg "\n" ;
}
2010-07-14 11:51:44 +00:00
# add the IPAPPEND flag
my $ os = $ nthash { $ node } - > [ 0 ] - > { os } ;
if ( $ os !~ /fedora12|fedora13/ ) {
print $ pcfg " IPAPPEND 2\n" ;
}
2009-02-21 14:25:29 +00:00
}
2007-10-26 22:44:33 +00:00
close ( $ pcfg ) ;
my $ inetn = inet_aton ( $ node ) ;
unless ( $ inetn ) {
2013-10-18 11:50:22 +00:00
syslog ( "local4|err" , "xCAT unable to resolve IP for $node in pxe plugin" ) ;
2007-10-26 22:44:33 +00:00
return ;
}
} else { #TODO: actually, should possibly default to xCAT image?
print $ pcfg "LOCALBOOT 0\n" ;
close ( $ pcfg ) ;
}
2008-02-28 00:17:24 +00:00
my $ mactab = xCAT::Table - > new ( 'mac' ) ; #to get all the hostnames
my % ipaddrs ;
2008-07-11 19:02:39 +00:00
unless ( inet_aton ( $ node ) ) {
2013-10-18 11:50:22 +00:00
syslog ( "local4|err" , "xCAT unable to resolve IP in pxe plugin" ) ;
2008-07-11 19:02:39 +00:00
return ;
}
2007-10-26 22:44:33 +00:00
my $ ip = inet_ntoa ( inet_aton ( $ node ) ) ; ;
unless ( $ ip ) {
2013-10-18 11:50:22 +00:00
syslog ( "local4|err" , "xCAT unable to resolve IP in pxe plugin" ) ;
2007-10-26 22:44:33 +00:00
return ;
}
2008-02-28 00:17:24 +00:00
$ ipaddrs { $ ip } = 1 ;
if ( $ mactab ) {
2008-07-11 19:02:39 +00:00
my $ ment = $ machash { $ node } - > [ 0 ] ; #$mactab->getNodeAttribs($node,['mac']);
2008-02-28 00:17:24 +00:00
if ( $ ment and $ ment - > { mac } ) {
my @ macs = split ( /\|/ , $ ment - > { mac } ) ;
foreach ( @ macs ) {
if ( /!(.*)/ ) {
2008-03-21 20:00:07 +00:00
if ( inet_aton ( $ 1 ) ) {
$ ipaddrs { inet_ntoa ( inet_aton ( $ 1 ) ) } = 1 ;
}
2008-02-28 00:17:24 +00:00
}
}
}
}
2008-09-09 22:28:30 +00:00
my $ hassymlink = eval { symlink ( "" , "" ) ; 1 } ;
2008-02-28 00:17:24 +00:00
foreach $ ip ( keys % ipaddrs ) {
my @ ipa = split ( /\./ , $ ip ) ;
my $ pname = sprintf ( "%02X%02X%02X%02X" , @ ipa ) ;
unlink ( $ tftpdir . "/pxelinux.cfg/" . $ pname ) ;
2008-09-09 22:28:30 +00:00
if ( $ hassymlink ) {
symlink ( $ node , $ tftpdir . "/pxelinux.cfg/" . $ pname ) ;
} else {
link ( $ tftpdir . "/pxelinux.cfg/" . $ node , $ tftpdir . "/pxelinux.cfg/" . $ pname ) ;
}
2008-02-28 00:17:24 +00:00
}
2007-10-26 22:44:33 +00:00
}
my $ errored = 0 ;
sub pass_along {
my $ resp = shift ;
2008-05-07 20:45:48 +00:00
if ( $ resp and ( $ resp - > { errorcode } and $ resp - > { errorcode } - > [ 0 ] ) or ( $ resp - > { error } and $ resp - > { error } - > [ 0 ] ) ) {
2007-10-26 22:44:33 +00:00
$ errored = 1 ;
}
2008-05-07 20:45:48 +00:00
foreach ( @ { $ resp - > { node } } ) {
if ( $ _ - > { error } or $ _ - > { errorcode } ) {
$ errored = 1 ;
}
2009-09-25 20:46:26 +00:00
if ( $ _ - > { _addkcmdlinehandled } ) {
$ addkcmdlinehandled - > { $ _ - > { name } - > [ 0 ] } = 1 ;
return ; #Don't send back to client this internal hint
}
2008-05-07 20:45:48 +00:00
}
2009-09-25 20:46:26 +00:00
$ callback - > ( $ resp ) ;
2007-10-26 22:44:33 +00:00
}
2009-08-03 02:00:13 +00:00
2009-09-25 20:30:05 +00:00
sub preprocess_request {
2008-05-02 01:43:35 +00:00
#Assume shared tftp directory for boring people, but for cool people, help sync up tftpdirectory contents when
#they specify no sharedtftp in site table
2012-05-23 05:30:19 +00:00
#my $stab = xCAT::Table->new('site');
2009-09-25 20:30:05 +00:00
my $ req = shift ;
2009-09-25 21:08:41 +00:00
my $ callback1 = shift ;
my $ command = $ req - > { command } - > [ 0 ] ;
my $ sub_req = shift ;
2013-08-27 16:48:37 +00:00
my $ nodes = $ req - > { node } ;
2009-09-25 21:08:41 +00:00
my @ args = ( ) ;
if ( ref ( $ req - > { arg } ) ) {
@ args = @ { $ req - > { arg } } ;
} else {
@ args = ( $ req - > { arg } ) ;
}
@ ARGV = @ args ;
2009-09-25 21:14:48 +00:00
Getopt::Long:: Configure ( "bundling" ) ;
2009-09-25 21:08:41 +00:00
Getopt::Long:: Configure ( "pass_through" ) ;
if ( ! GetOptions ( 'h|?|help' = > \ $ HELP , 'v|version' = > \ $ VERSION ) ) {
if ( $ usage { $ command } ) {
my % rsp ;
$ rsp { data } - > [ 0 ] = $ usage { $ command } ;
$ callback1 - > ( \ % rsp ) ;
}
return ;
}
if ( $ HELP ) {
if ( $ usage { $ command } ) {
my % rsp ;
$ rsp { data } - > [ 0 ] = $ usage { $ command } ;
$ callback1 - > ( \ % rsp ) ;
}
return ;
}
if ( $ VERSION ) {
my $ ver = xCAT::Utils - > Version ( ) ;
my % rsp ;
$ rsp { data } - > [ 0 ] = "$ver" ;
$ callback1 - > ( \ % rsp ) ;
return ;
}
if ( @ ARGV == 0 ) {
if ( $ usage { $ command } ) {
my % rsp ;
$ rsp { data } - > [ 0 ] = $ usage { $ command } ;
$ callback1 - > ( \ % rsp ) ;
}
return ;
}
2013-10-18 11:50:22 +00:00
#my $sent = $stab->getAttribs({key=>'sharedtftp'},'value');
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "sharedtftp" ) ;
2012-05-23 05:30:19 +00:00
my $ t_entry = $ entries [ 0 ] ;
if ( defined ( $ t_entry ) and ( $ t_entry == 0 or $ t_entry =~ /no/i ) ) {
2013-08-27 16:48:37 +00:00
# check for computenodes and servicenodes from the noderange, if so error out
2013-12-12 16:20:40 +00:00
my @ SN ;
my @ CN ;
xCAT::ServiceNodeUtils - > getSNandCPnodes ( \ @$ nodes , \ @ SN , \ @ CN ) ;
unless ( ( $ args [ 0 ] eq 'stat' ) or ( $ args [ 0 ] eq 'enact' ) ) { # ok for these options
if ( ( @ SN > 0 ) && ( @ CN > 0 ) ) { # there are both SN and CN
2013-08-27 16:48:37 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Nodeset was run with a noderange containing both service nodes and compute nodes. This is not valid. You must submit with either compute nodes in the noderange or service nodes. \n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback1 ) ;
return ;
2013-12-12 16:20:40 +00:00
}
}
2013-08-27 16:48:37 +00:00
2008-05-02 01:43:35 +00:00
$ req - > { '_disparatetftp' } = [ 1 ] ;
2009-03-28 15:03:36 +00:00
if ( $ req - > { inittime } - > [ 0 ] ) {
2009-03-28 06:15:05 +00:00
return [ $ req ] ;
}
2013-10-18 11:50:22 +00:00
if ( @ CN > 0 ) { # if compute nodes broadcast to all servicenodes
return xCAT::Scope - > get_broadcast_scope ( $ req , @ _ ) ;
2013-09-13 11:37:25 +00:00
}
2008-05-02 01:43:35 +00:00
}
return [ $ req ] ;
}
2007-10-26 22:44:33 +00:00
sub process_request {
$ request = shift ;
$ callback = shift ;
my $ sub_req = shift ;
2013-10-09 16:28:09 +00:00
$ ::callback = $ callback ;
2007-10-26 22:44:33 +00:00
my @ args ;
my @ nodes ;
2008-02-02 19:39:33 +00:00
my @ rnodes ;
2007-10-26 22:44:33 +00:00
if ( ref ( $ request - > { node } ) ) {
2008-02-02 19:39:33 +00:00
@ rnodes = @ { $ request - > { node } } ;
2007-10-26 22:44:33 +00:00
} else {
2008-02-02 19:39:33 +00:00
if ( $ request - > { node } ) { @ rnodes = ( $ request - > { node } ) ; }
2007-10-26 22:44:33 +00:00
}
2009-01-08 07:00:27 +00:00
2008-02-02 19:39:33 +00:00
unless ( @ rnodes ) {
2007-10-26 22:44:33 +00:00
if ( $ usage { $ request - > { command } - > [ 0 ] } ) {
$ callback - > ( { data = > $ usage { $ request - > { command } - > [ 0 ] } } ) ;
}
return ;
}
2008-06-25 14:50:11 +00:00
2008-05-02 01:43:35 +00:00
#if not shared, then help sync up
2009-05-07 13:58:31 +00:00
if ( $ request - > { '_disparatetftp' } - > [ 0 ] ) { #reading hint from preprocess_command
2008-05-02 01:43:35 +00:00
@ nodes = ( ) ;
foreach ( @ rnodes ) {
2012-08-09 04:07:40 +00:00
if ( xCAT::NetworkUtils - > nodeonmynet ( $ _ ) ) {
2008-05-02 01:43:35 +00:00
push @ nodes , $ _ ;
2011-12-05 14:53:13 +00:00
} else {
2012-03-16 10:31:33 +00:00
xCAT::MsgUtils - > message ( "S" , "$_: pxe netboot: stop configuration because of none sharedtftp and not on same network with its xcatmaster." ) ;
2008-05-02 01:43:35 +00:00
}
}
} else {
@ nodes = @ rnodes ;
}
2009-09-25 21:08:41 +00:00
2012-03-16 10:31:33 +00:00
# return directly if no nodes in the same network
unless ( @ nodes ) {
xCAT::MsgUtils - > message ( "S" , "xCAT: pxe netboot: no valid nodes. Stop the operation on this server." ) ;
return ;
}
2009-09-25 21:08:41 +00:00
if ( ref ( $ request - > { arg } ) ) {
@ args = @ { $ request - > { arg } } ;
} else {
@ args = ( $ request - > { arg } ) ;
}
#now run the begin part of the prescripts
unless ( $ args [ 0 ] eq 'stat' ) { # or $args[0] eq 'enact') {
$ errored = 0 ;
if ( $ request - > { '_disparatetftp' } - > [ 0 ] ) { #the call is distrubuted to the service node already, so only need to handles my own children
$ sub_req - > ( { command = > [ 'runbeginpre' ] ,
node = > \ @ nodes ,
arg = > [ $ args [ 0 ] , '-l' ] } , \ & pass_along ) ;
} else { #nodeset did not distribute to the service node, here we need to let runednpre to distribute the nodes to their masters
$ sub_req - > ( { command = > [ 'runbeginpre' ] ,
node = > \ @ rnodes ,
arg = > [ $ args [ 0 ] ] } , \ & pass_along ) ;
}
2011-05-16 19:59:10 +00:00
if ( $ errored ) {
my $ rsp ;
$ rsp - > { errorcode } - > [ 0 ] = 1 ;
$ rsp - > { error } - > [ 0 ] = "Failed in running begin prescripts\n" ;
$ callback - > ( $ rsp ) ;
return ;
}
2009-09-25 21:08:41 +00:00
}
2009-09-25 20:30:05 +00:00
2009-09-25 21:08:41 +00:00
#end prescripts code
2009-09-25 20:30:05 +00:00
if ( ! - r "$tftpdir/pxelinux.0" ) {
unless ( - r "/usr/lib/syslinux/pxelinux.0" or - r "/usr/share/syslinux/pxelinux.0" ) {
$ callback - > ( { error = > [ "Unable to find pxelinux.0 " ] , errorcode = > [ 1 ] } ) ;
2008-02-26 18:48:43 +00:00
return ;
2008-06-27 15:57:58 +00:00
}
2009-09-25 20:30:05 +00:00
if ( - r "/usr/lib/syslinux/pxelinux.0" ) {
copy ( "/usr/lib/syslinux/pxelinux.0" , "$tftpdir/pxelinux.0" ) ;
} else {
copy ( "/usr/share/syslinux/pxelinux.0" , "$tftpdir/pxelinux.0" ) ;
}
2008-02-26 18:48:43 +00:00
chmod ( 0644 , "$tftpdir/pxelinux.0" ) ;
2008-06-27 15:57:58 +00:00
}
2009-09-25 20:30:05 +00:00
unless ( - r "$tftpdir/pxelinux.0" ) {
2008-06-27 15:57:58 +00:00
$ callback - > ( { errror = > [ "Unable to find pxelinux.0 from syslinux" ] , errorcode = > [ 1 ] } ) ;
return ;
}
2008-02-02 19:39:33 +00:00
2007-10-26 22:44:33 +00:00
2009-05-07 04:31:33 +00:00
$ errored = 0 ;
2009-09-25 21:08:41 +00:00
my $ inittime = 0 ;
if ( exists ( $ request - > { inittime } ) ) { $ inittime = $ request - > { inittime } - > [ 0 ] ; }
if ( ! $ inittime ) { $ inittime = 0 ; }
2009-03-28 04:25:03 +00:00
unless ( $ args [ 0 ] eq 'stat' ) { # or $args[0] eq 'enact') {
2009-09-25 20:30:05 +00:00
$ sub_req - > ( { command = > [ 'setdestiny' ] ,
2009-09-25 21:08:41 +00:00
node = > \ @ nodes ,
inittime = > [ $ inittime ] ,
2013-07-04 05:20:16 +00:00
arg = > \ @ args } , \ & pass_along ) ;
2007-10-26 22:44:33 +00:00
}
if ( $ errored ) { return ; }
2008-07-11 19:02:39 +00:00
#Time to actually configure the nodes, first extract database data with the scalable calls
my $ bptab = xCAT::Table - > new ( 'bootparams' , - create = > 1 ) ;
my $ chaintab = xCAT::Table - > new ( 'chain' ) ;
my $ mactab = xCAT::Table - > new ( 'mac' ) ; #to get all the hostnames
2010-07-14 11:51:44 +00:00
my $ typetab = xCAT::Table - > new ( 'nodetype' ) ;
2012-02-15 21:04:42 +00:00
my $ restab = xCAT::Table - > new ( 'noderes' ) ;
2013-04-25 12:07:48 +00:00
my $ linuximgtab = xCAT::Table - > new ( 'linuximage' , - create = > 1 ) ;
2012-02-15 21:04:42 +00:00
my % nrhash = % { $ restab - > getNodesAttribs ( \ @ nodes , [ qw( tftpdir ) ] ) } ;
2009-09-25 20:46:26 +00:00
my % bphash = % { $ bptab - > getNodesAttribs ( \ @ nodes , [ qw( kernel initrd kcmdline addkcmdline ) ] ) } ;
2008-07-11 19:02:39 +00:00
my % chainhash = % { $ chaintab - > getNodesAttribs ( \ @ nodes , [ qw( currstate ) ] ) } ;
my % machash = % { $ mactab - > getNodesAttribs ( \ @ nodes , [ qw( mac ) ] ) } ;
2013-04-25 12:07:48 +00:00
my % nthash = % { $ typetab - > getNodesAttribs ( \ @ nodes , [ qw( os provmethod ) ] ) } ;
2007-10-26 22:44:33 +00:00
foreach ( @ nodes ) {
my % response ;
2012-02-15 21:04:42 +00:00
my $ tftpdir ;
if ( $ nrhash { $ _ } and $ nrhash { $ _ } - > [ 0 ] and $ nrhash { $ _ } - > [ 0 ] - > { tftpdir } ) {
$ tftpdir = $ nrhash { $ _ } - > [ 0 ] - > { tftpdir } ;
} else {
$ tftpdir = $ globaltftpdir ;
}
2007-10-26 22:44:33 +00:00
$ response { node } - > [ 0 ] - > { name } - > [ 0 ] = $ _ ;
if ( $ args [ 0 ] eq 'stat' ) {
2012-02-15 21:04:42 +00:00
$ response { node } - > [ 0 ] - > { data } - > [ 0 ] = getstate ( $ _ , $ tftpdir ) ;
2007-10-26 22:44:33 +00:00
$ callback - > ( \ % response ) ;
} elsif ( $ args [ 0 ] ) { #If anything else, send it on to the destiny plugin, then setstate
2013-04-25 12:07:48 +00:00
my $ ent = $ nthash { $ _ } - > [ 0 ] ;
my $ osimgname = $ ent - > { 'provmethod' } ;
my $ linuximghash = undef ;
unless ( $ osimgname =~ /^(install|netboot|statelite)$/ ) {
$ linuximghash = $ linuximgtab - > getAttribs ( { imagename = > $ osimgname } , 'boottarget' , 'addkcmdline' ) ;
}
( $ rc , $ errstr ) = setstate ( $ _ , \ % bphash , \ % chainhash , \ % machash , \ % nthash , $ tftpdir , $ linuximghash ) ;
2009-03-18 14:11:30 +00:00
if ( $ rc ) {
$ response { node } - > [ 0 ] - > { errorcode } - > [ 0 ] = $ rc ;
$ response { node } - > [ 0 ] - > { errorc } - > [ 0 ] = $ errstr ;
$ callback - > ( \ % response ) ;
}
2007-10-26 22:44:33 +00:00
}
}
2009-09-25 21:08:41 +00:00
my $ inittime = 0 ;
if ( exists ( $ request - > { inittime } ) ) { $ inittime = $ request - > { inittime } - > [ 0 ] ; }
if ( ! $ inittime ) { $ inittime = 0 ; }
#dhcp stuff -- inittime is set when xcatd on sn is started
2010-03-03 18:20:33 +00:00
unless ( ( $ args [ 0 ] eq 'stat' ) || ( $ inittime ) || ( $ args [ 0 ] eq 'offline' ) ) {
2009-09-25 21:08:41 +00:00
my $ do_dhcpsetup = 1 ;
2012-05-23 05:30:19 +00:00
#my $sitetab = xCAT::Table->new('site');
#if ($sitetab) {
#(my $ref) = $sitetab->getAttribs({key => 'dhcpsetup'}, 'value');
2012-08-09 04:07:40 +00:00
my @ entries = xCAT::TableUtils - > get_site_attribute ( "dhcpsetup" ) ;
2012-05-23 05:30:19 +00:00
my $ t_entry = $ entries [ 0 ] ;
if ( defined ( $ t_entry ) ) {
if ( $ t_entry =~ /0|n|N/ ) { $ do_dhcpsetup = 0 ; }
2009-09-25 21:08:41 +00:00
}
2012-05-23 05:30:19 +00:00
#}
2009-09-25 21:08:41 +00:00
if ( $ do_dhcpsetup ) {
if ( $ request - > { '_disparatetftp' } - > [ 0 ] ) { #reading hint from preprocess_command
$ sub_req - > ( { command = > [ 'makedhcp' ] , arg = > [ '-l' ] ,
node = > \ @ nodes } , $ callback ) ;
} else {
$ sub_req - > ( { command = > [ 'makedhcp' ] ,
node = > \ @ nodes } , $ callback ) ;
}
}
2009-03-27 22:09:54 +00:00
}
2010-11-01 17:56:57 +00:00
#unlink the files for 'offline' command
if ( $ args [ 0 ] eq 'offline' ) {
foreach my $ node ( @ nodes ) {
my % ipaddrs ;
unless ( inet_aton ( $ node ) ) {
2013-10-18 11:50:22 +00:00
syslog ( "local4|err" , "xCAT unable to resolve IP in pxe plugin" ) ;
2010-11-01 17:56:57 +00:00
return ;
}
my $ ip = inet_ntoa ( inet_aton ( $ node ) ) ; ;
unless ( $ ip ) {
2013-10-18 11:50:22 +00:00
syslog ( "local4|err" , "xCAT unable to resolve IP in pxe plugin" ) ;
2010-11-01 17:56:57 +00:00
return ;
}
$ ipaddrs { $ ip } = 1 ;
if ( $ mactab ) {
my $ ment = $ machash { $ node } - > [ 0 ] ; #$mactab->getNodeAttribs($node,['mac']);
if ( $ ment and $ ment - > { mac } ) {
my @ macs = split ( /\|/ , $ ment - > { mac } ) ;
foreach ( @ macs ) {
if ( /!(.*)/ ) {
if ( inet_aton ( $ 1 ) ) {
$ ipaddrs { inet_ntoa ( inet_aton ( $ 1 ) ) } = 1 ;
}
}
}
}
}
my $ hassymlink = eval { symlink ( "" , "" ) ; 1 } ;
unlink ( $ tftpdir . "/pxelinux.cfg/" . $ node ) ;
foreach $ ip ( keys % ipaddrs ) {
my @ ipa = split ( /\./ , $ ip ) ;
my $ pname = sprintf ( "%02X%02X%02X%02X" , @ ipa ) ;
unlink ( $ tftpdir . "/pxelinux.cfg/" . $ pname ) ;
#if ($hassymlink) {
#symlink($node,$tftpdir."/pxelinux.cfg/".$pname);
#} else {
#link($tftpdir."/pxelinux.cfg/".$node,$tftpdir."/pxelinux.cfg/".$pname);
#}
}
}
}
2009-09-25 21:08:41 +00:00
#now run the end part of the prescripts
unless ( $ args [ 0 ] eq 'stat' ) { # or $args[0] eq 'enact')
$ errored = 0 ;
2009-09-25 21:14:48 +00:00
if ( $ request - > { '_disparatetftp' } - > [ 0 ] ) { #the call is distrubuted to the service node already, so only need to handles my own children
2009-09-25 21:08:41 +00:00
$ sub_req - > ( { command = > [ 'runendpre' ] ,
node = > \ @ nodes ,
arg = > [ $ args [ 0 ] , '-l' ] } , \ & pass_along ) ;
2009-09-25 21:14:48 +00:00
} else { #nodeset did not distribute to the service node, here we need to let runednpre to distribute the nodes to their masters
2009-09-25 21:08:41 +00:00
$ sub_req - > ( { command = > [ 'runendpre' ] ,
node = > \ @ rnodes ,
arg = > [ $ args [ 0 ] ] } , \ & pass_along ) ;
}
2011-05-16 19:59:10 +00:00
if ( $ errored ) {
my $ rsp ;
$ rsp - > { errorcode } - > [ 0 ] = 1 ;
$ rsp - > { error } - > [ 0 ] = "Failed in running end prescripts\n" ;
$ callback - > ( $ rsp ) ;
return ;
}
2009-09-25 21:08:41 +00:00
}
2008-06-27 14:35:20 +00:00
2007-10-26 22:44:33 +00:00
}
2008-09-26 23:07:45 +00:00
#----------------------------------------------------------------------------
= head3 getNodesetStates
returns the nodeset state for the given nodes . The possible nodeset
states are: netboot , install , boot and discover .
Arguments:
nodes - - - a pointer to an array of nodes
states - - a pointer to a hash table . This hash will be filled by this
2009-03-25 02:56:58 +00:00
function . The key is the nodeset status and the value is a pointer
to an array of nodes .
2008-09-26 23:07:45 +00:00
Returns:
( return code , error message )
= cut
#-----------------------------------------------------------------------------
sub getNodesetStates {
my $ noderef = shift ;
if ( $ noderef =~ /xCAT_plugin::pxe/ ) {
$ noderef = shift ;
}
my @ nodes = @$ noderef ;
my $ hashref = shift ;
2012-03-29 18:14:03 +00:00
my $ noderestab = xCAT::Table - > new ( 'noderes' ) ; #in order to detect per-node tftp directories
my % nrhash = % { $ noderestab - > getNodesAttribs ( \ @ nodes , [ qw( tftpdir ) ] ) } ;
2008-09-26 23:07:45 +00:00
if ( @ nodes > 0 ) {
foreach my $ node ( @ nodes ) {
2012-03-29 18:14:03 +00:00
my $ tftpdir ;
if ( $ nrhash { $ node } - > [ 0 ] and $ nrhash { $ node } - > [ 0 ] - > { tftpdir } ) {
$ tftpdir = $ nrhash { $ node } - > [ 0 ] - > { tftpdir } ;
} else {
$ tftpdir = $ globaltftpdir ;
}
2012-03-29 22:22:32 +00:00
my $ tmp = getstate ( $ node , $ tftpdir ) ;
2008-09-26 23:07:45 +00:00
my @ a = split ( ' ' , $ tmp ) ;
$ stat = $ a [ 0 ] ;
2009-03-25 02:56:58 +00:00
if ( exists ( $ hashref - > { $ stat } ) ) {
my $ pa = $ hashref - > { $ stat } ;
push ( @$ pa , $ node ) ;
}
else {
$ hashref - > { $ stat } = [ $ node ] ;
}
2008-09-26 23:07:45 +00:00
}
}
return ( 0 , "" ) ;
}
2007-10-26 22:44:33 +00:00
1 ;