Skip to content

Commit 16f49f8

Browse files
committed
Close cleanup; only try to run power actions against non-suspended & installed servers; closes pterodactyl#2760
1 parent 26d409c commit 16f49f8

File tree

5 files changed

+39
-106
lines changed

5 files changed

+39
-106
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ This file is a running track of new features and fixes to each version of the pa
33

44
This project follows [Semantic Versioning](http://semver.org) guidelines.
55

6+
## v1.1.3
7+
### Fixed
8+
* Server bulk power actions command will no longer attempt to run commands against installing or suspended servers.
9+
610
## v1.1.2
711
### Fixed
812
* Fixes an exception thrown while trying to validate IP access for the client API.

app/Console/Commands/Server/BulkPowerActionCommand.php

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
namespace Pterodactyl\Console\Commands\Server;
44

5+
use Pterodactyl\Models\Server;
56
use Illuminate\Console\Command;
6-
use GuzzleHttp\Exception\RequestException;
77
use Illuminate\Validation\ValidationException;
88
use Illuminate\Validation\Factory as ValidatorFactory;
99
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
10-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
10+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
1111

1212
class BulkPowerActionCommand extends Command
1313
{
14-
/**
15-
* @var \Pterodactyl\Repositories\Wings\DaemonPowerRepository
16-
*/
17-
private $powerRepository;
18-
19-
/**
20-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
21-
*/
22-
private $repository;
23-
24-
/**
25-
* @var \Illuminate\Validation\Factory
26-
*/
27-
private $validator;
28-
2914
/**
3015
* @var string
3116
*/
@@ -40,36 +25,19 @@ class BulkPowerActionCommand extends Command
4025
protected $description = 'Perform bulk power management on large groupings of servers or nodes at once.';
4126

4227
/**
43-
* BulkPowerActionCommand constructor.
28+
* Handle the bulk power request.
4429
*
4530
* @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
46-
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
4731
* @param \Illuminate\Validation\Factory $validator
48-
*/
49-
public function __construct(
50-
DaemonPowerRepository $powerRepository,
51-
ServerRepositoryInterface $repository,
52-
ValidatorFactory $validator
53-
) {
54-
parent::__construct();
55-
56-
$this->repository = $repository;
57-
$this->validator = $validator;
58-
$this->powerRepository = $powerRepository;
59-
}
60-
61-
/**
62-
* Handle the bulk power request.
63-
*
6432
* @throws \Illuminate\Validation\ValidationException
6533
*/
66-
public function handle()
34+
public function handle(DaemonPowerRepository $powerRepository, ValidatorFactory $validator)
6735
{
6836
$action = $this->argument('action');
6937
$nodes = empty($this->option('nodes')) ? [] : explode(',', $this->option('nodes'));
7038
$servers = empty($this->option('servers')) ? [] : explode(',', $this->option('servers'));
7139

72-
$validator = $this->validator->make([
40+
$validator = $validator->make([
7341
'action' => $action,
7442
'nodes' => $nodes,
7543
'servers' => $servers,
@@ -89,23 +57,18 @@ public function handle()
8957
throw new ValidationException($validator);
9058
}
9159

92-
$count = $this->repository->getServersForPowerActionCount($servers, $nodes);
60+
$count = $this->getQueryBuilder($servers, $nodes)->count();
9361
if (! $this->confirm(trans('command/messages.server.power.confirm', ['action' => $action, 'count' => $count])) && $this->input->isInteractive()) {
9462
return;
9563
}
9664

9765
$bar = $this->output->createProgressBar($count);
98-
$servers = $this->repository->getServersForPowerAction($servers, $nodes);
99-
100-
$servers->each(function ($server) use ($action, &$bar) {
66+
$this->getQueryBuilder($servers, $nodes)->each(function (Server $server) use ($action, $powerRepository, &$bar) {
10167
$bar->clear();
10268

10369
try {
104-
$this->powerRepository
105-
->setNode($server->node)
106-
->setServer($server)
107-
->send($action);
108-
} catch (RequestException $exception) {
70+
$powerRepository->setServer($server)->send($action);
71+
} catch (DaemonConnectionException $exception) {
10972
$this->output->error(trans('command/messages.server.power.action_failed', [
11073
'name' => $server->name,
11174
'id' => $server->id,
@@ -120,4 +83,28 @@ public function handle()
12083

12184
$this->line('');
12285
}
86+
87+
/**
88+
* Returns the query builder instance that will return the servers that should be affected.
89+
*
90+
* @param array $servers
91+
* @param array $nodes
92+
* @return \Illuminate\Database\Eloquent\Builder
93+
*/
94+
protected function getQueryBuilder(array $servers, array $nodes)
95+
{
96+
$instance = Server::query()
97+
->where('suspended', false)
98+
->where('installed', Server::STATUS_INSTALLED);
99+
100+
if (! empty($nodes) && ! empty($servers)) {
101+
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
102+
} else if (empty($nodes) && ! empty($servers)) {
103+
$instance->whereIn('id', $servers);
104+
} else if (! empty($nodes) && empty($servers)) {
105+
$instance->whereIn('node_id', $nodes);
106+
}
107+
108+
return $instance->with('node');
109+
}
123110
}

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,6 @@ public function getDaemonServiceData(Server $server, bool $refresh = false): arr
9595
*/
9696
public function getByUuid(string $uuid): Server;
9797

98-
/**
99-
* Return all of the servers that should have a power action performed against them.
100-
*
101-
* @param int[] $servers
102-
* @param int[] $nodes
103-
* @param bool $returnCount
104-
* @return int|\Illuminate\Support\LazyCollection
105-
*/
106-
public function getServersForPowerAction(array $servers = [], array $nodes = [], bool $returnCount = false);
107-
108-
/**
109-
* Return the total number of servers that will be affected by the query.
110-
*
111-
* @param int[] $servers
112-
* @param int[] $nodes
113-
* @return int
114-
*/
115-
public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int;
116-
11798
/**
11899
* Check if a given UUID and UUID-Short string are unique to a server.
119100
*

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -194,45 +194,6 @@ public function getByUuid(string $uuid): Server
194194
}
195195
}
196196

197-
/**
198-
* Return all of the servers that should have a power action performed against them.
199-
*
200-
* @param int[] $servers
201-
* @param int[] $nodes
202-
* @param bool $returnCount
203-
* @return int|\Illuminate\Support\LazyCollection
204-
*/
205-
public function getServersForPowerAction(array $servers = [], array $nodes = [], bool $returnCount = false)
206-
{
207-
$instance = $this->getBuilder();
208-
209-
if (! empty($nodes) && ! empty($servers)) {
210-
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
211-
} else if (empty($nodes) && ! empty($servers)) {
212-
$instance->whereIn('id', $servers);
213-
} else if (! empty($nodes) && empty($servers)) {
214-
$instance->whereIn('node_id', $nodes);
215-
}
216-
217-
if ($returnCount) {
218-
return $instance->count();
219-
}
220-
221-
return $instance->with('node')->cursor();
222-
}
223-
224-
/**
225-
* Return the total number of servers that will be affected by the query.
226-
*
227-
* @param int[] $servers
228-
* @param int[] $nodes
229-
* @return int
230-
*/
231-
public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int
232-
{
233-
return $this->getServersForPowerAction($servers, $nodes, true);
234-
}
235-
236197
/**
237198
* Check if a given UUID and UUID-Short string are unique to a server.
238199
*

tests/Unit/Commands/Server/BulkPowerActionCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testSendAction()
5353
$this->repository->expects('getServersForPowerAction')->with([], [])->andReturn($servers);
5454

5555
for ($i = 0; $i < count($servers); $i++) {
56-
$this->powerRepository->expects('setNode->setServer->send')->with('kill')->andReturnNull();
56+
$this->powerRepository->expects('setServer->send')->with('kill')->andReturnNull();
5757
}
5858

5959
$display = $this->runCommand($this->getCommand(), ['action' => 'kill'], ['yes']);
@@ -107,7 +107,7 @@ public function testSendWithEmptyOptions()
107107
->andReturn(1);
108108

109109
$this->repository->expects('getServersForPowerAction')->with([], [])->andReturn(Collection::make([$server]));
110-
$this->powerRepository->expects('setNode->setServer->send')->with('kill')->andReturnNull();
110+
$this->powerRepository->expects('setServer->send')->with('kill')->andReturnNull();
111111

112112
$display = $this->runCommand($this->getCommand(), [
113113
'action' => 'kill',

0 commit comments

Comments
 (0)