Skip to content

Commit 573e5c4

Browse files
committed
refactor
1 parent b570ccd commit 573e5c4

File tree

1 file changed

+90
-119
lines changed

1 file changed

+90
-119
lines changed

app/Console/Commands/Init.php

Lines changed: 90 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Actions\Server\StopSentinel;
66
use App\Enums\ActivityTypes;
77
use App\Enums\ApplicationDeploymentStatus;
8-
use App\Jobs\CleanupHelperContainersJob;
98
use App\Models\ApplicationDeploymentQueue;
109
use App\Models\Environment;
1110
use App\Models\InstanceSettings;
@@ -18,63 +17,58 @@
1817

1918
class Init extends Command
2019
{
21-
protected $signature = 'app:init {--full-cleanup} {--cleanup-deployments} {--cleanup-proxy-networks}';
20+
protected $signature = 'app:init {--force-cloud}';
2221

2322
protected $description = 'Cleanup instance related stuffs';
2423

2524
public $servers = null;
2625

2726
public function handle()
2827
{
29-
$this->servers = Server::all();
30-
$this->alive();
31-
get_public_ips();
32-
if (version_compare('4.0.0-beta.312', config('version'), '<=')) {
33-
foreach ($this->servers as $server) {
34-
if ($server->settings->is_metrics_enabled === true) {
35-
$server->settings->update(['is_metrics_enabled' => false]);
36-
}
37-
if ($server->isFunctional()) {
38-
StopSentinel::dispatch($server);
39-
}
40-
}
41-
}
42-
43-
$full_cleanup = $this->option('full-cleanup');
44-
$cleanup_deployments = $this->option('cleanup-deployments');
45-
$cleanup_proxy_networks = $this->option('cleanup-proxy-networks');
46-
$this->replace_slash_in_environment_name();
47-
if ($cleanup_deployments) {
48-
echo "Running cleanup deployments.\n";
49-
$this->cleanup_in_progress_application_deployments();
28+
if (isCloud() && ! $this->option('force-cloud') ) {
29+
echo "Skipping init as we are on cloud and --force-cloud option is not set\n";
5030

5131
return;
5232
}
53-
if ($cleanup_proxy_networks) {
54-
echo "Running cleanup proxy networks.\n";
55-
$this->cleanup_unused_network_from_coolify_proxy();
5633

57-
return;
34+
$this->servers = Server::all();
35+
if (isCloud()) {
36+
37+
} else {
38+
$this->send_alive_signal();
39+
get_public_ips();
5840
}
59-
if ($full_cleanup) {
60-
// Required for falsely deleted coolify db
61-
$this->restore_coolify_db_backup();
62-
$this->update_traefik_labels();
41+
42+
// Backward compatibility
43+
$this->disable_metrics();
44+
$this->replace_slash_in_environment_name();
45+
$this->restore_coolify_db_backup();
46+
//
47+
$this->update_traefik_labels();
48+
if (! isCloud() || $this->option('force-cloud')) {
6349
$this->cleanup_unused_network_from_coolify_proxy();
50+
}
51+
if (isCloud()) {
6452
$this->cleanup_unnecessary_dynamic_proxy_configuration();
53+
} else {
6554
$this->cleanup_in_progress_application_deployments();
66-
$this->cleanup_stucked_helper_containers();
67-
$this->call('cleanup:queue');
68-
$this->call('cleanup:stucked-resources');
69-
if (! isCloud()) {
70-
try {
71-
$localhost = $this->servers->where('id', 0)->first();
72-
$localhost->setupDynamicProxyConfiguration();
73-
} catch (\Throwable $e) {
74-
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
75-
}
76-
}
55+
}
56+
$this->call('cleanup:queue');
57+
$this->call('cleanup:stucked-resources');
7758

59+
if (isCloud()) {
60+
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
61+
if ($response->successful()) {
62+
$services = $response->json();
63+
File::put(base_path('templates/service-templates.json'), json_encode($services));
64+
}
65+
} else {
66+
try {
67+
$localhost = $this->servers->where('id', 0)->first();
68+
$localhost->setupDynamicProxyConfiguration();
69+
} catch (\Throwable $e) {
70+
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
71+
}
7872
$settings = InstanceSettings::get();
7973
if (! is_null(env('AUTOUPDATE', null))) {
8074
if (env('AUTOUPDATE') == true) {
@@ -83,18 +77,21 @@ public function handle()
8377
$settings->update(['is_auto_update_enabled' => false]);
8478
}
8579
}
86-
if (isCloud()) {
87-
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
88-
if ($response->successful()) {
89-
$services = $response->json();
90-
File::put(base_path('templates/service-templates.json'), json_encode($services));
80+
}
81+
}
82+
83+
private function disable_metrics()
84+
{
85+
if (version_compare('4.0.0-beta.312', config('version'), '<=')) {
86+
foreach ($this->servers as $server) {
87+
if ($server->settings->is_metrics_enabled === true) {
88+
$server->settings->update(['is_metrics_enabled' => false]);
89+
}
90+
if ($server->isFunctional()) {
91+
StopSentinel::dispatch($server);
9192
}
9293
}
93-
94-
return;
9594
}
96-
$this->cleanup_stucked_helper_containers();
97-
$this->call('cleanup:stucked-resources');
9895
}
9996

10097
private function update_traefik_labels()
@@ -108,33 +105,28 @@ private function update_traefik_labels()
108105

109106
private function cleanup_unnecessary_dynamic_proxy_configuration()
110107
{
111-
if (isCloud()) {
112-
foreach ($this->servers as $server) {
113-
try {
114-
if (! $server->isFunctional()) {
115-
continue;
116-
}
117-
if ($server->id === 0) {
118-
continue;
119-
}
120-
$file = $server->proxyPath().'/dynamic/coolify.yaml';
121-
122-
return instant_remote_process([
123-
"rm -f $file",
124-
], $server, false);
125-
} catch (\Throwable $e) {
126-
echo "Error in cleaning up unnecessary dynamic proxy configuration: {$e->getMessage()}\n";
108+
foreach ($this->servers as $server) {
109+
try {
110+
if (! $server->isFunctional()) {
111+
continue;
127112
}
113+
if ($server->id === 0) {
114+
continue;
115+
}
116+
$file = $server->proxyPath().'/dynamic/coolify.yaml';
128117

118+
return instant_remote_process([
119+
"rm -f $file",
120+
], $server, false);
121+
} catch (\Throwable $e) {
122+
echo "Error in cleaning up unnecessary dynamic proxy configuration: {$e->getMessage()}\n";
129123
}
124+
130125
}
131126
}
132127

133128
private function cleanup_unused_network_from_coolify_proxy()
134129
{
135-
if (isCloud()) {
136-
return;
137-
}
138130
foreach ($this->servers as $server) {
139131
if (! $server->isFunctional()) {
140132
continue;
@@ -175,39 +167,32 @@ private function cleanup_unused_network_from_coolify_proxy()
175167

176168
private function restore_coolify_db_backup()
177169
{
178-
try {
179-
$database = StandalonePostgresql::withTrashed()->find(0);
180-
if ($database && $database->trashed()) {
181-
echo "Restoring coolify db backup\n";
182-
$database->restore();
183-
$scheduledBackup = ScheduledDatabaseBackup::find(0);
184-
if (! $scheduledBackup) {
185-
ScheduledDatabaseBackup::create([
186-
'id' => 0,
187-
'enabled' => true,
188-
'save_s3' => false,
189-
'frequency' => '0 0 * * *',
190-
'database_id' => $database->id,
191-
'database_type' => 'App\Models\StandalonePostgresql',
192-
'team_id' => 0,
193-
]);
170+
if (version_compare('4.0.0-beta.179', config('version'), '<=')) {
171+
try {
172+
$database = StandalonePostgresql::withTrashed()->find(0);
173+
if ($database && $database->trashed()) {
174+
echo "Restoring coolify db backup\n";
175+
$database->restore();
176+
$scheduledBackup = ScheduledDatabaseBackup::find(0);
177+
if (! $scheduledBackup) {
178+
ScheduledDatabaseBackup::create([
179+
'id' => 0,
180+
'enabled' => true,
181+
'save_s3' => false,
182+
'frequency' => '0 0 * * *',
183+
'database_id' => $database->id,
184+
'database_type' => 'App\Models\StandalonePostgresql',
185+
'team_id' => 0,
186+
]);
187+
}
194188
}
195-
}
196-
} catch (\Throwable $e) {
197-
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
198-
}
199-
}
200-
201-
private function cleanup_stucked_helper_containers()
202-
{
203-
foreach ($this->servers as $server) {
204-
if ($server->isFunctional()) {
205-
CleanupHelperContainersJob::dispatch($server);
189+
} catch (\Throwable $e) {
190+
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
206191
}
207192
}
208193
}
209194

210-
private function alive()
195+
private function send_alive_signal()
211196
{
212197
$id = config('app.id');
213198
$version = config('version');
@@ -225,23 +210,7 @@ private function alive()
225210
echo "Error in alive: {$e->getMessage()}\n";
226211
}
227212
}
228-
// private function cleanup_ssh()
229-
// {
230213

231-
// TODO: it will cleanup [email protected]
232-
// try {
233-
// $files = Storage::allFiles('ssh/keys');
234-
// foreach ($files as $file) {
235-
// Storage::delete($file);
236-
// }
237-
// $files = Storage::allFiles('ssh/mux');
238-
// foreach ($files as $file) {
239-
// Storage::delete($file);
240-
// }
241-
// } catch (\Throwable $e) {
242-
// echo "Error in cleaning ssh: {$e->getMessage()}\n";
243-
// }
244-
// }
245214
private function cleanup_in_progress_application_deployments()
246215
{
247216
// Cleanup any failed deployments
@@ -263,11 +232,13 @@ private function cleanup_in_progress_application_deployments()
263232

264233
private function replace_slash_in_environment_name()
265234
{
266-
$environments = Environment::all();
267-
foreach ($environments as $environment) {
268-
if (str_contains($environment->name, '/')) {
269-
$environment->name = str_replace('/', '-', $environment->name);
270-
$environment->save();
235+
if (version_compare('4.0.0-beta.298', config('version'), '<=')) {
236+
$environments = Environment::all();
237+
foreach ($environments as $environment) {
238+
if (str_contains($environment->name, '/')) {
239+
$environment->name = str_replace('/', '-', $environment->name);
240+
$environment->save();
241+
}
271242
}
272243
}
273244
}

0 commit comments

Comments
 (0)