lasthourcloud/app/Console/Commands/Init.php

245 lines
8.9 KiB
PHP
Raw Normal View History

2023-06-30 09:42:59 +00:00
<?php
namespace App\Console\Commands;
2023-06-30 20:26:40 +00:00
use App\Enums\ApplicationDeploymentStatus;
use App\Models\Application;
2023-06-30 09:42:59 +00:00
use App\Models\ApplicationDeploymentQueue;
2023-11-15 09:20:48 +00:00
use App\Models\InstanceSettings;
use App\Models\Service;
2023-11-06 17:04:18 +00:00
use App\Models\ServiceApplication;
2023-11-06 19:58:03 +00:00
use App\Models\ServiceDatabase;
2023-11-06 17:04:18 +00:00
use App\Models\StandaloneMariadb;
use App\Models\StandaloneMongodb;
2023-10-25 09:43:18 +00:00
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
2023-06-30 09:42:59 +00:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
2023-10-25 07:41:41 +00:00
use Illuminate\Support\Facades\Storage;
2023-06-30 09:42:59 +00:00
class Init extends Command
{
2023-11-06 20:08:02 +00:00
protected $signature = 'app:init {--cleanup}';
2023-06-30 09:42:59 +00:00
protected $description = 'Cleanup instance related stuffs';
2023-06-30 09:42:59 +00:00
public function handle()
{
$this->alive();
2023-11-06 20:08:02 +00:00
$cleanup = $this->option('cleanup');
if ($cleanup) {
$this->cleanup_stucked_resources();
$this->cleanup_ssh();
}
2023-06-30 20:26:40 +00:00
$this->cleanup_in_progress_application_deployments();
}
private function alive()
{
$id = config('app.id');
2023-11-15 09:20:48 +00:00
$settings = InstanceSettings::get();
$do_not_track = data_get($settings, 'do_not_track');
2023-11-15 09:26:31 +00:00
if ($do_not_track == true) {
echo "Skipping alive as do_not_track is enabled\n";
2023-11-15 09:20:48 +00:00
return;
}
try {
echo "I am alive!\n";
Http::get("https://get.coollabs.io/coolify/v4/alive?appId=$id");
} catch (\Throwable $e) {
echo "Error in alive: {$e->getMessage()}\n";
}
}
2023-10-25 09:43:18 +00:00
private function cleanup_ssh()
{
2023-10-25 07:41:41 +00: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) {
2023-11-06 19:58:03 +00:00
echo "Error in cleaning ssh: {$e->getMessage()}\n";
2023-10-25 07:41:41 +00:00
}
}
2023-06-30 20:26:40 +00:00
private function cleanup_in_progress_application_deployments()
{
// Cleanup any failed deployments
2023-06-30 09:42:59 +00:00
try {
$halted_deployments = ApplicationDeploymentQueue::where('status', '==', 'in_progress')->get();
2023-06-30 20:26:40 +00:00
foreach ($halted_deployments as $deployment) {
$deployment->status = ApplicationDeploymentStatus::FAILED->value;
$deployment->save();
}
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-06-30 09:42:59 +00:00
echo "Error: {$e->getMessage()}\n";
}
}
2023-10-25 09:43:18 +00: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 09:43:18 +00:00
foreach ($applications as $application) {
2023-11-06 17:04:18 +00:00
if (!data_get($application, 'environment')) {
ray('Application without environment', $application->name);
$application->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($application, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Application without server', $application->name);
$application->delete();
}
if (!$application->destination()) {
ray('Application without destination', $application->name);
$application->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in application: {$e->getMessage()}\n";
}
try {
$postgresqls = StandalonePostgresql::all();
2023-10-25 09:43:18 +00:00
foreach ($postgresqls as $postgresql) {
2023-11-06 17:04:18 +00:00
if (!data_get($postgresql, 'environment')) {
ray('Postgresql without environment', $postgresql->name);
$postgresql->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($postgresql, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Postgresql without server', $postgresql->name);
$postgresql->delete();
}
if (!$postgresql->destination()) {
ray('Postgresql without destination', $postgresql->name);
$postgresql->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in postgresql: {$e->getMessage()}\n";
}
try {
$redis = StandaloneRedis::all();
2023-10-25 09:43:18 +00:00
foreach ($redis as $redis) {
2023-11-06 17:04:18 +00:00
if (!data_get($redis, 'environment')) {
ray('Redis without environment', $redis->name);
$redis->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($redis, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Redis without server', $redis->name);
$redis->delete();
}
if (!$redis->destination()) {
ray('Redis without destination', $redis->name);
$redis->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in redis: {$e->getMessage()}\n";
}
try {
$mongodbs = StandaloneMongodb::all();
2023-10-25 09:43:18 +00:00
foreach ($mongodbs as $mongodb) {
2023-11-06 17:04:18 +00:00
if (!data_get($mongodb, 'environment')) {
ray('Mongodb without environment', $mongodb->name);
$mongodb->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($mongodb, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Mongodb without server', $mongodb->name);
$mongodb->delete();
}
if (!$mongodb->destination()) {
ray('Mongodb without destination', $mongodb->name);
$mongodb->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in mongodb: {$e->getMessage()}\n";
}
try {
2023-10-25 09:43:18 +00:00
$mysqls = StandaloneMysql::all();
foreach ($mysqls as $mysql) {
2023-11-06 17:04:18 +00:00
if (!data_get($mysql, 'environment')) {
2023-10-25 09:43:18 +00:00
ray('Mysql without environment', $mysql->name);
$mysql->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($mysql, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Mysql without server', $mysql->name);
$mysql->delete();
}
2023-10-25 09:43:18 +00:00
if (!$mysql->destination()) {
ray('Mysql without destination', $mysql->name);
$mysql->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in mysql: {$e->getMessage()}\n";
}
try {
$mariadbs = StandaloneMariadb::all();
2023-10-25 09:43:18 +00:00
foreach ($mariadbs as $mariadb) {
2023-11-06 17:04:18 +00:00
if (!data_get($mariadb, 'environment')) {
2023-10-25 09:43:18 +00:00
ray('Mariadb without environment', $mariadb->name);
$mariadb->delete();
}
2023-11-06 17:15:23 +00:00
if (!data_get($mariadb, 'destination.server')) {
2023-11-06 17:04:18 +00:00
ray('Mariadb without server', $mariadb->name);
$mariadb->delete();
}
2023-10-25 09:43:18 +00:00
if (!$mariadb->destination()) {
ray('Mariadb without destination', $mariadb->name);
$mariadb->delete();
}
}
2023-11-06 17:04:18 +00:00
} catch (\Throwable $e) {
echo "Error in mariadb: {$e->getMessage()}\n";
}
try {
$services = Service::all();
2023-10-25 09:43:18 +00:00
foreach ($services as $service) {
2023-11-06 17:04:18 +00:00
if (!data_get($service, 'environment')) {
ray('Service without environment', $service->name);
$service->delete();
}
2023-11-06 17:04:18 +00:00
if (!data_get($service, 'server')) {
ray('Service without server', $service->name);
$service->delete();
}
if (!$service->destination()) {
ray('Service without destination', $service->name);
$service->delete();
}
}
} catch (\Throwable $e) {
2023-11-06 17:04:18 +00:00
echo "Error in service: {$e->getMessage()}\n";
}
try {
$serviceApplications = ServiceApplication::all();
foreach ($serviceApplications as $service) {
if (!data_get($service, 'service')) {
ray('ServiceApplication without service', $service->name);
$service->delete();
}
}
} catch (\Throwable $e) {
echo "Error in serviceApplications: {$e->getMessage()}\n";
}
2023-11-06 19:58:03 +00:00
try {
$serviceDatabases = ServiceDatabase::all();
foreach ($serviceDatabases as $service) {
if (!data_get($service, 'service')) {
ray('ServiceDatabase without service', $service->name);
$service->delete();
}
}
} catch (\Throwable $e) {
echo "Error in ServiceDatabases: {$e->getMessage()}\n";
}
}
2023-06-30 09:42:59 +00:00
}