Skip to content

Commit

Permalink
Merge pull request #188 from lbr38/devel
Browse files Browse the repository at this point in the history
4.6.0
  • Loading branch information
lbr38 authored Aug 27, 2024
2 parents 2ace309 + e5050be commit 8154568
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 239 deletions.
2 changes: 1 addition & 1 deletion www/config/properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// Debian repo default values
'debian_distributions' => array('bookworm' => 'Debian 12', 'bullseye' => 'Debian 11', 'buster' => 'Debian 10', 'stretch' => 'Debian 9', 'jessie' => 'Debian 8', 'wheezy' => 'Debian 7'),
'ubuntu_distributions' => array('jammy' => 'Ubuntu 22.04', 'hirsute' => 'Ubuntu 21.04', 'groovy' => 'Ubuntu 20.10', 'focal' => 'Ubuntu 20.04', 'eoan' => 'Ubuntu 19.10', 'disco' => 'Ubuntu 19.04', 'cosmic' => 'Ubuntu 18.10', 'bionic' => 'Ubuntu 18.04', 'xenial' => 'Ubuntu 16.04', 'trusty' => 'Ubuntu 14.04'),
'ubuntu_distributions' => array('noble' => 'Ubuntu 24.04', 'jammy' => 'Ubuntu 22.04', 'hirsute' => 'Ubuntu 21.04', 'groovy' => 'Ubuntu 20.10', 'focal' => 'Ubuntu 20.04', 'eoan' => 'Ubuntu 19.10', 'disco' => 'Ubuntu 19.04', 'cosmic' => 'Ubuntu 18.10', 'bionic' => 'Ubuntu 18.04', 'xenial' => 'Ubuntu 16.04', 'trusty' => 'Ubuntu 14.04'),
'sections' => array('main', 'contrib', 'non-free', 'restricted', 'universe', 'multiverse'),

// DEB default values
Expand Down
4 changes: 4 additions & 0 deletions www/controllers/App/Config/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public static function get()
if (!defined('WS_LOGS_DIR')) {
define('WS_LOGS_DIR', LOGS_DIR . '/websocket');
}
// Websocket requests logs dir
if (!defined('WS_REQUESTS_LOGS_DIR')) {
define('WS_REQUESTS_LOGS_DIR', LOGS_DIR . '/websocket-requests');
}
if (!defined('EXCEPTIONS_LOG')) {
define('EXCEPTIONS_LOG', LOGS_DIR . '/exceptions');
}
Expand Down
1 change: 1 addition & 0 deletions www/controllers/App/Structure/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static function create()
LOGS_DIR,
MAIN_LOGS_DIR,
WS_LOGS_DIR,
WS_REQUESTS_LOGS_DIR,
PID_DIR,
TEMP_DIR,
HOSTS_DIR,
Expand Down
78 changes: 61 additions & 17 deletions www/controllers/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,29 @@ public function getPackageTimeline(string $id, string $packageName)
/**
* Position of the container block in the timeline according to the last displayed
*/
if ($contentPosition == 'left') {
$content .= '<div class="timeline-container timeline-container-left">';
}
if ($contentPosition == 'right') {
$content .= '<div class="timeline-container timeline-container-right">';
}
$content .= '<div class="timeline-container timeline-container-' . $contentPosition . '">';

$content .= '<div class="timeline-container-content">';
$content .= '<span class="timeline-event-date">' . DateTime::createFromFormat('Y-m-d', $event['Date'])->format('d-m-Y') . ' ' . $event['Time'] . '</span>';
$content .= '<div class="flex align-item-center">';
/**
* Display the date, time and state of the package
*/
$content .= '<div class="table-container">';
$content .= '<img src="/assets/icons/' . $contentIcon . '.svg" class="icon" />';
$content .= '<div class="flex flex-direction-column">';
$content .= '<p>' . DateTime::createFromFormat('Y-m-d', $event['Date'])->format('d-m-Y') . '</p>';
$content .= '<p class="lowopacity-cst">' . $event['Time'] . '</p>';
$content .= '</div>';
$content .= '<div class="flex flex-direction-column">';
$content .= '<p>' . $contentText . '</p>';
$content .= '<p class="lowopacity-cst copy">Version: ' . $event['Version'] . '</p>';
$content .= '</div>';
$content .= '</div>';

/**
* If this event is the result of an update, install or uninstall event, then we indicate the Id of the event
*/
// if (!empty($event['Id_event'])) {
// $content .= '<a href="#' . $event['Id_event'] . '" >';
// }
$content .= '<span>' . $contentText . '</span>';
$content .= '</div>';

$content .= '<span class="timeline-event-version">Version : <b>' . $event['Version'] . '</b></span>';
$content .= '</div>';

