Files
pterodactyl-panel/app/Console/Commands/Server/BulkPowerActionCommand.php
T

104 lines
3.7 KiB
PHP
Raw Normal View History

2018-03-02 20:58:58 -06:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-03-02 20:58:58 -06:00
namespace Pterodactyl\Console\Commands\Server;
2026-10-05 18:10:18 -04:00
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
2018-03-02 20:58:58 -06:00
use Illuminate\Console\Command;
2022-10-14 10:59:20 -06:00
use Illuminate\Database\Eloquent\Builder;
2018-03-02 20:58:58 -06:00
use Illuminate\Validation\Factory as ValidatorFactory;
2026-10-05 18:10:18 -04:00
use Illuminate\Validation\ValidationException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Server;
2018-03-02 20:58:58 -06:00
2026-10-05 18:10:18 -04:00
#[Description('Perform bulk power management on large groupings of servers or nodes at once.')]
#[Signature('p:server:bulk-power
2018-03-02 20:58:58 -06:00
{action : The action to perform (start, stop, restart, kill)}
2018-05-13 10:50:56 -04:00
{--servers= : A comma separated list of servers.}
2026-10-05 18:10:18 -04:00
{--nodes= : A comma separated list of nodes.}')]
class BulkPowerActionCommand extends Command
{
2018-03-02 20:58:58 -06:00
/**
* Handle the bulk power request.
2018-03-02 20:58:58 -06:00
*
2024-10-21 19:18:20 -06:00
* @throws ValidationException
2018-03-02 20:58:58 -06:00
*/
2026-10-05 18:10:18 -04:00
public function handle(ValidatorFactory $validators): void
2018-03-02 20:58:58 -06:00
{
$action = $this->argument('action');
$nodes = empty($this->option('nodes')) ? [] : explode(',', $this->option('nodes'));
$servers = empty($this->option('servers')) ? [] : explode(',', $this->option('servers'));
2026-10-05 18:10:18 -04:00
$validator = $validators->make([
2018-03-02 20:58:58 -06:00
'action' => $action,
'nodes' => $nodes,
'servers' => $servers,
], [
2026-10-05 18:10:18 -04:00
'action' => ['string', 'in:start,stop,kill,restart'],
'nodes' => ['array'],
'nodes.*' => ['integer', 'min:1'],
'servers' => ['array'],
'servers.*' => ['integer', 'min:1'],
2018-03-02 20:58:58 -06:00
]);
if ($validator->fails()) {
foreach ($validator->getMessageBag()->all() as $message) {
$this->output->error($message);
}
2018-03-02 21:26:42 -06:00
throw new ValidationException($validator);
2018-03-02 20:58:58 -06:00
}
$count = $this->getQueryBuilder($servers, $nodes)->count();
2026-10-05 18:10:18 -04:00
if ($this->input->isInteractive() && ! $this->confirm(trans('command/messages.server.power.confirm', ['action' => $action, 'count' => $count]))) {
2018-03-02 20:58:58 -06:00
return;
}
$bar = $this->output->createProgressBar($count);
2026-10-05 18:10:18 -04:00
$this->getQueryBuilder($servers, $nodes)->each(function (Server $server) use ($action, &$bar): void {
2018-03-02 20:58:58 -06:00
$bar->clear();
try {
2026-10-05 18:10:18 -04:00
Daemon::server($server)->power($action);
} catch (DaemonConnectionException $daemonConnectionException) {
2018-03-02 20:58:58 -06:00
$this->output->error(trans('command/messages.server.power.action_failed', [
'name' => $server->name,
'id' => $server->id,
'node' => $server->node->name,
2026-10-05 18:10:18 -04:00
'message' => $daemonConnectionException->getMessage(),
2018-03-02 20:58:58 -06:00
]));
}
$bar->advance();
$bar->display();
});
2018-03-02 20:58:58 -06:00
$this->line('');
}
/**
* Returns the query builder instance that will return the servers that should be affected.
2026-10-05 18:10:18 -04:00
*
* @param list<string> $servers
* @param list<string> $nodes
* @return Builder<Server>
*/
2022-10-14 10:59:20 -06:00
protected function getQueryBuilder(array $servers, array $nodes): Builder
{
2021-01-30 19:12:22 -08:00
$instance = Server::query()->whereNull('status');
2026-10-05 18:10:18 -04:00
if ($servers !== [] && $nodes !== []) {
$instance->where(fn (Builder $targets): Builder => $targets->whereIn('id', $servers)->orWhereIn('node_id', $nodes));
} elseif ($servers !== []) {
$instance->whereIn('id', $servers);
2026-10-05 18:10:18 -04:00
} elseif ($nodes !== []) {
$instance->whereIn('node_id', $nodes);
}
return $instance->with('node');
}
2018-03-02 20:58:58 -06:00
}