From 0f28acac008c8d4b7a747fedced53b5f243f6538 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 2 Jun 2023 15:15:12 +0200 Subject: [PATCH] save --- .../Proxy/CheckProxySettingsInSync.php | 2 +- app/Actions/Proxy/InstallProxy.php | 27 +---- app/Http/Livewire/ActivityMonitor.php | 5 +- .../Livewire/Project/Application/Previews.php | 4 +- app/Http/Livewire/Server/Form.php | 1 - app/Http/Livewire/Server/Proxy.php | 30 ++--- app/Http/Livewire/Server/Proxy/Deploy.php | 43 +++++++ app/Http/Livewire/Server/Proxy/Status.php | 29 +++++ ....php => ApplicationContainerStatusJob.php} | 33 +---- app/Jobs/ApplicationDeploymentJob.php | 2 +- app/Jobs/ProxyContainerStatusJob.php | 57 +++++++++ app/Models/Server.php | 1 + bootstrap/helpers/docker.php | 9 +- database/seeders/ServerSeeder.php | 5 +- database/seeders/ServerSettingSeeder.php | 2 +- .../components/applications/navbar.blade.php | 3 +- .../views/components/forms/textarea.blade.php | 3 +- resources/views/components/loading.blade.php | 2 +- .../views/components/server/navbar.blade.php | 4 +- .../views/livewire/activity-monitor.blade.php | 6 + .../project/application/deployments.blade.php | 114 +++++++++--------- .../project/application/status.blade.php | 6 +- .../views/livewire/server/form.blade.php | 29 +++-- .../views/livewire/server/proxy.blade.php | 70 ++++------- .../livewire/server/proxy/deploy.blade.php | 44 +++++++ .../livewire/server/proxy/status.blade.php | 17 +++ resources/views/servers.blade.php | 22 ++-- routes/web.php | 4 +- 28 files changed, 346 insertions(+), 228 deletions(-) create mode 100644 app/Http/Livewire/Server/Proxy/Deploy.php create mode 100644 app/Http/Livewire/Server/Proxy/Status.php rename app/Jobs/{ContainerStatusJob.php => ApplicationContainerStatusJob.php} (51%) create mode 100644 app/Jobs/ProxyContainerStatusJob.php create mode 100644 resources/views/livewire/server/proxy/deploy.blade.php create mode 100644 resources/views/livewire/server/proxy/status.blade.php diff --git a/app/Actions/Proxy/CheckProxySettingsInSync.php b/app/Actions/Proxy/CheckProxySettingsInSync.php index 88c01d624..874dd830e 100644 --- a/app/Actions/Proxy/CheckProxySettingsInSync.php +++ b/app/Actions/Proxy/CheckProxySettingsInSync.php @@ -20,7 +20,7 @@ class CheckProxySettingsInSync $final_output = Str::of($output)->trim()->value; } $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->extra_attributes->proxy_last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; $server->save(); if (is_null($output) || $reset) { instant_remote_process([ diff --git a/app/Actions/Proxy/InstallProxy.php b/app/Actions/Proxy/InstallProxy.php index 8e447ae07..d0a1a8356 100644 --- a/app/Actions/Proxy/InstallProxy.php +++ b/app/Actions/Proxy/InstallProxy.php @@ -31,41 +31,24 @@ class InstallProxy $configuration = Str::of($configuration)->trim()->value; } $docker_compose_yml_base64 = base64_encode($configuration); - $server->extra_attributes->last_applied_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; + $server->extra_attributes->proxy_last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; $server->save(); - - // $env_file_base64 = base64_encode( - // $this->getEnvContents() - // ); $activity = remote_process([ + "echo 'Creating required Docker networks...'", ...$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 'Creating Docker Compose file...'", "echo 'Pulling docker image...'", 'docker compose pull -q', - "echo 'Stopping proxy...'", + "echo 'Stopping old proxy...'", 'docker compose down -v --remove-orphans', - "echo 'Starting proxy...'", + "echo 'Starting new proxy...'", 'docker compose up -d --remove-orphans', "echo 'Proxy installed successfully...'" ], $server); return $activity; } - - // protected function getEnvContents() - // { - // $data = [ - // 'LETS_ENCRYPT_EMAIL' => '', - // ]; - - // return collect($data) - // ->map(fn ($v, $k) => "{$k}={$v}") - // ->push(PHP_EOL) - // ->implode(PHP_EOL); - // } } diff --git a/app/Http/Livewire/ActivityMonitor.php b/app/Http/Livewire/ActivityMonitor.php index 47cd31c36..4352b9dc9 100644 --- a/app/Http/Livewire/ActivityMonitor.php +++ b/app/Http/Livewire/ActivityMonitor.php @@ -8,6 +8,7 @@ use Spatie\Activitylog\Models\Activity; class ActivityMonitor extends Component { + public bool $header = false; public $activityId; public $isPollingActive = false; @@ -50,8 +51,4 @@ class ActivityMonitor extends Component ]); $this->activity->save(); } - public function render() - { - return view('livewire.activity-monitor'); - } } diff --git a/app/Http/Livewire/Project/Application/Previews.php b/app/Http/Livewire/Project/Application/Previews.php index 5bbc87690..9e924c0c1 100644 --- a/app/Http/Livewire/Project/Application/Previews.php +++ b/app/Http/Livewire/Project/Application/Previews.php @@ -2,7 +2,7 @@ namespace App\Http\Livewire\Project\Application; -use App\Jobs\ContainerStatusJob; +use App\Jobs\ApplicationContainerStatusJob; use App\Models\Application; use App\Models\ApplicationPreview; use Illuminate\Support\Collection; @@ -24,7 +24,7 @@ class Previews extends Component } public function loadStatus($pull_request_id) { - dispatch(new ContainerStatusJob( + dispatch(new ApplicationContainerStatusJob( application: $this->application, container_name: generate_container_name($this->application->uuid, $pull_request_id), pull_request_id: $pull_request_id diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php index 83d7567f5..21fc9b281 100644 --- a/app/Http/Livewire/Server/Form.php +++ b/app/Http/Livewire/Server/Form.php @@ -30,7 +30,6 @@ class Form extends Component public function installDocker() { $activity = resolve(InstallDocker::class)($this->server); - $this->emit('newMonitorActivity', $activity->id); } public function validateServer() diff --git a/app/Http/Livewire/Server/Proxy.php b/app/Http/Livewire/Server/Proxy.php index 8c637e1fe..59eb81635 100644 --- a/app/Http/Livewire/Server/Proxy.php +++ b/app/Http/Livewire/Server/Proxy.php @@ -11,25 +11,21 @@ use Livewire\Component; class Proxy extends Component { - protected $listeners = ['serverValidated']; public Server $server; public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2; public $proxy_settings = null; - public function mount() - { - $this->proxyStatus(); - } + protected $listeners = ['serverValidated', 'saveConfiguration']; public function serverValidated() { - $this->server->settings->refresh(); + $this->server->refresh(); } public function installProxy() { 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 + $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->saveConfiguration($this->server); } @@ -37,15 +33,9 @@ class Proxy extends Component $this->emit('newMonitorActivity', $activity->id); } - public function proxyStatus() + public function setProxy(string $proxy_type) { - $this->server->extra_attributes->proxy_status = get_container_status(server: $this->server, container_id: 'coolify-proxy'); - $this->server->save(); - $this->server->refresh(); - } - public function setProxy() - { - $this->server->extra_attributes->proxy_type = $this->selectedProxy->value; + $this->server->extra_attributes->proxy_type = $proxy_type; $this->server->extra_attributes->proxy_status = 'exited'; $this->server->save(); } @@ -57,17 +47,17 @@ class Proxy extends Component $this->server->extra_attributes->proxy_status = 'exited'; $this->server->save(); } - public function saveConfiguration() + public function saveConfiguration(Server $server) { try { $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); - $this->server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; - $this->server->save(); + $server->extra_attributes->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", - ], $this->server); + ], $server); } catch (\Exception $e) { return general_error_handler($e); } diff --git a/app/Http/Livewire/Server/Proxy/Deploy.php b/app/Http/Livewire/Server/Proxy/Deploy.php new file mode 100644 index 000000000..58f5e5851 --- /dev/null +++ b/app/Http/Livewire/Server/Proxy/Deploy.php @@ -0,0 +1,43 @@ + 'proxyStatusUpdated']; + public function proxyStatusUpdated() + { + $this->server->refresh(); + } + 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->saveConfiguration($this->server); + } + $activity = resolve(InstallProxy::class)($this->server); + $this->emit('newMonitorActivity', $activity->id); + } + public function stop() + { + instant_remote_process([ + "docker rm -f coolify-proxy", + ], $this->server); + $this->server->extra_attributes->proxy_status = 'exited'; + $this->server->save(); + $this->emit('proxyStatusUpdated'); + } + private function saveConfiguration(Server $server) + { + $this->emit('saveConfiguration', $server); + } +} diff --git a/app/Http/Livewire/Server/Proxy/Status.php b/app/Http/Livewire/Server/Proxy/Status.php new file mode 100644 index 000000000..513e90b9b --- /dev/null +++ b/app/Http/Livewire/Server/Proxy/Status.php @@ -0,0 +1,29 @@ + 'proxyStatusUpdated']; + public function proxyStatusUpdated() + { + ray('Status: ' . $this->server->extra_attributes->proxy_status); + $this->server->refresh(); + } + public function proxyStatus() + { + try { + dispatch(new ProxyContainerStatusJob( + server: $this->server + )); + $this->emit('proxyStatusUpdated'); + } catch (\Exception $e) { + ray($e->getMessage()); + } + } +} diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ApplicationContainerStatusJob.php similarity index 51% rename from app/Jobs/ContainerStatusJob.php rename to app/Jobs/ApplicationContainerStatusJob.php index 9ff18f9ba..78145eb1c 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ApplicationContainerStatusJob.php @@ -13,7 +13,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; -class ContainerStatusJob implements ShouldQueue, ShouldBeUnique +class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -35,7 +35,6 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeUnique { try { $status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false); - ray('Container ' . $this->container_name . ' statuus is ' . $status); if ($this->pull_request_id) { $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id); $preview->status = $status; @@ -55,34 +54,4 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeUnique $this->application->save(); } } - // protected function check_all_servers() - // { - // $servers = Server::all()->reject(fn (Server $server) => $server->settings->is_build_server); - // $applications = Application::all(); - // $not_found_applications = $applications; - // $containers = collect(); - // foreach ($servers as $server) { - // $output = instant_remote_process(['docker ps -a -q --format \'{{json .}}\''], $server); - // $containers = $containers->concat(format_docker_command_output_to_json($output)); - // } - // foreach ($containers as $container) { - // $found_application = $applications->filter(function ($value, $key) use ($container) { - // return $value->uuid == $container['Names']; - // })->first(); - // if ($found_application) { - // $not_found_applications = $not_found_applications->filter(function ($value, $key) use ($found_application) { - // return $value->uuid != $found_application->uuid; - // }); - // $found_application->status = $container['State']; - // $found_application->save(); - // Log::info('Found application: ' . $found_application->uuid . '. Set status to: ' . $found_application->status); - // } - // } - // foreach ($not_found_applications as $not_found_application) { - // $not_found_application->status = 'exited'; - // $not_found_application->save(); - // Log::info('Not found application: ' . $not_found_application->uuid . '. Set status to: ' . $not_found_application->status); - // } - // } - } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index f77272657..296f5237b 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -277,7 +277,7 @@ COPY --from=$this->build_image_name /app/{$this->application->publish_directory} 'status' => $status, ]); } - dispatch(new ContainerStatusJob( + dispatch(new ApplicationContainerStatusJob( application: $this->application, container_name: $this->container_name, pull_request_id: $this->pull_request_id diff --git a/app/Jobs/ProxyContainerStatusJob.php b/app/Jobs/ProxyContainerStatusJob.php new file mode 100644 index 000000000..b9341f275 --- /dev/null +++ b/app/Jobs/ProxyContainerStatusJob.php @@ -0,0 +1,57 @@ +server->id)]; + } + public function __construct(Server $server) + { + $this->server = $server; + } + public function uniqueId(): int + { + return $this->server->id; + } + public function handle(): void + { + try { + $container = get_container_status(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: false); + $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') { + $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->save(); + } + } catch (\Exception $e) { + ray($e->getMessage()); + } + } +} diff --git a/app/Models/Server.php b/app/Models/Server.php index c6c183ef7..81d790fc6 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -58,6 +58,7 @@ class Server extends BaseModel { return Server::where('team_id', session('currentTeam')->id)->whereRelation('settings', 'is_validated', true)->get(); } + static public function destinations(string|null $server_uuid = null) { if ($server_uuid) { diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 0efd25c84..def788668 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -29,14 +29,17 @@ function format_docker_labels_to_json($rawOutput): Collection })[0]; } -function get_container_status(Server $server, string $container_id, bool $throwError = false) +function get_container_status(Server $server, string $container_id, bool $all_data = false, bool $throwError = false) { - $container = instant_remote_process(["docker inspect --format '{{json .State}}' {$container_id}"], $server, $throwError); + $container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError); if (!$container) { return 'exited'; } $container = format_docker_command_output_to_json($container); - return $container[0]['Status']; + if ($all_data) { + return $container[0]; + } + return $container[0]['State']['Status']; } function generate_container_name(string $uuid, int|null $pull_request_id = null) diff --git a/database/seeders/ServerSeeder.php b/database/seeders/ServerSeeder.php index 423c259f2..e60f7da7d 100644 --- a/database/seeders/ServerSeeder.php +++ b/database/seeders/ServerSeeder.php @@ -36,10 +36,7 @@ class ServerSeeder extends Seeder 'description' => "This is a test docker container", 'ip' => "coolify-testing-host-2", 'team_id' => $root_team->id, - 'private_key_id' => $private_key_1->id, - 'extra_attributes' => ServerMetadata::from([ - // - ]), + 'private_key_id' => $private_key_1->id ]); } } diff --git a/database/seeders/ServerSettingSeeder.php b/database/seeders/ServerSettingSeeder.php index e36edcc7a..c05e630a2 100644 --- a/database/seeders/ServerSettingSeeder.php +++ b/database/seeders/ServerSettingSeeder.php @@ -19,7 +19,7 @@ class ServerSettingSeeder extends Seeder $server_2->settings->save(); $server_3 = Server::find(2)->load(['settings']); - $server_3->settings->is_part_of_swarm = true; + $server_3->settings->is_part_of_swarm = false; $server_3->settings->is_validated = false; $server_3->settings->save(); } diff --git a/resources/views/components/applications/navbar.blade.php b/resources/views/components/applications/navbar.blade.php index 87caed2d7..fd6ba61e7 100644 --- a/resources/views/components/applications/navbar.blade.php +++ b/resources/views/components/applications/navbar.blade.php @@ -15,8 +15,8 @@ ]) }}"> +
- - diff --git a/resources/views/components/forms/textarea.blade.php b/resources/views/components/forms/textarea.blade.php index 3514c8856..60ebb525d 100644 --- a/resources/views/components/forms/textarea.blade.php +++ b/resources/views/components/forms/textarea.blade.php @@ -4,12 +4,11 @@ 'label' => $attributes->has('label'), 'helper' => $attributes->has('helper'), 'instantSave' => $attributes->has('instantSave'), - 'noLabel' => $attributes->has('noLabel'), 'noDirty' => $attributes->has('noDirty'), ])
- @if (!$noLabel) + @if ($label)
- @forelse ($servers as $server) - {{ $server->name }} - @empty -
-
Without a server, you won't be able to do much.
-
Let's create your - first one.
-
- @endforelse +
+ @forelse ($servers as $server) + {{ $server->name }} + @empty +
+
Without a server, you won't be able to do much.
+
Let's create your + first one.
+
+ @endforelse +
diff --git a/routes/web.php b/routes/web.php index 686a0ca1b..2f329e016 100644 --- a/routes/web.php +++ b/routes/web.php @@ -195,10 +195,8 @@ Route::middleware(['auth'])->group(function () { })->name('source.github.show'); }); Route::middleware(['auth'])->group(function () { - - Route::get('/servers', fn () => view('servers', [ - 'servers' => Server::validated(), + 'servers' => Server::where('team_id', session('currentTeam')->id)->get(), ]))->name('servers'); Route::get('/server/new', fn () => view('server.new', [