LET ME SAY THIS RIGHT OF THE BAT, I have not used this in my test environment, only on my dev box. So it may work well for production or it may fall flat. (it shouldn't because the code is solid). After my holiday I'll test it completely in my test environment before moving it to production.
I think I was able to create a solution for this issue. I attempted to do a git fork / pull request but I’m not sure it worked so for the sake of documentation I’ll update what I was able to do there.
First I created two new FOG Server pages. One is to pull the system variables I need into the fog postdownload bash scripts. The second page allows me to update/change a registered fog target computer name from a postdownload script. Just some background on this, my target machine names are all calculated based on the computer serial number and a OU prefix. Currently I’m using an unused field in FOG (Other1) to hold this OU prefix. So to properly and automatically name the target computer I need to pick up the serial number from smbios and combine it with the value from the other1 field in the host information. Below is what I’ve worked out to extend FOG to what I need.
Create the following file: /var/www/html/fog/service/sethostname.php
<?php
require_once('../commons/base.inc.php');
FOGCore::getClass('SetHostName');
Create the following file: /var/www/html/fog/service/hostinfo.php
<?php
require_once('../commons/base.inc.php');
FOGCore::getClass('Hostinfo');
Create the following class file: /var/www/html/fog/lib/fog/sethostname.class.php
<?php
class SetHostName extends FOGBase {
protected $macSimple;
protected $newName;
protected $oldName;
public function __construct($check = false) {
parent::__construct();
self::stripAndDecode($_REQUEST);
$this->macSimple = strtolower(str_replace(array(':','-'),':',substr($_REQUEST['mac'],0,20)));
$this->newName = substr(trim($_REQUEST['newname']," \t\n\r\0"),0,20);
$this->oldName = substr(trim($_REQUEST['oldname']," \t\n\r\0"),0,20);
ob_start();
header('Content-Type: text/plain');
header('Connection: close');
if ((strlen($this->newName) > 3) & (strlen($this->oldName) > 0)) {
$query = sprintf("UPDATE hosts JOIN hostMAC ON (hostMAC.hmHostID = hosts.hostID) SET hostName='%s' WHERE ( (hostMAC.hmMAC='%s') AND (hostName LIKE '%s') );", $this->newName, $this->macSimple, $this->oldName);
self::$DB->query($query);
echo "OK";
} else {
echo "Fail";
}
flush();
ob_flush();
ob_end_flush();
}
}
Create the following class file: /var/www/html/fog/lib/fog/hostinfo.class.php
<?php
class HostInfo extends FOGBase {
protected $macSimple;
protected $repFields = array(
'hostName' => 'hostname',
'hostDesc' => 'hostdesc',
'imageOSID' => 'imageosid',
'imagePath' => 'imagepath',
'hostUseAD' => 'hostusead',
'hostADDomain' => 'hostaddomain',
'hostADOU' => 'hostadou',
'hostProductKey' => 'hostproductkey',
'iPrimaryUser' => 'primaryuser',
'iOtherTag' => 'othertag',
'iOtherTag1' => 'othertag1',
'lName' => 'location',
'iSysman' => 'sysman',
'iSysproduct' => 'sysproduct',
'iSysserial' => 'sysserial',
'iMbman' => 'mbman',
'iMbserial' => 'mbserial',
'iMbasset' => 'mbasset',
'iMbproductname' => 'mbproductname',
'iCaseman' => 'caseman',
'iCaseserial' => 'caseserial',
'iCaseasset' => 'caseasset',
);
public function __construct($check = false) {
parent::__construct();
self::stripAndDecode($_REQUEST);
$this->macSimple = strtolower(str_replace(array(':','-'),':',substr($_REQUEST['mac'],0,20)));
$query = sprintf("SELECT hostName,hostDesc,imageOSID,imagePath,hostUseAD,hostADDomain,hostADOU,hostProductKey,iPrimaryUser,iOtherTag,iOtherTag1,lName,iSysman,iSysproduct,iSysserial,iMbman,iMbserial,iMbasset,iMbproductname,iCaseman,iCaseserial,iCaseasset FROM (((hostMAC INNER JOIN (hosts LEFT JOIN images ON hosts.hostImage = images.imageID) ON hostMAC.hmHostID = hosts.hostID) LEFT JOIN inventory ON hosts.hostID = inventory.iHostID) LEFT JOIN locationAssoc ON hosts.hostID = locationAssoc.laHostID) LEFT JOIN location ON locationAssoc.laLocationID = location.lID WHERE (hostMAC.hmMAC='%s');", $this->macSimple);
$tmp = (array)self::$DB->query($query)->fetch('','fetch_all')->get();
ob_start();
header('Content-Type: text/plain');
header('Connection: close');
foreach ((array)$tmp AS $i => &$DataRow) {
foreach ((array)$DataRow AS $j => &$DataField) {
echo "export " . $this->repFields[$j] . "=\"" . $DataField . "\"\n";
unset($DataField);
}
unset($DataRow);
};
flush();
ob_flush();
ob_end_flush();
}
}
And finally the post install bits
Edit /images/postdownloadscripts/fog.postdownload and insert the following command before
your custom post install script
. ${postdownpath}fog.hostinfo
Create the following file: /images/postdownloadscripts/fog.hostinfo
#!/bin/bash
. /usr/share/fog/lib/funcs.sh;
wget -q -O /tmp/hinfo.txt "http://<fog_server_IP>/fog/service/hostinfo.php?mac=$mac"
. /tmp/hinfo.txt
rm -f /tmp/hinfo.txt
If everything works as expected you should now have access to the following bash variables in your post install script
$hostname == name of the host (should overwrite existing $hostname)
$hostdesc == Description of host
$imageosid == Operating System ID (should be the same as $osid)
$imagepath == The root path of the image(should also be the image name)
$hostusead == 1 or 0 to add host to AD
$hostaddomain == host domain name
$hostadou == host target ou
$hostproductkey == host product key
$primaryuser == Value from Primary User field
$othertag == Value from OtherTag field
$othertag1 == Value from OtherTag1 field
$location == Location Name from location plugin
$sysman == System Manufacturer from smbios
$sysproduct == System Product Name from smbios (from full registration)
$sysserial == System Serial Number from smbios (from full registration)
$mbman == Motherboard Manufacturer from smbios (from full registration)
$mbserial == Motherboard Serial Number from smbios (from full registration)
$mbasset == Motherboard Asset tag from smbios (from full registration)
$mbproductname == Motherboard Product Name from smbios (from full registration)
$caseman == Case Manufacturer from smbios (from full registration)
$caseserial == Case Serial Number from smbios (from full registration)
$caseasset == Case Asset tag from smbios (from full registration)
And the last bit is to call the set host name function.
wget -q -“http://<fog_server_IP>/fog/service/sethostname.php?mac=$mac&oldname=$oldhostname&newname=$newhostname”
For this script to work you must supply the host mac address, its new host name and for safety sake its old host name to prevent an accidental host name change.