This commit is contained in:
Andras Bacsai
2024-09-23 20:29:22 +02:00
parent b570ccd7d3
commit 573e5c4913

View File

@@ -5,7 +5,6 @@ namespace App\Console\Commands;
use App\Actions\Server\StopSentinel; use App\Actions\Server\StopSentinel;
use App\Enums\ActivityTypes; use App\Enums\ActivityTypes;
use App\Enums\ApplicationDeploymentStatus; use App\Enums\ApplicationDeploymentStatus;
use App\Jobs\CleanupHelperContainersJob;
use App\Models\ApplicationDeploymentQueue; use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment; use App\Models\Environment;
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
@@ -18,7 +17,7 @@ use Illuminate\Support\Facades\Http;
class Init extends Command class Init extends Command
{ {
protected $signature = 'app:init {--full-cleanup} {--cleanup-deployments} {--cleanup-proxy-networks}'; protected $signature = 'app:init {--force-cloud}';
protected $description = 'Cleanup instance related stuffs'; protected $description = 'Cleanup instance related stuffs';
@@ -26,9 +25,63 @@ class Init extends Command
public function handle() public function handle()
{ {
if (isCloud() && ! $this->option('force-cloud') ) {
echo "Skipping init as we are on cloud and --force-cloud option is not set\n";
return;
}
$this->servers = Server::all(); $this->servers = Server::all();
$this->alive(); if (isCloud()) {
get_public_ips();
} else {
$this->send_alive_signal();
get_public_ips();
}
// Backward compatibility
$this->disable_metrics();
$this->replace_slash_in_environment_name();
$this->restore_coolify_db_backup();
//
$this->update_traefik_labels();
if (! isCloud() || $this->option('force-cloud')) {
$this->cleanup_unused_network_from_coolify_proxy();
}
if (isCloud()) {
$this->cleanup_unnecessary_dynamic_proxy_configuration();
} else {
$this->cleanup_in_progress_application_deployments();
}
$this->call('cleanup:queue');
$this->call('cleanup:stucked-resources');
if (isCloud()) {
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
if ($response->successful()) {
$services = $response->json();
File::put(base_path('templates/service-templates.json'), json_encode($services));
}
} else {
try {
$localhost = $this->servers->where('id', 0)->first();
$localhost->setupDynamicProxyConfiguration();
} catch (\Throwable $e) {
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
}
$settings = InstanceSettings::get();
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]);
}
}
}
}
private function disable_metrics()
{
if (version_compare('4.0.0-beta.312', config('version'), '<=')) { if (version_compare('4.0.0-beta.312', config('version'), '<=')) {
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
if ($server->settings->is_metrics_enabled === true) { if ($server->settings->is_metrics_enabled === true) {
@@ -39,62 +92,6 @@ class Init extends Command
} }
} }
} }
$full_cleanup = $this->option('full-cleanup');
$cleanup_deployments = $this->option('cleanup-deployments');
$cleanup_proxy_networks = $this->option('cleanup-proxy-networks');
$this->replace_slash_in_environment_name();
if ($cleanup_deployments) {
echo "Running cleanup deployments.\n";
$this->cleanup_in_progress_application_deployments();
return;
}
if ($cleanup_proxy_networks) {
echo "Running cleanup proxy networks.\n";
$this->cleanup_unused_network_from_coolify_proxy();
return;
}
if ($full_cleanup) {
// Required for falsely deleted coolify db
$this->restore_coolify_db_backup();
$this->update_traefik_labels();
$this->cleanup_unused_network_from_coolify_proxy();
$this->cleanup_unnecessary_dynamic_proxy_configuration();
$this->cleanup_in_progress_application_deployments();
$this->cleanup_stucked_helper_containers();
$this->call('cleanup:queue');
$this->call('cleanup:stucked-resources');
if (! isCloud()) {
try {
$localhost = $this->servers->where('id', 0)->first();
$localhost->setupDynamicProxyConfiguration();
} catch (\Throwable $e) {
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
}
}
$settings = InstanceSettings::get();
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]);
}
}
if (isCloud()) {
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
if ($response->successful()) {
$services = $response->json();
File::put(base_path('templates/service-templates.json'), json_encode($services));
}
}
return;
}
$this->cleanup_stucked_helper_containers();
$this->call('cleanup:stucked-resources');
} }
private function update_traefik_labels() private function update_traefik_labels()
@@ -108,33 +105,28 @@ class Init extends Command
private function cleanup_unnecessary_dynamic_proxy_configuration() private function cleanup_unnecessary_dynamic_proxy_configuration()
{ {
if (isCloud()) { foreach ($this->servers as $server) {
foreach ($this->servers as $server) { try {
try { if (! $server->isFunctional()) {
if (! $server->isFunctional()) { continue;
continue;
}
if ($server->id === 0) {
continue;
}
$file = $server->proxyPath().'/dynamic/coolify.yaml';
return instant_remote_process([
"rm -f $file",
], $server, false);
} catch (\Throwable $e) {
echo "Error in cleaning up unnecessary dynamic proxy configuration: {$e->getMessage()}\n";
} }
if ($server->id === 0) {
continue;
}
$file = $server->proxyPath().'/dynamic/coolify.yaml';
return instant_remote_process([
"rm -f $file",
], $server, false);
} catch (\Throwable $e) {
echo "Error in cleaning up unnecessary dynamic proxy configuration: {$e->getMessage()}\n";
} }
} }
} }
private function cleanup_unused_network_from_coolify_proxy() private function cleanup_unused_network_from_coolify_proxy()
{ {
if (isCloud()) {
return;
}
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
if (! $server->isFunctional()) { if (! $server->isFunctional()) {
continue; continue;
@@ -175,39 +167,32 @@ class Init extends Command
private function restore_coolify_db_backup() private function restore_coolify_db_backup()
{ {
try { if (version_compare('4.0.0-beta.179', config('version'), '<=')) {
$database = StandalonePostgresql::withTrashed()->find(0); try {
if ($database && $database->trashed()) { $database = StandalonePostgresql::withTrashed()->find(0);
echo "Restoring coolify db backup\n"; if ($database && $database->trashed()) {
$database->restore(); echo "Restoring coolify db backup\n";
$scheduledBackup = ScheduledDatabaseBackup::find(0); $database->restore();
if (! $scheduledBackup) { $scheduledBackup = ScheduledDatabaseBackup::find(0);
ScheduledDatabaseBackup::create([ if (! $scheduledBackup) {
'id' => 0, ScheduledDatabaseBackup::create([
'enabled' => true, 'id' => 0,
'save_s3' => false, 'enabled' => true,
'frequency' => '0 0 * * *', 'save_s3' => false,
'database_id' => $database->id, 'frequency' => '0 0 * * *',
'database_type' => 'App\Models\StandalonePostgresql', 'database_id' => $database->id,
'team_id' => 0, 'database_type' => 'App\Models\StandalonePostgresql',
]); 'team_id' => 0,
]);
}
} }
} } catch (\Throwable $e) {
} catch (\Throwable $e) { echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
}
}
private function cleanup_stucked_helper_containers()
{
foreach ($this->servers as $server) {
if ($server->isFunctional()) {
CleanupHelperContainersJob::dispatch($server);
} }
} }
} }
private function alive() private function send_alive_signal()
{ {
$id = config('app.id'); $id = config('app.id');
$version = config('version'); $version = config('version');
@@ -225,23 +210,7 @@ class Init extends Command
echo "Error in alive: {$e->getMessage()}\n"; echo "Error in alive: {$e->getMessage()}\n";
} }
} }
// private function cleanup_ssh()
// {
// TODO: it will cleanup id.root@host.docker.internal
// 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";
// }
// }
private function cleanup_in_progress_application_deployments() private function cleanup_in_progress_application_deployments()
{ {
// Cleanup any failed deployments // Cleanup any failed deployments
@@ -263,11 +232,13 @@ class Init extends Command
private function replace_slash_in_environment_name() private function replace_slash_in_environment_name()
{ {
$environments = Environment::all(); if (version_compare('4.0.0-beta.298', config('version'), '<=')) {
foreach ($environments as $environment) { $environments = Environment::all();
if (str_contains($environment->name, '/')) { foreach ($environments as $environment) {
$environment->name = str_replace('/', '-', $environment->name); if (str_contains($environment->name, '/')) {
$environment->save(); $environment->name = str_replace('/', '-', $environment->name);
$environment->save();
}
} }
} }
} }