This commit is contained in:
Andras Bacsai 2023-05-15 13:45:37 +02:00
parent 845a8e5076
commit ec3ae7f6de
13 changed files with 280 additions and 173 deletions

View File

@ -2,34 +2,32 @@
namespace App\Actions\Proxy;
use App\Enums\ActivityTypes;
use App\Enums\ProxyTypes;
use App\Models\Server;
use Spatie\Activitylog\Models\Activity;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
class CheckProxySettingsInSync
{
public function __invoke(Server $server)
{
// @TODO What is the mechanism to make sure setting in sync?
$folder_name = match ($server->extra_attributes->proxy) {
ProxyTypes::TRAEFIK_V2->value => 'proxy',
};
$container_name = 'coolify-proxy';
$proxy_path = config('coolify.proxy_config_path');
$output = instantRemoteProcess([
// Folder exists, in ~/projects/<folder-name>
'if [ -d "projects/' . $folder_name . '" ]; then echo "true"; else echo "false"; fi',
// Container of name <container-name> is running
<<<EOT
[[ "$(docker inspect -f '{{.State.Running}}' $container_name 2>/dev/null)" == "true" ]] && echo "true" || echo "false"
EOT,
"cat $proxy_path/docker-compose.yml",
], $server, false);
if (is_null($output)) {
$final_output = Str::of(getProxyConfiguration($server))->trim();
} else {
$final_output = Str::of($output)->trim();
}
$docker_compose_yml_base64 = base64_encode($final_output);
$server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
if (is_null($output)) {
instantRemoteProcess([
"mkdir -p $proxy_path",
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
], $server);
return collect(
explode(PHP_EOL, $output)
)->every(fn ($output) => $output === 'true');
}
return $final_output;
}
}

View File

