coolify/app/Console/Commands/Init.php

275 lines
10 KiB
PHP
Raw Normal View History

2023-06-30 11:42:59 +02:00
<?php
namespace App\Console\Commands;
2023-06-30 22:26:40 +02:00
use App\Enums\ApplicationDeploymentStatus;
use App\Jobs\CleanupHelperContainersJob;
use App\Models\Application;
2023-06-30 11:42:59 +02:00
use App\Models\ApplicationDeploymentQueue;
2023-11-15 10:20:48 +01:00
use App\Models\InstanceSettings;
use App\Models\Server;
use App\Models\Service;
2023-11-06 18:04:18 +01:00
use App\Models\ServiceApplication;
2023-11-06 20:58:03 +01:00
use App\Models\ServiceDatabase;
2023-11-06 18:04:18 +01:00
use App\Models\StandaloneMariadb;
use App\Models\StandaloneMongodb;
2023-10-25 11:43:18 +02:00
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
2023-06-30 11:42:59 +02:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
2023-06-30 11:42:59 +02:00
class Init extends Command
{
2023-11-06 21:08:02 +01:00
protected $signature = 'app:init {--cleanup}';
2023-06-30 11:42:59 +02:00
protected $description = 'Cleanup instance related stuffs';
2023-06-30 11:42:59 +02:00
public function handle()
{
$this->alive();
2023-11-06 21:08:02 +01:00
$cleanup = $this->option('cleanup');
if ($cleanup) {
2023-11-29 15:03:21 +01:00
echo "Running cleanup\n";
2023-11-06 21:08:02 +01:00
$this->cleanup_stucked_resources();
2023-11-29 15:23:03 +01:00
// $this->cleanup_ssh();
2023-11-06 21:08:02 +01:00
}
2023-06-30 22:26:40 +02:00
$this->cleanup_in_progress_application_deployments();
$this->cleanup_stucked_helper_containers();
try {
setup_dynamic_configuration();
} catch (\Throwable $e) {
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
}
2023-12-11 20:55:58 +01:00
$settings = InstanceSettings::get();
2023-12-11 23:26:49 +01:00
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]);
}
2023-12-11 20:55:58 +01:00
}
}
private function cleanup_stucked_helper_containers()
{
$servers = Server::all();
foreach ($servers as $server) {
if ($server->isFunctional()) {
CleanupHelperContainersJob::dispatch($server);
}
}
2023-06-30 22:26:40 +02:00
}
private function alive()
{
$id = config('app.id');
2023-11-17 13:59:45 +01:00
$version = config('version');
2023-11-15 10:20:48 +01:00
$settings = InstanceSettings::get();
$do_not_track = data_get($settings, 'do_not_track');
2023-11-15 10:26:31 +01:00
if ($do_not_track == true) {
echo "Skipping alive as do_not_track is enabled\n";
2023-11-15 10:20:48 +01:00
return;
}
try {
Http::get("https://get.coollabs.io/coolify/v4/alive?appId=$id&version=$version");
echo "I am alive!\n";
} catch (\Throwable $e) {
echo "Error in alive: {$e->getMessage()}\n";
}
}
2023-11-29 15:23:03 +01:00
// private function cleanup_ssh()
// {
// TODO: it will cleanup id.root@host.docker.internal
2023-11-29 15:23:03 +01:00
// 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";
// }
// }
2023-06-30 22:26:40 +02:00
private function cleanup_in_progress_application_deployments()
{
// Cleanup any failed deployments
2023-06-30 11:42:59 +02:00
try {
$halted_deployments = ApplicationDeploymentQueue::where('status', '==', 'in_progress')->get();
2023-06-30 22:26:40 +02:00
foreach ($halted_deployments as $deployment) {
$deployment->status = ApplicationDeploymentStatus::FAILED->value;
$deployment->save();
}
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-06-30 11:42:59 +02:00
echo "Error: {$e->getMessage()}\n";
}
}
2023-10-25 11:43:18 +02:00
private function cleanup_stucked_resources()
{
// Cleanup any resources that are not attached to any environment or destination or server
try {
$applications = Application::all();
2023-10-25 11:43:18 +02:00
foreach ($applications as $application) {
2023-11-06 18:04:18 +01:00
if (!data_get($application, 'environment')) {
echo 'Application without environment' . $application->name . 'deleting\n';
$application->delete();
}
if (!$application->destination()) {
echo 'Application without destination' . $application->name . 'deleting\n';
$application->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($application, 'destination.server')) {
echo 'Application without server' . $application->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$application->delete();
}
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in application: {$e->getMessage()}\n";
}
try {
$postgresqls = StandalonePostgresql::all();
2023-10-25 11:43:18 +02:00
foreach ($postgresqls as $postgresql) {
2023-11-06 18:04:18 +01:00
if (!data_get($postgresql, 'environment')) {
echo 'Postgresql without environment' . $postgresql->name . 'deleting\n';
$postgresql->delete();
}
if (!$postgresql->destination()) {
echo 'Postgresql without destination' . $postgresql->name . 'deleting\n';
$postgresql->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($postgresql, 'destination.server')) {
echo 'Postgresql without server' . $postgresql->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$postgresql->delete();
}
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in postgresql: {$e->getMessage()}\n";
}
try {
$redis = StandaloneRedis::all();
2023-10-25 11:43:18 +02:00
foreach ($redis as $redis) {
2023-11-06 18:04:18 +01:00
if (!data_get($redis, 'environment')) {
echo 'Redis without environment' . $redis->name . 'deleting\n';
$redis->delete();
}
if (!$redis->destination()) {
echo 'Redis without destination' . $redis->name . 'deleting\n';
$redis->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($redis, 'destination.server')) {
echo 'Redis without server' . $redis->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$redis->delete();
}
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in redis: {$e->getMessage()}\n";
}
try {
$mongodbs = StandaloneMongodb::all();
2023-10-25 11:43:18 +02:00
foreach ($mongodbs as $mongodb) {
2023-11-06 18:04:18 +01:00
if (!data_get($mongodb, 'environment')) {
echo 'Mongodb without environment' . $mongodb->name . 'deleting\n';
$mongodb->delete();
}
if (!$mongodb->destination()) {
echo 'Mongodb without destination' . $mongodb->name . 'deleting\n';
$mongodb->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($mongodb, 'destination.server')) {
echo 'Mongodb without server' . $mongodb->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$mongodb->delete();
}
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in mongodb: {$e->getMessage()}\n";
}
try {
2023-10-25 11:43:18 +02:00
$mysqls = StandaloneMysql::all();
foreach ($mysqls as $mysql) {
2023-11-06 18:04:18 +01:00
if (!data_get($mysql, 'environment')) {
echo 'Mysql without environment' . $mysql->name . 'deleting\n';
2023-10-25 11:43:18 +02:00
$mysql->delete();
}
if (!$mysql->destination()) {
echo 'Mysql without destination' . $mysql->name . 'deleting\n';
2023-10-25 11:43:18 +02:00
$mysql->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($mysql, 'destination.server')) {
echo 'Mysql without server' . $mysql->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$mysql->delete();
}
2023-10-25 11:43:18 +02:00
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in mysql: {$e->getMessage()}\n";
}
try {
$mariadbs = StandaloneMariadb::all();
2023-10-25 11:43:18 +02:00
foreach ($mariadbs as $mariadb) {
2023-11-06 18:04:18 +01:00
if (!data_get($mariadb, 'environment')) {
echo 'Mariadb without environment' . $mariadb->name . 'deleting\n';
2023-10-25 11:43:18 +02:00
$mariadb->delete();
}
if (!$mariadb->destination()) {
echo 'Mariadb without destination' . $mariadb->name . 'deleting\n';
2023-10-25 11:43:18 +02:00
$mariadb->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($mariadb, 'destination.server')) {
echo 'Mariadb without server' . $mariadb->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$mariadb->delete();
}
2023-10-25 11:43:18 +02:00
}
2023-11-06 18:04:18 +01:00
} catch (\Throwable $e) {
echo "Error in mariadb: {$e->getMessage()}\n";
}
try {
$services = Service::all();
2023-10-25 11:43:18 +02:00
foreach ($services as $service) {
2023-11-06 18:04:18 +01:00
if (!data_get($service, 'environment')) {
echo 'Service without environment' . $service->name . 'deleting\n';
$service->delete();
}
if (!$service->destination()) {
echo 'Service without destination' . $service->name . 'deleting\n';
$service->delete();
}
2023-11-29 15:03:21 +01:00
if (!data_get($service, 'server')) {
echo 'Service without server' . $service->name . 'deleting\n';
2023-11-29 15:03:21 +01:00
$service->delete();
}
}
} catch (\Throwable $e) {
2023-11-06 18:04:18 +01:00
echo "Error in service: {$e->getMessage()}\n";
}
try {
$serviceApplications = ServiceApplication::all();
foreach ($serviceApplications as $service) {
if (!data_get($service, 'service')) {
echo 'ServiceApplication without service' . $service->name . 'deleting\n';
2023-11-06 18:04:18 +01:00
$service->delete();
}
}
} catch (\Throwable $e) {
echo "Error in serviceApplications: {$e->getMessage()}\n";
}
2023-11-06 20:58:03 +01:00
try {
$serviceDatabases = ServiceDatabase::all();
foreach ($serviceDatabases as $service) {
if (!data_get($service, 'service')) {
echo 'ServiceDatabase without service' . $service->name . 'deleting\n';
2023-11-06 20:58:03 +01:00
$service->delete();
}
}
} catch (\Throwable $e) {
echo "Error in ServiceDatabases: {$e->getMessage()}\n";
}
}
2023-06-30 11:42:59 +02:00
}