/**
* If the previous block was on the left, we display the next one on the right and vice versa
Expand Down Expand Up @@ -1200,7 +1201,7 @@ public function hostExec(array $hostsId, string $action)
*/
if ($action == 'reset') {
/**
* Reset host general informations
* Reset host data in database
*/
$this->model->resetHost($hostId);
}
Expand All @@ -1209,10 +1210,20 @@ public function hostExec(array $hostsId, string $action)
* Case where the requested action is a delete
*/
if ($action == 'delete') {
/**
* First, reset host data in database
*/
$this->model->resetHost($hostId);

/**
* Set host status to 'deleted' in database
*/
$this->model->setHostInactive($hostId);

/**
* Add a new ws request to disconnect the host
*/
$this->newWsRequest($hostId, 'disconnect');
}

/**
Expand Down Expand Up @@ -1550,9 +1561,9 @@ public function getWsRequests(string|null $status = null)
/**
* Update websocket request in database
*/
public function updateWsRequest(int $id, string $status, string|null $info = null, string|null $summary = null)
public function updateWsRequest(int $id, string $status, string|null $info = null, string|null $infoJson = null)
{
$this->model->updateWsRequest($id, $status, $info, $summary);
$this->model->updateWsRequest($id, $status, $info, $infoJson);
}

/**
Expand Down Expand Up @@ -1602,4 +1613,37 @@ public function cancelWsRequest(int $id)
{
$this->model->cancelWsRequest($id);
}

/**
* Delete websocket request from database
*/
public function deleteWsRequest(int $id)
{
$this->model->deleteWsRequest($id);
}

/**
* Get request log details
* Request log is a file stored in the websocket-requests logs directory
*/
public function getRequestLog(int $id)
{
$logFile = WS_REQUESTS_LOGS_DIR . '/request-' . $id . '.log';

if (!file_exists($logFile)) {
throw new Exception('Log file does not exist');
}

if (!is_readable($logFile)) {
throw new Exception('Log file is not readable');
}

$content = file_get_contents($logFile);

if ($content === false) {
throw new Exception('Error while reading log file');
}

return $content;
}
}
52 changes: 44 additions & 8 deletions www/controllers/Websocket/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function authentication($conn, $message)
public function responseFromRequestId($conn, $message)
{
$info = 'Completed';
$summary = '';
$infoJson = null;

/**
* Retrieve request Id
Expand All @@ -91,15 +91,24 @@ public function responseFromRequestId($conn, $message)
* Retrieve JSON summary, if any
*/
if (!empty($message['response-to-request']['summary'])) {
$summary = json_encode($message['response-to-request']['summary']);
$infoJson = json_encode($message['response-to-request']['summary']);
}

/**
* Retrieve log, if any
*/
if (!empty($message['response-to-request']['log'])) {
if (!file_put_contents(WS_REQUESTS_LOGS_DIR . '/request-' . $requestId . '.log', $message['response-to-request']['log'])) {
$this->log('[conn #' . $conn->resourceId . '] Error while writing request #' . $requestId . ' log to file ' . WS_REQUESTS_LOGS_DIR . '/request-' . $requestId . '.log');
}
}

$this->log('[conn #' . $conn->resourceId . '] Sended response for request #' . $requestId . ' with status "' . $status . '"');

/**
* Update request status and response in database
*/
$this->hostController->updateWsRequest($requestId, $status, $info, $summary);
$this->hostController->updateWsRequest($requestId, $status, $info, $infoJson);

$this->layoutContainerStateController->update('hosts/overview');
$this->layoutContainerStateController->update('hosts/list');
Expand Down Expand Up @@ -142,6 +151,11 @@ public function requests($socket)
continue;
}

/**
* Retrieve host hostname, for better logs
*/
$hostname = $this->hostController->getHostnameById($request['Id_host']);

/**
* If target host is not authenticated (not in $clients), skip
*/
Expand All @@ -164,7 +178,7 @@ public function requests($socket)
$nextRetry = strtotime('+10 minutes');
}

$this->log('[server] Request #' . $request['Id'] . ' for host #' . $request['Id_host']. ' cannot be processed: host is not connected or not authenticated (retry ' . $request['Retry'] . '/3 - next retry ~' . date('H:i:s', $nextRetry) . ')');
$this->log('[server] Request #' . $request['Id'] . ' for host ' . $hostname . ' #' . $request['Id_host']. ' cannot be processed: host is not connected or not authenticated (retry ' . $request['Retry'] . '/3 - next retry ~' . date('H:i:s', $nextRetry) . ')');

