This commit is contained in:
Andras Bacsai 2023-06-20 20:19:31 +02:00
parent 9f0ca1cc2e
commit 3bdea4c931
15 changed files with 50 additions and 46 deletions

View File

@ -20,7 +20,7 @@ public function __invoke(Server $server, bool $reset = false)
$final_output = Str::of($output)->trim()->value;
}
$docker_compose_yml_base64 = base64_encode($final_output);
$server->extra_attributes->proxy_last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
if (is_null($output) || $reset) {
instant_remote_process([

View File

@ -12,9 +12,9 @@ class InstallProxy
{
public function __invoke(Server $server): Activity
{
if (is_null(data_get($server, 'extra_attributes.proxy_type'))) {
$server->extra_attributes->proxy_type = ProxyTypes::TRAEFIK_V2->value;
$server->extra_attributes->proxy_status = ProxyStatus::EXITED->value;
if (is_null(data_get($server, 'proxy.type'))) {
$server->proxy->type = ProxyTypes::TRAEFIK_V2->value;
$server->proxy->status = ProxyStatus::EXITED->value;
$server->save();
}
$proxy_path = config('coolify.proxy_config_path');
@ -38,7 +38,7 @@ public function __invoke(Server $server): Activity
$configuration = Str::of($configuration)->trim()->value;
}
$docker_compose_yml_base64 = base64_encode($configuration);
$server->extra_attributes->proxy_last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
$activity = remote_process([
"echo 'Creating required Docker networks...'",

View File

@ -9,8 +9,8 @@
class ServerMetadata extends Data
{
public function __construct(
public ?ProxyTypes $proxy_type,
public ?ProxyStatus $proxy_status
public ?ProxyTypes $type,
public ?ProxyStatus $status
) {
}
}

View File

@ -23,14 +23,14 @@ public function serverValidated()
}
public function switchProxy()
{
$this->server->extra_attributes->proxy_type = null;
$this->server->proxy->type = null;
$this->server->save();
}
public function installProxy()
{
if (
$this->server->extra_attributes->proxy_last_applied_settings &&
$this->server->extra_attributes->proxy_last_saved_settings !== $this->server->extra_attributes->proxy_last_applied_settings
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
$this->saveConfiguration($this->server);
}
@ -40,8 +40,8 @@ public function installProxy()
public function setProxy(string $proxy_type)
{
$this->server->extra_attributes->proxy_type = $proxy_type;
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->proxy->type = $proxy_type;
$this->server->proxy->status = 'exited';
$this->server->save();
}
public function stopProxy()
@ -49,7 +49,7 @@ public function stopProxy()
instant_remote_process([
"docker rm -f coolify-proxy",
], $this->server);
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->proxy->status = 'exited';
$this->server->save();
}
public function saveConfiguration(Server $server)
@ -58,7 +58,7 @@ public function saveConfiguration(Server $server)
$proxy_path = config('coolify.proxy_config_path');
$this->proxy_settings = Str::of($this->proxy_settings)->trim()->value;
$docker_compose_yml_base64 = base64_encode($this->proxy_settings);
$server->extra_attributes->proxy_last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
instant_remote_process([
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",

View File

@ -19,8 +19,8 @@ public function proxyStatusUpdated()
public function deploy()
{
if (
$this->server->extra_attributes->proxy_last_applied_settings &&
$this->server->extra_attributes->proxy_last_saved_settings !== $this->server->extra_attributes->proxy_last_applied_settings
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
) {
$this->saveConfiguration($this->server);
}
@ -32,7 +32,7 @@ public function stop()
instant_remote_process([
"docker rm -f coolify-proxy",
], $this->server);
$this->server->extra_attributes->proxy_status = 'exited';
$this->server->proxy->status = 'exited';
$this->server->save();
$this->emit('proxyStatusUpdated');
}

View File

@ -29,7 +29,7 @@ public function handle()
{
try {
$container_name = 'coolify-proxy';
$servers = Server::whereRelation('settings', 'is_reachable', true)->where('extra_attributes->proxy_type', ProxyTypes::TRAEFIK_V2)->get();
$servers = Server::whereRelation('settings', 'is_reachable', true)->where('proxy->type', ProxyTypes::TRAEFIK_V2)->get();
foreach ($servers as $server) {
$status = get_container_status(server: $server, container_id: $container_name);

View File

@ -3,8 +3,6 @@
namespace App\Jobs;
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
@ -12,8 +10,8 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Str;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Str;
class ProxyContainerStatusJob implements ShouldQueue, ShouldBeUnique
{
@ -39,13 +37,13 @@ public function handle(): void
try {
$container = get_container_status(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: true);
$status = $container['State']['Status'];
if ($this->server->extra_attributes->proxy_status !== $status) {
$this->server->extra_attributes->proxy_status = $status;
if ($this->server->extra_attributes->proxy_status === 'running') {
if ($this->server->proxy->status !== $status) {
$this->server->proxy->status = $status;
if ($this->server->proxy->status === 'running') {
$traefik = $container['Config']['Labels']['org.opencontainers.image.title'];
$version = $container['Config']['Labels']['org.opencontainers.image.version'];
if (isset($version) && isset($traefik) && $traefik === 'Traefik' && Str::of($version)->startsWith('v2')) {
$this->server->extra_attributes->proxy_type = ProxyTypes::TRAEFIK_V2->value;
$this->server->proxy->type = ProxyTypes::TRAEFIK_V2->value;
}
}
$this->server->save();

View File

@ -4,9 +4,17 @@
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
class Server extends BaseModel
{
use SchemalessAttributesTrait;
protected $schemalessAttributes = [
'proxy',
];
public $casts = [
'proxy' => SchemalessAttributes::class,
];
protected static function booted()
{
static::created(function ($server) {
@ -25,16 +33,14 @@ protected static function booted()
'port',
'team_id',
'private_key_id',
'extra_attributes',
'proxy',
];
public $casts = [
'extra_attributes' => SchemalessAttributes::class,
];
public function scopeWithExtraAttributes(): Builder
public function scopeWithProxy(): Builder
{
return $this->extra_attributes->modelScope();
return $this->proxy->modelScope();
}
public function isEmpty()
{

View File

@ -21,7 +21,7 @@ public function up(): void
$table->string('user')->default('root');
$table->foreignId('team_id');
$table->foreignId('private_key_id');
$table->schemalessAttributes('extra_attributes');
$table->schemalessAttributes('proxy');
$table->timestamps();
});
}

View File

@ -77,9 +77,9 @@ public function run(): void
'team_id' => 0,
'private_key_id' => 0
];
$server_details['extra_attributes'] = ServerMetadata::from([
'proxy_type' => ProxyTypes::TRAEFIK_V2->value,
'proxy_status' => ProxyStatus::EXITED->value
$server_details['proxy'] = ServerMetadata::from([
'type' => ProxyTypes::TRAEFIK_V2->value,
'status' => ProxyStatus::EXITED->value
]);
$server = Server::create($server_details);
$server->settings->is_reachable = true;

View File

@ -28,9 +28,9 @@ public function run(): void
'ip' => "coolify-testing-host",
'team_id' => $root_team->id,
'private_key_id' => $private_key_1->id,
'extra_attributes' => ServerMetadata::from([
'proxy_type' => ProxyTypes::TRAEFIK_V2->value,
'proxy_status' => ProxyStatus::EXITED->value
'proxy' => ServerMetadata::from([
'type' => ProxyTypes::TRAEFIK_V2->value,
'status' => ProxyStatus::EXITED->value
]),
]);
Server::create([

View File

@ -2,7 +2,7 @@
<x-naked-modal show="stopProxy" action="stopProxy" title="Stop Proxy"
message='This proxy will be stopped. It is not reversible. <br>All resources will be unavailable. <br>Please think again.' />
@if ($server->settings->is_reachable)
@if ($server->extra_attributes->proxy_type)
@if ($server->proxy->type)
<div x-init="$wire.checkProxySettingsInSync">
<div wire:loading wire:target="checkProxySettingsInSync">
<x-loading />
@ -13,15 +13,15 @@
<div class="flex items-center gap-2">
<h2>Proxy</h2>
<x-forms.button type="submit">Save</x-forms.button>
@if ($server->extra_attributes->proxy_status === 'exited')
@if ($server->proxy->status === 'exited')
<x-forms.button wire:click.prevent="switchProxy">Switch Proxy</x-forms.button>
@endif
<livewire:server.proxy.status :server="$server" />
</div>
<div class="pt-3 pb-4 ">Traefik v2</div>
@if (
$server->extra_attributes->proxy_last_applied_settings &&
$server->extra_attributes->proxy_last_saved_settings !== $server->extra_attributes->proxy_last_applied_settings)
$server->proxy->last_applied_settings &&
$server->proxy->last_saved_settings !== $server->proxy->last_applied_settings)
<div class="text-red-500 ">Configuration out of sync. Restart to get the new configs.
</div>
@endif

View File

@ -1,6 +1,6 @@
<div>
@if ($server->settings->is_reachable)
@if ($server->extra_attributes->proxy_status === 'running')
@if ($server->proxy->status === 'running')
<div class="flex gap-4">
<div class="group">
<label tabindex="0" class="flex items-center gap-2 cursor-pointer hover:text-white"> Links

View File

@ -1,9 +1,9 @@
<div>
@if ($server->settings->is_reachable)
<div wire:poll.10000ms="proxyStatus" x-init="$wire.proxyStatus">
@if ($server->extra_attributes->proxy_status === 'running')
@if ($server->proxy->status === 'running')
<x-status.running />
@elseif ($server->extra_attributes->proxy_status === 'restarting')
@elseif ($server->proxy->status === 'restarting')
<x-status.restarting />
@else
<x-status.stopped />

View File

@ -71,7 +71,7 @@
'server' => Server::ownedByCurrentTeam(['name', 'description', 'ip', 'port', 'user'])->whereUuid(request()->server_uuid)->firstOrFail(),
]))->name('server.show');
Route::get('/server/{server_uuid}/proxy', fn () => view('server.proxy', [
'server' => Server::ownedByCurrentTeam(['name', 'extra_attributes'])->whereUuid(request()->server_uuid)->firstOrFail(),
'server' => Server::ownedByCurrentTeam(['name', 'proxy'])->whereUuid(request()->server_uuid)->firstOrFail(),
]))->name('server.proxy');
Route::get('/server/{server_uuid}/private-key', fn () => view('server.private-key', [
'server' => Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->firstOrFail(),