From 3fb99a5e8c229885602198bea4dce26bcf0fcc4f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 10 Jun 2020 17:12:02 +0200 Subject: [serversetup-bwlp-ipxe] Start refactoring ipxe script generator This is WIP. Mostly restored all the old functionality. Boot entries are fetched when selected, not embedded in the main script, so password protection is a bit stronger. Hopefully allows for other script generators in the future. --- .../serversetup-bwlp-ipxe/inc/bootentry.inc.php | 149 ++++++++++----------- 1 file changed, 69 insertions(+), 80 deletions(-) (limited to 'modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php') diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php index e89380ce..7b8fb4b5 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php @@ -14,7 +14,11 @@ abstract class BootEntry public abstract function supportsMode($mode); - public abstract function toScript($failLabel, $mode); + /** + * @param ScriptBuilderBase $builder + * @return string + */ + public abstract function toScript($builder); public abstract function toArray(); @@ -54,6 +58,9 @@ abstract class BootEntry if ($module === '.exec') { return new StandardBootEntry($data); } + if ($module === '.special') { + return new SpecialBootEntry($data); + } return null; } @@ -84,7 +91,7 @@ abstract class BootEntry public static function newCustomBootEntry($initData) { - if (empty($initData['script'])) + if (!is_array($initData) || empty($initData)) return null; return new CustomBootEntry($initData); } @@ -135,8 +142,10 @@ class StandardBootEntry extends BootEntry * @var ExecData same for EFI */ protected $efi; - - protected $arch; // Constants below + /** + * @var string BootEntry Constants above + */ + protected $arch; const KEYS = ['executable', 'initRd', 'commandLine', 'replace', 'imageFree', 'autoUnload', 'resetConsole', 'dhcpOptions']; @@ -257,74 +266,13 @@ class StandardBootEntry extends BootEntry return false; } - public function toScript($failLabel, $mode) + public function toScript($builder) { - if (!$this->supportsMode($mode)) { - return "prompt Entry doesn't have an executable for mode $mode\n"; - } - if ($this->arch === BootEntry::AGNOSTIC || $mode == BootEntry::BIOS) { - $entry = $this->pcbios; - } else { - $entry = $this->efi; - } - $entry->sanitize(); - - $script = ''; - if ($entry->resetConsole) { - $script .= "console ||\n"; - } - if ($entry->imageFree) { - $script .= "imgfree ||\n"; - } - foreach ($entry->dhcpOptions as $opt) { - if (empty($opt['value'])) { - $val = '${}'; - } else { - if (empty($opt['hex'])) { - $val = bin2hex($opt['value']); - } else { - $val = $opt['value']; - } - preg_match_all('/[0-9a-f]{2}/', $val, $out); - $val = implode(':', $out[0]); - } - $script .= 'set net${idx}/' . $opt['opt'] . ':hex ' . $val - . ' || prompt Cannot override DHCP server option ' . $opt['opt'] . ". Press any key to continue anyways.\n"; - } - $initrds = []; - if (!empty($entry->initRd)) { - foreach (array_values($entry->initRd) as $i => $initrd) { - if (empty($initrd)) - continue; - $script .= "initrd --name initrd$i $initrd || goto $failLabel\n"; - $initrds[] = "initrd$i"; - } - } - $script .= "boot "; - if ($entry->autoUnload) { - $script .= "-a "; - } - if ($entry->replace) { - $script .= "-r "; - } - $script .= $entry->executable; - if (!empty($initrds)) { - if ($mode === BootEntry::BIOS) { - $script .= " initrd=" . implode(',', $initrds); - } else { - foreach ($initrds as $initrd) { - $script .= " initrd=$initrd"; - } - } - } - if (!empty($entry->commandLine)) { - $script .= ' ' . $entry->commandLine; - } - $script .= " || goto $failLabel\n"; - if ($entry->resetConsole) { - $script .= "goto start ||\n"; - } - return $script; + if ($this->arch === BootEntry::AGNOSTIC) // Same as below, could construct fall-through but this is more clear + return $builder->execDataToScript($this->pcbios, null, null); + return $builder->execDataToScript(null, + $this->supportsMode(BootEntry::BIOS) ? $this->pcbios : null, + $this->supportsMode(BootEntry::EFI) ? $this->efi : null); } public function addFormFields(&$array) @@ -347,12 +295,22 @@ class StandardBootEntry extends BootEntry class CustomBootEntry extends BootEntry { - protected $script; + /** + * @var string iPXE + */ + protected $ipxe; + + protected $bash; + + protected $grub; public function __construct($data) { - if (is_array($data) && isset($data['script'])) { - $this->script = $data['script']; + if (is_array($data)) { + $this->ipxe = $data['script'] ?? ''; // LEGACY + foreach (['bash', 'grub'] as $key) { + $this->{$key} = $data[$key] ?? ''; + } } } @@ -361,22 +319,24 @@ class CustomBootEntry extends BootEntry return true; } - public function toScript($failLabel, $mode) + public function toScript($builder) { - return str_replace('%fail%', $failLabel, $this->script) . "\n"; + if ($builder instanceof ScriptBuilderIpxe) + return $this->ipxe; + return ''; } public function addFormFields(&$array) { $array['entry'] = [ - 'script' => $this->script, + 'script' => $this->ipxe, ]; $array['script_checked'] = 'checked'; } public function toArray() { - return ['script' => $this->script]; + return ['script' => $this->ipxe]; } } @@ -394,9 +354,10 @@ class MenuBootEntry extends BootEntry return true; } - public function toScript($failLabel, $mode) + public function toScript($builder) { - return 'chain -ar ${self}&menuid=' . $this->menuId . ' || goto ' . $failLabel . "\n"; + $menu = IPxeMenu::get($this->menuId); + return $builder->menuToScript($menu); } public function toArray() @@ -409,3 +370,31 @@ class MenuBootEntry extends BootEntry } } +class SpecialBootEntry extends BootEntry +{ + + private $type; + + public function __construct($type) + { + $this->type = $type['type'] ?? $type; + } + + public function supportsMode($mode) + { + return true; + } + + public function toScript($builder) + { + return $builder->getSpecial($this->type); + } + + public function toArray() + { + return []; + } + + public function addFormFields(&$array) { } + +} \ No newline at end of file -- cgit v1.2.3-55-g7522