feat: delete resource command
This commit is contained in:
parent
51e601a303
commit
5580a4e704
86
app/Console/Commands/DeleteResource.php
Normal file
86
app/Console/Commands/DeleteResource.php
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\Application;
|
||||||
|
use App\Models\Service;
|
||||||
|
use App\Models\StandalonePostgresql;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
use function Laravel\Prompts\confirm;
|
||||||
|
use function Laravel\Prompts\select;
|
||||||
|
|
||||||
|
class DeleteResource extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'app:delete-resource';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Delete a resource from the database';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$resource = select(
|
||||||
|
'What resource do you want to delete?',
|
||||||
|
['Application', 'Database', 'Service'],
|
||||||
|
);
|
||||||
|
if ($resource === 'Application') {
|
||||||
|
$this->deleteApplication();
|
||||||
|
} elseif ($resource === 'Database') {
|
||||||
|
$this->deleteDatabase();
|
||||||
|
} elseif ($resource === 'Service') {
|
||||||
|
$this->deleteService();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private function deleteApplication() {
|
||||||
|
$applications = Application::all();
|
||||||
|
$application = select(
|
||||||
|
'What application do you want to delete?',
|
||||||
|
$applications->pluck('name')->toArray(),
|
||||||
|
);
|
||||||
|
$application = $applications->where('name', $application)->first();
|
||||||
|
$confirmed = confirm("Are you sure you want to delete {$application->name}?");
|
||||||
|
if (!$confirmed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$application->delete();
|
||||||
|
}
|
||||||
|
private function deleteDatabase() {
|
||||||
|
$databases = StandalonePostgresql::all();
|
||||||
|
$database = select(
|
||||||
|
'What database do you want to delete?',
|
||||||
|
$databases->pluck('name')->toArray(),
|
||||||
|
);
|
||||||
|
$database = $databases->where('name', $database)->first();
|
||||||
|
$confirmed = confirm("Are you sure you want to delete {$database->name}?");
|
||||||
|
if (!$confirmed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$database->delete();
|
||||||
|
}
|
||||||
|
private function deleteService() {
|
||||||
|
$services = Service::all();
|
||||||
|
$service = select(
|
||||||
|
'What service do you want to delete?',
|
||||||
|
$services->pluck('name')->toArray(),
|
||||||
|
);
|
||||||
|
$service = $services->where('name', $service)->first();
|
||||||
|
$confirmed = confirm("Are you sure you want to delete {$service->name}?");
|
||||||
|
if (!$confirmed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$service->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -18,10 +18,16 @@ class Application extends BaseModel
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
static::deleting(function ($application) {
|
static::deleting(function ($application) {
|
||||||
|
// Stop Container
|
||||||
|
instant_remote_process(
|
||||||
|
["docker rm -f {$application->uuid}"],
|
||||||
|
$application->destination->server,
|
||||||
|
false
|
||||||
|
);
|
||||||
$application->settings()->delete();
|
$application->settings()->delete();
|
||||||
$storages = $application->persistentStorages()->get();
|
$storages = $application->persistentStorages()->get();
|
||||||
foreach ($storages as $storage) {
|
foreach ($storages as $storage) {
|
||||||
instant_remote_process(["docker volume rm -f $storage->name"], $application->destination->server);
|
instant_remote_process(["docker volume rm -f $storage->name"], $application->destination->server, false);
|
||||||
}
|
}
|
||||||
$application->persistentStorages()->delete();
|
$application->persistentStorages()->delete();
|
||||||
$application->environment_variables()->delete();
|
$application->environment_variables()->delete();
|
||||||
@ -233,7 +239,7 @@ class Application extends BaseModel
|
|||||||
}
|
}
|
||||||
public function isHealthcheckDisabled(): bool
|
public function isHealthcheckDisabled(): bool
|
||||||
{
|
{
|
||||||
if (data_get($this, 'dockerfile') || data_get($this, 'build_pack') === 'dockerfile' || data_get($this,'health_check_enabled') === false) {
|
if (data_get($this, 'dockerfile') || data_get($this, 'build_pack') === 'dockerfile' || data_get($this, 'health_check_enabled') === false) {
|
||||||
ray('dockerfile');
|
ray('dockerfile');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ use Illuminate\Support\Collection;
|
|||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Spatie\Url\Url;
|
|
||||||
|
|
||||||
class Service extends BaseModel
|
class Service extends BaseModel
|
||||||
{
|
{
|
||||||
@ -20,6 +19,7 @@ class Service extends BaseModel
|
|||||||
static::deleted(function ($service) {
|
static::deleted(function ($service) {
|
||||||
$storagesToDelete = collect([]);
|
$storagesToDelete = collect([]);
|
||||||
foreach ($service->applications()->get() as $application) {
|
foreach ($service->applications()->get() as $application) {
|
||||||
|
instant_remote_process(["docker rm -f {$application->name}-{$service->uuid}"], $service->server, false);
|
||||||
$storages = $application->persistentStorages()->get();
|
$storages = $application->persistentStorages()->get();
|
||||||
foreach ($storages as $storage) {
|
foreach ($storages as $storage) {
|
||||||
$storagesToDelete->push($storage);
|
$storagesToDelete->push($storage);
|
||||||
@ -27,6 +27,7 @@ class Service extends BaseModel
|
|||||||
$application->persistentStorages()->delete();
|
$application->persistentStorages()->delete();
|
||||||
}
|
}
|
||||||
foreach ($service->databases()->get() as $database) {
|
foreach ($service->databases()->get() as $database) {
|
||||||
|
instant_remote_process(["docker rm -f {$database->name}-{$service->uuid}"], $service->server, false);
|
||||||
$storages = $database->persistentStorages()->get();
|
$storages = $database->persistentStorages()->get();
|
||||||
foreach ($storages as $storage) {
|
foreach ($storages as $storage) {
|
||||||
$storagesToDelete->push($storage);
|
$storagesToDelete->push($storage);
|
||||||
|
@ -29,9 +29,20 @@ class StandalonePostgresql extends BaseModel
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
static::deleted(function ($database) {
|
static::deleted(function ($database) {
|
||||||
|
// Stop Container
|
||||||
|
instant_remote_process(
|
||||||
|
["docker rm -f {$database->uuid}"],
|
||||||
|
$database->destination->server,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
// Stop TCP Proxy
|
||||||
|
if ($database->is_public) {
|
||||||
|
instant_remote_process(["docker rm -f {$database->uuid}-proxy"], $database->destination->server, false);
|
||||||
|
}
|
||||||
$database->scheduledBackups()->delete();
|
$database->scheduledBackups()->delete();
|
||||||
$database->persistentStorages()->delete();
|
$database->persistentStorages()->delete();
|
||||||
$database->environment_variables()->delete();
|
$database->environment_variables()->delete();
|
||||||
|
// Remove Volume
|
||||||
instant_remote_process(['docker volume rm postgres-data-' . $database->uuid], $database->destination->server, false);
|
instant_remote_process(['docker volume rm postgres-data-' . $database->uuid], $database->destination->server, false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user