Skip to content

Commit 3611ce7

Browse files
authored
Merge pull request pterodactyl#1402 from JamsheedMistri/feature/bulk-reinstall-command
Adding bulk reinstall command
2 parents b94289c + 215351e commit 3611ce7

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public function loadEggRelations(Server $server, bool $refresh = false): Server;
3636
*/
3737
public function getDataForRebuild(int $server = null, int $node = null): Collection;
3838

39+
/**
40+
* Return a collection of servers with their associated data for reinstall operations.
41+
*
42+
* @param int|null $server
43+
* @param int|null $node
44+
* @return \Illuminate\Support\Collection
45+
*/
46+
public function getDataForReinstall(int $server = null, int $node = null): Collection;
47+
3948
/**
4049
* Return a server model and all variables associated with the server.
4150
*

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public function getDataForRebuild(int $server = null, int $node = null): Collect
7676
return $instance->get($this->getColumns());
7777
}
7878

79+
/**
80+
* Return a collection of servers with their associated data for reinstall operations.
81+
*
82+
* @param int|null $server
83+
* @param int|null $node
84+
* @return \Illuminate\Support\Collection
85+
*/
86+
public function getDataForReinstall(int $server = null, int $node = null): Collection
87+
{
88+
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'pack', 'egg', 'node']);
89+
90+
if (! is_null($server) && is_null($node)) {
91+
$instance = $instance->where('id', '=', $server);
92+
} elseif (is_null($server) && ! is_null($node)) {
93+
$instance = $instance->where('node_id', '=', $node);
94+
}
95+
96+
return $instance->get($this->getColumns());
97+
}
98+
7999
/**
80100
* Return a server model and all variables associated with the server.
81101
*

resources/lang/en/command/messages.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
],
4343
'server' => [
4444
'rebuild_failed' => 'Rebuild request for ":name" (#:id) on node ":node" failed with error: :message',
45+
'reinstall' => [
46+
'failed' => 'Reinstall request for ":name" (#:id) on node ":node" failed with error: :message',
47+
'confirm' => 'You are about to reinstall against a group of servers. Do you wish to continue?',
48+
],
4549
'power' => [
4650
'confirm' => 'You are about to perform a :action against :count servers. Do you wish to continue?',
4751
'action_failed' => 'Power action request for ":name" (#:id) on node ":node" failed with error: :message',

0 commit comments

Comments
 (0)