From ca312a6ace43a6fde58d1c09057c7b0bd34f15a2 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 27 Nov 2019 17:25:46 +0100 Subject: [statistics/rebootcontrol] Implement editing subnet --- .../rebootcontrol/pages/jumphost.inc.php | 167 +++++++++++++++++++++ .../rebootcontrol/pages/subnet.inc.php | 150 ++++++++++++++++++ modules-available/rebootcontrol/pages/task.inc.php | 101 +++++++++++++ 3 files changed, 418 insertions(+) create mode 100644 modules-available/rebootcontrol/pages/jumphost.inc.php create mode 100644 modules-available/rebootcontrol/pages/subnet.inc.php create mode 100644 modules-available/rebootcontrol/pages/task.inc.php (limited to 'modules-available/rebootcontrol/pages') diff --git a/modules-available/rebootcontrol/pages/jumphost.inc.php b/modules-available/rebootcontrol/pages/jumphost.inc.php new file mode 100644 index 00000000..111560ef --- /dev/null +++ b/modules-available/rebootcontrol/pages/jumphost.inc.php @@ -0,0 +1,167 @@ + $host['host'], + 'port' => $host['port'], + 'username' => $host['username'], + ]], $script, 5, $host['sshkey']); + if (!Taskmanager::isTask($task)) + return; + TaskmanagerCallback::addCallback($task, 'rbcConnCheck', $hostid); + Util::redirect('?do=rebootcontrol&show=task&type=checkhost&what=task&taskid=' . $task['id']); + } + + private static function saveJumpHost() + { + User::assertPermission('jumphost.edit'); + $id = Request::post('hostid', Request::REQUIRED, 'string'); + $host = Request::post('host', Request::REQUIRED, 'string'); + $port = Request::post('port', Request::REQUIRED, 'int'); + if ($port < 1 || $port > 65535) { + Message::addError('invalid-port', $port); + return; + } + $username = Request::post('username', Request::REQUIRED, 'string'); + $sshkey = Request::post('sshkey', Request::REQUIRED, 'string'); + $script = preg_replace('/\r\n?/', "\n", Request::post('script', Request::REQUIRED, 'string')); + if ($id === 'new') { + $ret = Database::exec('INSERT INTO reboot_jumphost (host, port, username, sshkey, script, reachable) + VALUE (:host, :port, :username, :sshkey, :script, 0)', compact('host', 'port', 'username', 'sshkey', 'script')); + $id = Database::lastInsertId(); + } else { + $ret = Database::exec('UPDATE reboot_jumphost SET + host = :host, port = :port, username = :username, sshkey = :sshkey, script = :script, reachable = 0 + WHERE hostid = :id', compact('host', 'port', 'username', 'sshkey', 'script', 'id')); + if ($ret === 0) { + $ret = Database::queryFirst('SELECT hostid FROM reboot_jumphost WHERE hostid = :id', ['id' => $id]); + if ($ret !== false) { + $ret = 1; + } + } + } + if ($ret > 0) { + Message::addSuccess('jumphost-saved', $id); + self::execCheckConnection($id); + } else { + Message::addError('no-such-jumphost', $id); + } + } + + /* + * Render + */ + + public static function doRender() + { + $id = Request::get('id', false, 'string'); + if ($id !== false) { + self::showJumpHost($id); + } else { + self::showJumpHosts(); + } + } + + private static function showJumpHosts() + { + User::assertPermission('jumphost.*'); + $hosts = []; + $res = Database::simpleQuery('SELECT hostid, host, port, Count(jxs.subnetid) AS subnetCount, reachable + FROM reboot_jumphost jh + LEFT JOIN reboot_jumphost_x_subnet jxs USING (hostid) + GROUP BY hostid + ORDER BY hostid'); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $hosts[] = $row; + } + $data = [ + 'jumpHosts' => $hosts + ]; + Permission::addGlobalTags($data['perms'], null, ['jumphost.edit', 'jumphost.assign-subnet']); + Render::addTemplate('jumphost-list', $data); + } + + private static function showJumpHost($id) + { + User::assertPermission('jumphost.edit'); + if ($id === 'new') { + $host = ['hostid' => 'new', 'port' => 22, 'script' => "# Assume bash\n" + . "MACS='%MACS%'\n" + . "IP='%IP'\n" + . "EW=false\n" + . "WOL=false\n" + . "command -v etherwake > /dev/null && ( [ \"\$(id -u)\" = 0 ] || [ -u \"\$(which etherwake)\" ] ) && EW=true\n" + . "command -v wakeonlan > /dev/null && WOL=true\n" + . "if \$EW && ( ! \$WOL || [ \"\$IP\" = '255.255.255.255' ] ); then\n" + . "\tifaces=\"\$(ls -1 /sys/class/net/)\"\n" + . "\t[ -z \"\$ifaces\" ] && ifaces=eth0\n" + . "\tfor ifc in \$ifaces; do\n" + . "\t\t[ \"\$ifc\" = 'lo' ] && continue\n" + . "\t\tfor mac in \$MACS; do\n" + . "\t\t\tetherwake -i \"\$ifc\" \"\$mac\"\n" + . "\t\tdone\n" + . "\tdone\n" + . "elif \$WOL; then\n" + . "\twakeonlan -i \"\$IP\" \$MACS\n" + . "else\n" + . "\techo 'No suitable WOL tool found' >&2\n" + . "\texit 1\n" + . "fi\n"]; + } else { + $host = self::getJumpHost($id); + } + Render::addTemplate('jumphost-edit', $host); + } + + public static function doAjax() + { + + } + + /* + * MISC + */ + + private static function getJumpHost($hostid) + { + $host = Database::queryFirst('SELECT hostid, host, port, username, sshkey, script + FROM reboot_jumphost + WHERE hostid = :id', ['id' => $hostid]); + if ($host === false) { + Message::addError('no-such-jumphost', $hostid); + Util::redirect('?do=rebootcontrol'); + } + return $host; + } + +} \ No newline at end of file diff --git a/modules-available/rebootcontrol/pages/subnet.inc.php b/modules-available/rebootcontrol/pages/subnet.inc.php new file mode 100644 index 00000000..946d2d64 --- /dev/null +++ b/modules-available/rebootcontrol/pages/subnet.inc.php @@ -0,0 +1,150 @@ + $range['end_l']) { + Message::addError('invalid-range', $range['start'], $range['end']); + return; + } + $ret = Database::exec('INSERT INTO reboot_subnet (start, end, fixed, isdirect) + VALUES (:start, :end, 1, 0)', [ + 'start' => sprintf('%u', $range['start_l']), + 'end' => sprintf('%u', $range['end_l']), + ], true); + if ($ret === false) { + Message::addError('subnet-already-exists'); + } else { + Message::addSuccess('subnet-created'); + Util::redirect('?do=rebootcontrol&show=subnet&what=subnet&id=' . Database::lastInsertId()); + } + } + + private static function editSubnet() + { + User::assertPermission('subnet.flag'); + $id = Request::post('id', Request::REQUIRED, 'int'); + $subnet = Database::queryFirst('SELECT subnetid + FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]); + if ($subnet === false) { + Message::addError('invalid-subnet', $id); + return; + } + $params = [ + 'id' => $id, + 'fixed' => !empty(Request::post('fixed', false, 'string')), + 'isdirect' => !empty(Request::post('isdirect', false, 'string')), + ]; + Database::exec('UPDATE reboot_subnet SET fixed = :fixed, isdirect = If(:fixed, :isdirect, isdirect) + WHERE subnetid = :id', $params); + if (User::hasPermission('jumphost.assign-subnet')) { + $hosts = Request::post('jumphost', [], 'array'); + if (!empty($hosts)) { + $hosts = array_keys($hosts); + Database::exec('DELETE FROM reboot_jumphost_x_subnet WHERE subnetid = :id AND hostid NOT IN (:hosts)', + ['id' => $id, 'hosts' => $hosts]); + $hosts = array_map(function($item) use ($id) { + return [$item, $id]; + }, $hosts); + Database::exec('INSERT IGNORE INTO reboot_jumphost_x_subnet (hostid, subnetid) VALUES :hosts', ['hosts' => $hosts]); + } + } + Message::addSuccess('subnet-updated'); + } + + /* + * Render + */ + + public static function doRender() + { + $what = Request::get('what', 'list', 'string'); + if ($what === 'list') { + self::showSubnets(); + } elseif ($what === 'subnet') { + self::showSubnet(); + } + } + + private static function showSubnets() + { + User::assertPermission('subnet.*'); + $nets = []; + $res = Database::simpleQuery('SELECT subnetid, start, end, fixed, isdirect, + lastdirectcheck, lastseen, seencount, Count(hxs.hostid) AS jumphostcount + FROM reboot_subnet + LEFT JOIN reboot_jumphost_x_subnet hxs USING (subnetid) + GROUP BY subnetid, start, end + ORDER BY start ASC, end DESC'); + $deadline = strtotime('-60 days'); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $row['start_s'] = long2ip($row['start']); + $row['end_s'] = long2ip($row['end']); + $row['lastseen_s'] = Util::prettyTime($row['lastseen']); + if ($row['lastseen'] && $row['lastseen'] < $deadline) { + $row['lastseen_class'] = 'text-danger'; + } + $nets[] = $row; + } + $data = ['subnets' => $nets]; + Render::addTemplate('subnet-list', $data); + Module::isAvailable('js_ip'); + } + + private static function showSubnet() + { + User::assertPermission('subnet.*'); + $id = Request::get('id', Request::REQUIRED, 'int'); + $subnet = Database::queryFirst('SELECT subnetid, start, end, fixed, isdirect + FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]); + if ($subnet === false) { + Message::addError('invalid-subnet', $id); + return; + } + $subnet['start_s'] = long2ip($subnet['start']); + $subnet['end_s'] = long2ip($subnet['end']); + $res = Database::simpleQuery('SELECT h.hostid, h.host, h.port, hxs.subnetid FROM reboot_jumphost h + LEFT JOIN reboot_jumphost_x_subnet hxs ON (h.hostid = hxs.hostid AND hxs.subnetid = :id) + ORDER BY h.host ASC', ['id' => $id]); + $jh = []; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $row['checked'] = $row['subnetid'] === null ? '' : 'checked'; + $jh[] = $row; + } + $subnet['jumpHosts'] = $jh; + Permission::addGlobalTags($subnet['perms'], null, ['subnet.flag', 'jumphost.view', 'jumphost.assign-subnet']); + Render::addTemplate('subnet-edit', $subnet); + } + + public static function doAjax() + { + + } + +} \ No newline at end of file diff --git a/modules-available/rebootcontrol/pages/task.inc.php b/modules-available/rebootcontrol/pages/task.inc.php new file mode 100644 index 00000000..15449aaf --- /dev/null +++ b/modules-available/rebootcontrol/pages/task.inc.php @@ -0,0 +1,101 @@ + $task['id'], + 'locationId' => $td['locationId'], + 'locationName' => Location::getName($td['locationId']), + ]; + $uuids = array_map(function ($entry) { + return $entry['machineuuid']; + }, $td['clients']); + $data['clients'] = RebootQueries::getMachinesByUuid($uuids); + Render::addTemplate('status-reboot', $data); + } elseif ($type === 'exec') { + $data = [ + 'taskId' => $task['id'], + ]; + Render::addTemplate('status-exec', $data); + } elseif ($type === 'checkhost') { + $ip = array_key_first($td['result']); + $data = [ + 'taskId' => $task['id'], + 'host' => $ip, + ]; + Render::addTemplate('status-checkconnection', $data); + } else { + Message::addError('unknown-task-type'); + } + } + + private static function showTaskList() + { + // Append list of active reboot/shutdown tasks + $allowedLocs = User::getAllowedLocations("*"); + $active = RebootControl::getActiveTasks($allowedLocs); + if (empty($active)) { + Message::addInfo('no-current-tasks'); + } else { + foreach ($active as &$entry) { + $entry['locationName'] = Location::getName($entry['locationId']); + } + unset($entry); + Render::addTemplate('task-list', ['list' => $active]); + } + } + + public static function doAjax() + { + + } + +} + + +// Remove when we require >= 7.3.0 +if (!function_exists('array_key_first')) { + function array_key_first(array $arr) { + foreach($arr as $key => $unused) { + return $key; + } + return NULL; + } +} -- cgit v1.2.3-55-g7522