misc_pterodactyl-panel/app/Console/Commands/Server/BulkPowerActionCommand.php

105 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2018-03-03 02:58:58 +00:00
<?php
namespace Pterodactyl\Console\Commands\Server;
use Pterodactyl\Models\Server;
2018-03-03 02:58:58 +00:00
use Illuminate\Console\Command;
2018-03-03 03:26:42 +00:00
use Illuminate\Validation\ValidationException;
2018-03-03 02:58:58 +00:00
use Illuminate\Validation\Factory as ValidatorFactory;
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2018-03-03 02:58:58 +00:00
class BulkPowerActionCommand extends Command
{
/**
* @var string
*/
protected $signature = 'p:server:bulk-power
{action : The action to perform (start, stop, restart, kill)}
2018-05-13 14:50:56 +00:00
{--servers= : A comma separated list of servers.}
{--nodes= : A comma separated list of nodes.}';
2018-03-03 02:58:58 +00:00
/**
* @var string
*/
protected $description = 'Perform bulk power management on large groupings of servers or nodes at once.';
/**
* Handle the bulk power request.
2018-03-03 02:58:58 +00:00
*
2018-03-03 03:26:42 +00:00
* @throws \Illuminate\Validation\ValidationException
2018-03-03 02:58:58 +00:00
*/
public function handle(DaemonPowerRepository $powerRepository, ValidatorFactory $validator)
2018-03-03 02:58:58 +00:00
{
$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([
2018-03-03 02:58:58 +00:00
'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);
}
2018-03-03 03:26:42 +00:00
throw new ValidationException($validator);
2018-03-03 02:58:58 +00:00
}
$count = $this->getQueryBuilder($servers, $nodes)->count();
2021-01-23 20:33:34 +00:00
if (!$this->confirm(trans('command/messages.server.power.confirm', ['action' => $action, 'count' => $count])) && $this->input->isInteractive()) {
2018-03-03 02:58:58 +00:00
return;
}
$bar = $this->output->createProgressBar($count);
$this->getQueryBuilder($servers, $nodes)->each(function (Server $server) use ($action, $powerRepository, &$bar) {
2018-03-03 02:58:58 +00:00
$bar->clear();
try {
$powerRepository->setServer($server)->send($action);
} catch (DaemonConnectionException $exception) {
2018-03-03 02:58:58 +00:00
$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();
});
2018-03-03 02:58:58 +00:00
$this->line('');
}
/**
* Returns the query builder instance that will return the servers that should be affected.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function getQueryBuilder(array $servers, array $nodes)
{
2021-01-31 03:12:22 +00:00
$instance = Server::query()->whereNull('status');
2021-01-23 20:33:34 +00:00
if (!empty($nodes) && !empty($servers)) {
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
2021-01-23 20:33:34 +00:00
} elseif (empty($nodes) && !empty($servers)) {
$instance->whereIn('id', $servers);
2021-01-23 20:33:34 +00:00
} elseif (!empty($nodes) && empty($servers)) {
$instance->whereIn('node_id', $nodes);
}
return $instance->with('node');
}
2018-03-03 02:58:58 +00:00
}