exec('DROP TABLE IF EXISTS "machines"'); $db -> exec('DROP TABLE IF EXISTS "keys"'); $db -> exec('DROP TABLE IF EXISTS "permissions"'); $db -> exec('DROP TABLE IF EXISTS "states"'); $db -> exec('DROP TABLE IF EXISTS "values"'); $db -> exec('CREATE TABLE "machines" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique, last_update TIMESTAMP default CURRENT_TIMESTAMP);'); $db -> exec('CREATE TABLE "keys" (id integer primary key AUTOINCREMENT, key varchar(128) not null unique, comment varchar(128) not null);'); $db -> exec('CREATE TABLE "states" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique)'); $db -> exec('CREATE TABLE "permissions" (key_id integer not null, value_id integer not null)'); $db -> exec('CREATE TABLE "values" (id integer primary key AUTOINCREMENT, machine_id integer not null, state_id integer not null, value integer)'); $stm = $db -> prepare('INSERT INTO states (name) VALUES (:state)'); if (! $stm) die(); foreach (array('running', 'ping', 'power') as $state) { $stm -> bindValue('state', $state); if (! $stm -> execute()) die(); } echo 'ok' . "\n"; die(); } if (array_key_exists('machine',$_GET)) { $db -> add_machine($_GET['machine']); if (array_key_exists('new_key', $_GET) && array_key_exists('comment', $_GET)) $db -> add_key_for($_GET['machine'], $_GET['new_key'], $_GET['comment']); $stm = $db -> prepare( 'SELECT states.name, keys.key, keys.comment FROM machines' . ' JOIN "values" ON "values".machine_id = machines.id' . ' JOIN permissions ON permissions.value_id = "values".id' . ' JOIN states ON "values".state_id = states.id' . ' JOIN keys ON permissions.key_id = keys.id' . ' WHERE machines.name=:machine'); $stm -> bindValue('machine', $_GET['machine']); $result = $stm -> execute(); while ($row = $result -> fetchArray()) { echo 'permission = "' . $row['name'] . '", key = "' . $row['key'] . '", comment = "' . $row['comment'] . '"
' . "\n"; } echo '?machine=' . $_GET['machine'] . '&new_key=running&comment=comment' . "\n"; die(); } if (array_key_exists('ddns_machines', $_GET)) { $ddns_tokens = file('/srv/http/vhosts/eckner.net/ddns/tokens'); foreach ($ddns_tokens as $ddns_token) { $db -> add_machine(explode(' ', trim($ddns_token), 2)[1]); } echo 'done.' . "\n"; die(); } if (array_key_exists('lights_out_machines', $_GET)) { $result = $db -> query( 'SELECT machines.name AS machine FROM machines' . ' WHERE machines.name LIKE ' . "'" . '%-lo' . "'" ); $power_id = $db -> get_state_id('power'); $ping_id = $db -> get_state_id('ping'); while ($row = $result -> fetchArray()) { unset($machine_id); foreach ($db -> get_machines() as $machine) { if ($machine['machine'] == substr($row['machine'],0,-3)) { $machine_id = $machine['machine_id']; break; } } if (!isset($machine_id)) foreach ($db -> get_machines() as $machine) { if ($machine['machine'] == substr($row['machine'],0,strlen($machine['machine']))) { $machine_id = $machine['machine_id']; break; } } $db -> add_key_for($machine_id, $power_id, 'lights-out'); $db -> add_key_for($machine_id, $ping_id, 'lights-out'); } echo 'done.' . "\n"; die(); } if (array_key_exists('ddns', $_GET)) { $result = $db -> query( 'SELECT keys.id FROM keys' . ' WHERE keys.comment = ' . "'" . 'ddns' . "'" ); $row = $result -> fetchArray(); if (! $row) $key_id = $db -> add_key('ddns'); else $key_id = $row['id']; $stm = $db -> prepare('INSERT INTO permissions (key_id, value_id) VALUES (:key_id, :value_id)'); $stm -> bindValue('key_id', $key_id); $getstm = $db -> prepare( 'SELECT "values".id as value_id FROM "values"' . ' JOIN states ON states.id = "values".state_id' . ' WHERE states.name = :state' . ' AND NOT EXISTS (' . 'SELECT 1 FROM permissions' . ' WHERE permissions.value_id = "values".id' . ' AND permissions.key_id = :key_id' . ')' ); $getstm -> bindValue('state', 'running'); $getstm -> bindValue('key_id', $key_id); $result = $getstm -> execute(); while ($row = $result -> fetchArray()) { $stm -> bindValue('value_id', $row['value_id']); $stm -> execute(); } $getstm -> close(); $stm -> close(); $stm = $db -> prepare('SELECT keys.key from keys WHERE keys.id = :key_id'); $stm -> bindValue('key_id', $key_id); $result = $stm -> execute(); $result = $result -> fetchArray(); echo $result['key'] . "\n"; $stm -> close(); die(); } ?>

Query machines

query('SELECT machines.name AS machine FROM machines'); while ($row = $result -> fetchArray()) { echo '' . $row['machine'] . '
' . "\n"; } ?>

Global Tasks

initialize machines from ddns tokens
ddns
initialize keys for lights-out machines

Danger zone

drop