|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * This software is licensed under the terms of the MIT license. |
| 7 | + * https://opensource.org/licenses/MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Pterodactyl\Console\Commands\Server; |
| 11 | + |
| 12 | +use Webmozart\Assert\Assert; |
| 13 | +use Illuminate\Console\Command; |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 16 | +use Pterodactyl\Services\Servers\ServerConfigurationStructureService; |
| 17 | +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; |
| 18 | + |
| 19 | +class BulkReinstallActionCommand extends Command |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService |
| 23 | + */ |
| 24 | + protected $configurationStructureService; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface |
| 28 | + */ |
| 29 | + protected $daemonRepository; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $description = 'Reinstall a single server, all servers on a node, or all servers on the panel.'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface |
| 38 | + */ |
| 39 | + protected $repository; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $signature = 'p:server:reinstall |
| 45 | + {server? : The ID of the server to reinstall.} |
| 46 | + {--node= : ID of the node to reinstall all servers on. Ignored if server is passed.}'; |
| 47 | + |
| 48 | + /** |
| 49 | + * BulkReinstallActionCommand constructor. |
| 50 | + * |
| 51 | + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository |
| 52 | + * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService |
| 53 | + * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + DaemonServerRepositoryInterface $daemonRepository, |
| 57 | + ServerConfigurationStructureService $configurationStructureService, |
| 58 | + ServerRepositoryInterface $repository |
| 59 | + ) { |
| 60 | + parent::__construct(); |
| 61 | + |
| 62 | + $this->configurationStructureService = $configurationStructureService; |
| 63 | + $this->daemonRepository = $daemonRepository; |
| 64 | + $this->repository = $repository; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Handle command execution. |
| 69 | + */ |
| 70 | + public function handle() |
| 71 | + { |
| 72 | + $servers = $this->getServersToProcess(); |
| 73 | + |
| 74 | + if (! $this->confirm(trans('command/messages.server.reinstall.confirm'))) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $bar = $this->output->createProgressBar(count($servers)); |
| 79 | + |
| 80 | + $servers->each(function ($server) use ($bar) { |
| 81 | + $bar->clear(); |
| 82 | + |
| 83 | + try { |
| 84 | + $this->daemonRepository->setServer($server)->reinstall(); |
| 85 | + } catch (RequestException $exception) { |
| 86 | + $this->output->error(trans('command/messages.server.reinstall.failed', [ |
| 87 | + 'name' => $server->name, |
| 88 | + 'id' => $server->id, |
| 89 | + 'node' => $server->node->name, |
| 90 | + 'message' => $exception->getMessage(), |
| 91 | + ])); |
| 92 | + } |
| 93 | + |
| 94 | + $bar->advance(); |
| 95 | + $bar->display(); |
| 96 | + }); |
| 97 | + |
| 98 | + $this->line(''); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Return the servers to be reinstalled. |
| 103 | + * |
| 104 | + * @return \Illuminate\Database\Eloquent\Collection |
| 105 | + */ |
| 106 | + private function getServersToProcess() |
| 107 | + { |
| 108 | + Assert::nullOrIntegerish($this->argument('server'), 'Value passed in server argument must be null or an integer, received %s.'); |
| 109 | + Assert::nullOrIntegerish($this->option('node'), 'Value passed in node option must be null or integer, received %s.'); |
| 110 | + |
| 111 | + return $this->repository->getDataForReinstall($this->argument('server'), $this->option('node')); |
| 112 | + } |
| 113 | +} |
0 commit comments