diff options
author | Simon Rettberg | 2017-11-03 17:53:13 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-11-03 17:53:13 +0100 |
commit | 764ec440d8594c68a6c778f58aee2d7888b89204 (patch) | |
tree | e4db86bb1ef7fad95a4d12c57d1d35e36d888333 | |
parent | [sysconfig] Fix: Empty config.tgz modules disappeared from the list (diff) | |
download | slx-admin-764ec440d8594c68a6c778f58aee2d7888b89204.tar.gz slx-admin-764ec440d8594c68a6c778f58aee2d7888b89204.tar.xz slx-admin-764ec440d8594c68a6c778f58aee2d7888b89204.zip |
[sysconfig/runmode] Deliver bare config.tgz with just global hook data when "no sysconfig" is set for a runmode
-rw-r--r-- | modules-available/runmode/baseconfig/getconfig.inc.php | 3 | ||||
-rw-r--r-- | modules-available/runmode/inc/runmode.inc.php | 21 | ||||
-rw-r--r-- | modules-available/sysconfig/api.inc.php | 85 | ||||
-rw-r--r-- | modules-available/sysconfig/inc/configtgz.inc.php | 49 | ||||
-rw-r--r-- | modules-available/sysconfig/inc/sysconfig.inc.php | 2 |
5 files changed, 104 insertions, 56 deletions
diff --git a/modules-available/runmode/baseconfig/getconfig.inc.php b/modules-available/runmode/baseconfig/getconfig.inc.php index fe04b5ef..2d622fc7 100644 --- a/modules-available/runmode/baseconfig/getconfig.inc.php +++ b/modules-available/runmode/baseconfig/getconfig.inc.php @@ -14,9 +14,6 @@ $foofoo = function($machineUuid) { if ($config->systemdDefaultTarget !== false) { ConfigHolder::add('SLX_SYSTEMD_TARGET', $config->systemdDefaultTarget, 10000); } - if ($config->noSysconfig) { - ConfigHolder::add('SLX_NO_CONFIG_TGZ', '1', 10000); - } // Disable exam mode - not sure if this is generally a good idea; for now, all modes we can think of would // not make sense that way so do this for now if (ConfigHolder::get('SLX_EXAM') !== false) { diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index 780f12c9..271542b8 100644 --- a/modules-available/runmode/inc/runmode.inc.php +++ b/modules-available/runmode/inc/runmode.inc.php @@ -63,6 +63,27 @@ class RunMode } /** + * @param string $machineuuid + * @param bool $detailed whether to return meta data about machine, not just machineuuid + * @param bool $assoc use machineuuid as array key + * @return false|array {'machineuuid', 'isclient', 'module', 'modeid', 'modedata', + * <'hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'>} + */ + public static function getRunMode($machineuuid, $detailed = false) + { + if ($detailed) { + $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen'; + } else { + $sel = ''; + } + return Database::queryFirst( + "SELECT m.machineuuid, r.isclient, r.module, r.modeid, r.modedata $sel + FROM machine m INNER JOIN runmode r USING (machineuuid) + WHERE m.machineuuid = :machineuuid LIMIT 1", + compact('machineuuid')); + } + + /** * @param string|\Module $module * @param bool true = wrap in array where key is modeid * @return array key=machineuuid, value={'machineuuid', 'modeid', 'modedata'} diff --git a/modules-available/sysconfig/api.inc.php b/modules-available/sysconfig/api.inc.php index d6cdc0e6..897b44a7 100644 --- a/modules-available/sysconfig/api.inc.php +++ b/modules-available/sysconfig/api.inc.php @@ -9,18 +9,6 @@ $uuid = Request::any('uuid', false, 'string'); if ($uuid !== false && strlen($uuid) !== 36) { $uuid = false; } -$locationId = false; -if (Module::isAvailable('locations')) { - $locationId = Location::getFromIpAndUuid($ip, $uuid); - if ($locationId !== false) { - $locationChain = Location::getLocationRootChain($locationId); - $locationChain[] = 0; - } -} -if ($locationId === false) { - $locationId = 0; - $locationChain = array(0); -} // What we do if we can't supply the requested config function deliverEmpty($message) @@ -30,34 +18,59 @@ function deliverEmpty($message) die('Config file could not be found or read!'); } -// Get config module path +$runmode = false; +if (Module::isAvailable('runmode')) { + $runmode = RunMode::getRunMode($uuid); + if ($runmode !== false) { + $runmode = RunMode::getModuleConfig($runmode['module']); + } +} +if ($runmode !== false && $runmode->noSysconfig && file_exists(SysConfig::GLOBAL_MINIMAL_CONFIG)) { + $row = array('filepath' => SysConfig::GLOBAL_MINIMAL_CONFIG); +} else { + $locationId = false; + if (Module::isAvailable('locations')) { + $locationId = Location::getFromIpAndUuid($ip, $uuid); + if ($locationId !== false) { + $locationChain = Location::getLocationRootChain($locationId); + $locationChain[] = 0; + } + } + if ($locationId === false) { + $locationId = 0; + $locationChain = array(0); + } + + // Get config module path -// We get all the configs for the whole location chain up to root -$res = Database::simpleQuery("SELECT c.title, c.filepath, c.status, cl.locationid FROM configtgz c" - . " INNER JOIN configtgz_location cl USING (configid)" - . " WHERE cl.locationid IN (" . implode(',', $locationChain) . ")"); -$best = 1000; -$row = false; -while ($r = $res->fetch(PDO::FETCH_ASSOC)) { - settype($r['locationid'], 'int'); - $index = array_search($r['locationid'], $locationChain); - if ($index === false || $index > $best) - continue; - if (!file_exists($r['filepath'])) { - if ($r['locationid'] === 0) { - EventLog::failure("The global config.tgz '{$r['title']}' was not found at '{$r['filepath']}'. Please regenerate the system configuration"); - } else { - EventLog::warning("config.tgz '{$r['title']}' for location $locationId not found at '{$r['filepath']}', trying fallback...."); + // We get all the configs for the whole location chain up to root + $res = Database::simpleQuery("SELECT c.title, c.filepath, c.status, cl.locationid FROM configtgz c" + . " INNER JOIN configtgz_location cl USING (configid)" + . " WHERE cl.locationid IN (" . implode(',', $locationChain) . ")"); + + $best = 1000; + $row = false; + while ($r = $res->fetch(PDO::FETCH_ASSOC)) { + settype($r['locationid'], 'int'); + $index = array_search($r['locationid'], $locationChain); + if ($index === false || $index > $best) + continue; + if (!file_exists($r['filepath'])) { + if ($r['locationid'] === 0) { + EventLog::failure("The global config.tgz '{$r['title']}' was not found at '{$r['filepath']}'. Please regenerate the system configuration"); + } else { + EventLog::warning("config.tgz '{$r['title']}' for location $locationId not found at '{$r['filepath']}', trying fallback...."); + } + continue; } - continue; + $best = $index; + $row = $r; } - $best = $index; - $row = $r; -} -if ($row === false) { - // TODO Not found in DB - deliverEmpty("No config.tgz for location $locationId found (src $ip)"); + if ($row === false) { + // TODO Not found in DB + deliverEmpty("No config.tgz for location $locationId found (src $ip)"); + } } Header('Content-Type: application/gzip'); diff --git a/modules-available/sysconfig/inc/configtgz.inc.php b/modules-available/sysconfig/inc/configtgz.inc.php index c03d1c5e..dbf1bc99 100644 --- a/modules-available/sysconfig/inc/configtgz.inc.php +++ b/modules-available/sysconfig/inc/configtgz.inc.php @@ -12,7 +12,7 @@ class ConfigTgz { ; } - + public function id() { return $this->configId; @@ -101,22 +101,9 @@ class ConfigTgz if (!empty($module['filepath']) && file_exists($module['filepath'])) $files[] = $module['filepath']; } - // Get stuff other modules want to inject - $handler = function($hook) { - include $hook->file; - return isset($file) ? $file : false; - }; - foreach (Hook::load('config-tgz') as $hook) { - $file = $handler($hook); - if ($file !== false) { - $files[] = $file; - } - } - // Hand over to tm - $task = Taskmanager::submit('RecompressArchive', array( - 'inputFiles' => $files, - 'outputFile' => $this->file - )); + + $task = self::recompress($files, $this->file); + // Wait for completion if ($timeoutMs > 0 && !Taskmanager::isFailed($task) && !Taskmanager::isFinished($task)) $task = Taskmanager::waitComplete($task, $timeoutMs); @@ -199,6 +186,32 @@ class ConfigTgz */ /** + * @param string[] $files source files to include + * @param string $destFile where to store final result + * @return false|array taskmanager task + */ + private static function recompress($files, $destFile) + { + // Get stuff other modules want to inject + $handler = function($hook) { + include $hook->file; + return isset($file) ? $file : false; + }; + foreach (Hook::load('config-tgz') as $hook) { + $file = $handler($hook); + if ($file !== false) { + $files[] = $file; + } + } + + // Hand over to tm + return Taskmanager::submit('RecompressArchive', array( + 'inputFiles' => $files, + 'outputFile' =>$destFile + )); + } + + /** * Marks all modules as outdated and triggers generate() * on each one. This mostly makes sense to call if a global module * that is injected via a hook has changed. @@ -215,6 +228,8 @@ class ConfigTgz $module->generate(); } } + // Build the global "empty" config that just includes global hooks + self::recompress([], SysConfig::GLOBAL_MINIMAL_CONFIG); } public static function insert($title, $moduleIds) diff --git a/modules-available/sysconfig/inc/sysconfig.inc.php b/modules-available/sysconfig/inc/sysconfig.inc.php index 15bd4104..13549948 100644 --- a/modules-available/sysconfig/inc/sysconfig.inc.php +++ b/modules-available/sysconfig/inc/sysconfig.inc.php @@ -3,6 +3,8 @@ class SysConfig { + const GLOBAL_MINIMAL_CONFIG = '/opt/openslx/configs/config-global.tgz'; + public static function getAll() { $res = Database::simpleQuery("SELECT c.configid, c.title, c.filepath, c.status, Group_Concat(cl.locationid) AS locs FROM configtgz c" |