2018-03-03 02:58:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Console\Commands\Server;
|
|
|
|
|
2020-11-29 20:50:22 +00:00
|
|
|
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;
|
2020-04-11 22:35:32 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
|
2020-11-29 20:50:22 +00:00
|
|
|
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.';
|
|
|
|
|
|
|
|
/**
|
2020-11-29 20:50:22 +00:00
|
|
|
* Handle the bulk power request.
|
2018-03-03 02:58:58 +00:00
|
|
|
*
|
2020-04-11 22:35:32 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Validation\Factory $validator
|
2018-03-03 03:26:42 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-03-03 02:58:58 +00:00
|
|
|
*/
|
2020-11-29 20:50:22 +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'));
|
|
|
|
|
2020-11-29 20:50:22 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2020-11-29 20:50:22 +00:00
|
|
|
$count = $this->getQueryBuilder($servers, $nodes)->count();
|
2020-02-08 21:30:49 +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);
|
2020-11-29 20:50:22 +00:00
|
|
|
$this->getQueryBuilder($servers, $nodes)->each(function (Server $server) use ($action, $powerRepository, &$bar) {
|
2018-03-03 02:58:58 +00:00
|
|
|
$bar->clear();
|
|
|
|
|
|
|
|
try {
|
2020-11-29 20:50:22 +00:00
|
|
|
$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();
|
2019-09-05 04:05:46 +00:00
|
|
|
});
|
2018-03-03 02:58:58 +00:00
|
|
|
|
|
|
|
$this->line('');
|
|
|
|
}
|
2020-11-29 20:50:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the query builder instance that will return the servers that should be affected.
|
|
|
|
*
|
|
|
|
* @param array $servers
|
|
|
|
* @param array $nodes
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
protected function getQueryBuilder(array $servers, array $nodes)
|
|
|
|
{
|
|
|
|
$instance = Server::query()
|
|
|
|
->where('suspended', false)
|
|
|
|
->where('installed', Server::STATUS_INSTALLED);
|
|
|
|
|
|
|
|
if (! empty($nodes) && ! empty($servers)) {
|
|
|
|
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
|
2021-01-23 20:09:16 +00:00
|
|
|
} elseif (empty($nodes) && ! empty($servers)) {
|
2020-11-29 20:50:22 +00:00
|
|
|
$instance->whereIn('id', $servers);
|
2021-01-23 20:09:16 +00:00
|
|
|
} elseif (! empty($nodes) && empty($servers)) {
|
2020-11-29 20:50:22 +00:00
|
|
|
$instance->whereIn('node_id', $nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance->with('node');
|
|
|
|
}
|
2018-03-03 02:58:58 +00:00
|
|
|
}
|