Skip to content

Commit 38075c6

Browse files
committed
Update panel to send correct service option configuration to the daemon.
1 parent 1a5a1f8 commit 38075c6

File tree

12 files changed

+282
-83
lines changed

12 files changed

+282
-83
lines changed

app/Console/Commands/Server/RebuildServerCommand.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
use Webmozart\Assert\Assert;
1313
use Illuminate\Console\Command;
1414
use GuzzleHttp\Exception\RequestException;
15-
use Pterodactyl\Services\Servers\EnvironmentService;
1615
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
16+
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
1717
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
1818

1919
class RebuildServerCommand extends Command
2020
{
21+
/**
22+
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
23+
*/
24+
protected $configurationStructureService;
25+
2126
/**
2227
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
2328
*/
@@ -28,11 +33,6 @@ class RebuildServerCommand extends Command
2833
*/
2934
protected $description = 'Rebuild a single server, all servers on a node, or all servers on the panel.';
3035

31-
/**
32-
* @var \Pterodactyl\Services\Servers\EnvironmentService
33-
*/
34-
protected $environmentService;
35-
3636
/**
3737
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
3838
*/
@@ -49,18 +49,18 @@ class RebuildServerCommand extends Command
4949
* RebuildServerCommand constructor.
5050
*
5151
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository
52-
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
52+
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
5353
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
5454
*/
5555
public function __construct(
5656
DaemonServerRepositoryInterface $daemonRepository,
57-
EnvironmentService $environmentService,
57+
ServerConfigurationStructureService $configurationStructureService,
5858
ServerRepositoryInterface $repository
5959
) {
6060
parent::__construct();
6161

62+
$this->configurationStructureService = $configurationStructureService;
6263
$this->daemonRepository = $daemonRepository;
63-
$this->environmentService = $environmentService;
6464
$this->repository = $repository;
6565
}
6666

@@ -74,19 +74,7 @@ public function handle()
7474

7575
$servers->each(function ($server) use ($bar) {
7676
$bar->clear();
77-
$json = [
78-
'build' => [
79-
'image' => $server->image,
80-
'env|overwrite' => $this->environmentService->process($server),
81-
],
82-
'service' => [
83-
'type' => $server->option->service->folder,
84-
'option' => $server->option->tag,
85-
'pack' => object_get($server, 'pack.uuid'),
86-
'skip_scripts' => $server->skip_scripts,
87-
],
88-
'rebuild' => true,
89-
];
77+
$json = array_merge($this->configurationStructureService->handle($server), ['rebuild' => true]);
9078

9179
try {
9280
$this->daemonRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($json);

app/Contracts/Repository/Daemon/ServerRepositoryInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99

1010
namespace Pterodactyl\Contracts\Repository\Daemon;
1111

12+
use Psr\Http\Message\ResponseInterface;
13+
1214
interface ServerRepositoryInterface extends BaseRepositoryInterface
1315
{
1416
/**
1517
* Create a new server on the daemon for the panel.
1618
*
17-
* @param int $id
19+
* @param array $structure
1820
* @param array $overrides
19-
* @param bool $start
2021
* @return \Psr\Http\Message\ResponseInterface
22+
*
23+
* @throws \GuzzleHttp\Exception\RequestException
2124
*/
22-
public function create($id, array $overrides = [], $start = false);
25+
public function create(array $structure, array $overrides = []): ResponseInterface;
2326

2427
/**
2528
* Update server details on the daemon.

app/Contracts/Repository/ServiceOptionRepositoryInterface.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Pterodactyl\Contracts\Repository;
1111

1212
use Pterodactyl\Models\ServiceOption;
13+
use Illuminate\Database\Eloquent\Collection;
1314

1415
interface ServiceOptionRepositoryInterface extends RepositoryInterface
1516
{
@@ -23,15 +24,21 @@ interface ServiceOptionRepositoryInterface extends RepositoryInterface
2324
*/
2425
public function getWithVariables(int $id): ServiceOption;
2526

27+
/**
28+
* Return all of the service options and their relations to be used in the daemon API.
29+
*
30+
* @return \Illuminate\Database\Eloquent\Collection
31+
*/
32+
public function getAllWithCopyAttributes(): Collection;
33+
2634
/**
2735
* Return a service option with the scriptFrom and configFrom relations loaded onto the model.
2836
*
29-
* @param int $id
37+
* @param int|string $value
38+
* @param string $column
3039
* @return \Pterodactyl\Models\ServiceOption
31-
*
32-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
3340
*/
34-
public function getWithCopyAttributes(int $id): ServiceOption;
41+
public function getWithCopyAttributes($value, string $column = 'id'): ServiceOption;
3542

3643
/**
3744
* Return all of the data needed to export a service.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Http\Controllers\API\Remote;
11+
12+
use Illuminate\Http\JsonResponse;
13+
use Pterodactyl\Http\Controllers\Controller;
14+
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
15+
use Pterodactyl\Services\Services\Options\OptionConfigurationFileService;
16+
17+
class OptionRetrievalController extends Controller
18+
{
19+
/**
20+
* @var \Pterodactyl\Services\Services\Options\OptionConfigurationFileService
21+
*/
22+
protected $configurationFileService;
23+
24+
/**
25+
* @var \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface
26+
*/
27+
protected $repository;
28+
29+
/**
30+
* OptionUpdateController constructor.
31+
*
32+
* @param \Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface $repository
33+
* @param \Pterodactyl\Services\Services\Options\OptionConfigurationFileService $configurationFileService
34+
*/
35+
public function __construct(
36+
ServiceOptionRepositoryInterface $repository,
37+
OptionConfigurationFileService $configurationFileService
38+
) {
39+
$this->configurationFileService = $configurationFileService;
40+
$this->repository = $repository;
41+
}
42+
43+
/**
44+
* Return a JSON array of service options and the SHA1 hash of thier configuration file.
45+
*
46+
* @return \Illuminate\Http\JsonResponse
47+
*/
48+
public function index(): JsonResponse
49+
{
50+
$options = $this->repository->getAllWithCopyAttributes();
51+
52+
$response = [];
53+
$options->each(function ($option) use (&$response) {
54+
$response[$option->uuid] = sha1(json_encode($this->configurationFileService->handle($option)));
55+
});
56+
57+
return response()->json($response);
58+
}
59+
60+
/**
61+
* Return the configuration file for a single service option for the Daemon.
62+
*
63+
* @param string $uuid
64+
* @return \Illuminate\Http\JsonResponse
65+
*
66+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
67+
*/
68+
public function download(string $uuid): JsonResponse
69+
{
70+
$option = $this->repository->getWithCopyAttributes($uuid, 'uuid');
71+
72+
return response()->json($this->configurationFileService->handle($option));
73+
}
74+
}

app/Repositories/Daemon/ServerRepository.php

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,29 @@
1010
namespace Pterodactyl\Repositories\Daemon;
1111

1212
use Webmozart\Assert\Assert;
13-
use Pterodactyl\Services\Servers\EnvironmentService;
13+
use Psr\Http\Message\ResponseInterface;
1414
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
15-
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface as DatabaseServerRepositoryInterface;
1615

1716
class ServerRepository extends BaseRepository implements ServerRepositoryInterface
1817
{
1918
/**
20-
* {@inheritdoc}
19+
* Create a new server on the daemon for the panel.
20+
*
21+
* @param array $structure
22+
* @param array $overrides
23+
* @return \Psr\Http\Message\ResponseInterface
24+
*
25+
* @throws \GuzzleHttp\Exception\RequestException
2126
*/
22-
public function create($id, array $overrides = [], $start = false)
27+
public function create(array $structure, array $overrides = []): ResponseInterface
2328
{
24-
Assert::numeric($id, 'First argument passed to create must be numeric, received %s.');
25-
Assert::boolean($start, 'Third argument passed to create must be boolean, received %s.');
26-
27-
$repository = $this->app->make(DatabaseServerRepositoryInterface::class);
28-
$environment = $this->app->make(EnvironmentService::class);
29-
30-
$server = $repository->getDataForCreation($id);
31-
32-
$data = [
33-
'uuid' => (string) $server->uuid,
34-
'user' => $server->username,
35-
'build' => [
36-
'default' => [
37-
'ip' => $server->allocation->ip,
38-
'port' => $server->allocation->port,
39-
],
40-
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
41-
return $item->pluck('port');
42-
})->toArray(),
43-
'env' => $environment->process($server),
44-
'memory' => (int) $server->memory,
45-
'swap' => (int) $server->swap,
46-
'io' => (int) $server->io,
47-
'cpu' => (int) $server->cpu,
48-
'disk' => (int) $server->disk,
49-
'image' => $server->image,
50-
],
51-
'service' => [
52-
'type' => $server->option->service->folder,
53-
'option' => $server->option->tag,
54-
'pack' => object_get($server, 'pack.uuid'),
55-
'skip_scripts' => $server->skip_scripts,
56-
],
57-
'rebuild' => false,
58-
'start_on_completion' => $start,
59-
];
60-
6129
// Loop through overrides.
6230
foreach ($overrides as $key => $value) {
63-
array_set($data, $key, $value);
31+
array_set($structure, $key, $value);
6432
}
6533

6634
return $this->getHttpClient()->request('POST', 'servers', [
67-
'json' => $data,
35+
'json' => $structure,
6836
]);
6937
}
7038

app/Repositories/Eloquent/ServerRepository.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getDataForRebuild($server = null, $node = null)
4747
Assert::nullOrIntegerish($server, 'First argument passed to getDataForRebuild must be null or integer, received %s.');
4848
Assert::nullOrIntegerish($node, 'Second argument passed to getDataForRebuild must be null or integer, received %s.');
4949

50-
$instance = $this->getBuilder()->with('node', 'option.service', 'pack');
50+
$instance = $this->getBuilder()->with('allocation', 'allocations', 'pack', 'option', 'node');
5151

5252
if (! is_null($server) && is_null($node)) {
5353
$instance = $instance->where('id', '=', $server);
@@ -111,9 +111,7 @@ public function getVariablesWithValues($id, $returnWithObject = false)
111111
*/
112112
public function getDataForCreation($id)
113113
{
114-
$instance = $this->getBuilder()->with('allocation', 'allocations', 'pack', 'option.service')
115-
->find($id, $this->getColumns());
116-
114+
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'pack', 'option'])->find($id, $this->getColumns());
117115
if (! $instance) {
118116
throw new RecordNotFoundException();
119117
}

app/Repositories/Eloquent/ServiceOptionRepository.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace Pterodactyl\Repositories\Eloquent;
1111

12+
use Webmozart\Assert\Assert;
1213
use Pterodactyl\Models\ServiceOption;
14+
use Illuminate\Database\Eloquent\Collection;
1315
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1416
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
1517

@@ -42,18 +44,31 @@ public function getWithVariables(int $id): ServiceOption
4244
return $instance;
4345
}
4446

47+
/**
48+
* Return all of the service options and their relations to be used in the daemon API.
49+
*
50+
* @return \Illuminate\Database\Eloquent\Collection
51+
*/
52+
public function getAllWithCopyAttributes(): Collection
53+
{
54+
return $this->getBuilder()->with('scriptFrom', 'configFrom')->get($this->getColumns());
55+
}
56+
4557
/**
4658
* Return a service option with the scriptFrom and configFrom relations loaded onto the model.
4759
*
48-
* @param int $id
60+
* @param int|string $value
61+
* @param string $column
4962
* @return \Pterodactyl\Models\ServiceOption
5063
*
5164
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5265
*/
53-
public function getWithCopyAttributes(int $id): ServiceOption
66+
public function getWithCopyAttributes($value, string $column = 'id'): ServiceOption
5467
{
68+
Assert::true((is_digit($value) || is_string($value)), 'First argument passed to getWithCopyAttributes must be an integer or string, received %s.');
69+
5570
/** @var \Pterodactyl\Models\ServiceOption $instance */
56-
$instance = $this->getBuilder()->with('scriptFrom', 'configFrom')->find($id, $this->getColumns());
71+
$instance = $this->getBuilder()->with('scriptFrom', 'configFrom')->where($column, '=', $value)->first($this->getColumns());
5772
if (! $instance) {
5873
throw new RecordNotFoundException;
5974
}

0 commit comments

Comments
 (0)