5
5
use App \Actions \Server \StopSentinel ;
6
6
use App \Enums \ActivityTypes ;
7
7
use App \Enums \ApplicationDeploymentStatus ;
8
- use App \Jobs \CleanupHelperContainersJob ;
9
8
use App \Models \ApplicationDeploymentQueue ;
10
9
use App \Models \Environment ;
11
10
use App \Models \InstanceSettings ;
18
17
19
18
class Init extends Command
20
19
{
21
- protected $ signature = 'app:init {--full-cleanup} {--cleanup-deployments} {--cleanup-proxy-networks } ' ;
20
+ protected $ signature = 'app:init {--force-cloud } ' ;
22
21
23
22
protected $ description = 'Cleanup instance related stuffs ' ;
24
23
25
24
public $ servers = null ;
26
25
27
26
public function handle ()
28
27
{
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" ;
50
30
51
31
return ;
52
32
}
53
- if ($ cleanup_proxy_networks ) {
54
- echo "Running cleanup proxy networks. \n" ;
55
- $ this ->cleanup_unused_network_from_coolify_proxy ();
56
33
57
- return ;
34
+ $ this ->servers = Server::all ();
35
+ if (isCloud ()) {
36
+
37
+ } else {
38
+ $ this ->send_alive_signal ();
39
+ get_public_ips ();
58
40
}
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 ' )) {
63
49
$ this ->cleanup_unused_network_from_coolify_proxy ();
50
+ }
51
+ if (isCloud ()) {
64
52
$ this ->cleanup_unnecessary_dynamic_proxy_configuration ();
53
+ } else {
65
54
$ 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 ' );
77
58
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
+ }
78
72
$ settings = InstanceSettings::get ();
79
73
if (! is_null (env ('AUTOUPDATE ' , null ))) {
80
74
if (env ('AUTOUPDATE ' ) == true ) {
@@ -83,18 +77,21 @@ public function handle()
83
77
$ settings ->update (['is_auto_update_enabled ' => false ]);
84
78
}
85
79
}
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 );
91
92
}
92
93
}
93
-
94
- return ;
95
94
}
96
- $ this ->cleanup_stucked_helper_containers ();
97
- $ this ->call ('cleanup:stucked-resources ' );
98
95
}
99
96
100
97
private function update_traefik_labels ()
@@ -108,33 +105,28 @@ private function update_traefik_labels()
108
105
109
106
private function cleanup_unnecessary_dynamic_proxy_configuration ()
110
107
{
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 ;
127
112
}
113
+ if ($ server ->id === 0 ) {
114
+ continue ;
115
+ }
116
+ $ file = $ server ->proxyPath ().'/dynamic/coolify.yaml ' ;
128
117
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" ;
129
123
}
124
+
130
125
}
131
126
}
132
127
133
128
private function cleanup_unused_network_from_coolify_proxy ()
134
129
{
135
- if (isCloud ()) {
136
- return ;
137
- }
138
130
foreach ($ this ->servers as $ server ) {
139
131
if (! $ server ->isFunctional ()) {
140
132
continue ;
@@ -175,39 +167,32 @@ private function cleanup_unused_network_from_coolify_proxy()
175
167
176
168
private function restore_coolify_db_backup ()
177
169
{
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
+ }
194
188
}
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" ;
206
191
}
207
192
}
208
193
}
209
194
210
- private function alive ()
195
+ private function send_alive_signal ()
211
196
{
212
197
$ id = config ('app.id ' );
213
198
$ version = config ('version ' );
@@ -225,23 +210,7 @@ private function alive()
225
210
echo "Error in alive: {$ e ->getMessage ()}\n" ;
226
211
}
227
212
}
228
- // private function cleanup_ssh()
229
- // {
230
213
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
- // }
245
214
private function cleanup_in_progress_application_deployments ()
246
215
{
247
216
// Cleanup any failed deployments
@@ -263,11 +232,13 @@ private function cleanup_in_progress_application_deployments()
263
232
264
233
private function replace_slash_in_environment_name ()
265
234
{
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
+ }
271
242
}
272
243
}
273
244
}
0 commit comments