public function hostdeploy() { $Host = new Host($this->REQUEST['id']); $taskTypeID = $this->REQUEST['type']; $TaskType = new TaskType($_REQUEST['type']); $snapin = '-1'; $enableShutdown = false; $enableSnapins = $_REQUEST['type'] == 17 ? false : -1; $taskName = 'Quick Deploy'; try { if ($TaskType->isUpload() && $Host->getImage()->isValid() && $Host->getImage()->get('protected')) { throw new Exception(sprintf('%s: %s %s: %s %s', _('Hostname'), $Host->get('name'), _('Image'), $Host->getImage()->get('name'), _('is protected'))); } $Host->createImagePackage($taskTypeID, $taskName, false, false, $enableSnapins, false, $this->FOGUser->get('name')); $this->FOGCore->setMessage('Successfully created tasking!'); $this->FOGCore->redirect('?node=task&sub=active'); } catch (Exception $e) { printf('<div class="task-start-failed"><p>%s</p><p>%s</p></div>', _('Failed to create deploy task'), $e->getMessage()); } }
// Post_Stage2.php // Triggered: After image upload // Actions: Moves uploaded image to final location via FTP // require_once '../commons/base.inc.php'; try { $Host = $FOGCore->getHostItem(false); // Task for Host $Task = $Host->get('task'); if (!$Task->isValid()) { throw new Exception(sprintf('%s: %s (%s)', _('No Active Task found for Host'), $Host->get('name'), $MACAddress)); } $TaskType = new TaskType($Task->get('typeID')); // Get the storage group $StorageGroup = $Task->getStorageGroup(); if ($TaskType->isUpload() && !$StorageGroup->isValid()) { throw new Exception(_('Invalid Storage Group')); } // Get the storage node. $StorageNode = $StorageGroup->getMasterStorageNode(); if ($TaskType->isUpload() && !$StorageNode) { throw new Exception(_('Could not find a Storage Node. Is there one enabled within this Storage Group?')); } // Image Name store for logging the image task later. $Image = $Task->getImage(); $ImageName = $Image->get('name'); // Sets the class for ftp of files and deletion as necessary. $ftp = $FOGFTP; // Sets the mac address for tftp delete later. $mactftp = strtolower(str_replace(':', '-', $_REQUEST['mac'])); // Sets the mac address for ftp upload later.
public function checkIfExist($taskTypeID) { // TaskType: Variables $TaskType = new TaskType($taskTypeID); $isUpload = $TaskType->isUpload(); // Image: Variables $Image = $this->getImage(); $StorageGroup = $Image->getStorageGroup(); $StorageNode = $isUpload ? $StorageGroup->getMasterStorageNode() : $this->getOptimalStorageNode(); if (!$isUpload) { $this->HookManager->processEvent('HOST_NEW_SETTINGS', array('Host' => &$this, 'StorageNode' => &$StorageNode, 'StorageGroup' => &$StorageGroup)); } if (!$StorageGroup || !$StorageGroup->isValid()) { throw new Exception(_('No Storage Group found for this image')); } if (!$StorageNode || !$StorageNode->isValid()) { throw new Exception(_('No Storage Node found for this image')); } if (in_array($TaskType->get('id'), array('1', '8', '15', '17')) && in_array($Image->get('osID'), array('5', '6', '7'))) { // FTP $this->FOGFTP->set('username', $StorageNode->get('user'))->set('password', $StorageNode->get('pass'))->set('host', $this->FOGCore->resolveHostname($StorageNode->get('ip'))); if ($this->FOGFTP->connect()) { if (!$this->FOGFTP->chdir(rtrim($StorageNode->get('path'), '/') . '/' . $Image->get('path'))) { return false; } } $this->FOGFTP->close(); } return true; }
/** @function getTasking() Finds out if there's a tasking for the relevant host. * if there is, returns the printTasking, otherwise * presents the menu. * @return void */ public function getTasking() { $Task = $this->Host->get('task'); if (!$Task->isValid()) { if ($this->FOGCore->getSetting('FOG_NO_MENU')) { $this->noMenu(); } else { $this->printDefault(); } } else { if ($this->Host->get('mac')->isImageIgnored()) { $this->printImageIgnored(); } $TaskType = new TaskType($Task->get('typeID')); $imagingTasks = array(1, 2, 8, 15, 16, 17, 24); if ($TaskType->isMulticast()) { $MulticastSessionAssoc = current($this->getClass('MulticastSessionsAssociationManager')->find(array('taskID' => $Task->get('id')))); $MulticastSession = new MulticastSessions($MulticastSessionAssoc->get('msID')); if ($MulticastSession && $MulticastSession->isValid()) { $this->Host->set('imageID', $MulticastSession->get('image')); } } if (in_array($TaskType->get('id'), $imagingTasks)) { $Image = $Task->getImage(); $StorageGroup = $Image->getStorageGroup(); $StorageNode = $StorageGroup->getOptimalStorageNode(); $this->HookManager->processEvent('BOOT_TASK_NEW_SETTINGS', array('Host' => &$this->Host, 'StorageNode' => &$StorageNode, 'StorageGroup' => &$StorageGroup)); if ($TaskType->isUpload() || $TaskType->isMulticast()) { $StorageNode = $StorageGroup->getMasterStorageNode(); } $osid = $Image->get('osID'); $storage = in_array($TaskType->get('id'), $imagingTasks) ? sprintf('%s:/%s/%s', $this->FOGCore->resolveHostname(trim($StorageNode->get('ip'))), trim($StorageNode->get('path'), '/'), $TaskType->isUpload() ? 'dev/' : '') : null; } if ($this->Host && $this->Host->isValid()) { $mac = $this->Host->get('mac'); } else { $mac = $_REQUEST['mac']; } $clamav = in_array($TaskType->get('id'), array(21, 22)) ? sprintf('%s:%s', $this->FOGCore->resolveHostname(trim($StorageNode->get('ip'))), '/opt/fog/clamav') : null; $storageip = in_array($TaskType->get('id'), $imagingTasks) ? $this->FOGCore->resolveHostname($StorageNode->get('ip')) : null; $img = in_array($TaskType->get('id'), $imagingTasks) ? $Image->get('path') : null; $imgFormat = in_array($TaskType->get('id'), $imagingTasks) ? $Image->get('format') : null; $imgType = in_array($TaskType->get('id'), $imagingTasks) ? $Image->getImageType()->get('type') : null; $imgPartitionType = in_array($TaskType->get('id'), $imagingTasks) ? $Image->getImagePartitionType()->get('type') : null; $imgid = in_array($TaskType->get('id'), $imagingTasks) ? $Image->get('id') : null; $ftp = $this->FOGCore->resolveHostname($this->FOGCore->getSetting('FOG_TFTP_HOST')); $chkdsk = $this->FOGCore->getSetting('FOG_DISABLE_CHKDSK') == 1 ? 0 : 1; $PIGZ_COMP = in_array($TaskType->get('id'), $imagingTasks) ? $Image->get('compress') > -1 && is_numeric($Image->get('compress')) ? $Image->get('compress') : $this->FOGCore->getSetting('FOG_PIGZ_COMP') : $this->FOGCore->getSetting('FOG_PIGZ_COMP'); $kernelArgsArray = array("mac={$mac}", "ftp={$ftp}", "storage={$storage}", "storageip={$storageip}", "web={$this->web}", "osid={$osid}", "consoleblank=0", "irqpoll", "hostname=" . $this->Host->get('name'), array('value' => "clamav={$clamav}", 'active' => in_array($TaskType->get('id'), array(21, 22))), array('value' => "chkdsk={$chkdsk}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "img={$img}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "imgType={$imgType}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "imgPartitionType={$imgPartitionType}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "imgid={$imgid}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "imgFormat={$imgFormat}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => "PIGZ_COMP=-{$PIGZ_COMP}", 'active' => in_array($TaskType->get('id'), $imagingTasks)), array('value' => 'shutdown=1', 'active' => $Task->get('shutdown')), array('value' => 'adon=1', 'active' => $this->Host->get('useAD')), array('value' => 'addomain=' . $this->Host->get('ADDomain'), 'active' => $this->Host->get('useAD')), array('value' => 'adou=' . $this->Host->get('ADOU'), 'active' => $this->Host->get('useAD')), array('value' => 'aduser='******'ADUser'), 'active' => $this->Host->get('useAD')), array('value' => 'adpass='******'ADPass'), 'active' => $this->Host->get('useAD')), array('value' => 'fdrive=' . $this->Host->get('kernelDevice'), 'active' => $this->Host->get('kernelDevice')), array('value' => 'hostearly=1', 'active' => $this->FOGCore->getSetting('FOG_CHANGE_HOSTNAME_EARLY') && in_array($TaskType->get('id'), $imagingTasks) ? true : false), array('value' => 'pct=' . (is_numeric($this->FOGCore->getSetting('FOG_UPLOADRESIZEPCT')) && $this->FOGCore->getSetting('FOG_UPLOADRESIZEPCT') >= 5 && $this->FOGCore->getSetting('FOG_UPLOADRESIZEPCT') < 100 ? $this->FOGCore->getSetting('FOG_UPLOADRESIZEPCT') : '5'), 'active' => $TaskType->isUpload() && in_array($TaskType->get('id'), $imagingTasks) ? true : false), array('value' => 'ignorepg=' . ($this->FOGCore->getSetting('FOG_UPLOADIGNOREPAGEHIBER') ? 1 : 0), 'active' => $TaskType->isUpload() && in_array($TaskType->get('id'), $imagingTasks) ? true : false), array('value' => 'port=' . ($TaskType->isMulticast() ? $MulticastSession->get('port') : null), 'active' => $TaskType->isMulticast()), array('value' => 'mining=1', 'active' => $this->FOGCore->getSetting('FOG_MINING_ENABLE')), array('value' => 'miningcores=' . $this->FOGCore->getSetting('FOG_MINING_MAX_CORES'), 'active' => $this->FOGCore->getSetting('FOG_MINING_ENABLE')), array('value' => 'winuser='******'passreset'), 'active' => $TaskType->get('id') == '11' ? true : false), array('value' => 'miningpath=' . $this->FOGCore->getSetting('FOG_MINING_PACKAGE_PATH'), 'active' => $this->FOGCore->getSetting('FOG_MINING_ENABLE')), array('value' => 'isdebug=yes', 'active' => $Task->get('isDebug')), array('value' => 'debug', 'active' => $this->FOGCore->getSetting('FOG_KERNEL_DEBUG')), $TaskType->get('kernelArgs'), $this->FOGCore->getSetting('FOG_KERNEL_ARGS'), $this->Host->get('kernelArgs')); if ($Task->get('typeID') == 12 || $Task->get('typeID') == 13) { $this->printDefault(); } else { if ($Task->get('typeID') == 4) { $Send['memtest'] = array("#!ipxe", "{$this->memdisk} iso raw", "{$this->memtest}", "boot"); $this->parseMe($Send); } else { $this->printTasking($kernelArgsArray); } } } }
/** deploy_post() actually create the deployment task * @return void */ public function deploy_post() { $Data = $this->obj; $TaskType = new TaskType($_REQUEST['type']); $Snapin = $_REQUEST['snapin'] ? new Snapin($_REQUEST['snapin']) : -1; $enableShutdown = $_REQUEST['shutdown'] ? true : false; $enableSnapins = $TaskType->get('id') != '17' ? $Snapin instanceof Snapin && $Snapin->isValid() ? $Snapin->get('id') : $Snapin : false; $enableDebug = $_REQUEST['debug'] == 'true' || $_REQUEST['isDebugTask'] ? true : false; $scheduleDeployTime = $this->nice_date($_REQUEST['scheduleSingleTime']); $imagingTasks = in_array($TaskType->get('id'), array(1, 2, 8, 15, 16, 17, 24)); try { if (!$TaskType || !$TaskType->isValid()) { throw new Exception(_('Task type is not valid')); } $taskName = $TaskType->get('name') . ' Task'; if ($Data && $Data->isValid()) { // Error Checking if ($Data instanceof Host && $imagingTasks) { if (!$Data->getImage() || !$Data->getImage()->isValid()) { throw new Exception(_('You need to assign an image to the host')); } if ($TaskType->isUpload() && $Data->getImage()->get('protected')) { throw new Exception(_('You cannot upload to this image as it is currently protected')); } if (!$Data->checkIfExist($TaskType->get('id'))) { throw new Exception(_('You must first upload an image to create a download task')); } } else { if ($Data instanceof Group && $imagingTasks) { if ($TaskType->isMulticast() && !$Data->doMembersHaveUniformImages()) { throw new Exception(_('Hosts do not contain the same image assignments')); } unset($NoImage, $ImageExists, $Tasks); foreach ((array) $Data->get('hosts') as $Host) { if ($Host && $Host->isValid() && !$Host->get('pending')) { $NoImage[] = !$Host->getImage() || !$Host->getImage()->isValid(); } } if (in_array(true, $NoImage)) { throw new Exception(_('One or more hosts do not have an image set')); } foreach ((array) $Data->get('hosts') as $Host) { if ($Host && $Host->isValid() && !$Host->get('pending')) { $ImageExists[] = !$Host->checkIfExist($TaskType->get('id')); } } if (in_array(true, $ImageExists)) { throw new Exception(_('One or more hosts have an image that does not exist')); } foreach ((array) $Data->get('hosts') as $Host) { if ($Host && $Host->isValid() && $Host->get('task') && $Host->get('task')->isValid()) { $Tasks[] = $Host->get('task'); } } if (count($Tasks) > 0) { throw new Exception(_('One or more hosts are currently in a task')); } } } $passreset = trim($_REQUEST['account']); if ($TaskType->get('id') == 11 && empty($passreset)) { throw new Exception(_('Password reset requires a user account to reset')); } try { if ($_REQUEST['scheduleType'] == 'instant') { if ($Data instanceof Group) { foreach ((array) $Data->get('hosts') as $Host) { if ($Host && $Host->isValid() && !$Host->get('pending')) { if ($Host->createImagePackage($TaskType->get('id'), $taskName, $enableShutdown, $enableDebug, $enableSnapins, $Data instanceof Group, $_SESSION['FOG_USERNAME'], $passreset)) { $success[] = sprintf('<li>%s – %s</li>', $Host->get('name'), $Host->getImage()->get('name')); } } } } else { if ($Data instanceof Host) { if ($Data->createImagePackage($TaskType->get('id'), $taskName, $enableShutdown, $enableDebug, $enableSnapins, $Data instanceof Group, $_SESSION['FOG_USERNAME'], $passreset)) { $success[] = sprintf('<li>%s – %s</li>', $Data->get('name'), $Data->getImage()->get('name')); } } } } else { if ($_REQUEST['scheduleType'] == 'single') { if ($scheduleDeployTime < $this->nice_date()) { throw new Exception(sprintf('%s<br>%s: %s', _('Scheduled date is in the past'), _('Date'), $scheduleDeployTime->format('Y/d/m H:i'))); } $ScheduledTask = new ScheduledTask(array('taskType' => $TaskType->get('id'), 'name' => $taskName, 'hostID' => $Data->get('id'), 'shutdown' => $enableShutdown, 'other2' => $enableSnapins, 'isGroupTask' => $Data instanceof Group, 'type' => 'S', 'scheduleTime' => $scheduleDeployTime->getTimestamp(), 'other3' => $this->FOGUser->get('name'))); } else { if ($_REQUEST['scheduleType'] == 'cron') { $ScheduledTask = new ScheduledTask(array('taskType' => $TaskType->get('id'), 'name' => $taskName, 'hostID' => $Data->get('id'), 'shutdown' => $enableShutdown, 'other2' => $enableSnapins, 'isGroupTask' => $Data instanceof Group, 'type' => 'C', 'other3' => $this->FOGUser->get('name'), 'minute' => $_REQUEST['scheduleCronMin'], 'hour' => $_REQUEST['scheduleCronHour'], 'dayOfMonth' => $_REQUEST['scheduleCronDOM'], 'month' => $_REQUEST['scheduleCronMonth'], 'dayOfWeek' => $_REQUEST['scheduleCronDOW'])); } } } if ($ScheduledTask && $ScheduledTask->save()) { if ($Data instanceof Group) { foreach ((array) $Data->get('hosts') as $Host) { if ($Host && $Host->isValid() && !$Host->get('pending')) { $success[] = sprintf('<li>%s – %s</li>', $Host->get('name'), $Host->getImage()->get('name')); } } } else { if ($Data instanceof Host) { if ($Data && $Data->isValid() && !$Data->get('pending')) { $success[] = sprintf('<li>%s – %s</li>', $Data->get('name'), $Data->getImage()->get('name')); } } } } } catch (Exception $e) { $error[] = sprintf('%s: %s', $Data instanceof Group ? $Host->get('name') : $Data->get('name'), $e->getMessage()); } } // Failure if (count($error)) { throw new Exception('<ul><li>' . implode('</li><li>', $error) . '</li></ul>'); } } catch (Exception $e) { // Failure printf('<div class="task-start-failed"><p>%s</p><p>%s</p></div>', _('Failed to create deployment tasking for the following hosts'), $e->getMessage()); } // Success if (count($success)) { printf('<div class="task-start-ok"><p>%s</p><p>%s%s%s</p></div>', sprintf(_('Successfully created tasks for deployment to the following Hosts'), $Data instanceof Group ? $Host->getImage()->get('name') : $Data->getImage()->get('name')), $_REQUEST['scheduleType'] == 'cron' ? sprintf('%s: %s', _('Cron Schedule'), implode(' ', array($_REQUEST['scheduleCronMin'], $_REQUEST['scheduleCronHour'], $_REQUEST['scheduleCronDOM'], $_REQUEST['scheduleCronMonth'], $_REQUEST['scheduleCronDOW']))) : '', $_REQUEST['scheduleType'] == 'single' ? sprintf('%s: %s', _('Scheduled to start at'), $scheduleDeployTime->format('Y/m/d H:i')) : '', count($success) ? sprintf('<ul>%s</ul>', implode('', $success)) : ''); } }