diff options
author | Simon Rettberg | 2014-10-09 16:01:11 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-10-09 16:01:11 +0200 |
commit | e1dc0d3c99217504de2ac8467156274786efc0bd (patch) | |
tree | 130d7fed1fff8aaaffe5942cf2a3d6bb1dad03c8 /apis | |
parent | Minor fixes and improvements (diff) | |
download | slx-admin-e1dc0d3c99217504de2ac8467156274786efc0bd.tar.gz slx-admin-e1dc0d3c99217504de2ac8467156274786efc0bd.tar.xz slx-admin-e1dc0d3c99217504de2ac8467156274786efc0bd.zip |
Big load of changes
- Added callback functionality for taskmanager tasks. You can launch
a task and define a callback function to be run when the task finished.
This requires activating the cronjob
- Added cron functionality: Add cronjob that calls the cron api every 5
minutes to use it. (See cron.inc.php)
- Added eventlog
- Added missing translations
- Merged main-menu-login and main-menu-logout
Diffstat (limited to 'apis')
-rw-r--r-- | apis/cron.inc.php | 28 | ||||
-rw-r--r-- | apis/init.inc.php | 61 | ||||
-rw-r--r-- | apis/update.inc.php | 137 |
3 files changed, 102 insertions, 124 deletions
diff --git a/apis/cron.inc.php b/apis/cron.inc.php new file mode 100644 index 00000000..2d0fc66c --- /dev/null +++ b/apis/cron.inc.php @@ -0,0 +1,28 @@ +<?php + +/* + * cronjob callback. This script does periodic checks, logging, + * housekeeping etc. Should be called every 5 mins by cron. + * Make a crontab entry that runs this as the same user the + * www-php is normally run as, eg. */ +// */5 * * * * www-data php /path/to/api.php cron + +if (!isLocalExecution()) + exit(0); + +switch (mt_rand(1, 10)) { +case 1: + Database::exec("DELETE FROM clientlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 90"); + break; +case 2: + Database::exec("DELETE FROM eventlog WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 90"); + break; +case 3: + Database::exec("DELETE FROM property WHERE dateline <> 0 AND dateline < UNIX_TIMESTAMP()"); + break; +case 4: + Database::exec("DELETE FROM callback WHERE (UNIX_TIMESTAMP() - dateline) > 86400"); + break; +} + +Trigger::checkCallbacks(); diff --git a/apis/init.inc.php b/apis/init.inc.php index a1344fc4..617114d7 100644 --- a/apis/init.inc.php +++ b/apis/init.inc.php @@ -1,62 +1,9 @@ <?php +// via cron: +// @reboot www-data php /path/to/api.php init + if (!isLocalExecution()) exit(0); -EventLog::info('System boot...'); -$everythingFine = true; - -DefaultData::populate(); - -// Tasks: fire away -$mountId = Trigger::mount(); -$ldadpId = Trigger::ldadp(); -$autoIp = Trigger::autoUpdateServerIp(); -$ipxeId = Trigger::ipxe(); - -// Check status of all tasks -// Mount vm store -if ($mountId === false) { - EventLog::info('No VM store type defined.'); - $everythingFine = false; -} else { - $res = Taskmanager::waitComplete($mountId, 5000); - if (Taskmanager::isFailed($res)) { - EventLog::failure('Mounting VM store failed: ' . $res['data']['messages']); - $everythingFine = false; - } -} -// LDAP AD Proxy -if ($ldadpId === false) { - EventLog::failure('Cannot start LDAP-AD-Proxy: Taskmanager unreachable!'); - $everythingFine = false; -} else { - $res = Taskmanager::waitComplete($ldadpId, 5000); - if (Taskmanager::isFailed($res)) { - EventLog::failure('Starting LDAP-AD-Proxy failed: ' . $res['data']['messages']); - $everythingFine = false; - } -} -// Primary IP address -if (!$autoIp) { - EventLog::failure("The server's IP address could not be determined automatically, and there is no active address configured."); - $everythingFine = false; -} -// iPXE generation -if ($ipxeId === false) { - EventLog::failure('Cannot generate PXE menu: Taskmanager unreachable!'); - $everythingFine = false; -} else { - $res = Taskmanager::waitComplete($ipxeId, 5000); - if (Taskmanager::isFailed($res)) { - EventLog::failure('Update PXE Menu failed: ' . $res['data']['error']); - $everythingFine = false; - } -} - -// Just so we know booting is done (and we don't expect any more errors from booting up) -if ($everythingFine) { - EventLog::info('Bootup finished without errors.'); -} else { - EventLog::info('There were errors during bootup. Maybe the server is not fully configured yet.'); -} +Event::systemBooted(); diff --git a/apis/update.inc.php b/apis/update.inc.php index 4fb4b3fb..384fe0be 100644 --- a/apis/update.inc.php +++ b/apis/update.inc.php @@ -39,8 +39,48 @@ while ($currentVersion < $targetVersion) { } Message::addSuccess('db-update-done'); +Eventlog::info("Database updated to version $currentVersion"); Util::redirect('index.php?do=Main'); +//////////////// + +function tableHasColumn($table, $column) +{ + $table = preg_replace('/\W/', '', $table); + $column = preg_replace('/\W/', '', $column); + $res = Database::simpleQuery("DESCRIBE `$table`", array(), false); + if ($res !== false) { + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ((is_array($column) && in_array($row['Field'], $column)) || (is_string($column) && $row['Field'] === $column)) + return true; + } + } + return false; +} + +function tableDropColumn($table, $column) +{ + $table = preg_replace('/\W/', '', $table); + $column = preg_replace('/\W/', '', $column); + $res = Database::simpleQuery("DESCRIBE `$table`", array(), false); + if ($res !== false) { + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ((is_array($column) && in_array($row['Field'], $column)) || (is_string($column) && $row['Field'] === $column)) + Database::exec("ALTER TABLE `$table` DROP `{$row['Field']}`"); + } + } +} + +function tableExists($table) +{ + $res = Database::simpleQuery("SHOW TABLES", array(), false); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ($row['Tables_in_openslx'] === $table) + return true; + } + return false; +} + // The update functions. Number at the end refers to current version, the function will update to the next version // ####################### // ##### 2014-05-28 @@ -48,17 +88,7 @@ Util::redirect('index.php?do=Main'); function update_1() { - $res = Database::simpleQuery("DESCRIBE property", array(), false); - $type = false; - if ($res === false) - return; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if ($row['Field'] !== 'dateline') - continue; - $type = $row['Type']; - break; - } - if ($type === false) { + if (!tableHasColumn('property', 'dateline')) { Database::exec("ALTER TABLE `property` ADD `dateline` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `name` , ADD INDEX ( `dateline` )"); } else { Database::exec("ALTER TABLE `property` CHANGE `dateline` `dateline` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'"); @@ -71,17 +101,9 @@ function update_1() // Add 'news' table to database schema function update_2() { - $res = Database::simpleQuery("show tables", array(), false); - $found = false; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if ($row['Tables_in_openslx'] !== 'news') - continue; - $found = true; - break; - } - if ($found === false) { + if (!tableExists('news')) { // create table - Database::exec("CREATE TABLE `news` ( + Database::exec("CREATE TABLE IF NOT EXISTS `news` ( `newsid` int(10) unsigned NOT NULL AUTO_INCREMENT, `dateline` int(10) unsigned NOT NULL, `title` varchar(200) DEFAULT NULL, @@ -99,32 +121,8 @@ function update_2() // Remove setting descriptions from DB, put into json files now function update_3() { - $res = Database::simpleQuery("DESCRIBE setting", array(), false); - if ($res !== false) { - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - switch ($row['Field']) { - case 'de': - case 'en': - case 'pt': - case 'description': - Database::exec("ALTER TABLE setting DROP {$row['Field']}"); - break; - } - } - } - $res = Database::simpleQuery("DESCRIBE cat_setting", array(), false); - if ($res !== false) { - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - switch ($row['Field']) { - case 'de': - case 'en': - case 'pt': - case 'name': - Database::exec("ALTER TABLE cat_setting DROP {$row['Field']}"); - break; - } - } - } + tableDropColumn('setting', array('de', 'en', 'pt', 'description')); + tableDropColumn('cat_setting', array('de', 'en', 'pt', 'name')); return true; } @@ -133,27 +131,10 @@ function update_3() // Remove description column from permission table, add eventlog table function update_4() { - $res = Database::simpleQuery("DESCRIBE permission", array(), false); - if ($res !== false) { - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - switch ($row['Field']) { - case 'description': - Database::exec("ALTER TABLE permission DROP {$row['Field']}"); - break; - } - } - } - $res = Database::simpleQuery("show tables", array(), false); - $found = false; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if ($row['Tables_in_openslx'] !== 'eventlog') - continue; - $found = true; - break; - } - if ($found === false) { + tableDropColumn('permission', 'description'); + if (!tableExists('eventlog')) { // create table - Database::exec("CREATE TABLE `eventlog` ( + Database::exec("CREATE TABLE IF NOT EXISTS `eventlog` ( `logid` int(10) unsigned NOT NULL AUTO_INCREMENT, `dateline` int(10) unsigned NOT NULL, `logtypeid` varchar(30) NOT NULL, @@ -166,3 +147,25 @@ function update_4() } return true; } + +// ####################### +// ##### 2014-08-18 +// Add details column to eventlog table, add callback table +function update_5() +{ + if (!tableHasColumn('eventlog', 'extra')) + Database::exec("ALTER TABLE `eventlog` ADD `extra` TEXT NOT NULL"); + if (!tableHasColumn('user', 'lasteventid')) + Database::exec("ALTER TABLE `user` ADD `lasteventid` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'"); + if (!tableExists('callback')) { + Database::exec("CREATE TABLE IF NOT EXISTS `callback` ( + `taskid` varchar(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + `dateline` int(10) unsigned NOT NULL, + `cbfunction` varchar(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + PRIMARY KEY (`taskid`,`cbfunction`), + KEY `dateline` (`dateline`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 + "); + } + return true; +} |