coolify/app/Console/Commands/Init.php

179 lines
6.1 KiB
PHP
Raw Normal View History

2023-06-30 11:42:59 +02:00
<?php
namespace App\Console\Commands;
use App\Actions\Server\StopSentinel;
2023-06-30 22:26:40 +02:00
use App\Enums\ApplicationDeploymentStatus;
use App\Jobs\CleanupHelperContainersJob;
2023-06-30 11:42:59 +02:00
use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment;
2023-11-15 10:20:48 +01:00
use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup;
use App\Models\Server;
use App\Models\StandalonePostgresql;
2023-06-30 11:42:59 +02:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
2023-06-30 11:42:59 +02:00
class Init extends Command
{
2024-02-08 12:47:00 +01:00
protected $signature = 'app:init {--full-cleanup} {--cleanup-deployments}';
2024-06-10 22:43:34 +02:00
2023-06-30 11:42:59 +02:00
protected $description = 'Cleanup instance related stuffs';
2023-06-30 11:42:59 +02:00
public function handle()
{
$this->alive();
2024-04-25 13:52:52 +02:00
get_public_ips();
if (version_compare('4.0.0-beta.312', config('version'), '<=')) {
$servers = Server::all();
foreach ($servers as $server) {
$server->settings->update(['is_metrics_enabled' => false]);
if ($server->isFunctional()) {
StopSentinel::dispatch($server);
}
}
}
$full_cleanup = $this->option('full-cleanup');
2024-02-08 12:47:00 +01:00
$cleanup_deployments = $this->option('cleanup-deployments');
$this->replace_slash_in_environment_name();
2024-02-08 12:47:00 +01:00
if ($cleanup_deployments) {
echo "Running cleanup deployments.\n";
$this->cleanup_in_progress_application_deployments();
2024-06-10 22:43:34 +02:00
2024-02-08 12:47:00 +01:00
return;
}
if ($full_cleanup) {
// Required for falsely deleted coolify db
$this->restore_coolify_db_backup();
$this->cleanup_in_progress_application_deployments();
$this->cleanup_stucked_helper_containers();
$this->call('cleanup:queue');
$this->call('cleanup:stucked-resources');
2024-06-10 22:43:34 +02:00
if (! isCloud()) {
try {
$server = Server::find(0)->first();
$server->setupDynamicProxyConfiguration();
} catch (\Throwable $e) {
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
}
}
$settings = InstanceSettings::get();
2024-06-10 22:43:34 +02:00
if (! is_null(env('AUTOUPDATE', null))) {
if (env('AUTOUPDATE') == true) {
$settings->update(['is_auto_update_enabled' => true]);
} else {
$settings->update(['is_auto_update_enabled' => false]);
}
2023-12-11 23:26:49 +01:00
}
2024-06-10 22:43:34 +02:00
return;
2023-12-11 20:55:58 +01:00
}
$this->cleanup_stucked_helper_containers();
$this->call('cleanup:stucked-resources');
}
2024-04-25 13:52:52 +02:00
private function restore_coolify_db_backup()
{
try {
$database = StandalonePostgresql::withTrashed()->find(0);
2023-12-27 23:07:39 +01:00
if ($database && $database->trashed()) {
echo "Restoring coolify db backup\n";
$database->restore();
$scheduledBackup = ScheduledDatabaseBackup::find(0);
2024-06-10 22:43:34 +02:00
if (! $scheduledBackup) {
ScheduledDatabaseBackup::create([
'id' => 0,
'enabled' => true,
'save_s3' => false,
'frequency' => '0 0 * * *',
'database_id' => $database->id,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => 0,
]);
}
}
} catch (\Throwable $e) {
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
}
}
2024-06-10 22:43:34 +02:00
private function cleanup_stucked_helper_containers()
{
$servers = Server::all();
foreach ($servers as $server) {
if ($server->isFunctional()) {
CleanupHelperContainersJob::dispatch($server);
}
}
2023-06-30 22:26:40 +02:00
}
2024-06-10 22:43:34 +02:00
private function alive()
{
$id = config('app.id');
2023-11-17 13:59:45 +01:00
$version = config('version');
2023-11-15 10:20:48 +01:00
$settings = InstanceSettings::get();
$do_not_track = data_get($settings, 'do_not_track');
2023-11-15 10:26:31 +01:00
if ($do_not_track == true) {
echo "Skipping alive as do_not_track is enabled\n";
2024-06-10 22:43:34 +02:00
2023-11-15 10:20:48 +01:00
return;
}
try {
2024-01-11 11:34:05 +01:00
Http::get("https://undead.coolify.io/v4/alive?appId=$id&version=$version");
echo "I am alive!\n";
} catch (\Throwable $e) {
echo "Error in alive: {$e->getMessage()}\n";
}
}
2023-11-29 15:23:03 +01:00
// private function cleanup_ssh()
// {
// TODO: it will cleanup id.root@host.docker.internal
2023-11-29 15:23:03 +01:00
// try {
// $files = Storage::allFiles('ssh/keys');
// foreach ($files as $file) {
// Storage::delete($file);
// }
// $files = Storage::allFiles('ssh/mux');
// foreach ($files as $file) {
// Storage::delete($file);
// }
// } catch (\Throwable $e) {
// echo "Error in cleaning ssh: {$e->getMessage()}\n";
// }
// }
2023-06-30 22:26:40 +02:00
private function cleanup_in_progress_application_deployments()
{
// Cleanup any failed deployments
2023-06-30 11:42:59 +02:00
try {
if (isCloud()) {
return;
}
2024-02-08 12:47:00 +01:00
$queued_inprogress_deployments = ApplicationDeploymentQueue::whereIn('status', [ApplicationDeploymentStatus::IN_PROGRESS->value, ApplicationDeploymentStatus::QUEUED->value])->get();
foreach ($queued_inprogress_deployments as $deployment) {
ray($deployment->id, $deployment->status);
2024-02-08 12:37:56 +01:00
echo "Cleaning up deployment: {$deployment->id}\n";
2023-06-30 22:26:40 +02:00
$deployment->status = ApplicationDeploymentStatus::FAILED->value;
$deployment->save();
}
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-06-30 11:42:59 +02:00
echo "Error: {$e->getMessage()}\n";
}
}
private function replace_slash_in_environment_name()
{
$environments = Environment::all();
foreach ($environments as $environment) {
if (str_contains($environment->name, '/')) {
$environment->name = str_replace('/', '-', $environment->name);
$environment->save();
}
}
}
2023-06-30 11:42:59 +02:00
}