2017-07-24 00:57:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2019-12-22 05:01:38 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-10-20 04:07:07 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2021-03-21 18:49:42 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-07-24 00:57:43 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2020-10-20 04:07:07 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2019-11-24 23:08:54 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2021-03-21 18:49:42 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
|
|
|
class BuildModificationService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2018-01-21 22:02:03 +00:00
|
|
|
private $connection;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-24 23:08:54 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
2017-07-24 00:57:43 +00:00
|
|
|
*/
|
2018-01-21 22:02:03 +00:00
|
|
|
private $daemonServerRepository;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2019-12-22 05:01:38 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
|
|
|
private $structureService;
|
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
/**
|
|
|
|
* BuildModificationService constructor.
|
|
|
|
*
|
2019-12-22 05:01:38 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $structureService
|
2017-07-24 00:57:43 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2019-12-22 05:01:38 +00:00
|
|
|
ServerConfigurationStructureService $structureService,
|
2018-01-21 22:02:03 +00:00
|
|
|
ConnectionInterface $connection,
|
2020-10-20 04:07:07 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository
|
2017-07-24 00:57:43 +00:00
|
|
|
) {
|
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2018-01-21 22:02:03 +00:00
|
|
|
$this->connection = $connection;
|
2019-12-22 05:01:38 +00:00
|
|
|
$this->structureService = $structureService;
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the build details for a specified server.
|
|
|
|
*
|
2018-01-21 22:02:03 +00:00
|
|
|
* @return \Pterodactyl\Models\Server
|
2017-07-24 00:57:43 +00:00
|
|
|
*
|
2020-10-20 04:07:07 +00:00
|
|
|
* @throws \Throwable
|
2017-07-24 00:57:43 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2018-01-21 22:02:03 +00:00
|
|
|
public function handle(Server $server, array $data)
|
2017-07-24 00:57:43 +00:00
|
|
|
{
|
2018-01-21 22:02:03 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-07-24 00:57:43 +00:00
|
|
|
|
|
|
|
$this->processAllocations($server, $data);
|
2019-11-24 23:08:54 +00:00
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) {
|
|
|
|
try {
|
2020-10-20 04:07:07 +00:00
|
|
|
Allocation::query()->where('id', $data['allocation_id'])->where('server_id', $server->id)->firstOrFail();
|
|
|
|
} catch (ModelNotFoundException $ex) {
|
|
|
|
throw new DisplayException('The requested default allocation is not currently assigned to this server.');
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
// If any of these values are passed through in the data array go ahead and set
|
|
|
|
// them correctly on the server model.
|
|
|
|
$merge = Arr::only($data, ['oom_disabled', 'memory', 'swap', 'io', 'cpu', 'threads', 'disk', 'allocation_id']);
|
|
|
|
|
|
|
|
$server->forceFill(array_merge($merge, [
|
|
|
|
'database_limit' => Arr::get($data, 'database_limit', 0) ?? null,
|
|
|
|
'allocation_limit' => Arr::get($data, 'allocation_limit', 0) ?? null,
|
|
|
|
'backup_limit' => Arr::get($data, 'backup_limit', 0) ?? 0,
|
|
|
|
]))->saveOrFail();
|
|
|
|
|
|
|
|
$server = $server->fresh();
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2019-12-22 05:01:38 +00:00
|
|
|
$updateData = $this->structureService->handle($server);
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2021-03-21 18:49:42 +00:00
|
|
|
// Because Wings always fetches an updated configuration from the Panel when booting
|
|
|
|
// a server this type of exception can be safely "ignored" and just written to the logs.
|
|
|
|
// Ideally this request succeedes so we can apply resource modifications on the fly
|
|
|
|
// but if it fails it isn't the end of the world.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($updateData['build'])) {
|
2021-03-21 18:49:42 +00:00
|
|
|
try {
|
|
|
|
$this->daemonServerRepository->setServer($server)->update([
|
|
|
|
'build' => $updateData['build'],
|
|
|
|
]);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
Log::warning($exception, ['server_id' => $server->id]);
|
|
|
|
}
|
2020-11-03 03:58:03 +00:00
|
|
|
}
|
2019-12-22 21:45:40 +00:00
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
$this->connection->commit();
|
2018-01-21 22:02:03 +00:00
|
|
|
|
|
|
|
return $server;
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-20 04:07:07 +00:00
|
|
|
* Process the allocations being assigned in the data and ensure they are available for a server.
|
2017-07-24 00:57:43 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2018-01-21 22:02:03 +00:00
|
|
|
private function processAllocations(Server $server, array &$data)
|
2017-07-24 00:57:43 +00:00
|
|
|
{
|
2020-10-20 04:07:07 +00:00
|
|
|
if (empty($data['add_allocations']) && empty($data['remove_allocations'])) {
|
2017-07-24 00:57:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
// Handle the addition of allocations to this server. Only assign allocations that are not currently
|
|
|
|
// assigned to a different server, and only allocations on the same node as the server.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($data['add_allocations'])) {
|
2020-10-20 04:07:07 +00:00
|
|
|
$query = Allocation::query()
|
|
|
|
->where('node_id', $server->node_id)
|
|
|
|
->whereIn('id', $data['add_allocations'])
|
|
|
|
->whereNull('server_id');
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
// Keep track of all the allocations we're just now adding so that we can use the first
|
|
|
|
// one to reset the default allocation to.
|
|
|
|
$freshlyAllocated = $query->pluck('id')->first();
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2020-10-20 04:08:40 +00:00
|
|
|
$query->update(['server_id' => $server->id, 'notes' => null]);
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($data['remove_allocations'])) {
|
2017-07-24 00:57:43 +00:00
|
|
|
foreach ($data['remove_allocations'] as $allocation) {
|
2020-10-20 04:07:07 +00:00
|
|
|
// If we are attempting to remove the default allocation for the server, see if we can reassign
|
|
|
|
// to the first provided value in add_allocations. If there is no new first allocation then we
|
|
|
|
// will throw an exception back.
|
|
|
|
if ($allocation === ($data['allocation_id'] ?? $server->allocation_id)) {
|
|
|
|
if (empty($freshlyAllocated)) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new DisplayException('You are attempting to delete the default allocation for this server but there is no fallback allocation to use.');
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
// Update the default allocation to be the first allocation that we are creating.
|
|
|
|
$data['allocation_id'] = $freshlyAllocated;
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:07:07 +00:00
|
|
|
// Remove any of the allocations we got that are currently assigned to this server on
|
|
|
|
// this node. Also set the notes to null, otherwise when re-allocated to a new server those
|
|
|
|
// notes will be carried over.
|
|
|
|
Allocation::query()->where('node_id', $server->node_id)
|
|
|
|
->where('server_id', $server->id)
|
|
|
|
// Only remove the allocations that we didn't also attempt to add to the server...
|
|
|
|
->whereIn('id', array_diff($data['remove_allocations'], $data['add_allocations'] ?? []))
|
|
|
|
->update([
|
|
|
|
'notes' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
]);
|
2017-07-24 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|