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;
|
2025-09-01 14:13:55 +00:00
|
|
|
use App\Jobs\PullChangelog;
|
2023-06-30 09:42:59 +00:00
|
|
|
use App\Models\ApplicationDeploymentQueue;
|
2024-06-23 15:47:58 +00:00
|
|
|
use App\Models\Environment;
|
2025-09-05 17:27:49 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-12-27 22:06:22 +00:00
|
|
|
use App\Models\ScheduledDatabaseBackup;
|
2025-11-11 11:32:52 +00:00
|
|
|
use App\Models\ScheduledDatabaseBackupExecution;
|
|
|
|
|
use App\Models\ScheduledTaskExecution;
|
2023-11-16 19:48:25 +00:00
|
|
|
use App\Models\Server;
|
2023-10-24 08:10:55 +00:00
|
|
|
use App\Models\StandalonePostgresql;
|
2024-11-03 14:35:17 +00:00
|
|
|
use App\Models\User;
|
2025-11-11 11:32:52 +00:00
|
|
|
use Carbon\Carbon;
|
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;
|
2024-08-07 07:50:12 +00:00
|
|
|
use Illuminate\Support\Facades\File;
|
2023-11-15 09:18:41 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-06-30 09:42:59 +00:00
|
|
|
|
|
|
|
|
class Init extends Command
|
|
|
|
|
{
|
2025-09-05 17:27:49 +00:00
|
|
|
protected $signature = 'app:init';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-30 09:42:59 +00:00
|
|
|
protected $description = 'Cleanup instance related stuffs';
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
public $servers = null;
|
2024-08-06 08:53:13 +00:00
|
|
|
|
2025-09-05 17:27:49 +00:00
|
|
|
public InstanceSettings $settings;
|
|
|
|
|
|
2023-06-30 09:42:59 +00:00
|
|
|
public function handle()
|
|
|
|
|
{
|
2025-09-05 17:27:49 +00:00
|
|
|
Artisan::call('optimize:clear');
|
|
|
|
|
Artisan::call('optimize');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->pullTemplatesFromCDN();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not pull templates from CDN: {$e->getMessage()}\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->pullChangelogFromGitHub();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not changelogs from github: {$e->getMessage()}\n";
|
|
|
|
|
}
|
2024-11-15 09:47:30 +00:00
|
|
|
|
2025-09-05 17:27:49 +00:00
|
|
|
try {
|
|
|
|
|
$this->pullHelperImage();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Error in pullHelperImage command: {$e->getMessage()}\n";
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-09-05 17:27:49 +00:00
|
|
|
if (isCloud()) {
|
2024-02-08 11:47:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-05 11:45:24 +00:00
|
|
|
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->settings = instanceSettings();
|
2024-09-23 18:29:22 +00:00
|
|
|
$this->servers = Server::all();
|
2025-09-05 17:27:49 +00:00
|
|
|
|
|
|
|
|
$do_not_track = data_get($this->settings, 'do_not_track', true);
|
|
|
|
|
if ($do_not_track == false) {
|
2025-07-07 07:50:15 +00:00
|
|
|
$this->sendAliveSignal();
|
2024-08-05 11:45:24 +00:00
|
|
|
}
|
2025-09-05 17:27:49 +00:00
|
|
|
get_public_ips();
|
2024-09-23 18:29:22 +00:00
|
|
|
|
|
|
|
|
// Backward compatibility
|
2025-07-07 07:50:15 +00:00
|
|
|
$this->replaceSlashInEnvironmentName();
|
|
|
|
|
$this->restoreCoolifyDbBackup();
|
|
|
|
|
$this->updateUserEmails();
|
2024-09-23 18:29:22 +00:00
|
|
|
//
|
2025-07-07 07:50:15 +00:00
|
|
|
$this->updateTraefikLabels();
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->cleanupUnusedNetworkFromCoolifyProxy();
|
2024-11-15 10:33:00 +00:00
|
|
|
|
2025-08-19 09:04:23 +00:00
|
|
|
try {
|
2025-11-10 10:11:18 +00:00
|
|
|
$this->call('cleanup:redis', ['--restart' => true, '--clear-locks' => true]);
|
2025-08-19 09:04:23 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-09-05 17:27:49 +00:00
|
|
|
echo "Error in cleanup:redis command: {$e->getMessage()}\n";
|
2025-08-19 09:04:23 +00:00
|
|
|
}
|
2024-11-15 10:58:32 +00:00
|
|
|
try {
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->call('cleanup:names');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-09-05 17:27:49 +00:00
|
|
|
echo "Error in cleanup:names command: {$e->getMessage()}\n";
|
2024-11-12 13:48:58 +00:00
|
|
|
}
|
2025-07-07 07:50:15 +00:00
|
|
|
try {
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->call('cleanup:stucked-resources');
|
2025-08-19 12:22:48 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-09-05 17:27:49 +00:00
|
|
|
echo "Error in cleanup:stucked-resources command: {$e->getMessage()}\n";
|
2025-11-10 14:29:26 +00:00
|
|
|
echo "Continuing with initialization - cleanup errors will not prevent Coolify from starting\n";
|
2025-08-19 12:22:48 +00:00
|
|
|
}
|
|
|
|
|
try {
|
2025-09-05 17:27:49 +00:00
|
|
|
$updatedCount = ApplicationDeploymentQueue::whereIn('status', [
|
|
|
|
|
ApplicationDeploymentStatus::IN_PROGRESS->value,
|
|
|
|
|
ApplicationDeploymentStatus::QUEUED->value,
|
|
|
|
|
])->update([
|
|
|
|
|
'status' => ApplicationDeploymentStatus::FAILED->value,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($updatedCount > 0) {
|
|
|
|
|
echo "Marked {$updatedCount} stuck deployments as failed\n";
|
|
|
|
|
}
|
2025-07-07 07:50:15 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-09-05 17:27:49 +00:00
|
|
|
echo "Could not cleanup inprogress deployments: {$e->getMessage()}\n";
|
2025-07-07 07:50:15 +00:00
|
|
|
}
|
2025-08-19 12:22:48 +00:00
|
|
|
|
2025-11-11 11:32:52 +00:00
|
|
|
try {
|
|
|
|
|
$updatedTaskCount = ScheduledTaskExecution::where('status', 'running')->update([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Marked as failed during Coolify startup - job was interrupted',
|
|
|
|
|
'finished_at' => Carbon::now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($updatedTaskCount > 0) {
|
|
|
|
|
echo "Marked {$updatedTaskCount} stuck scheduled task executions as failed\n";
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not cleanup stuck scheduled task executions: {$e->getMessage()}\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$updatedBackupCount = ScheduledDatabaseBackupExecution::where('status', 'running')->update([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Marked as failed during Coolify startup - job was interrupted',
|
|
|
|
|
'finished_at' => Carbon::now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($updatedBackupCount > 0) {
|
|
|
|
|
echo "Marked {$updatedBackupCount} stuck database backup executions as failed\n";
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not cleanup stuck database backup executions: {$e->getMessage()}\n";
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
try {
|
|
|
|
|
$localhost = $this->servers->where('id', 0)->first();
|
2025-09-05 17:27:49 +00:00
|
|
|
if ($localhost) {
|
|
|
|
|
$localhost->setupDynamicProxyConfiguration();
|
|
|
|
|
}
|
2025-07-07 07:50:15 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
|
|
|
|
|
}
|
2025-09-05 17:27:49 +00:00
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
if (! is_null(config('constants.coolify.autoupdate', null))) {
|
|
|
|
|
if (config('constants.coolify.autoupdate') == true) {
|
|
|
|
|
echo "Enabling auto-update\n";
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->settings->update(['is_auto_update_enabled' => true]);
|
2025-07-07 07:50:15 +00:00
|
|
|
} else {
|
|
|
|
|
echo "Disabling auto-update\n";
|
2025-09-05 17:27:49 +00:00
|
|
|
$this->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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 13:48:58 +00:00
|
|
|
private function pullTemplatesFromCDN()
|
|
|
|
|
{
|
|
|
|
|
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
|
$services = $response->json();
|
2025-08-10 08:10:14 +00:00
|
|
|
File::put(base_path('templates/'.config('constants.services.file_name')), json_encode($services));
|
2024-11-12 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-15 09:47:30 +00:00
|
|
|
|
2025-08-10 18:14:38 +00:00
|
|
|
private function pullChangelogFromGitHub()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-09-01 14:13:55 +00:00
|
|
|
PullChangelog::dispatch();
|
2025-08-10 18:14:38 +00:00
|
|
|
echo "Changelog fetch initiated\n";
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
echo "Could not fetch changelog from GitHub: {$e->getMessage()}\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
private function updateUserEmails()
|
2024-11-03 14:35:17 +00:00
|
|
|
{
|
2024-11-03 15:14:12 +00:00
|
|
|
try {
|
2025-01-17 10:38:22 +00:00
|
|
|
User::whereRaw('email ~ \'[A-Z]\'')->get()->each(function (User $user) {
|
2025-09-05 17:27:49 +00:00
|
|
|
$user->update(['email' => $user->email]);
|
2025-01-17 10:38:22 +00:00
|
|
|
});
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-11-03 15:14:12 +00:00
|
|
|
echo "Error in updating user emails: {$e->getMessage()}\n";
|
|
|
|
|
}
|
2024-11-03 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
private function updateTraefikLabels()
|
2024-08-07 15:52:43 +00:00
|
|
|
{
|
2024-08-08 09:15:33 +00:00
|
|
|
try {
|
2025-01-07 14:31:43 +00:00
|
|
|
Server::where('proxy->type', 'TRAEFIK_V2')->update(['proxy->type' => 'TRAEFIK']);
|
|
|
|
|
} catch (\Throwable $e) {
|
2024-08-08 09:15:33 +00:00
|
|
|
echo "Error in updating traefik labels: {$e->getMessage()}\n";
|
|
|
|
|
}
|
2024-08-07 15:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
private function cleanupUnusedNetworkFromCoolifyProxy()
|
2024-08-05 11:45:24 +00:00
|
|
|
{
|
2024-08-06 08:53:13 +00:00
|
|
|
foreach ($this->servers as $server) {
|
2024-08-05 11:45:24 +00:00
|
|
|
if (! $server->isFunctional()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (! $server->isProxyShouldRun()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-08-08 09:15:33 +00:00
|
|
|
try {
|
|
|
|
|
['networks' => $networks, 'allNetworks' => $allNetworks] = collectDockerNetworksByServer($server);
|
|
|
|
|
$removeNetworks = $allNetworks->diff($networks);
|
|
|
|
|
$commands = collect();
|
2025-01-07 14:31:43 +00:00
|
|
|
foreach ($removeNetworks as $network) {
|
|
|
|
|
$out = instant_remote_process(["docker network inspect -f json $network | jq '.[].Containers | if . == {} then null else . end'"], $server, false);
|
|
|
|
|
if (empty($out)) {
|
|
|
|
|
$commands->push("docker network disconnect $network coolify-proxy >/dev/null 2>&1 || true");
|
|
|
|
|
$commands->push("docker network rm $network >/dev/null 2>&1 || true");
|
2024-08-08 09:15:33 +00:00
|
|
|
} 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 14:31:43 +00:00
|
|
|
$commands->push("docker network disconnect $network coolify-proxy >/dev/null 2>&1 || true");
|
|
|
|
|
$commands->push("docker network rm $network >/dev/null 2>&1 || true");
|
2024-08-08 09:15:33 +00:00
|
|
|
}
|
2024-08-05 16:16:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-05 11:45:24 +00:00
|
|
|
}
|
2024-08-08 09:15:33 +00:00
|
|
|
if ($commands->isNotEmpty()) {
|
|
|
|
|
remote_process(command: $commands, type: ActivityTypes::INLINE->value, server: $server, ignore_errors: false);
|
|
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-08-08 09:15:33 +00:00
|
|
|
echo "Error in cleaning up unused networks from coolify proxy: {$e->getMessage()}\n";
|
2024-08-05 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
private function restoreCoolifyDbBackup()
|
2024-01-02 20:03:51 +00:00
|
|
|
{
|
2024-11-25 10:28:08 +00:00
|
|
|
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()) {
|
|
|
|
|
$database->restore();
|
2025-01-07 14:31:43 +00:00
|
|
|
$scheduledBackup = ScheduledDatabaseBackup::find(0);
|
2024-09-23 18:29:22 +00:00
|
|
|
if (! $scheduledBackup) {
|
2025-01-07 14:31:43 +00:00
|
|
|
ScheduledDatabaseBackup::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 14:31:43 +00:00
|
|
|
'database_type' => \App\Models\StandalonePostgresql::class,
|
2024-09-23 18:29:22 +00:00
|
|
|
'team_id' => 0,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2023-12-27 22:06:22 +00:00
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-09-23 18:29:22 +00:00
|
|
|
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
|
2023-11-16 19:48:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-30 20:26:40 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-02-12 02:06:53 +00:00
|
|
|
// MapleDeploy branding: telemetry disabled — no phone-home signal
|
2025-07-07 07:50:15 +00:00
|
|
|
private function sendAliveSignal()
|
2023-11-15 09:18:41 +00:00
|
|
|
{
|
2026-02-12 02:06:53 +00:00
|
|
|
// Disabled for MapleDeploy: do not send telemetry to coolify.io
|
|
|
|
|
return;
|
2023-11-15 09:18:41 +00:00
|
|
|
}
|
2023-11-29 14:23:03 +00:00
|
|
|
|
2025-07-07 07:50:15 +00:00
|
|
|
private function replaceSlashInEnvironmentName()
|
2024-06-23 15:47:58 +00:00
|
|
|
{
|
2024-11-25 10:28:08 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2024-06-23 15:47:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-30 09:42:59 +00:00
|
|
|
}
|