coolify/app/Console/Commands/Init.php

107 lines
3.8 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\Models\Application;
2023-06-30 11:42:59 +02:00
use App\Models\ApplicationDeploymentQueue;
use App\Models\Service;
use App\Models\StandaloneMongodb;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
2023-06-30 11:42:59 +02:00
use Illuminate\Console\Command;
class Init extends Command
{
protected $signature = 'app:init';
protected $description = 'Cleanup instance related stuffs';
2023-06-30 11:42:59 +02:00
public function handle()
{
ray()->clearAll();
2023-06-30 22:26:40 +02:00
$this->cleanup_in_progress_application_deployments();
$this->cleanup_stucked_resources();
2023-06-30 22:26:40 +02:00
}
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";
}
}
private function cleanup_stucked_resources() {
// Cleanup any resources that are not attached to any environment or destination or server
try {
$applications = Application::all();
foreach($applications as $application) {
if (!$application->environment) {
ray('Application without environment', $application->name);
$application->delete();
}
if (!$application->destination()) {
ray('Application without destination', $application->name);
$application->delete();
}
}
$postgresqls = StandalonePostgresql::all();
foreach($postgresqls as $postgresql) {
if (!$postgresql->environment) {
ray('Postgresql without environment', $postgresql->name);
$postgresql->delete();
}
if (!$postgresql->destination()) {
ray('Postgresql without destination', $postgresql->name);
$postgresql->delete();
}
}
$redis = StandaloneRedis::all();
foreach($redis as $redis) {
if (!$redis->environment) {
ray('Redis without environment', $redis->name);
$redis->delete();
}
if (!$redis->destination()) {
ray('Redis without destination', $redis->name);
$redis->delete();
}
}
$mongodbs = StandaloneMongodb::all();
foreach($mongodbs as $mongodb) {
if (!$mongodb->environment) {
ray('Mongodb without environment', $mongodb->name);
$mongodb->delete();
}
if (!$mongodb->destination()) {
ray('Mongodb without destination', $mongodb->name);
$mongodb->delete();
}
}
$services = Service::all();
foreach($services as $service) {
if (!$service->environment) {
ray('Service without environment', $service->name);
$service->delete();
}
if (!$service->server) {
ray('Service without server', $service->name);
$service->delete();
}
if (!$service->destination()) {
ray('Service without destination', $service->name);
$service->delete();
}
}
} catch (\Throwable $e) {
echo "Error: {$e->getMessage()}\n";
}
}
2023-06-30 11:42:59 +02:00
}