summaryrefslogtreecommitdiffstats
path: root/modules-available/runmode
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/runmode')
-rw-r--r--modules-available/runmode/inc/runmode.inc.php141
-rw-r--r--modules-available/runmode/install.inc.php24
-rw-r--r--modules-available/runmode/lang/de/template-tags.json10
-rw-r--r--modules-available/runmode/lang/en/template-tags.json10
-rw-r--r--modules-available/runmode/page.inc.php29
-rw-r--r--modules-available/runmode/templates/machine-selector.html15
-rw-r--r--modules-available/runmode/templates/module-machine-list.html2
7 files changed, 168 insertions, 63 deletions
diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php
index 271542b8..d333af58 100644
--- a/modules-available/runmode/inc/runmode.inc.php
+++ b/modules-available/runmode/inc/runmode.inc.php
@@ -2,6 +2,9 @@
class RunMode
{
+ const DATA_DETAILED = 1;
+ const DATA_MACHINE_DATA = 2;
+ const DATA_STRINGS = 4;
private static $moduleConfigs = array();
@@ -29,58 +32,86 @@ class RunMode
* @param string|null $modeId an ID specific to the module to further specify the run mode, NULL to delete the run mode entry
* @param string|null $modeData optional, additional data for the run mode
* @param bool|null $isClient whether to count the machine as a client (in statistics etc.) NULL for looking at module's general runmode config
- * @return bool whether it was set
+ * @return bool whether it was set/deleted
*/
- public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData, $isClient)
+ public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData = null, $isClient = null)
{
- // - Check if module provides runmode config at all
- $config = self::getModuleConfig($moduleId);
- if ($config === false)
- return false;
+ if (is_object($moduleId)) {
+ $moduleId = $moduleId->getIdentifier();
+ }
// - Check if machine exists
$machine = Statistics::getMachine($machineuuid, Machine::NO_DATA);
if ($machine === false)
return false;
+ // - Delete entry if mode is null
+ if ($modeId === null) {
+ return Database::exec('DELETE FROM runmode WHERE machineuuid = :machineuuid', compact('machineuuid')) > 0;
+ }
// - Add/replace entry in runmode table
- if (is_null($modeId)) {
- Database::exec('DELETE FROM runmode WHERE machineuuid = :machineuuid', compact('machineuuid'));
- } else {
- if ($isClient === null) {
- $isClient = $config->isClient;
- }
- Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata, isclient)'
- . ' VALUES (:uuid, :module, :modeid, :modedata, :isclient)'
- . ' ON DUPLICATE KEY'
- . ' UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata), isclient = VALUES(isclient)', array(
- 'uuid' => $machineuuid,
- 'module' => $moduleId,
- 'modeid' => $modeId,
- 'modedata' => $modeData,
- 'isclient' => ($isClient ? 1 : 0),
- ));
+ // - Check if module provides runmode config at all
+ $config = self::getModuleConfig($moduleId);
+ if ($config === false)
+ return false;
+ if ($isClient === null) {
+ $isClient = $config->isClient;
}
+ Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata, isclient)'
+ . ' VALUES (:uuid, :module, :modeid, :modedata, :isclient)'
+ . ' ON DUPLICATE KEY'
+ . ' UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata), isclient = VALUES(isclient)', array(
+ 'uuid' => $machineuuid,
+ 'module' => $moduleId,
+ 'modeid' => $modeId,
+ 'modedata' => $modeData,
+ 'isclient' => ($isClient ? 1 : 0),
+ ));
return true;
}
/**
* @param string $machineuuid
- * @param bool $detailed whether to return meta data about machine, not just machineuuid
- * @param bool $assoc use machineuuid as array key
+ * @param int $returnData bitfield of data to return
* @return false|array {'machineuuid', 'isclient', 'module', 'modeid', 'modedata',
* <'hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'>}
*/
- public static function getRunMode($machineuuid, $detailed = false)
+ public static function getRunMode($machineuuid, $returnData = self::DATA_MACHINE_DATA)
{
- if ($detailed) {
- $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen';
+ if ($returnData === true) {
+ $returnData = self::DATA_MACHINE_DATA | self::DATA_DETAILED;
+ }
+ if ($returnData & self::DATA_MACHINE_DATA) {
+ if ($returnData & self::DATA_DETAILED) {
+ $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen';
+ } else {
+ $sel = '';
+ }
+ $res = 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'));
} else {
- $sel = '';
+ $res = Database::queryFirst('SELECT r.machineuuid, r.isclient, r.module, r.modeid, r.modedata
+ FROM runmode r
+ WHERE r.machineuuid = :machineuuid LIMIT 1',
+ compact('machineuuid'));
+ }
+ if ($res === false)
+ return false;
+ if ($returnData & self::DATA_STRINGS) {
+ $module = Module::get($res['module']);
+ if ($module === false) {
+ $res['moduleName'] = $res['module'];
+ } else {
+ $res['moduleName'] = $module->getDisplayName();
+ }
+ $mode = self::getModeName($res['module'], $res['modeid']);
+ if ($mode === false) {
+ $mode = '???? unknown';
+ }
+ $res['modeName'] = $mode;
}
- 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'));
+ return $res;
}
/**
@@ -129,7 +160,7 @@ class RunMode
$join = $sel = '';
}
$res = Database::simpleQuery(
- "SELECT r.machineuuid, r.modedata $sel
+ "SELECT r.machineuuid, r.modedata, r.isclient $sel
FROM runmode r $join
WHERE module = :module AND modeid = :modeId",
compact('module', 'modeId'));
@@ -148,6 +179,28 @@ class RunMode
}
/**
+ * Return assoc array of all configured clients.
+ * @param bool $withData also return data field?
+ * @param bool $isClient true = return clients only, false = return non-clients only, null = return both
+ * @return array all the entries from the table
+ */
+ public static function getAllClients($withData = false, $isClient = null)
+ {
+ $xtra = '';
+ if ($withData) {
+ $xtra .= ', modedata';
+ }
+ $res = Database::simpleQuery("SELECT machineuuid, module, modeid, isclient $xtra FROM runmode");
+ $ret = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if (!is_null($isClient) && ($row['isclient'] != 0) !== $isClient)
+ continue;
+ $ret[$row['machineuuid']] = $row;
+ }
+ return $ret;
+ }
+
+ /**
* Get display name of a module's mode. If the module doesn't have a getModeName
* method configured, the modeId is simply returned. Otherwise the return value of
* that method is passed through. getModeName by contract should return false if
@@ -168,6 +221,21 @@ class RunMode
return call_user_func($conf->getModeName, $modeId);
}
+ /**
+ * Delete given runmode.
+ *
+ * @param string|\Module $module Module runmode belongs to
+ * @param string $modeId run mode id
+ */
+ public static function deleteMode($module, $modeId)
+ {
+ if (is_object($module)) {
+ $module = $module->getIdentifier();
+ }
+ Database::exec('DELETE FROM runmode WHERE module = :module AND modeid = :modeId',
+ compact('module', 'modeId'));
+ }
+
}
/* *\
@@ -207,6 +275,10 @@ class RunModeModuleConfig
* @var bool If true, config.tgz should not be downloaded by the client
*/
public $noSysconfig = false;
+ /**
+ * @var bool Allow adding and removing machines to this mode via the generic form
+ */
+ public $allowGenericEditor = true;
public function __construct($file)
{
@@ -220,6 +292,7 @@ class RunModeModuleConfig
$this->loadType($data, 'configHook', 'string');
$this->loadType($data, 'isClient', 'boolean');
$this->loadType($data, 'noSysconfig', 'boolean');
+ $this->loadType($data, 'allowGenericEditor', 'boolean');
}
private function loadType($data, $key, $type)
diff --git a/modules-available/runmode/install.inc.php b/modules-available/runmode/install.inc.php
index ec710bfa..db7e07f6 100644
--- a/modules-available/runmode/install.inc.php
+++ b/modules-available/runmode/install.inc.php
@@ -12,21 +12,8 @@ $res[] = tableCreate('runmode', "
KEY `module` (`module`,`modeid`)
");
-if (!tableExists('machine')) {
- // Cannot add constraint yet
- $res[] = UPDATE_RETRY;
-} else {
- $c = tableGetContraints('runmode', 'machineuuid', 'machine', 'machineuuid');
- if ($c === false)
- finalResponse(UPDATE_FAILED, 'Cannot get constraints of runmode table: ' . Database::lastError());
- if (empty($c)) {
- $alter = Database::exec('ALTER TABLE runmode ADD FOREIGN KEY (machineuuid) REFERENCES machine (machineuuid)
- ON DELETE CASCADE ON UPDATE CASCADE');
- if ($alter === false)
- finalResponse(UPDATE_FAILED, 'Cannot add machineuuid constraint to runmode table: ' . Database::lastError());
- $res[] = UPDATE_DONE;
- }
-}
+$res[] = tableAddConstraint('runmode', 'machineuuid', 'machine', 'machineuuid',
+ 'ON DELETE CASCADE ON UPDATE CASCADE');
if (!tableHasColumn('runmode', 'isclient')) {
$ret = Database::exec("ALTER TABLE runmode ADD COLUMN isclient bool DEFAULT '1'");
@@ -38,9 +25,4 @@ if (!tableHasColumn('runmode', 'isclient')) {
}
// Create response for browser
-
-if (in_array(UPDATE_DONE, $res)) {
- finalResponse(UPDATE_DONE, 'Tables created successfully');
-}
-
-finalResponse(UPDATE_NOOP, 'Everything already up to date'); \ No newline at end of file
+responseFromArray($res);
diff --git a/modules-available/runmode/lang/de/template-tags.json b/modules-available/runmode/lang/de/template-tags.json
new file mode 100644
index 00000000..6b45b82c
--- /dev/null
+++ b/modules-available/runmode/lang/de/template-tags.json
@@ -0,0 +1,10 @@
+{
+ "lang_addNewMachines": "Clients",
+ "lang_assignMachineIntroText": "Definieren Sie hier Clients, die in einem speziellen Betriebsmodus gestartet werden sollen. Sie k\u00f6nnen Rechner anhand der UUID, IP, Hostname oder MAC-Adresse suchen.",
+ "lang_assignRunmodeToMachine": "Betriebsmodus",
+ "lang_confirmDelete": "Wollen Sie den Betriebsmodus f\u00fcr diesen Client entfernen?",
+ "lang_isclient": "Pool-Client",
+ "lang_machine": "Client",
+ "lang_mode": "Modus",
+ "lang_specialMachinesForMode": "Betriebsmodus"
+} \ No newline at end of file
diff --git a/modules-available/runmode/lang/en/template-tags.json b/modules-available/runmode/lang/en/template-tags.json
new file mode 100644
index 00000000..43fd3da5
--- /dev/null
+++ b/modules-available/runmode/lang/en/template-tags.json
@@ -0,0 +1,10 @@
+{
+ "lang_addNewMachines": "Clients",
+ "lang_assignMachineIntroText": "Define the clients which should start a special runmode configuration. You can search for clients by UUID, IP address, host name or MAC address.",
+ "lang_assignRunmodeToMachine": "Runmode",
+ "lang_confirmDelete": "Do you want to delete the runmode for this client?",
+ "lang_isclient": "Pool-Client",
+ "lang_machine": "Client",
+ "lang_mode": "Mode",
+ "lang_specialMachinesForMode": "Runmode"
+} \ No newline at end of file
diff --git a/modules-available/runmode/page.inc.php b/modules-available/runmode/page.inc.php
index 05f32f81..ef42e7be 100644
--- a/modules-available/runmode/page.inc.php
+++ b/modules-available/runmode/page.inc.php
@@ -26,20 +26,33 @@ class Page_RunMode extends Page
$machines = array_filter(Request::post('machines', [], 'array'), 'is_string');
$module = Request::post('module', false, 'string');
$modeId = Request::post('modeid', false, 'string');
- // TODO Validate
+ $modConfig = RunMode::getModuleConfig($module);
+ if ($modConfig === false) {
+ Message::addError('module-hasnt-runmode', $module);
+ return;
+ }
+ if (!$modConfig->allowGenericEditor) {
+ Message::addError('cannot-edit-module', $module);
+ return;
+ }
+ $test = RunMode::getModeName($module, $modeId);
+ if ($test === false) {
+ Message::addError('invalid-modeid', $module, $modeId);
+ return;
+ }
$active = 0;
foreach ($machines as $machine) {
$ret = RunMode::setRunMode($machine, $module, $modeId, null, null);
if ($ret) {
$active++;
} else {
- Message::addError('invalid-module-or-machine', $module, $machine);
+ Message::addError('runmode.machine-not-found', $machine);
}
}
$deleted = Database::exec('DELETE FROM runmode
WHERE module = :module AND modeid = :modeId AND machineuuid NOT IN (:machines)',
compact('module', 'modeId', 'machines'));
- Message::addError('enabled-removed-save', $active, $deleted);
+ Message::addSuccess('runmode.enabled-removed-save', $active, $deleted);
$redirect = Request::post('redirect', false, 'string');
if ($redirect !== false) {
Util::redirect($redirect);
@@ -48,7 +61,13 @@ class Page_RunMode extends Page
} elseif ($action === 'delete-machine') {
$machineuuid = Request::post('machineuuid', false, 'string');
if ($machineuuid === false) {
-
+ Message::addError('machine-not-found', $machineuuid);
+ } else {
+ if (RunMode::setRunMode($machineuuid, null, null)) {
+ Message::addSuccess('machine-removed', $machineuuid);
+ } else {
+ Message::addWarning('machine-not-runmode', $machineuuid);
+ }
}
}
}
@@ -135,7 +154,7 @@ class Page_RunMode extends Page
$moduleId = $module->getIdentifier();
$modeName = RunMode::getModeName($moduleId, $modeId);
if ($modeName === false) {
- Message::addError('invalid-modeid', $modeId);
+ Message::addError('invalid-modeid', $moduleId, $modeId);
Util::redirect('?do=runmode');
}
Render::addTemplate('machine-selector', [
diff --git a/modules-available/runmode/templates/machine-selector.html b/modules-available/runmode/templates/machine-selector.html
index d3ff7378..7f37f5a2 100644
--- a/modules-available/runmode/templates/machine-selector.html
+++ b/modules-available/runmode/templates/machine-selector.html
@@ -108,11 +108,22 @@ function renderMachineSelected(item, escape) {
}
document.addEventListener('DOMContentLoaded', function () {
+ Selectize.define("no_bs", function (options) {
+ var original = this.deleteSelection;
+ this.deleteSelection = (function() {
+ return function (e) {
+ if (!e || e.keyCode !== 8) {
+ return original.apply(this, arguments);
+ }
+ return false;
+ };
+ })();
+ });
var old = {{{machines}}} || [];
var $box = $('#machine-sel').selectize({
options: old,
items: old.map(function(x) { return x.machineuuid; }),
- plugins: ["remove_button"],
+ plugins: ["remove_button", "no_bs"],
valueField: 'machineuuid',
searchField: "combined",
openOnFocus: false,
@@ -121,7 +132,7 @@ document.addEventListener('DOMContentLoaded', function () {
load: loadMachines,
maxItems: null,
sortField: 'hostname',
- sortDirection: 'asc'
+ sortDirection: 'asc',
});
});
diff --git a/modules-available/runmode/templates/module-machine-list.html b/modules-available/runmode/templates/module-machine-list.html
index a749a4a7..45f574ef 100644
--- a/modules-available/runmode/templates/module-machine-list.html
+++ b/modules-available/runmode/templates/module-machine-list.html
@@ -3,7 +3,7 @@
<a href="?do={{module}}">{{modulename}}</a>
</h2>
-<form method="post" action="?do=runmode" onclick="return confirm('{{lang_confirmDelete}}')">
+<form method="post" action="?do=runmode" onsubmit="return confirm('{{lang_confirmDelete}}')">
<input type="hidden" name="token" value="{{token}}">
<input type="hidden" name="action" value="delete-machine">
<table class="table">