@ -5,103 +5,63 @@
use App\Enums\ActivityTypes;
use App\Enums\ProxyTypes;
use App\Models\Server;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
class InstallProxy
{
public Collection $networks;
public function __invoke(Server $server): Activity
{
$docker_compose_yml_base64 = base64_encode(
$this->getDockerComposeContents()
);
$proxy_path = config('coolify.proxy_config_path');
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
if ($networks->count() === 0) {
$this->networks = collect(['coolify']);
}
$create_networks_command = $this->networks->map(function ($network) {
return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
});
$configuration = instantRemoteProcess([
"cat $proxy_path/docker-compose.yml",
], $server, false);
if (is_null($configuration)) {
$configuration = Str::of(getProxyConfiguration($server))->trim();
} else {
$configuration = Str::of($configuration)->trim();
}
$docker_compose_yml_base64 = base64_encode($configuration);
$server->extra_attributes->last_applied_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
$env_file_base64 = base64_encode(
$this->getEnvContents()
);
$activity = remoteProcess([
"docker network ls --format '{{.Name}}' | grep '^coolify$' || docker network create coolify",
'mkdir -p projects',
'mkdir -p projects/proxy',
'mkdir -p projects/proxy/letsencrypt',
'cd projects/proxy',
"echo '$docker_compose_yml_base64' | base64 -d > docker-compose.yml",
"echo '$env_file_base64' | base64 -d > .env",
...$create_networks_command,
"echo 'Docker networks created...'",
"mkdir -p $proxy_path",
"cd $proxy_path",
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
"echo '$env_file_base64' | base64 -d > $proxy_path/.env",
"echo 'Docker compose file created...'",
"echo 'Pulling docker image...'",
'docker compose pull -q',
"echo 'Stopping proxy...'",
'docker compose down -v --remove-orphans',
"echo 'Starting proxy...'",
'docker compose up -d --remove-orphans',
'docker ps',
"echo 'Proxy installed successfully...'"
], $server, ActivityTypes::INLINE->value);
// Persist to Database
$server->extra_attributes->proxy = ProxyTypes::TRAEFIK_V2->value;
$server->save();
return $activity;
}
protected function getDockerComposeContents()
{
return Yaml::dump($this->getComposeData());
}
/**
* @return array
*/
protected function getComposeData(): array
{
$cwd = config('app.env') === 'local'
? config('proxy.project_path_on_host') . '/_testing_hosts/host_2_proxy'
: '.';
return [
"version" => "3.7",
"networks" => [
"coolify" => [
"external" => true,
],
],
"services" => [
"traefik" => [
"container_name" => "coolify-proxy",
"image" => "traefik:v2.10",
"restart" => "always",
"extra_hosts" => [
"host.docker.internal:host-gateway",
],
"networks" => [
"coolify",
],
"ports" => [
"80:80",
"443:443",
"8080:8080",
],
"volumes" => [
"/var/run/docker.sock:/var/run/docker.sock:ro",
"{$cwd}/letsencrypt:/letsencrypt",
"{$cwd}/traefik.auth:/auth/traefik.auth",
],
"command" => [
"--api.dashboard=true",
"--api.insecure=true",
"--entrypoints.http.address=:80",
"--entrypoints.https.address=:443",
"--providers.docker=true",
"--providers.docker.exposedbydefault=false",
],
"labels" => [
"traefik.enable=true",
"traefik.http.routers.traefik.entrypoints=http",
'traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_HOST}`)',
"traefik.http.routers.traefik.service=api@internal",
"traefik.http.services.traefik.loadbalancer.server.port=8080",
"traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
],
],
],
];
}
protected function getEnvContents()
{
$data = [

View File

@ -4,6 +4,7 @@
use App\Jobs\ContainerStatusJob;
use App\Jobs\DockerCleanupDanglingImagesJob;
use App\Jobs\ProxyCheckJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -16,6 +17,7 @@ protected function schedule(Schedule $schedule): void
{
$schedule->job(new ContainerStatusJob)->everyMinute();
$schedule->job(new DockerCleanupDanglingImagesJob)->everyMinute();
// $schedule->job(new ProxyCheckJob)->everyMinute();
}
/**

View File

@ -4,7 +4,8 @@
use App\Actions\Proxy\CheckProxySettingsInSync;
use App\Actions\Proxy\InstallProxy;
use App\Enums\ActivityTypes;
use App\Enums\ProxyTypes;
use Illuminate\Support\Str;
use App\Models\Server;
use Livewire\Component;
@ -12,34 +13,60 @@ class Proxy extends Component
{
public Server $server;
protected string $selectedProxy = '';
public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2;
public $is_proxy_installed;
public $is_check_proxy_complete = false;
public $is_proxy_settings_in_sync = false;
public $proxy_settings = null;
public function mount(Server $server)
{
$this->server = $server;
}
public function runInstallProxy()
public function installProxy()
{
$this->saveConfiguration($this->server);
$activity = resolve(InstallProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
}
public function proxyStatus()
{
$this->server->extra_attributes->proxy_status = checkContainerStatus(server: $this->server, container_id: 'coolify-proxy');
$this->server->save();
}
public function setProxy()
{
$this->server->extra_attributes->proxy_type = $this->selectedProxy->value;
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->save();
}
public function stopProxy()
{
instantRemoteProcess([
"docker rm -f coolify-proxy",
], $this->server);
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->save();
}
public function saveConfiguration()
{
try {
$proxy_path = config('coolify.proxy_config_path');
$this->proxy_settings = Str::of($this->proxy_settings)->trim();
$docker_compose_yml_base64 = base64_encode($this->proxy_settings);
$this->server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$this->server->save();
instantRemoteProcess([
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
], $this->server);
} catch (\Exception $e) {
return generalErrorHandler($e);
}
}
public function checkProxySettingsInSync()
{
$this->is_proxy_settings_in_sync = resolve(CheckProxySettingsInSync::class)($this->server);
try {
$this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server);
$this->is_check_proxy_complete = true;
} catch (\Exception $e) {
return generalErrorHandler($e);
}
public function render()
{
return view('livewire.server.proxy');
}
}

View File

@ -67,9 +67,7 @@ protected function checkContainerStatus()
return;
}
if ($application->destination->server) {
$container = instantRemoteProcess(["docker inspect --format '{{json .State}}' {$this->container_id}"], $application->destination->server);
$container = formatDockerCmdOutputToJson($container);
$application->status = $container[0]['Status'];
$application->status = checkContainerStatus(server: $application->destination->server, container_id: $this->container_id);
$application->save();
}
}

View File

@ -69,7 +69,6 @@ public function __construct(
protected function stopRunningContainer()
{
$this->executeNow([
"echo -n 'Removing old instance... '",
$this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"),
"echo 'Done.'",

45
app/Jobs/ProxyCheckJob.php Executable file
View File

@ -0,0 +1,45 @@
<?php
namespace App\Jobs;
use App\Actions\Proxy\InstallProxy;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ProxyCheckJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
}
/**
* Execute the job.
*/
public function handle()
{
try {
$container_name = 'coolify-proxy';
$configuration_path = config('coolify.proxy_config_path');
$servers = Server::whereRelation('settings', 'is_validated', true)->get();
foreach ($servers as $server) {
$status = checkContainerStatus(server: $server, container_id: $container_name);
if ($status === 'running') {
continue;
}
resolve(InstallProxy::class)($server);
}
} catch (\Throwable $th) {
//throw $th;
}
}
}

View File

@ -40,7 +40,7 @@ function generalErrorHandler(\Throwable $e, $that = null, $isJson = false)
'error' => $error->getMessage(),
]);
} else {
dump($error);
// dump($error);
}
}
}
@ -165,10 +165,9 @@ function instantRemoteProcess(array $command, Server $server, $throwError = true
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
if (!$throwError) {
return false;
return null;
}
Log::error($process->errorOutput());
throw new \RuntimeException('There was an error running the command.');
throw new \RuntimeException($process->errorOutput());
}
return $output;
}
@ -196,6 +195,7 @@ function generateRandomName()
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token\Builder;
use Symfony\Component\Yaml\Yaml;
if (!function_exists('generate_github_installation_token')) {
function generate_github_installation_token(GithubApp $source)
@ -244,3 +244,73 @@ function getParameters()
return Route::current()->parameters();
}
}
if (!function_exists('checkContainerStatus')) {
function checkContainerStatus(Server $server, string $container_id, bool $throwError = false)
{
$container = instantRemoteProcess(["docker inspect --format '{{json .State}}' {$container_id}"], $server, $throwError);
if (!$container) {
return 'exited';
}
$container = formatDockerCmdOutputToJson($container);
return $container[0]['Status'];
}
}
if (!function_exists('getProxyConfiguration')) {
function getProxyConfiguration(Server $server)
{
$proxy_config_path = config('coolify.proxy_config_path');
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
if ($networks->count() === 0) {
$networks = collect(['coolify']);
}
$array_of_networks = collect([]);
$networks->map(function ($network) use ($array_of_networks) {
$array_of_networks[$network] = [
"external" => true,
];
});
return Yaml::dump([
"version" => "3.8",
"networks" => $array_of_networks->toArray(),
"services" => [
"traefik" => [
"container_name" => "coolify-proxy", # Do not modify this! You will break everything!
"image" => "traefik:v2.10",
"restart" => "always",
"extra_hosts" => [
"host.docker.internal:host-gateway",
],
"networks" => $networks->toArray(), # Do not modify this! You will break everything!
"ports" => [
"80:80",
"443:443",
"8080:8080",
],
"volumes" => [
"/var/run/docker.sock:/var/run/docker.sock:ro",
"{$proxy_config_path}/letsencrypt:/letsencrypt", # Do not modify this! You will break everything!
"{$proxy_config_path}/traefik.auth:/auth/traefik.auth", # Do not modify this! You will break everything!
],
"command" => [
"--api.dashboard=true",
"--api.insecure=true",
"--entrypoints.http.address=:80",
"--entrypoints.https.address=:443",
"--providers.docker=true",
"--providers.docker.exposedbydefault=false",
],
"labels" => [
"traefik.enable=true", # Do not modify this! You will break everything!
"traefik.http.routers.traefik.entrypoints=http",
'traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_HOST}`)',
"traefik.http.routers.traefik.service=api@internal",
"traefik.http.services.traefik.loadbalancer.server.port=8080",
"traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
],
],
],
], 4, 2);
}
}

