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;
|
2023-11-16 19:48:25 +00:00
|
|
|
use App\Jobs\CleanupHelperContainersJob;
|
2023-10-24 08:10:55 +00:00
|
|
|
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;
|
2023-12-27 22:06:22 +00:00
|
|
|
use App\Models\ScheduledDatabaseBackup;
|
2023-11-16 19:48:25 +00:00
|
|
|
use App\Models\Server;
|
2023-10-24 08:10:55 +00:00
|
|
|
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;
|
2023-10-24 08:10:55 +00:00
|
|
|
use App\Models\StandaloneMongodb;
|
2023-10-25 09:43:18 +00:00
|
|
|
use App\Models\StandaloneMysql;
|
2023-10-24 08:10:55 +00:00
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use App\Models\StandaloneRedis;
|
2023-06-30 09:42:59 +00:00
|
|
|
use Illuminate\Console\Command;
|
2023-11-15 09:18:41 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
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-08-08 09:51:36 +00:00
|
|
|
|
2023-06-30 09:42:59 +00:00
|
|
|
public function handle()
|
|
|
|
{
|
2023-11-15 09:18:41 +00:00
|
|
|
$this->alive();
|
2023-11-06 20:08:02 +00:00
|
|
|
$cleanup = $this->option('cleanup');
|
|
|
|
if ($cleanup) {
|
2023-11-29 14:03:21 +00:00
|
|
|
echo "Running cleanup\n";
|
2023-11-06 20:08:02 +00:00
|
|
|
$this->cleanup_stucked_resources();
|
2023-12-27 22:06:22 +00:00
|
|
|
// Required for falsely deleted coolify db
|
|
|
|
$this->restore_coolify_db_backup();
|
|
|
|
|
2023-11-29 14:23:03 +00:00
|
|
|
// $this->cleanup_ssh();
|
2023-11-06 20:08:02 +00:00
|
|
|
}
|
2023-06-30 20:26:40 +00:00
|
|
|
$this->cleanup_in_progress_application_deployments();
|
2023-11-16 19:48:25 +00:00
|
|
|
$this->cleanup_stucked_helper_containers();
|
2023-12-12 12:20:26 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
setup_dynamic_configuration();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
|
|
|
|
}
|
2023-12-11 19:55:58 +00:00
|
|
|
|
|
|
|
$settings = InstanceSettings::get();
|
2023-12-11 22:26:49 +00: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 19:55:58 +00:00
|
|
|
}
|
2023-11-16 19:48:25 +00:00
|
|
|
}
|
2023-12-27 22:06:22 +00:00
|
|
|
private function restore_coolify_db_backup() {
|
|
|
|
try {
|
|
|
|
$database = StandalonePostgresql::withTrashed()->find(0);
|
|
|
|
if ($database) {
|
|
|
|
echo "Restoring coolify db backup\n";
|
|
|
|
$database->restore();
|
|
|
|
$scheduledBackup = ScheduledDatabaseBackup::find(0);
|
|
|
|
if (!$scheduledBackup) {
|
|
|
|
ScheduledDatabaseBackup::create([
|
|
|
|
'id' => 0,
|
|
|
|
'enabled' => true,
|
|
|
|
'save_s3' => false,
|
|
|
|
'frequency' => '0 0 * * *',
|
|
|
|
'database_id' => $database->id,
|
|
|
|
'database_type' => 'App\Models\StandalonePostgresql',
|
|
|
|
'team_id' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch(\Throwable $e) {
|
|
|
|
echo "Error in restoring coolify db backup: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 15:19:40 +00:00
|
|
|
private function cleanup_stucked_helper_containers()
|
|
|
|
{
|
2023-11-16 19:48:25 +00:00
|
|
|
$servers = Server::all();
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
if ($server->isFunctional()) {
|
|
|
|
CleanupHelperContainersJob::dispatch($server);
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 20:26:40 +00:00
|
|
|
}
|
2023-11-15 09:18:41 +00:00
|
|
|
private function alive()
|
|
|
|
{
|
|
|
|
$id = config('app.id');
|
2023-11-17 12:59:45 +00:00
|
|
|
$version = config('version');
|
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) {
|
2023-11-15 09:37:02 +00:00
|
|
|
echo "Skipping alive as do_not_track is enabled\n";
|
2023-11-15 09:20:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-15 09:18:41 +00:00
|
|
|
try {
|
2023-11-17 12:53:56 +00:00
|
|
|
Http::get("https://get.coollabs.io/coolify/v4/alive?appId=$id&version=$version");
|
2023-11-15 09:37:02 +00:00
|
|
|
echo "I am alive!\n";
|
2023-11-15 09:18:41 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in alive: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 14:23:03 +00:00
|
|
|
// private function cleanup_ssh()
|
|
|
|
// {
|
|
|
|
|
2023-11-29 15:19:40 +00:00
|
|
|
// TODO: it will cleanup id.root@host.docker.internal
|
2023-11-29 14:23:03 +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) {
|
|
|
|
// echo "Error in cleaning ssh: {$e->getMessage()}\n";
|
|
|
|
// }
|
|
|
|
// }
|
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 {
|
2023-12-13 11:13:20 +00:00
|
|
|
$halted_deployments = ApplicationDeploymentQueue::where('status', '==', ApplicationDeploymentStatus::IN_PROGRESS)->where('status', '==', ApplicationDeploymentStatus::QUEUED)->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()
|
|
|
|
{
|
2023-10-24 08:10:55 +00:00
|
|
|
// 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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Application without environment: ' . $application->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$application->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
if (!$application->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Application without destination: ' . $application->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$application->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($application, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Application without server: ' . $application->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$application->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-06 17:04:18 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in application: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
try {
|
2023-12-27 22:06:22 +00:00
|
|
|
$postgresqls = StandalonePostgresql::all()->where('id', '!=', 0);
|
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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Postgresql without environment: ' . $postgresql->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$postgresql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
if (!$postgresql->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Postgresql without destination: ' . $postgresql->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$postgresql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($postgresql, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Postgresql without server: ' . $postgresql->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$postgresql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-06 17:04:18 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in postgresql: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
try {
|
2023-10-24 08:10:55 +00:00
|
|
|
$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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Redis without environment: ' . $redis->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$redis->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
if (!$redis->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Redis without destination: ' . $redis->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$redis->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($redis, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Redis without server: ' . $redis->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$redis->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-06 17:04:18 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in redis: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-10-24 08:10:55 +00:00
|
|
|
$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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mongodb without environment: ' . $mongodb->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$mongodb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
if (!$mongodb->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mongodb without destination: ' . $mongodb->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$mongodb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($mongodb, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mongodb without server: ' . $mongodb->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$mongodb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
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-12-13 11:08:12 +00:00
|
|
|
echo 'Mysql without environment: ' . $mysql->name . ' soft deleting\n';
|
2023-10-25 09:43:18 +00:00
|
|
|
$mysql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
|
|
|
if (!$mysql->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mysql without destination: ' . $mysql->name . ' soft deleting\n';
|
2023-10-25 09:43:18 +00:00
|
|
|
$mysql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($mysql, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mysql without server: ' . $mysql->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$mysql->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
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-12-13 11:08:12 +00:00
|
|
|
echo 'Mariadb without environment: ' . $mariadb->name . ' soft deleting\n';
|
2023-10-25 09:43:18 +00:00
|
|
|
$mariadb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
|
|
|
if (!$mariadb->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mariadb without destination: ' . $mariadb->name . ' soft deleting\n';
|
2023-10-25 09:43:18 +00:00
|
|
|
$mariadb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($mariadb, 'destination.server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Mariadb without server: ' . $mariadb->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$mariadb->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-25 09:43:18 +00:00
|
|
|
}
|
2023-11-06 17:04:18 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in mariadb: {$e->getMessage()}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-10-24 08:10:55 +00:00
|
|
|
$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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Service without environment: ' . $service->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$service->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
if (!$service->destination()) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Service without destination: ' . $service->name . ' soft deleting\n';
|
2023-10-24 08:10:55 +00:00
|
|
|
$service->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-29 14:03:21 +00:00
|
|
|
if (!data_get($service, 'server')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'Service without server: ' . $service->name . ' soft deleting\n';
|
2023-11-29 14:03:21 +00:00
|
|
|
$service->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-29 14:03:21 +00:00
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
|
|
|
} 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')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'ServiceApplication without service: ' . $service->name . ' soft deleting\n';
|
2023-11-06 17:04:18 +00:00
|
|
|
$service->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-06 17:04:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in serviceApplications: {$e->getMessage()}\n";
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-11-06 19:58:03 +00:00
|
|
|
try {
|
|
|
|
$serviceDatabases = ServiceDatabase::all();
|
|
|
|
foreach ($serviceDatabases as $service) {
|
|
|
|
if (!data_get($service, 'service')) {
|
2023-12-13 11:08:12 +00:00
|
|
|
echo 'ServiceDatabase without service: ' . $service->name . ' soft deleting\n';
|
2023-11-06 19:58:03 +00:00
|
|
|
$service->delete();
|
2023-12-13 11:08:12 +00:00
|
|
|
continue;
|
2023-11-06 19:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
echo "Error in ServiceDatabases: {$e->getMessage()}\n";
|
|
|
|
}
|
2023-10-24 08:10:55 +00:00
|
|
|
}
|
2023-06-30 09:42:59 +00:00
|
|
|
}
|