2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-08-21 16:00:12 +00:00
|
|
|
use App\Models\Application;
|
2023-05-24 12:26:50 +00:00
|
|
|
use App\Models\Server;
|
|
|
|
use Illuminate\Support\Collection;
|
2023-08-17 11:14:46 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-05-24 12:26:50 +00:00
|
|
|
|
2023-08-21 16:00:12 +00:00
|
|
|
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection {
|
|
|
|
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
|
|
|
|
if (!$containers) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
return format_docker_command_output_to_json($containers);
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:26:50 +00:00
|
|
|
function format_docker_command_output_to_json($rawOutput): Collection
|
|
|
|
{
|
|
|
|
$outputLines = explode(PHP_EOL, $rawOutput);
|
2023-08-21 16:00:12 +00:00
|
|
|
if (count($outputLines) === 1) {
|
|
|
|
$outputLines = collect($outputLines[0]);
|
|
|
|
} else {
|
|
|
|
$outputLines = collect($outputLines);
|
|
|
|
}
|
|
|
|
return $outputLines
|
2023-08-17 11:14:46 +00:00
|
|
|
->reject(fn ($line) => empty($line))
|
|
|
|
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-24 12:26:50 +00:00
|
|
|
function format_docker_labels_to_json($rawOutput): Collection
|
|
|
|
{
|
|
|
|
$outputLines = explode(PHP_EOL, $rawOutput);
|
|
|
|
|
|
|
|
return collect($outputLines)
|
2023-08-17 11:14:46 +00:00
|
|
|
->reject(fn ($line) => empty($line))
|
2023-05-24 12:26:50 +00:00
|
|
|
->map(function ($outputLine) {
|
|
|
|
$outputArray = explode(',', $outputLine);
|
|
|
|
return collect($outputArray)
|
|
|
|
->map(function ($outputLine) {
|
|
|
|
return explode('=', $outputLine);
|
|
|
|
})
|
|
|
|
->mapWithKeys(function ($outputLine) {
|
|
|
|
return [$outputLine[0] => $outputLine[1]];
|
|
|
|
});
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:13:53 +00:00
|
|
|
function format_docker_envs_to_json($rawOutput)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$outputLines = json_decode($rawOutput, true, flags: JSON_THROW_ON_ERROR);
|
|
|
|
return collect(data_get($outputLines[0], 'Config.Env', []))->mapWithKeys(function ($env) {
|
|
|
|
$env = explode('=', $env);
|
|
|
|
return [$env[0] => $env[1]];
|
|
|
|
});
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:00:12 +00:00
|
|
|
function getApplicationContainerStatus(Application $application) {
|
2023-08-28 08:44:11 +00:00
|
|
|
$server = data_get($application,'destination.server');
|
2023-08-21 16:00:12 +00:00
|
|
|
$id = $application->id;
|
2023-08-28 08:44:11 +00:00
|
|
|
if (!$server) {
|
|
|
|
return 'exited';
|
|
|
|
}
|
2023-08-21 16:00:12 +00:00
|
|
|
$containers = getCurrentApplicationContainerStatus($server, $id);
|
|
|
|
if ($containers->count() > 0) {
|
|
|
|
$status = data_get($containers[0], 'State', 'exited');
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
return 'exited';
|
|
|
|
}
|
|
|
|
function getContainerStatus(Server $server, string $container_id, bool $all_data = false, bool $throwError = false)
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-08-21 16:00:12 +00:00
|
|
|
// check_server_connection($server);
|
2023-06-02 13:15:12 +00:00
|
|
|
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
|
2023-05-24 12:26:50 +00:00
|
|
|
if (!$container) {
|
|
|
|
return 'exited';
|
|
|
|
}
|
|
|
|
$container = format_docker_command_output_to_json($container);
|
2023-06-02 13:15:12 +00:00
|
|
|
if ($all_data) {
|
2023-08-24 19:42:47 +00:00
|
|
|
return $container[0];
|
2023-06-02 13:15:12 +00:00
|
|
|
}
|
2023-08-17 11:14:46 +00:00
|
|
|
return data_get($container[0], 'State.Status', 'exited');
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-05-30 13:52:17 +00:00
|
|
|
|
2023-08-21 16:00:12 +00:00
|
|
|
function generateApplicationContainerName(string $uuid, int $pull_request_id = 0)
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
2023-08-21 16:00:12 +00:00
|
|
|
$now = now()->format('YmdHis');
|
|
|
|
if ($pull_request_id !== 0 && $pull_request_id !== null) {
|
|
|
|
return $uuid . '-pr-' . $pull_request_id . '-' . $now;
|
2023-05-30 13:52:17 +00:00
|
|
|
} else {
|
2023-08-21 16:00:12 +00:00
|
|
|
return $uuid . '-' . $now;
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-11 20:41:47 +00:00
|
|
|
function get_port_from_dockerfile($dockerfile): int
|
|
|
|
{
|
2023-08-17 11:14:46 +00:00
|
|
|
$dockerfile_array = explode("\n", $dockerfile);
|
|
|
|
$found_exposed_port = null;
|
|
|
|
foreach ($dockerfile_array as $line) {
|
|
|
|
$line_str = Str::of($line)->trim();
|
|
|
|
if ($line_str->startsWith('EXPOSE')) {
|
|
|
|
$found_exposed_port = $line_str->replace('EXPOSE', '')->trim();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($found_exposed_port) {
|
|
|
|
return (int)$found_exposed_port->value();
|
2023-08-11 20:41:47 +00:00
|
|
|
}
|
2023-08-17 11:14:46 +00:00
|
|
|
return 80;
|
2023-08-11 20:41:47 +00:00
|
|
|
}
|