|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Console\Commands\Server; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use GuzzleHttp\Exception\RequestException; |
| 7 | +use Illuminate\Validation\Factory as ValidatorFactory; |
| 8 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 9 | +use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface; |
| 10 | + |
| 11 | +class BulkPowerActionCommand extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface |
| 15 | + */ |
| 16 | + private $powerRepository; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface |
| 20 | + */ |
| 21 | + private $repository; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Illuminate\Validation\Factory |
| 25 | + */ |
| 26 | + private $validator; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $signature = 'p:server:bulk-power |
| 32 | + {action : The action to perform (start, stop, restart, kill)} |
| 33 | + {--servers= : A comma seperated list of servers.} |
| 34 | + {--nodes= : A comma seperated list of nodes.}'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $description = 'Perform bulk power management on large groupings of servers or nodes at once.'; |
| 40 | + |
| 41 | + /** |
| 42 | + * BulkPowerActionCommand constructor. |
| 43 | + * |
| 44 | + * @param \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface $powerRepository |
| 45 | + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository |
| 46 | + * @param \Illuminate\Validation\Factory $validator |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + PowerRepositoryInterface $powerRepository, |
| 50 | + ServerRepositoryInterface $repository, |
| 51 | + ValidatorFactory $validator |
| 52 | + ) { |
| 53 | + parent::__construct(); |
| 54 | + |
| 55 | + $this->powerRepository = $powerRepository; |
| 56 | + $this->repository = $repository; |
| 57 | + $this->validator = $validator; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Handle the bulk power request. |
| 62 | + * |
| 63 | + * @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException |
| 64 | + */ |
| 65 | + public function handle() |
| 66 | + { |
| 67 | + $action = $this->argument('action'); |
| 68 | + $nodes = empty($this->option('nodes')) ? [] : explode(',', $this->option('nodes')); |
| 69 | + $servers = empty($this->option('servers')) ? [] : explode(',', $this->option('servers')); |
| 70 | + |
| 71 | + $validator = $this->validator->make([ |
| 72 | + 'action' => $action, |
| 73 | + 'nodes' => $nodes, |
| 74 | + 'servers' => $servers, |
| 75 | + ], [ |
| 76 | + 'action' => 'string|in:start,stop,kill,restart', |
| 77 | + 'nodes' => 'array', |
| 78 | + 'nodes.*' => 'integer|min:1', |
| 79 | + 'servers' => 'array', |
| 80 | + 'servers.*' => 'integer|min:1', |
| 81 | + ]); |
| 82 | + |
| 83 | + if ($validator->fails()) { |
| 84 | + foreach ($validator->getMessageBag()->all() as $message) { |
| 85 | + $this->output->error($message); |
| 86 | + } |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $count = $this->repository->getServersForPowerActionCount($servers, $nodes); |
| 92 | + if (! $this->confirm(trans('command/messages.server.power.confirm', ['action' => $action, 'count' => $count]))) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + $bar = $this->output->createProgressBar($count); |
| 97 | + $servers = $this->repository->getServersForPowerAction($servers, $nodes); |
| 98 | + |
| 99 | + foreach ($servers as $server) { |
| 100 | + $bar->clear(); |
| 101 | + |
| 102 | + try { |
| 103 | + $this->powerRepository->setServer($server)->sendSignal($action); |
| 104 | + } catch (RequestException $exception) { |
| 105 | + $this->output->error(trans('command/messages.server.power.action_failed', [ |
| 106 | + 'name' => $server->name, |
| 107 | + 'id' => $server->id, |
| 108 | + 'node' => $server->node->name, |
| 109 | + 'message' => $exception->getMessage(), |
| 110 | + ])); |
| 111 | + } |
| 112 | + |
| 113 | + $bar->advance(); |
| 114 | + $bar->display(); |
| 115 | + } |
| 116 | + |
| 117 | + $this->line(''); |
| 118 | + } |
| 119 | +} |
0 commit comments