From 97a0f7dcfdcf4a5263c1cc6c19160a0868abb5f2 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 12 Dec 2014 18:28:38 +0100 Subject: Rework config module class structure. Still some TODOs though.... --- inc/configmodule.inc.php | 99 ---------------------------- inc/configmodule/adauth.inc.php | 124 +++++++++++++++++++++++++++++++++++ inc/configmodule/branding.inc.php | 44 +++++++++++++ inc/configmodule/customodule.inc.php | 16 +++++ inc/configmodules.inc.php | 94 ++++++++++++++++++++++++++ inc/database.inc.php | 6 +- inc/trigger.inc.php | 32 +-------- 7 files changed, 285 insertions(+), 130 deletions(-) delete mode 100644 inc/configmodule.inc.php create mode 100644 inc/configmodule/adauth.inc.php create mode 100644 inc/configmodule/branding.inc.php create mode 100644 inc/configmodule/customodule.inc.php create mode 100644 inc/configmodules.inc.php (limited to 'inc') diff --git a/inc/configmodule.inc.php b/inc/configmodule.inc.php deleted file mode 100644 index 1788a53a..00000000 --- a/inc/configmodule.inc.php +++ /dev/null @@ -1,99 +0,0 @@ - $title)); - $id = Database::lastInsertId(); - if (!is_numeric($id)) Util::traceError('Inserting new AD config to DB did not yield a numeric insert id'); - // Entry created, now try to get a free port for the proxy - $res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module WHERE moduletype = 'AD_AUTH'"); - $ports = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if ($row['moduleid'] == $id) { - // ... - } else { - $data = json_decode($row['contents'], true); - if (isset($data['proxyport'])) $ports[] = $data['proxyport']; - } - } - $port = 3300; - while (in_array($port, $ports)) { - $port++; - } - // Port determined, carry on... - $ownEntry = array( - 'server' => $server, - 'searchbase' => $searchbase, - 'binddn' => $binddn, - 'bindpw' => $bindpw, - 'home' => $home, - 'proxyport' => $port - ); - $data = json_encode($ownEntry); - if ($data === false) Util::traceError('Serializing the AD data failed.'); - $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/AD_AUTH_id_' . $id . '.' . mt_rand() . '.tgz'; - Database::exec("UPDATE configtgz_module SET filepath = :filename, contents = :contents WHERE moduleid = :id LIMIT 1", array( - 'id' => $id, - 'filename' => $moduleTgz, - 'contents' => $data - )); - Database::exec("UNLOCK TABLES"); - // Add archive file name to array before returning it - $ownEntry['moduleid'] = $id; - $ownEntry['filename'] = $moduleTgz; - return $ownEntry; - } - - /** - * Get all existing AD proxy configs. - * - * @return array array of ad configs in DB with fields: - * moduleid, filename, server, searchbase, binddn, bindpw, home, proxyport - */ - public static function getAdConfigs() - { - $res = Database::simpleQuery("SELECT moduleid, filepath, contents FROM configtgz_module WHERE moduletype = 'AD_AUTH'"); - $mods = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data = json_decode($row['contents'], true); - $data['moduleid'] = $row['moduleid']; - $data['filename'] = $row['filepath']; - $mods[] = $data; - } - return $mods; - } - - public static function insertBrandingModule($title, $archive) - { - Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) " - . " VALUES (:title, 'BRANDING', '', '')", array('title' => $title)); - $id = Database::lastInsertId(); - if (!is_numeric($id)) Util::traceError('Inserting new Branding Module into DB did not yield a numeric insert id'); - // Move tgz - $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/BRANDING_id_' . $id . '.' . mt_rand() . '.tgz'; - $task = Taskmanager::submit('MoveFile', array( - 'source' => $archive, - 'destination' => $moduleTgz - )); - $task = Taskmanager::waitComplete($task, 3000); - if (Taskmanager::isFailed($task) || $task['statusCode'] !== TASK_FINISHED) { - Taskmanager::addErrorMessage($task); - Database::exec("DELETE FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array( - 'moduleid' => $id - )); - return false; - } - // Update with path - Database::exec("UPDATE configtgz_module SET filepath = :filename WHERE moduleid = :id LIMIT 1", array( - 'id' => $id, - 'filename' => $moduleTgz - )); - return true; - } - -} diff --git a/inc/configmodule/adauth.inc.php b/inc/configmodule/adauth.inc.php new file mode 100644 index 00000000..c0d4860c --- /dev/null +++ b/inc/configmodule/adauth.inc.php @@ -0,0 +1,124 @@ + $title, 'modid' => self::MODID)); + $id = Database::lastInsertId(); + if (!is_numeric($id)) Util::traceError('Inserting new AD config to DB did not yield a numeric insert id'); + // Entry created, now try to get a free port for the proxy + $res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module WHERE moduletype = :modid", array( + 'modid' => self::MODID + )); + $ports = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ($row['moduleid'] == $id) { + // ... + } else { + $data = json_decode($row['contents'], true); + if (isset($data['proxyport'])) $ports[] = $data['proxyport']; + } + } + $port = 3300; + while (in_array($port, $ports)) { + $port++; + } + // Port determined, carry on... + $ownEntry = array( + 'server' => $server, + 'searchbase' => $searchbase, + 'binddn' => $binddn, + 'bindpw' => $bindpw, + 'home' => $home, + 'proxyport' => $port + ); + $data = json_encode($ownEntry); + if ($data === false) Util::traceError('Serializing the AD data failed.'); + $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/AD_AUTH_id_' . $id . '.' . mt_rand() . '.tgz'; + Database::exec("UPDATE configtgz_module SET filepath = :filename, contents = :contents WHERE moduleid = :id LIMIT 1", array( + 'id' => $id, + 'filename' => $moduleTgz, + 'contents' => $data + )); + Database::exec("UNLOCK TABLES"); + // Add archive file name to array before returning it + $ownEntry['moduleid'] = $id; + $ownEntry['filename'] = $moduleTgz; + return $ownEntry; + } + + /** + * To be called if the server ip changes, as it's embedded in the AD module configs. + * This will then recreate all AD tgz modules. + */ + private static function rebuildAll($parent = NULL) + { + // Stop all running instances of ldadp + $task = Taskmanager::submit('LdadpLauncher', array( + 'parentTask' => $parent, + 'failOnParentFail' => false, + 'ids' => array() + )); + $ads = self::getAll(); + if (empty($ads)) // Nothing to do + return false; + + if (isset($task['id'])) + $parent = $task['id']; + foreach ($ads as $ad) { + $ad['parentTask'] = $parent; + $ad['failOnParentFail'] = false; + $ad['proxyip'] = Property::getServerIp(); + $task = Taskmanager::submit('CreateAdConfig', $ad); + if (isset($task['id'])) + $parent = $task['id']; + } + Trigger::ldadp($parent); + return $parent; + } + + /** + * Get all existing AD proxy configs. + * + * @return array array of ad configs in DB with fields: + * moduleid, filename, server, searchbase, binddn, bindpw, home, proxyport + */ + public static function getAll() + { + $res = Database::simpleQuery("SELECT moduleid, filepath, contents FROM configtgz_module WHERE moduletype = :modid", array( + 'modid' => self::MODID + )); + $mods = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data = json_decode($row['contents'], true); + $data['moduleid'] = $row['moduleid']; + $data['filename'] = $row['filepath']; + $mods[] = $data; + } + return $mods; + } + + // ############## Callbacks ############################# + + /** + * Server IP changed - rebuild all AD modules. + */ + public function event_serverIpChanged() + { + self::rebuildAll(); + } + +} diff --git a/inc/configmodule/branding.inc.php b/inc/configmodule/branding.inc.php new file mode 100644 index 00000000..f293fda6 --- /dev/null +++ b/inc/configmodule/branding.inc.php @@ -0,0 +1,44 @@ + $title, 'modid' => self::MODID)); + $id = Database::lastInsertId(); + if (!is_numeric($id)) + Util::traceError('Inserting new Branding Module into DB did not yield a numeric insert id'); + // Move tgz + $moduleTgz = CONFIG_TGZ_LIST_DIR . '/modules/BRANDING_id_' . $id . '.' . mt_rand() . '.tgz'; + $task = Taskmanager::submit('MoveFile', array( + 'source' => $archive, + 'destination' => $moduleTgz + )); + $task = Taskmanager::waitComplete($task, 3000); + if (Taskmanager::isFailed($task) || $task['statusCode'] !== TASK_FINISHED) { + Taskmanager::addErrorMessage($task); + Database::exec("DELETE FROM configtgz_module WHERE moduleid = :moduleid LIMIT 1", array( + 'moduleid' => $id + )); + return false; + } + // Update with path + Database::exec("UPDATE configtgz_module SET filepath = :filename WHERE moduleid = :id LIMIT 1", array( + 'id' => $id, + 'filename' => $moduleTgz + )); + return true; + } + +} diff --git a/inc/configmodule/customodule.inc.php b/inc/configmodule/customodule.inc.php new file mode 100644 index 00000000..89f0aca6 --- /dev/null +++ b/inc/configmodule/customodule.inc.php @@ -0,0 +1,16 @@ + $title, + 'description' => $description, + 'group' => $group, + 'unique' => $unique, + 'sortOrder' => $sortOrder, + 'moduleClass' => $moduleClass, + 'wizardClass' => $wizardClass + ); + } + + /** + * Will be called if the server's IP address changes. The event will be propagated + * to all config module classes so action can be taken if appropriate. + */ + public static function serverIpChanged() + { + self::loadDb(); + foreach (self::$moduleTypes as $module) { + $instance = new $module['moduleClass']; + $instance->event_serverIpChanged(); + } + } + +} + +/** + * Base class for config modules + */ +abstract class ConfigModule +{ + + public function event_serverIpChanged() + { + + } + +} diff --git a/inc/database.inc.php b/inc/database.inc.php index 85bee4b1..2c535d04 100644 --- a/inc/database.inc.php +++ b/inc/database.inc.php @@ -7,6 +7,10 @@ class Database { + /** + * + * @var \PDO Database handle + */ private static $dbh = false; private static $statements = array(); @@ -16,7 +20,7 @@ class Database */ public static function getExpectedSchemaVersion() { - return 7; + return 8; } public static function needSchemaUpdate() diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php index a5a149c8..38b25540 100644 --- a/inc/trigger.inc.php +++ b/inc/trigger.inc.php @@ -83,10 +83,11 @@ class Trigger */ public static function ldadp($parent = NULL) { + // TODO: Fetch list from ConfigModule_AdAuth (call loadDb first) $res = Database::simpleQuery("SELECT moduleid, configtgz.filepath FROM configtgz_module" . " INNER JOIN configtgz_x_module USING (moduleid)" . " INNER JOIN configtgz USING (configid)" - . " WHERE moduletype = 'AD_AUTH'"); + . " WHERE moduletype = 'AdAuth'"); // TODO: Multiconfig support $id = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { @@ -105,35 +106,6 @@ class Trigger return $task['id']; } - /** - * To be called if the server ip changes, as it's embedded in the AD module configs. - * This will then recreate all AD tgz modules. - */ - public static function rebuildAdModules($parent = NULL) - { - $task = Taskmanager::submit('LdadpLauncher', array( - 'parentTask' => $parent, - 'failOnParentFail' => false, - 'ids' => array() - )); // Stop all running instances - $ads = ConfigModule::getAdConfigs(); - if (empty($ads)) - return false; - - if (isset($task['id'])) - $parent = $task['id']; - foreach ($ads as $ad) { - $ad['parentTask'] = $parent; - $ad['failOnParentFail'] = false; - $ad['proxyip'] = Property::getServerIp(); - $task = Taskmanager::submit('CreateAdConfig', $ad); - if (isset($task['id'])) - $parent = $task['id']; - } - Trigger::ldadp($parent); - return $parent; - } - /** * Mount the VM store into the server. * -- cgit v1.2.3-55-g7522