This commit is contained in:
2025-09-03 17:21:27 +02:00
parent d5ecf0da96
commit b99533ced8
6 changed files with 19 additions and 86 deletions

View File

@ -16,7 +16,6 @@ require __DIR__ . '/../vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
$domain = $_ENV['MAIN_COOKIE_DOMAIN'] ?? '.lxdapp.local';
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
@ -33,7 +32,6 @@ if (session_status() === PHP_SESSION_NONE) {
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(true); // Enable autowiring globally
// Add settings
$settings = require __DIR__ . '/../src/Settings/Settings.php';
$settings($containerBuilder);
@ -58,7 +56,6 @@ $app->add(CorsMiddleware::class);
// Register middleware
(require __DIR__ . '/../src/Bootstrap/Middleware.php')($app);
// Register routes
// API contianer proxy route

View File

@ -2,7 +2,6 @@
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\BodyParsingMiddleware;
return function (App $app) {
// Add body parsing middleware (for JSON, form, etc.)

View File

@ -82,11 +82,10 @@ class LoginController
]);
}
$ip = $lxd->getContainerIP($name);
$nginx = $lxd->getContainerServiceStatus($name, 'nginx');
$mysql = $lxd->getContainerServiceStatus($name, 'mariadb');
if ($ip && $nginx === 'active' && $mysql === 'active') {
if ($nginx === 'active' && $mysql === 'active') {
// ---- CHANGED: do NOT return fields/creds here ----
return $this->json($response, [
'status' => 'ready',

View File

@ -1,47 +0,0 @@
<?php
require __DIR__ . '/../../vendor/autoload.php'; // Adjust path as needed
use App\Services\LxdService;
// Initialize LXD service
$lxdService = new LxdService();
// Define the directory containing access logs
$logDir = $_ENV['STATEDIR'] ?? "/var/www/html/lxd-app/api/public/last-access-logs";
// Define the idle threshold in minutes
$thresholdMinutes = 30;
// Iterate over all log files in the specified directory
foreach (glob($logDir . '/*.txt') as $filePath) {
// Extract the container name from the file name
$containerName = basename($filePath, '.txt');
// Get the last modified time of the file
$lastModified = filemtime($filePath);
if (!$lastModified) {
echo "Failed to get modification time for $containerName.\n";
continue;
}
$now = time();
$interval = $now - $lastModified;
// Check if the container has been idle for longer than the threshold
if ($interval > $thresholdMinutes * 60) {
echo "$containerName has been idle for over $thresholdMinutes minutes. Stopping...\n";
try {
// Check if the container exists and stop it if it does
if ($lxdService->containerExists($containerName)) {
$lxdService->stopContainer($containerName);
echo "Stopped container: $containerName\n";
} else {
echo "Container $containerName does not exist.\n";
}
} catch (Throwable $e) {
echo "Error stopping $containerName: " . $e->getMessage() . "\n";
}
}
}

View File

@ -10,23 +10,8 @@ class LxdService
$this->baseUrl = $_ENV['LXD_API_URL'] ?? 'https://localhost:8443';
}
/**
* Sends an HTTP request to the LXD API.
*
* @param string $method HTTP method (GET, POST, PUT, etc.)
* @param string $endpoint API endpoint
* @param array $body Request body (optional)
* @return array Response from the API
* @throws Exception
*/
private function request(string $method, string $endpoint, array $body = []): array {
// if (!isset($_ENV['LXD_CLIENT_CERT'], $_ENV['LXD_CLIENT_KEY'])) {
// throw new \Exception("LXD_CLIENT_CERT and LXD_CLIENT_KEY must be set in .env");
// }
$ch = curl_init("{$this->baseUrl}{$endpoint}");
private function curlInit($url, $method = '') {
$ch = curl_init($url);
// Paths to client certificate and key for TLS authentication
$clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt';
@ -38,13 +23,25 @@ class LxdService
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
return $ch;
}
/**
* Sends an HTTP request to the LXD API.
*
* @param string $method HTTP method (GET, POST, PUT, etc.)
* @param string $endpoint API endpoint
* @param array $body Request body (optional)
* @return array Response from the API
* @throws Exception
*/
private function request(string $method, string $endpoint, array $body = []): array {
$ch = $this->curlInit("{$this->baseUrl}{$endpoint}", $method);
if (!empty($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
@ -77,17 +74,7 @@ class LxdService
*/
private function requestRaw(string $endpoint): string {
$url = "{$this->baseUrl}{$endpoint}";
$ch = curl_init($url);
$clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt';
$clientKey = $_ENV['LXD_CLIENT_KEY'] ?? '/etc/ssl/lxdapp/client.key';
curl_setopt($ch, CURLOPT_SSLCERT, $clientCert);
curl_setopt($ch, CURLOPT_SSLKEY, $clientKey);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$ch = $this->curlInit($url);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

View File

@ -41,8 +41,6 @@ if ($state !== 'Running') {
}
// === Get container IP ===
$ip = $lxd->getContainerIP($container);
$nginx = $lxd->getContainerServiceStatus($container, 'nginx');
$mysql = $lxd->getContainerServiceStatus($container, 'mariadb');