View File

@ -1,9 +1,8 @@
<?php
return [
'version' => '4.0.0-nightly.2',
'mux_enabled' => env('MUX_ENABLED', true),
'dev_webhook' => env('SERVEO_URL'),
'base_config_path' => env('BASE_CONFIG_PATH', '/data/coolify'),
'proxy_config_path' => env('BASE_CONFIG_PATH', '/data/coolify') . "/proxy",
];

View File

@ -1,4 +1,4 @@
@props(['proxy_settings'])
<div class="mt-4">
<label>
<div>Edit config file</div>
@ -45,4 +45,3 @@
<button>Update</button>
</label>
</div>

View File

@ -1,3 +0,0 @@
<div>
Problems!
</div>

View File

@ -1,5 +1,3 @@
@extends('errors::minimal')
@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))
<div>
You are lost. <a href="{{ route('dashboard') }}">Go home</a>
</div>

View File

@ -1,40 +1,55 @@
<div>
<div class="flex items-center gap-2">
<h2>Proxy</h2>
@if ($this->server->extra_attributes->proxy)
<div>
<div>
Proxy type: {{ $this->server->extra_attributes->proxy }}
<div>{{ $this->server->extra_attributes->proxy_status }}</div>
</div>
<div id="proxy_options" x-init="$wire.checkProxySettingsInSync()" class="relative w-fit">
{{-- Proxy is being checked against DB information --}}
@if (!$this->is_check_proxy_complete)
<x-proxy.loading />
@endif
@if ($this->is_check_proxy_complete && !$this->is_proxy_settings_in_sync)
<x-proxy.problems />
@else
<x-proxy.options />
@endif
</div>
</div>
@else
{{-- There is no Proxy installed --}}
No proxy installed.
@if ($this->server->extra_attributes->proxy_status !== 'running')
<select wire:model="selectedProxy">
<option value="{{ \App\Enums\ProxyTypes::TRAEFIK_V2 }}">
{{ \App\Enums\ProxyTypes::TRAEFIK_V2 }}
</option>
</select>
<button wire:click="runInstallProxy">Install Proxy</button>
<x-inputs.button isBold wire:click="setProxy">Set Proxy</x-inputs.button>
@endif
@if ($this->server->extra_attributes->proxy_type)
<div wire:poll="proxyStatus">
@if (
$this->server->extra_attributes->last_applied_proxy_settings &&
$this->server->extra_attributes->last_saved_proxy_settings !==
$this->server->extra_attributes->last_applied_proxy_settings)
<div class="text-red-500">Configuration out of sync.</div>
@endif
@if ($this->server->extra_attributes->proxy_status !== 'running')
<x-inputs.button isBold wire:click="installProxy">
Install
</x-inputs.button>
@endif
<x-inputs.button isBold wire:click="stopProxy">Stop</x-inputs.button>
<span x-data="{ showConfiguration: false }">
<x-inputs.button isBold x-on:click="showConfiguration = !showConfiguration">Show Configuration
</x-inputs.button>
<div class="pt-4">
<livewire:activity-monitor />
</div>
<template x-if="showConfiguration">
<div x-init="$wire.checkProxySettingsInSync" class="pt-4">
<h1>Proxy Configuration</h1>
<div wire:loading wire:target="checkProxySettingsInSync">
<x-proxy.loading />
</div>
@isset($this->proxy_settings)
<form wire:submit.prevent='saveConfiguration'>
<x-inputs.button isBold>Save</x-inputs.button>
<x-inputs.button x-on:click="showConfiguration = false" isBold
wire:click.prevent="installProxy">
Apply
</x-inputs.button>
<textarea wire:model.defer="proxy_settings" class="w-full" rows="30"></textarea>
</form>
@endisset
</div>
</template>
</span>
</div>
@endif
</div>