mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 21:04:44 +02:00

* Fix these * Update phpstan * Transform these into their identifiers instead * Fix custom rule * License is wrong * Update these * Pint fixes * Fix this * Consolidate these * Never supported PHP 7 * Better evaluation * Fixes * Don’t need ignore * Replace trait with service * Subusers are simply the many to many relationship between Servers and Users * Adjust to remove ignores * Use new query builder instead! * wip * Update composer * Quick fixes * Use realtime facade * Small fixes * Convert to static to avoid new * Update to statics * Don’t modify protected properties directly * Run pint * Change to correct method * Give up and use the facade * Make sure this route is available * Filament hasn’t been loaded yet * This can be readonly * Typehint * These are no longer used * Quick fixes * Need doc block help * Always true * We use caddy with docker * Pint * Fix phpstan issues * Remove unused import --------- Co-authored-by: MartinOscar <40749467+RMartinOscar@users.noreply.github.com>
100 lines
3.4 KiB
PHP
100 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Server;
|
|
|
|
use App\Models\Server;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\Validation\Factory as ValidatorFactory;
|
|
use App\Repositories\Daemon\DaemonPowerRepository;
|
|
use Exception;
|
|
|
|
class BulkPowerActionCommand extends Command
|
|
{
|
|
protected $signature = 'p:server:bulk-power
|
|
{action : The action to perform (start, stop, restart, kill)}
|
|
{--servers= : A comma separated list of servers.}
|
|
{--nodes= : A comma separated list of nodes.}';
|
|
|
|
protected $description = 'Perform bulk power management on large groupings of servers or nodes at once.';
|
|
|
|
public function handle(DaemonPowerRepository $powerRepository, ValidatorFactory $validator): void
|
|
{
|
|
$action = $this->argument('action');
|
|
$nodes = empty($this->option('nodes')) ? [] : explode(',', $this->option('nodes'));
|
|
$servers = empty($this->option('servers')) ? [] : explode(',', $this->option('servers'));
|
|
|
|
$validator = $validator->make([
|
|
'action' => $action,
|
|
'nodes' => $nodes,
|
|
'servers' => $servers,
|
|
], [
|
|
'action' => 'string|in:start,stop,kill,restart',
|
|
'nodes' => 'array',
|
|
'nodes.*' => 'integer|min:1',
|
|
'servers' => 'array',
|
|
'servers.*' => 'integer|min:1',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
foreach ($validator->getMessageBag()->all() as $message) {
|
|
$this->output->error($message);
|
|
}
|
|
|
|
throw new ValidationException($validator);
|
|
}
|
|
|
|
$count = $this->getQueryBuilder($servers, $nodes)->count();
|
|
if (!$this->confirm(trans('command/messages.server.power.confirm', ['action' => $action, 'count' => $count])) && $this->input->isInteractive()) {
|
|
return;
|
|
}
|
|
|
|
$bar = $this->output->createProgressBar($count);
|
|
|
|
$this->getQueryBuilder($servers, $nodes)->get()->each(function ($server, int $index) use ($action, $powerRepository, &$bar): mixed {
|
|
$bar->clear();
|
|
|
|
if (!$server instanceof Server) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$powerRepository->setServer($server)->send($action);
|
|
} catch (Exception $exception) {
|
|
$this->output->error(trans('command/messages.server.power.action_failed', [
|
|
'name' => $server->name,
|
|
'id' => $server->id,
|
|
'node' => $server->node->name,
|
|
'message' => $exception->getMessage(),
|
|
]));
|
|
}
|
|
|
|
$bar->advance();
|
|
$bar->display();
|
|
|
|
return null;
|
|
});
|
|
|
|
$this->line('');
|
|
}
|
|
|
|
/**
|
|
* Returns the query builder instance that will return the servers that should be affected.
|
|
*/
|
|
protected function getQueryBuilder(array $servers, array $nodes): Builder
|
|
{
|
|
$instance = Server::query()->whereNull('status');
|
|
|
|
if (!empty($nodes) && !empty($servers)) {
|
|
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
|
|
} elseif (empty($nodes) && !empty($servers)) {
|
|
$instance->whereIn('id', $servers);
|
|
} elseif (!empty($nodes)) {
|
|
$instance->whereIn('node_id', $nodes);
|
|
}
|
|
|
|
return $instance->with('node');
|
|
}
|
|
}
|