/**
* Set new retry date in database and add an info message
Expand All @@ -175,7 +189,7 @@ public function requests($socket)
continue;
}

$this->log('[server] Request #' . $request['Id'] . ' for host #' . $request['Id_host']. ' cannot be processed: host is not connected or not authenticated (retry 3/3 - failed, will not retry)');
$this->log('[server] Request #' . $request['Id'] . ' for host ' . $hostname . ' #' . $request['Id_host']. ' cannot be processed: host is not connected or not authenticated (retry 3/3 - failed, will not retry)');

/**
* If all retries failed, update request status to 'failed' in database
Expand All @@ -193,16 +207,38 @@ public function requests($socket)
$hostWsConnectionId = $this->hostController->getWsConnectionIdByHostId($request['Id_host']);

/**
* Retrieve host hostname, for better logs
* If request is 'disconnect', close connection and remove it from database
*/
$hostname = $this->hostController->getHostnameById($request['Id_host']);
if ($request['Request'] == 'disconnect') {
foreach ($socket->getClients() as $client) {
if ($client->resourceId == $hostWsConnectionId) {
/**
* Send a message to the host to inform that the connection will be closed
*/
$client->send(json_encode(array('info' => 'You will now be disconnected from the server')));

/**
* Close connection
*/
$client->close();
$this->log('[server] Closed connection with host ' . $hostname . ' (connection #' . $client->resourceId . ') as requested by request #' . $request['Id']);
}
}

/**
* Delete the disconnect request from database
*/
$this->hostController->deleteWsRequest($request['Id']);
continue;
}

/**
* Any other request
* Send message to target host through websocket
*/
foreach ($socket->getClients() as $client) {
if ($client->resourceId == $hostWsConnectionId) {
$this->log('[server] Sending request #' . $request['Id'] . ' to ' . $hostname . ' through connection #' . $client->resourceId);
$this->log('[server] Sending request #' . $request['Id'] . ' to host ' . $hostname . ' through connection #' . $client->resourceId);

$client->send(json_encode(array('request-id' => $request['Id'], 'request' => $request['Request'])));

Expand Down
15 changes: 15 additions & 0 deletions www/controllers/ajax/host.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@
response(HTTP_OK, $content);
}

/**
* Show request log details
*/
if ($action == "getRequestLog" and !empty($_POST['id'])) {
$myhost = new \Controllers\Host();

try {
$content = $myhost->getRequestLog($_POST['id']);
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}

response(HTTP_OK, $content);
}

/**
* Cancel a request sent to a host
*/
Expand Down
48 changes: 45 additions & 3 deletions www/models/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -1471,25 +1471,53 @@ public function setHostInactive(string $hostId)
}

/**
* Reset les données d'un hôte
* Reset host data
*/
public function resetHost(string $hostId)
{
try {
/**
* Reset host general informations
*/
$stmt = $this->db->prepare("UPDATE hosts SET Os = null, Os_version = null, Profile = null, Env = null, Kernel = null, Arch = null WHERE id = :id");
$stmt->bindValue(':id', $hostId);
$stmt->execute();

/**
* On supprime toutes les tables dans cette base de données
* Retrieve all requests made to the host
*/
$stmt = $this->db->prepare("SELECT Id FROM ws_requests WHERE Id_host = :id");
$stmt->bindValue(':id', $hostId);
$result = $stmt->execute();

/**
* Delete all requests logs files
*/
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
if (file_exists(WS_REQUESTS_LOGS_DIR . '/request-' . $row['Id'] . '.log')) {
if (!unlink(WS_REQUESTS_LOGS_DIR . '/request-' . $row['Id'] . '.log')) {
throw new \Exception('Unable to delete request log file: ' . WS_REQUESTS_LOGS_DIR . '/request-' . $row['Id'] . '.log');
}
}
}

/**
* Delete all requests in ws_requests table
*/
$stmt = $this->db->prepare("DELETE FROM ws_requests WHERE Id_host = :id");
$stmt->bindValue(':id', $hostId);
$stmt->execute();

/**
* Delete all tables in host database
*/
$this->host_db->exec("DROP TABLE events");
$this->host_db->exec("DROP TABLE packages");
$this->host_db->exec("DROP TABLE packages_available");
$this->host_db->exec("DROP TABLE packages_history");

/**
* Puis on les re-génère à vide
* Then we regenerate them empty
*/
$this->host_db->generateHostTables();
} catch (\Exception $e) {
Expand Down Expand Up @@ -1863,4 +1891,18 @@ public function cancelWsRequest(int $id)
\Controllers\Common::dbError($e);
}
}

/**
* Delete websocket request from database
*/
public function deleteWsRequest(int $id)
{
try {
$stmt = $this->db->prepare("DELETE FROM ws_requests WHERE Id = :id");
$stmt->bindValue(':id', $id);
$stmt->execute();
} catch (\Exception $e) {
\Controllers\Common::dbError($e);
}
}
}
Loading

0 comments on commit 8154568

Please sign in to comment.