coolify/app/Console/Commands/Init.php

274 lines
9.8 KiB
PHP
Raw Normal View History

2023-06-30 09:42:59 +00:00
<?php
namespace App\Console\Commands;
2024-08-05 11:51:34 +00:00
use App\Enums\ActivityTypes;
2023-06-30 20:26:40 +00:00
use App\Enums\ApplicationDeploymentStatus;
2024-11-15 10:58:32 +00:00
use App\Jobs\CheckHelperImageJob;
2023-06-30 09:42:59 +00:00
use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment;
use App\Models\ScheduledDatabaseBackup;
use App\Models\Server;
use App\Models\StandalonePostgresql;
use App\Models\User;
2023-06-30 09:42:59 +00:00
use Illuminate\Console\Command;
2024-11-15 09:47:30 +00:00
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
2025-01-07 13:52:08 +00:00
use Throwable;
2023-06-30 09:42:59 +00:00
class Init extends Command
{
2024-09-23 18:29:22 +00:00
protected $signature = 'app:init {--force-cloud}';
2024-06-10 20:43:34 +00:00
2023-06-30 09:42:59 +00:00
protected $description = 'Cleanup instance related stuffs';
2025-01-07 13:52:08 +00:00
public $servers;
2023-06-30 09:42:59 +00:00
public function handle()
{
2024-11-15 09:47:30 +00:00
$this->optimize();
2024-09-23 18:54:56 +00:00
if (isCloud() && ! $this->option('force-cloud')) {
2024-09-23 18:29:22 +00:00
echo "Skipping init as we are on cloud and --force-cloud option is not set\n";
2024-06-10 20:43:34 +00:00
2024-02-08 11:47:00 +00:00
return;
}
2024-09-23 18:29:22 +00:00
$this->servers = Server::all();
2025-01-07 13:52:08 +00:00
if (! isCloud()) {
2024-09-23 18:29:22 +00:00
$this->send_alive_signal();
get_public_ips();
}
2024-09-23 18:29:22 +00:00
// Backward compatibility
$this->replace_slash_in_environment_name();
$this->restore_coolify_db_backup();
$this->update_user_emails();
2024-09-23 18:29:22 +00:00
//
$this->update_traefik_labels();
if (! isCloud() || $this->option('force-cloud')) {
$this->cleanup_unused_network_from_coolify_proxy();
2024-09-23 18:29:22 +00:00
}
if (isCloud()) {
$this->cleanup_unnecessary_dynamic_proxy_configuration();
2024-09-23 18:29:22 +00:00
} else {
$this->cleanup_in_progress_application_deployments();
2024-09-23 18:29:22 +00:00
}
$this->call('cleanup:redis');
2024-09-23 18:29:22 +00:00
$this->call('cleanup:stucked-resources');
2024-11-15 10:58:32 +00:00
try {
$this->pullHelperImage();
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-11-15 10:58:32 +00:00
//
}
2024-09-23 18:29:22 +00:00
if (isCloud()) {
try {
$this->pullTemplatesFromCDN();
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
echo "Could not pull templates from CDN: {$e->getMessage()}\n";
}
}
if (! isCloud()) {
try {
$this->pullTemplatesFromCDN();
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
echo "Could not pull templates from CDN: {$e->getMessage()}\n";
2024-09-23 18:29:22 +00:00
}
try {
$localhost = $this->servers->where('id', 0)->first();
$localhost->setupDynamicProxyConfiguration();
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-09-23 18:29:22 +00:00
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
}
$settings = instanceSettings();
2024-11-12 14:53:05 +00:00
if (! is_null(config('constants.coolify.autoupdate', null))) {
if (config('constants.coolify.autoupdate') == true) {
$settings->update(['is_auto_update_enabled' => true]);
} else {
$settings->update(['is_auto_update_enabled' => false]);
}
2023-12-11 22:26:49 +00:00
}
2024-09-23 18:29:22 +00:00
}
}
2024-11-15 10:58:32 +00:00
private function pullHelperImage()
{
CheckHelperImageJob::dispatch();
}
private function pullTemplatesFromCDN()
{
$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));
}
}
2024-11-15 09:47:30 +00:00
private function optimize()
{
Artisan::call('optimize:clear');
Artisan::call('optimize');
}
2024-04-25 11:52:52 +00:00
private function update_user_emails()
{
2024-11-03 15:14:12 +00:00
try {
2025-01-07 13:52:08 +00:00
User::query()->whereRaw('email ~ \'[A-Z]\'')->get()->each(fn (User $user) => $user->update(['email' => strtolower($user->email)]));
} catch (Throwable $e) {
2024-11-03 15:14:12 +00:00
echo "Error in updating user emails: {$e->getMessage()}\n";
}
}
2024-08-07 15:52:43 +00:00
private function update_traefik_labels()
{
try {
2025-01-07 13:52:08 +00:00
Server::query()->where('proxy->type', 'TRAEFIK_V2')->update(['proxy->type' => 'TRAEFIK']);
} catch (Throwable $e) {
echo "Error in updating traefik labels: {$e->getMessage()}\n";
}
2024-08-07 15:52:43 +00:00
}
private function cleanup_unnecessary_dynamic_proxy_configuration()
{
2024-09-23 18:29:22 +00:00
foreach ($this->servers as $server) {
try {
if (! $server->isFunctional()) {
continue;
}
2024-09-23 18:29:22 +00:00
if ($server->id === 0) {
continue;
}
$file = $server->proxyPath().'/dynamic/coolify.yaml';
2024-09-23 18:29:22 +00:00
return instant_remote_process([
"rm -f $file",
], $server, false);
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-09-23 18:29:22 +00:00
echo "Error in cleaning up unnecessary dynamic proxy configuration: {$e->getMessage()}\n";
}
}
2025-01-07 13:52:08 +00:00
return null;
}
private function cleanup_unused_network_from_coolify_proxy()
{
foreach ($this->servers as $server) {
if (! $server->isFunctional()) {
continue;
}
if (! $server->isProxyShouldRun()) {
continue;
}
try {
['networks' => $networks, 'allNetworks' => $allNetworks] = collectDockerNetworksByServer($server);
$removeNetworks = $allNetworks->diff($networks);
$commands = collect();
2025-01-07 13:52:08 +00:00
foreach ($removeNetworks as $removeNetwork) {
$out = instant_remote_process(["docker network inspect -f json {$removeNetwork} | jq '.[].Containers | if . == {} then null else . end'"], $server, false);
if ($out === null || $out === '' || $out === '0') {
$commands->push("docker network disconnect {$removeNetwork} coolify-proxy >/dev/null 2>&1 || true");
$commands->push("docker network rm {$removeNetwork} >/dev/null 2>&1 || true");
} else {
$data = collect(json_decode($out, true));
if ($data->count() === 1) {
// If only coolify-proxy itself is connected to that network (it should not be possible, but who knows)
$isCoolifyProxyItself = data_get($data->first(), 'Name') === 'coolify-proxy';
if ($isCoolifyProxyItself) {
2025-01-07 13:52:08 +00:00
$commands->push("docker network disconnect {$removeNetwork} coolify-proxy >/dev/null 2>&1 || true");
$commands->push("docker network rm {$removeNetwork} >/dev/null 2>&1 || true");
}
}
}
}
if ($commands->isNotEmpty()) {
remote_process(command: $commands, type: ActivityTypes::INLINE->value, server: $server, ignore_errors: false);
}
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
echo "Error in cleaning up unused networks from coolify proxy: {$e->getMessage()}\n";
}
}
}
private function restore_coolify_db_backup()
{
if (version_compare('4.0.0-beta.179', config('constants.coolify.version'), '<=')) {
2024-09-23 18:29:22 +00:00
try {
$database = StandalonePostgresql::withTrashed()->find(0);
if ($database && $database->trashed()) {
echo "Restoring coolify db backup\n";
$database->restore();
2025-01-07 13:52:08 +00:00
$scheduledBackup = ScheduledDatabaseBackup::query()->find(0);
2024-09-23 18:29:22 +00:00
if (! $scheduledBackup) {
2025-01-07 13:52:08 +00:00
ScheduledDatabaseBackup::query()->create([
2024-09-23 18:29:22 +00:00
'id' => 0,
'enabled' => true,
'save_s3' => false,
'frequency' => '0 0 * * *',
'database_id' => $database->id,
2025-01-07 13:52:08 +00:00
'database_type' => StandalonePostgresql::class,
2024-09-23 18:29:22 +00:00
'team_id' => 0,
]);
}
}
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-09-23 18:29:22 +00:00
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
}
}
2023-06-30 20:26:40 +00:00
}
2024-06-10 20:43:34 +00:00
2024-09-23 18:29:22 +00:00
private function send_alive_signal()
{
$id = config('app.id');
$version = config('constants.coolify.version');
$settings = instanceSettings();
2023-11-15 09:20:48 +00:00
$do_not_track = data_get($settings, 'do_not_track');
2023-11-15 09:26:31 +00:00
if ($do_not_track == true) {
2024-12-10 09:46:05 +00:00
echo "Do_not_track is enabled\n";
2024-06-10 20:43:34 +00:00
2023-11-15 09:20:48 +00:00
return;
}
try {
2024-01-11 10:34:05 +00:00
Http::get("https://undead.coolify.io/v4/alive?appId=$id&version=$version");
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2024-12-10 09:46:05 +00:00
echo "Error in sending live signal: {$e->getMessage()}\n";
}
}
2023-11-29 14:23:03 +00:00
2023-06-30 20:26:40 +00:00
private function cleanup_in_progress_application_deployments()
{
// Cleanup any failed deployments
2023-06-30 09:42:59 +00:00
try {
if (isCloud()) {
return;
}
2025-01-07 13:52:08 +00:00
$queued_inprogress_deployments = ApplicationDeploymentQueue::query()->whereIn('status', [ApplicationDeploymentStatus::IN_PROGRESS->value, ApplicationDeploymentStatus::QUEUED->value])->get();
foreach ($queued_inprogress_deployments as $queued_inprogress_deployment) {
$queued_inprogress_deployment->status = ApplicationDeploymentStatus::FAILED->value;
$queued_inprogress_deployment->save();
2023-06-30 20:26:40 +00:00
}
2025-01-07 13:52:08 +00:00
} catch (Throwable $e) {
2023-06-30 09:42:59 +00:00
echo "Error: {$e->getMessage()}\n";
}
}
private function replace_slash_in_environment_name()
{
if (version_compare('4.0.0-beta.298', config('constants.coolify.version'), '<=')) {
2024-09-23 18:29:22 +00:00
$environments = Environment::all();
foreach ($environments as $environment) {
if (str_contains($environment->name, '/')) {
$environment->name = str_replace('/', '-', $environment->name);
$environment->save();
}
}
}
}
2023-06-30 09:42:59 +00:00
}