diff options
Diffstat (limited to 'modules-available/dnbd3/page.inc.php')
-rw-r--r-- | modules-available/dnbd3/page.inc.php | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/modules-available/dnbd3/page.inc.php b/modules-available/dnbd3/page.inc.php index d0842c23..6b0df8e4 100644 --- a/modules-available/dnbd3/page.inc.php +++ b/modules-available/dnbd3/page.inc.php @@ -25,7 +25,7 @@ class Page_Dnbd3 extends Page } elseif ($action === 'savelocations') { $this->saveServerLocations(); } elseif ($action === 'toggle-usage') { - $this->toggleUsage(); + $this->saveGenericSettings(); } if (Request::isPost()) { Util::redirect('?do=dnbd3'); @@ -34,7 +34,7 @@ class Page_Dnbd3 extends Page private function editServer() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); if (!isset($server['machineuuid'])) { Message::addError('not-automatic-server', $server['ip']); return; @@ -45,12 +45,15 @@ class Page_Dnbd3 extends Page $overrideIp = false; $sip = Request::post('fixedip', null, 'string'); if (empty($sip)) { + // Reset IP override $overrideIp = null; - } elseif ($server['fixedip'] !== $overrideIp) { + } elseif ($server['fixedip'] !== $sip) { + // IP override is set and different from current value if (Dnbd3Util::matchAddress($sip) === false) { Message::addError('invalid-ip', $sip); return; } + // Dupcheck $res = Database::queryFirst('SELECT serverid FROM dnbd3_server s LEFT JOIN machine m USING (machineuuid) WHERE s.fixedip = :ip OR m.clientip = :ip', ['ip' => $sip]); @@ -68,7 +71,7 @@ class Page_Dnbd3 extends Page } $advancedSettings = []; foreach (Request::post('extra', [], 'array') as $name => $value) { - $value = preg_replace('/[^0-9KMGTmhdBb]/', '', $value); + $value = preg_replace('/[^0-9KMGTmhdBbtruefals]/', '', $value); if ($value === '') continue; $advancedSettings[$name] = $value; @@ -77,18 +80,20 @@ class Page_Dnbd3 extends Page json_encode(compact('bgr', 'firewall', 'advancedSettings')), false); } - private function toggleUsage() + private function saveGenericSettings() { User::assertPermission('toggle-usage'); $enabled = Request::post('enabled', false, 'bool'); $nfs = Request::post('with-nfs', false, 'bool'); + $preferLocal = Request::post('prefer-local', false, 'bool'); Dnbd3::setEnabled($enabled); Dnbd3::setNfsFallback($nfs); + Dnbd3::setPreferLocal($preferLocal); } private function saveServerLocations() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); $this->assertPermission($server); $locids = Request::post('location', [], 'array'); if (empty($locids)) { @@ -129,7 +134,7 @@ class Page_Dnbd3 extends Page private function deleteServer() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); $this->assertPermission($server); if ($server['fixedip'] === '<self>') return; @@ -174,7 +179,7 @@ class Page_Dnbd3 extends Page $NOW = time(); $externalAllowed = User::hasPermission('configure.external'); $locsRunmode = User::getAllowedLocations('configure.proxy'); - while ($server = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $server) { if (!is_null($server['machineuuid'])) { // Auto proxy if (!isset($dynClients[$server['machineuuid']])) { @@ -208,7 +213,7 @@ class Page_Dnbd3 extends Page $server['diskUsePercent'] = 0; } $server['self'] = ($server['fixedip'] === '<self>'); - if (isset($server['clientip']) && !is_null($server['clientip'])) { + if (isset($server['clientip'])) { if ($NOW - $server['lastseen'] > 360) { $server['slxDown'] = true; } else { @@ -251,7 +256,9 @@ class Page_Dnbd3 extends Page 'enabled' => Dnbd3::isEnabled(), 'enabled_checked_s' => Dnbd3::isEnabled() ? 'checked' : '', 'nfs_checked_s' => Dnbd3::hasNfsFallback() ? 'checked' : '', - 'rebootcontrol' => Module::isAvailable('rebootcontrol', false) + 'local_checked_s' => Dnbd3::preferLocal() ? 'checked' : '', + 'rebootcontrol' => Module::isAvailable('rebootcontrol', false), + 'show_enable_warning' => count($servers) > 1 && !Dnbd3::isEnabled(), ); Permission::addGlobalTags($data['perms'], null, ['view.details', 'refresh', 'toggle-usage', 'configure.proxy', 'configure.external']); Render::addTemplate('page-serverlist', $data); @@ -261,9 +268,10 @@ class Page_Dnbd3 extends Page { User::assertPermission('view.details'); Module::isAvailable('js_stupidtable'); - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); Render::addTemplate('page-proxy-header', $server); - $stats = Dnbd3Rpc::query($server['ip'], true, true, true, true, true, true); + $stats = Dnbd3Rpc::query($server['ip'], [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_CLIENTS, + Dnbd3Rpc::QUERY_IMAGES, Dnbd3Rpc::QUERY_SPACE, Dnbd3Rpc::QUERY_CONFIG, Dnbd3Rpc::QUERY_ALTSERVERS]); if (!is_array($stats) || !isset($stats['runId'])) { Message::addError('server-unreachable'); return; @@ -354,8 +362,9 @@ class Page_Dnbd3 extends Page $loc['clientCount'] = 0; $loc['recCount'] = 0; } + unset($loc); $showLocs = false; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { settype($row['locationid'], 'int'); $loc =& $locCount[$row['locationid']]; $loc['clientCount'] = $row['cnt']; @@ -407,7 +416,7 @@ class Page_Dnbd3 extends Page private function showServerLocationEdit() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); $this->assertPermission($server); // Get selected ones $res = Database::simpleQuery('SELECT locationid FROM dnbd3_server_x_location WHERE serverid = :serverid', @@ -433,11 +442,9 @@ class Page_Dnbd3 extends Page Render::addTemplate('page-server-locations', $server); } - private function getServerById($serverId = false) + private function getServerFromQuery(): array { - if ($serverId === false) { - $serverId = Request::any('server', false, 'int'); - } + $serverId = Request::any('server', false, 'int'); if ($serverId === false) { if (AJAX) die('Missing parameter'); @@ -492,6 +499,8 @@ class Page_Dnbd3 extends Page $this->ajaxReboot(); } elseif ($action === 'cachemap') { $this->ajaxCacheMap(); + } elseif ($action === 'stats') { + $this->ajaxStats(); } else { die($action . '???'); } @@ -513,12 +522,12 @@ class Page_Dnbd3 extends Page if ($res !== false) die('{"error": "Server with this IP already exists", "fatal": true}'); // Query - $reply = Dnbd3Rpc::query($ip,true, false, false, true); - if ($reply === Dnbd3Rpc::QUERY_UNREACHABLE) + $reply = Dnbd3Rpc::query($ip, [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_SPACE]); + if ($reply === Dnbd3Rpc::ERROR_UNREACHABLE) die('{"error": "Could not reach server"}'); - if ($reply === Dnbd3Rpc::QUERY_NOT_200) + if ($reply === Dnbd3Rpc::ERROR_NOT_200) die('{"error": "Server did not reply with 200 OK"}'); - if ($reply === Dnbd3Rpc::QUERY_NOT_JSON) + if ($reply === Dnbd3Rpc::ERROR_NOT_JSON) die('{"error": "No JSON received from server"}'); if (!is_array($reply) || !isset($reply['uptime']) || !isset($reply['clientCount'])) die('{"error": "Reply does not suggest this is a dnbd3 server"}'); @@ -527,7 +536,7 @@ class Page_Dnbd3 extends Page private function ajaxEditServer() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); if (!isset($server['machineuuid'])) { echo 'Not automatic server.'; return; @@ -547,6 +556,7 @@ class Page_Dnbd3 extends Page 'dnbd3.bgrMinClients', 'dnbd3.bgrWindowSize', 'dnbd3.autoFreeDiskSpaceDelay', + 'dnbd3.sparseFiles', 'limits.maxClients', 'limits.maxImages', 'limits.maxPayload', @@ -558,7 +568,7 @@ class Page_Dnbd3 extends Page private function ajaxReboot() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); if (!isset($server['machineuuid'])) { die('Not automatic server.'); } @@ -597,18 +607,18 @@ class Page_Dnbd3 extends Page private function ajaxCacheMap() { - $server = $this->getServerById(); + $server = $this->getServerFromQuery(); $imgId = Request::any('id', 0, 'int'); if ($imgId <= 0) { Header('HTTP/1.1 400 Bad Request'); die('Invalid/no image id'); } $data = Dnbd3Rpc::getCacheMap($server['ip'], $imgId); - if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) { + if ($data === Dnbd3Rpc::ERROR_UNREACHABLE) { Header('HTTP/1.1 504 Gateway Timeout'); die('Proxy not reachable'); } - if ($data === Dnbd3Rpc::QUERY_NOT_200) { + if ($data === Dnbd3Rpc::ERROR_NOT_200) { Header('HTTP/1.1 503 Service Unavailable'); die("Proxy didn't reply with 200 OK"); } @@ -616,30 +626,16 @@ class Page_Dnbd3 extends Page die($data); } - private function genChunk($acc) + private function ajaxStats() { - static $last = -1; - static $count = 0; - if ($acc !== false) { - if ($acc > 15) { - $acc = 15; - } - $acc = round($acc); - if ($last === $acc) { - $count++; - return ''; - } - } - if ($last !== -1) { - if ($count === 1) - return '<b style="background:#0' . dechex($acc) . '0"></b>'; - $line = '<b style="background:#0' . dechex($last) . '0;flex-grow:' . $count . '"></b>'; - } else { - $line = ''; + $lookup = Dnbd3::getActiveServers(); + $result = Dnbd3Rpc::getStatsMulti(array_keys($lookup), [Dnbd3Rpc::QUERY_STATS]); + $return = []; + foreach ($result as $ip => $data) { + $return[$lookup[$ip]] = $data; } - $last = $acc; - $count = 1; - return $line; + Header('Content-Type: application/json; charset=utf-8'); + die(json_encode($return)); } } |