summaryrefslogblamecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/ipxebuilder.inc.php
blob: a2b25f55b21c41295398c322da32706afa10605b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                                                  
                                                        
           
                                                                                               












                                                                                       
                                                             









                                                                                              
                            




                                                         
                                                                         











                                                                                    
                                                                            





                                                                                                                               
                                                                                                            



                                                                                
                                                                        





                                                                                                                   
<?php

class IPxeBuilder
{

	const PROP_IPXE_BUILDSTRING = 'ipxe.compile-time';
	const PROP_IPXE_HASH = 'ipxe.commit-hash';
	const PROP_IPXE_COMPILE_TASKID = 'ipxe-task-id';
	const VERSION_LIST_TASK = 'ipxe-version-list-id';
	const PROP_VERSION_SELECT_TASKID = 'ipxe-version-select-id';

	/**
	 * Checkout given commit/ref of ipxe repo. Returns the according task-id, or null on error
	 *
	 * @param string $version version ref (commit, tag, ...)
	 * @param ?string $parent parent task id, if any
	 */
	public static function setIpxeVersion(string $version, ?string $parent = null): ?string
	{
		$task = Taskmanager::submit('IpxeVersion', [
			'action' => 'CHECKOUT',
			'ref' => $version,
			'parentTask' => $parent,
		]);
		if (!Taskmanager::isTask($task))
			return null;
		TaskmanagerCallback::addCallback($task, 'ipxeVersionSet');
		Property::set(IPxeBuilder::PROP_VERSION_SELECT_TASKID, $task['id'], 2);
		return $task['id'];
	}

	public static function getVersionTaskResult(): ?array
	{
		$task = Taskmanager::status(IPxeBuilder::VERSION_LIST_TASK);
		if (!Taskmanager::isTask($task) || Taskmanager::isFailed($task)) {
			$task = Taskmanager::submit('IpxeVersion',
				['id' => IPxeBuilder::VERSION_LIST_TASK, 'action' => 'LIST']);
		}
		$task = Taskmanager::waitComplete($task);
		if (Taskmanager::isFinished($task) && !Taskmanager::isFailed($task)) {
			return $task['data'];
		}
		return null;
	}

	/**
	 * Callback when compile Taskmanager job finished
	 */
	public static function compileCompleteCallback(array $task): void
	{
		if (!Taskmanager::isFinished($task) || Taskmanager::isFailed($task))
			return;
		$version = 'Unknown';
		if (isset($task['data']['hash'])) {
			$hash = $task['data']['hash'];
			Property::set(IPxeBuilder::PROP_IPXE_HASH, $hash);
			$version = $hash;
			$list = IPxeBuilder::getVersionTaskResult();
			if (isset($list['versions'])) {
				foreach ($list['versions'] as $v) {
					if ($v['hash'] === $version) {
						// Do NOT change (see below)
						$version = date('Y-m-d H:i', $v['date']) . ' (' . substr($version, 0, 7) . ')';
						break;
					}
				}
			}
		}
		// Do NOT change the format of this string -- we depend on it in ScriptBuilderIpxe::output()
		$buildString = date('d.m.Y H:i') . ', Version: ' . $version;
		Property::set(IPxeBuilder::PROP_IPXE_BUILDSTRING, $buildString);
	}

	public static function setIPxeVersionCallback(array $task): void
	{
		if (!Taskmanager::isFinished($task) || Taskmanager::isFailed($task) || empty($task['data']['ref']))
			return;
		Property::set(IPxeBuilder::PROP_IPXE_HASH, $task['data']['ref']);
	}
}