2017-07-19 20:49:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Services\Servers;
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Enums\ServerState;
|
2025-09-08 13:12:33 -04:00
|
|
|
use App\Exceptions\DisplayException;
|
|
|
|
|
use App\Exceptions\Model\DataValidationException;
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Exceptions\Service\Deployment\NoViableAllocationException;
|
2025-06-26 01:49:43 +02:00
|
|
|
use App\Exceptions\Service\Deployment\NoViableNodeException;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Allocation;
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Models\Egg;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Objects\DeploymentObject;
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Models\Server;
|
|
|
|
|
use App\Models\User;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Repositories\Daemon\DaemonServerRepository;
|
|
|
|
|
use App\Services\Deployment\AllocationSelectionService;
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Services\Deployment\FindViableNodesService;
|
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
use Throwable;
|
|
|
|
|
use Webmozart\Assert\Assert;
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2017-08-27 15:10:51 -05:00
|
|
|
class ServerCreationService
|
2017-07-19 20:49:41 -05:00
|
|
|
{
|
2018-01-28 17:14:14 -06:00
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* ServerCreationService constructor.
|
2017-07-21 21:17:42 -05:00
|
|
|
*/
|
2017-07-19 20:49:41 -05:00
|
|
|
public function __construct(
|
2022-10-14 10:59:20 -06:00
|
|
|
private AllocationSelectionService $allocationSelectionService,
|
|
|
|
|
private ConnectionInterface $connection,
|
|
|
|
|
private DaemonServerRepository $daemonServerRepository,
|
|
|
|
|
private FindViableNodesService $findViableNodesService,
|
|
|
|
|
private ServerDeletionService $serverDeletionService,
|
|
|
|
|
private VariableValidatorService $validatorService
|
2024-11-22 09:27:57 +01:00
|
|
|
) {}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2017-07-21 21:17:42 -05:00
|
|
|
/**
|
2018-01-28 17:14:14 -06:00
|
|
|
* Create a server on the Panel and trigger a request to the Daemon to begin the server
|
2018-02-10 14:01:49 -06:00
|
|
|
* creation process. This function will attempt to set as many additional values
|
|
|
|
|
* as possible given the input data. For example, if an allocation_id is passed with
|
|
|
|
|
* no node_id the node_is will be picked from the allocation.
|
2017-07-21 21:17:42 -05:00
|
|
|
*
|
2025-08-07 11:16:32 +02:00
|
|
|
* @param array<mixed, mixed> $data
|
2025-03-03 14:41:19 -05:00
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws Throwable
|
|
|
|
|
* @throws DisplayException
|
|
|
|
|
* @throws ValidationException
|
|
|
|
|
* @throws NoViableAllocationException
|
2017-07-21 21:17:42 -05:00
|
|
|
*/
|
2024-10-19 18:41:08 -04:00
|
|
|
public function handle(array $data, ?DeploymentObject $deployment = null): Server
|
2017-07-19 20:49:41 -05:00
|
|
|
{
|
2024-05-12 22:24:37 +02:00
|
|
|
if (!isset($data['oom_killer']) && isset($data['oom_disabled'])) {
|
2024-05-08 11:52:28 +02:00
|
|
|
$data['oom_killer'] = !$data['oom_disabled'];
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 15:48:36 +02:00
|
|
|
/** @var Egg $egg */
|
|
|
|
|
$egg = Egg::query()->findOrFail($data['egg_id']);
|
|
|
|
|
|
|
|
|
|
// Fill missing fields from egg
|
2025-10-07 23:42:28 +02:00
|
|
|
$data['image'] ??= Arr::first($egg->docker_images);
|
|
|
|
|
$data['startup'] ??= Arr::first($egg->startup_commands);
|
2024-06-13 15:48:36 +02:00
|
|
|
|
2025-07-18 08:23:48 +02:00
|
|
|
// If a deployment object has been passed we need to get the allocation and node that the server should use.
|
2025-06-26 01:49:43 +02:00
|
|
|
if ($deployment) {
|
2025-07-18 08:23:48 +02:00
|
|
|
$nodes = $this->findViableNodesService->handle(
|
|
|
|
|
Arr::get($data, 'memory', 0),
|
|
|
|
|
Arr::get($data, 'disk', 0),
|
|
|
|
|
Arr::get($data, 'cpu', 0),
|
|
|
|
|
$deployment->getTags(),
|
|
|
|
|
)->pluck('id');
|
|
|
|
|
|
|
|
|
|
if ($nodes->isEmpty()) {
|
|
|
|
|
throw new NoViableNodeException(trans('exceptions.deployment.no_viable_nodes'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ports = $deployment->getPorts();
|
|
|
|
|
if (!empty($ports)) {
|
|
|
|
|
$allocation = $this->allocationSelectionService->setDedicated($deployment->isDedicated())
|
|
|
|
|
->setNodes($nodes->toArray())
|
|
|
|
|
->setPorts($ports)
|
|
|
|
|
->handle();
|
2020-10-08 22:34:52 -07:00
|
|
|
|
2025-06-26 01:49:43 +02:00
|
|
|
$data['allocation_id'] = $allocation->id;
|
|
|
|
|
$data['node_id'] = $allocation->node_id;
|
|
|
|
|
}
|
2025-07-18 08:23:48 +02:00
|
|
|
|
|
|
|
|
if (empty($data['node_id'])) {
|
|
|
|
|
$data['node_id'] = $nodes->first();
|
|
|
|
|
}
|
2025-08-07 11:16:32 +02:00
|
|
|
} else {
|
2025-08-12 21:02:49 +02:00
|
|
|
$data['node_id'] ??= Allocation::find($data['allocation_id'])?->node_id;
|
2018-02-10 14:01:49 -06:00
|
|
|
}
|
2025-07-18 08:23:48 +02:00
|
|
|
|
2025-06-26 01:49:43 +02:00
|
|
|
Assert::false(empty($data['node_id']), 'Expected a non-empty node_id in server creation data.');
|
2018-02-10 14:01:49 -06:00
|
|
|
|
2018-01-28 17:14:14 -06:00
|
|
|
$eggVariableData = $this->validatorService
|
|
|
|
|
->setUserLevel(User::USER_LEVEL_ADMIN)
|
2019-11-16 13:33:01 -08:00
|
|
|
->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
|
2018-01-28 17:14:14 -06:00
|
|
|
|
2019-12-16 21:02:30 -08:00
|
|
|
// Due to the design of the Daemon, we need to persist this server to the disk
|
|
|
|
|
// before we can actually create it on the Daemon.
|
|
|
|
|
//
|
|
|
|
|
// If that connection fails out we will attempt to perform a cleanup by just
|
|
|
|
|
// deleting the server itself from the system.
|
2025-09-08 13:12:33 -04:00
|
|
|
/** @var Server $server */
|
2020-10-09 21:08:27 -07:00
|
|
|
$server = $this->connection->transaction(function () use ($data, $eggVariableData) {
|
|
|
|
|
// Create the server and assign any additional allocations to it.
|
|
|
|
|
$server = $this->createModel($data);
|
|
|
|
|
|
2025-06-26 01:49:43 +02:00
|
|
|
if ($server->allocation_id) {
|
|
|
|
|
$this->storeAssignedAllocations($server, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 21:08:27 -07:00
|
|
|
$this->storeEggVariables($server, $eggVariableData);
|
2019-12-16 21:02:30 -08:00
|
|
|
|
2020-10-09 21:08:27 -07:00
|
|
|
return $server;
|
2020-11-08 13:19:52 -08:00
|
|
|
}, 5);
|
2018-01-28 17:14:14 -06:00
|
|
|
|
2019-12-16 21:02:30 -08:00
|
|
|
try {
|
2025-01-01 15:20:16 -05:00
|
|
|
$this->daemonServerRepository
|
|
|
|
|
->setServer($server)
|
|
|
|
|
->create($data['start_on_completion'] ?? false);
|
|
|
|
|
} catch (ConnectionException $exception) {
|
2021-08-29 14:09:43 -07:00
|
|
|
$this->serverDeletionService->withForce()->handle($server);
|
2019-12-16 21:02:30 -08:00
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
2018-01-28 17:14:14 -06:00
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store the server in the database and return the model.
|
|
|
|
|
*
|
2025-03-03 14:41:19 -05:00
|
|
|
* @param array<array-key, mixed> $data
|
|
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws DataValidationException
|
2018-01-28 17:14:14 -06:00
|
|
|
*/
|
|
|
|
|
private function createModel(array $data): Server
|
|
|
|
|
{
|
2018-03-03 22:20:53 -06:00
|
|
|
$uuid = $this->generateUniqueUuidCombo();
|
|
|
|
|
|
2024-03-16 15:01:21 -04:00
|
|
|
return Server::create([
|
2019-11-16 13:33:01 -08:00
|
|
|
'external_id' => Arr::get($data, 'external_id'),
|
2018-03-03 22:20:53 -06:00
|
|
|
'uuid' => $uuid,
|
2024-04-13 21:51:22 -04:00
|
|
|
'uuid_short' => substr($uuid, 0, 8),
|
2019-11-16 13:33:01 -08:00
|
|
|
'node_id' => Arr::get($data, 'node_id'),
|
|
|
|
|
'name' => Arr::get($data, 'name'),
|
|
|
|
|
'description' => Arr::get($data, 'description') ?? '',
|
2024-04-18 03:50:20 -04:00
|
|
|
'status' => ServerState::Installing,
|
2019-11-16 13:33:01 -08:00
|
|
|
'skip_scripts' => Arr::get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
|
|
|
|
|
'owner_id' => Arr::get($data, 'owner_id'),
|
|
|
|
|
'memory' => Arr::get($data, 'memory'),
|
|
|
|
|
'swap' => Arr::get($data, 'swap'),
|
|
|
|
|
'disk' => Arr::get($data, 'disk'),
|
|
|
|
|
'io' => Arr::get($data, 'io'),
|
|
|
|
|
'cpu' => Arr::get($data, 'cpu'),
|
2020-03-29 14:41:55 -04:00
|
|
|
'threads' => Arr::get($data, 'threads'),
|
2024-05-08 09:51:08 +02:00
|
|
|
'oom_killer' => Arr::get($data, 'oom_killer') ?? false,
|
2019-11-16 13:33:01 -08:00
|
|
|
'allocation_id' => Arr::get($data, 'allocation_id'),
|
|
|
|
|
'egg_id' => Arr::get($data, 'egg_id'),
|
|
|
|
|
'startup' => Arr::get($data, 'startup'),
|
|
|
|
|
'image' => Arr::get($data, 'image'),
|
2020-08-24 18:50:25 +02:00
|
|
|
'database_limit' => Arr::get($data, 'database_limit') ?? 0,
|
|
|
|
|
'allocation_limit' => Arr::get($data, 'allocation_limit') ?? 0,
|
|
|
|
|
'backup_limit' => Arr::get($data, 'backup_limit') ?? 0,
|
2024-06-01 15:52:13 -04:00
|
|
|
'docker_labels' => Arr::get($data, 'docker_labels'),
|
2017-07-19 20:49:41 -05:00
|
|
|
]);
|
2018-01-28 17:14:14 -06:00
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2018-01-28 17:14:14 -06:00
|
|
|
/**
|
|
|
|
|
* Configure the allocations assigned to this server.
|
2025-03-03 14:41:19 -05:00
|
|
|
*
|
|
|
|
|
* @param array{allocation_id: int, allocation_additional?: ?int[]} $data
|
2018-01-28 17:14:14 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
private function storeAssignedAllocations(Server $server, array $data): void
|
2018-01-28 17:14:14 -06:00
|
|
|
{
|
2017-07-19 20:49:41 -05:00
|
|
|
$records = [$data['allocation_id']];
|
2025-03-03 14:41:19 -05:00
|
|
|
if (isset($data['allocation_additional'])) {
|
2017-07-19 20:49:41 -05:00
|
|
|
$records = array_merge($records, $data['allocation_additional']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 16:39:50 +02:00
|
|
|
Allocation::query()
|
|
|
|
|
->whereIn('id', array_values(array_unique($records)))
|
|
|
|
|
->whereNull('server_id')
|
|
|
|
|
->lockForUpdate()
|
|
|
|
|
->get()
|
|
|
|
|
->each(function (Allocation $allocation) use ($server) {
|
|
|
|
|
$allocation->server_id = $server->id;
|
2025-11-08 21:54:41 +01:00
|
|
|
$allocation->is_locked = true;
|
2025-09-10 16:39:50 +02:00
|
|
|
$allocation->save();
|
|
|
|
|
});
|
2018-01-28 17:14:14 -06:00
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2018-01-28 17:14:14 -06:00
|
|
|
/**
|
|
|
|
|
* Process environment variables passed for this server and store them in the database.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
private function storeEggVariables(Server $server, Collection $variables): void
|
2018-01-28 17:14:14 -06:00
|
|
|
{
|
2025-03-30 21:56:49 -04:00
|
|
|
foreach ($variables as $variable) {
|
|
|
|
|
$server->serverVariables()->forceCreate([
|
|
|
|
|
'variable_id' => $variable->id,
|
|
|
|
|
'variable_value' => $variable->value ?? '',
|
|
|
|
|
]);
|
2017-10-26 23:49:54 -05:00
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
}
|
2018-02-10 14:01:49 -06:00
|
|
|
|
2018-03-03 22:20:53 -06:00
|
|
|
/**
|
|
|
|
|
* Create a unique UUID and UUID-Short combo for a server.
|
|
|
|
|
*/
|
|
|
|
|
private function generateUniqueUuidCombo(): string
|
|
|
|
|
{
|
|
|
|
|
$uuid = Uuid::uuid4()->toString();
|
|
|
|
|
|
2024-03-16 15:01:21 -04:00
|
|
|
$shortUuid = str($uuid)->substr(0, 8);
|
2024-04-13 21:51:22 -04:00
|
|
|
if (Server::query()->where('uuid', $uuid)->orWhere('uuid_short', $shortUuid)->exists()) {
|
2018-03-03 22:20:53 -06:00
|
|
|
return $this->generateUniqueUuidCombo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
}
|