-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBulkPowerActionCommand.php
More file actions
189 lines (164 loc) · 5.68 KB
/
BulkPowerActionCommand.php
File metadata and controls
189 lines (164 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Pterodactyl\Console\Commands\Server;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Factory as ValidatorFactory;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
class BulkPowerActionCommand extends Command
{
protected $signature = 'p:server:bulk-power
{action : The action to perform (start, stop, restart, kill)}
{--servers= : A comma-separated list of server IDs.}
{--nodes= : A comma-separated list of node IDs.}';
protected $description = 'Performs bulk power management on servers or nodes.';
/**
* Frok by xql.dev Love Pyrodactyl <3
* Good Docs For New Developper xp
*/
public function __construct(
private DaemonPowerRepository $powerRepository,
private ValidatorFactory $validator
) {
parent::__construct();
}
/**
* Handle the bulk power request.
*
* @throws ValidationException
*/
public function handle(): void
{
$action = $this->argument('action');
$nodes = $this->parseOptionList($this->option('nodes'));
$servers = $this->parseOptionList($this->option('servers'));
$this->validateInput($action, $nodes, $servers);
$query = $this->getQueryBuilder($servers, $nodes);
$count = $query->count();
if ($count === 0) {
$this->output->warning('No servers found matching the specified criteria.');
return;
}
if (!$this->confirmAction($action, $count)) {
$this->output->info('Bulk power action cancelled.');
return;
}
$this->processServers($query, $action);
}
/**
* Parse comma-separated option string into an array.
*/
private function parseOptionList(?string $option): array
{
return array_filter(array_map('trim', explode(',', $option ?? '')));
}
/**
* Validate input parameters.
*
* @throws ValidationException
*/
private function validateInput(string $action, array $nodes, array $servers): void
{
$validator = $this->validator->make([
'action' => $action,
'nodes' => $nodes,
'servers' => $servers,
], [
'action' => 'required|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);
}
throw new ValidationException($validator);
}
if (empty($nodes) && empty($servers)) {
throw new ValidationException($this->validator->make(
[],
['servers' => 'required_without:nodes']
));
}
}
/**
* Confirm the bulk action with the user.
*/
private function confirmAction(string $action, int $count): bool
{
if (!$this->input->isInteractive()) {
return true;
}
return $this->confirm(trans('command/messages.server.power.confirm', [
'action' => $action,
'count' => $count,
]));
}
/**
* Process servers with the specified power action.
*/
private function processServers(Builder $query, string $action): void
{
$bar = $this->output->createProgressBar($query->count());
$bar->start();
$query->chunk(100, function ($servers) use ($action, $bar) {
foreach ($servers as $server) {
$this->processServer($server, $action);
$bar->advance();
}
});
$bar->finish();
$this->line('');
$this->output->success(sprintf('Bulk power action "%s" completed.', $action));
}
/**
* Process a single server's power action.
*/
private function processServer(Server $server, string $action): void
{
try {
$this->powerRepository->setServer($server)->send($action);
Log::info('Bulk power action successful', [
'action' => $action,
'server_id' => $server->id,
'server_name' => $server->name,
'node_id' => $server->node_id,
]);
} catch (DaemonConnectionException $exception) {
$this->output->error(trans('command/messages.server.power.action_failed', [
'name' => $server->name,
'id' => $server->id,
'node' => $server->node->name,
'message' => $exception->getMessage(),
]));
Log::error('Bulk power action failed', [
'action' => $action,
'server_id' => $server->id,
'server_name' => $server->name,
'node_id' => $server->node_id,
'error' => $exception->getMessage(),
]);
}
}
/**
* Returns the query builder instance for affected servers.
*/
private function getQueryBuilder(array $servers, array $nodes): Builder
{
$query = Server::query()
->whereNull('status')
->with('node');
if (!empty($servers)) {
$query->whereIn('id', $servers);
}
if (!empty($nodes)) {
$query->orWhereIn('node_id', $nodes);
}
return $query;
}
}