forked from urvishpatelce/lxd-app
fix
This commit is contained in:
@ -16,7 +16,6 @@ require __DIR__ . '/../vendor/autoload.php';
|
|||||||
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
|
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
$domain = $_ENV['MAIN_COOKIE_DOMAIN'] ?? '.lxdapp.local';
|
|
||||||
session_set_cookie_params([
|
session_set_cookie_params([
|
||||||
'lifetime' => 0,
|
'lifetime' => 0,
|
||||||
'path' => '/',
|
'path' => '/',
|
||||||
@ -33,7 +32,6 @@ if (session_status() === PHP_SESSION_NONE) {
|
|||||||
$containerBuilder = new ContainerBuilder();
|
$containerBuilder = new ContainerBuilder();
|
||||||
$containerBuilder->useAutowiring(true); // Enable autowiring globally
|
$containerBuilder->useAutowiring(true); // Enable autowiring globally
|
||||||
|
|
||||||
|
|
||||||
// Add settings
|
// Add settings
|
||||||
$settings = require __DIR__ . '/../src/Settings/Settings.php';
|
$settings = require __DIR__ . '/../src/Settings/Settings.php';
|
||||||
$settings($containerBuilder);
|
$settings($containerBuilder);
|
||||||
@ -58,7 +56,6 @@ $app->add(CorsMiddleware::class);
|
|||||||
// Register middleware
|
// Register middleware
|
||||||
(require __DIR__ . '/../src/Bootstrap/Middleware.php')($app);
|
(require __DIR__ . '/../src/Bootstrap/Middleware.php')($app);
|
||||||
|
|
||||||
|
|
||||||
// Register routes
|
// Register routes
|
||||||
|
|
||||||
// API contianer proxy route
|
// API contianer proxy route
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Middleware\ErrorMiddleware;
|
use Slim\Middleware\ErrorMiddleware;
|
||||||
use Slim\Middleware\BodyParsingMiddleware;
|
|
||||||
|
|
||||||
return function (App $app) {
|
return function (App $app) {
|
||||||
// Add body parsing middleware (for JSON, form, etc.)
|
// Add body parsing middleware (for JSON, form, etc.)
|
||||||
|
|||||||
@ -82,11 +82,10 @@ class LoginController
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ip = $lxd->getContainerIP($name);
|
|
||||||
$nginx = $lxd->getContainerServiceStatus($name, 'nginx');
|
$nginx = $lxd->getContainerServiceStatus($name, 'nginx');
|
||||||
$mysql = $lxd->getContainerServiceStatus($name, 'mariadb');
|
$mysql = $lxd->getContainerServiceStatus($name, 'mariadb');
|
||||||
|
|
||||||
if ($ip && $nginx === 'active' && $mysql === 'active') {
|
if ($nginx === 'active' && $mysql === 'active') {
|
||||||
// ---- CHANGED: do NOT return fields/creds here ----
|
// ---- CHANGED: do NOT return fields/creds here ----
|
||||||
return $this->json($response, [
|
return $this->json($response, [
|
||||||
'status' => 'ready',
|
'status' => 'ready',
|
||||||
|
|||||||
@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -10,23 +10,8 @@ class LxdService
|
|||||||
$this->baseUrl = $_ENV['LXD_API_URL'] ?? 'https://localhost:8443';
|
$this->baseUrl = $_ENV['LXD_API_URL'] ?? 'https://localhost:8443';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function curlInit($url, $method = '') {
|
||||||
|
$ch = curl_init($url);
|
||||||
/**
|
|
||||||
* 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}");
|
|
||||||
|
|
||||||
// Paths to client certificate and key for TLS authentication
|
// Paths to client certificate and key for TLS authentication
|
||||||
$clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt';
|
$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_VERIFYPEER, false);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
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)) {
|
if (!empty($body)) {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||||
|
|
||||||
@ -77,17 +74,7 @@ class LxdService
|
|||||||
*/
|
*/
|
||||||
private function requestRaw(string $endpoint): string {
|
private function requestRaw(string $endpoint): string {
|
||||||
$url = "{$this->baseUrl}{$endpoint}";
|
$url = "{$this->baseUrl}{$endpoint}";
|
||||||
$ch = curl_init($url);
|
$ch = $this->curlInit($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');
|
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||||
|
|||||||
@ -41,8 +41,6 @@ if ($state !== 'Running') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// === Get container IP ===
|
// === Get container IP ===
|
||||||
$ip = $lxd->getContainerIP($container);
|
|
||||||
|
|
||||||
$nginx = $lxd->getContainerServiceStatus($container, 'nginx');
|
$nginx = $lxd->getContainerServiceStatus($container, 'nginx');
|
||||||
$mysql = $lxd->getContainerServiceStatus($container, 'mariadb');
|
$mysql = $lxd->getContainerServiceStatus($container, 'mariadb');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user