Complete base implementation of services for administrative server creation
This commit is contained in:
parent
f842aae3d3
commit
8daec38622
22 changed files with 633 additions and 141 deletions
|
@ -29,7 +29,7 @@ use Illuminate\Contracts\Encryption\Encrypter;
|
|||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
|
||||
class CreationService
|
||||
class DatabaseManagementService
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\DatabaseManager
|
|
@ -106,13 +106,18 @@ class BuildModificationService
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the build array.
|
||||
* Return the build array or an item out of the build array.
|
||||
*
|
||||
* @return array
|
||||
* @param string|null $attribute
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function getBuild()
|
||||
public function getBuild($attribute = null)
|
||||
{
|
||||
return $this->build;
|
||||
if (is_null($attribute)) {
|
||||
return $this->build;
|
||||
}
|
||||
|
||||
return array_get($this->build, $attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -133,17 +138,7 @@ class BuildModificationService
|
|||
$data['allocation_id'] = array_get($data, 'allocation_id', $server->allocation_id);
|
||||
$this->database->beginTransaction();
|
||||
|
||||
$this->setBuild('memory', (int) array_get($data, 'memory', $server->memory));
|
||||
$this->setBuild('swap', (int) array_get($data, 'swap', $server->swap));
|
||||
$this->setBuild('io', (int) array_get($data, 'io', $server->io));
|
||||
$this->setBuild('cpu', (int) array_get($data, 'cpu', $server->cpu));
|
||||
$this->setBuild('disk', (int) array_get($data, 'disk', $server->disk));
|
||||
|
||||
$this->processAllocations($server, $data);
|
||||
$allocations = $this->allocationRepository->findWhere([
|
||||
['server_id', '=', $server->id],
|
||||
]);
|
||||
|
||||
if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) {
|
||||
try {
|
||||
$allocation = $this->allocationRepository->findFirstWhere([
|
||||
|
@ -157,6 +152,24 @@ class BuildModificationService
|
|||
$this->setBuild('default', ['ip' => $allocation->ip, 'port' => $allocation->port]);
|
||||
}
|
||||
|
||||
$server = $this->repository->update($server->id, [
|
||||
'memory' => array_get($data, 'memory', $server->memory),
|
||||
'swap' => array_get($data, 'swap', $server->swap),
|
||||
'io' => array_get($data, 'io', $server->io),
|
||||
'cpu' => array_get($data, 'cpu', $server->cpu),
|
||||
'disk' => array_get($data, 'disk', $server->disk),
|
||||
'allocation_id' => array_get($data, 'allocation_id', $server->allocation_id),
|
||||
]);
|
||||
|
||||
$allocations = $this->allocationRepository->findWhere([
|
||||
['server_id', '=', $server->id],
|
||||
]);
|
||||
|
||||
$this->setBuild('memory', (int) $server->memory);
|
||||
$this->setBuild('swap', (int) $server->swap);
|
||||
$this->setBuild('io', (int) $server->io);
|
||||
$this->setBuild('cpu', (int) $server->cpu);
|
||||
$this->setBuild('disk', (int) $server->disk);
|
||||
$this->setBuild('ports|overwrite', $allocations->groupBy('ip')->map(function ($item) {
|
||||
return $item->pluck('port');
|
||||
})->toArray());
|
||||
|
|
|
@ -138,7 +138,7 @@ class CreationService
|
|||
public function create(array $data)
|
||||
{
|
||||
// @todo auto-deployment
|
||||
$validator = $this->validatorService->setAdmin()->setFields($data['environment'])->validate($data['option_id']);
|
||||
$validator = $this->validatorService->isAdmin()->setFields($data['environment'])->validate($data['option_id']);
|
||||
$uniqueShort = bin2hex(random_bytes(4));
|
||||
|
||||
$this->database->beginTransaction();
|
||||
|
@ -151,7 +151,7 @@ class CreationService
|
|||
'description' => $data['description'],
|
||||
'skip_scripts' => isset($data['skip_scripts']),
|
||||
'suspended' => false,
|
||||
'owner_id' => $data['user_id'],
|
||||
'owner_id' => $data['owner_id'],
|
||||
'memory' => $data['memory'],
|
||||
'swap' => $data['swap'],
|
||||
'disk' => $data['disk'],
|
||||
|
|
153
app/Services/Servers/DeletionService.php
Normal file
153
app/Services/Servers/DeletionService.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Services\Servers;
|
||||
|
||||
use Illuminate\Log\Writer;
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Services\Database\DatabaseManagementService;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
|
||||
class DeletionService
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Database\DatabaseManagementService
|
||||
*/
|
||||
protected $databaseManagementService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
*/
|
||||
protected $databaseRepository;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $force = false;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
|
||||
/**
|
||||
* DeletionService constructor.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionInterface $database
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
|
||||
* @param \Pterodactyl\Services\Database\DatabaseManagementService $databaseManagementService
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||
* @param \Illuminate\Log\Writer $writer
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $database,
|
||||
DaemonServerRepositoryInterface $daemonServerRepository,
|
||||
DatabaseRepositoryInterface $databaseRepository,
|
||||
DatabaseManagementService $databaseManagementService,
|
||||
ServerRepositoryInterface $repository,
|
||||
Writer $writer
|
||||
) {
|
||||
$this->daemonServerRepository = $daemonServerRepository;
|
||||
$this->database = $database;
|
||||
$this->databaseManagementService = $databaseManagementService;
|
||||
$this->databaseRepository = $databaseRepository;
|
||||
$this->repository = $repository;
|
||||
$this->writer = $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the server should be forcibly deleted from the panel (ignoring daemon errors) or not.
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return $this
|
||||
*/
|
||||
public function withForce($bool = true)
|
||||
{
|
||||
$this->force = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a server from the panel and remove any associated databases from hosts.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function handle($server)
|
||||
{
|
||||
if (! $server instanceof Server) {
|
||||
$server = $this->repository->withColumns(['id', 'node_id', 'uuid'])->find($server);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->delete();
|
||||
} catch (RequestException $exception) {
|
||||
$response = $exception->getResponse();
|
||||
|
||||
if (is_null($response) || (! is_null($response) && $response->getStatusCode() !== 404)) {
|
||||
$this->writer->warning($exception);
|
||||
|
||||
// If not forcing the deletion, throw an exception, otherwise just log it and
|
||||
// continue with server deletion process in the panel.
|
||||
if (! $this->force) {
|
||||
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
|
||||
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->database->beginTransaction();
|
||||
|
||||
$this->databaseRepository->withColumns('id')->findWhere([['server_id', '=', $server->id]])->each(function ($item) {
|
||||
$this->databaseManagementService->delete($item->id);
|
||||
});
|
||||
|
||||
$this->repository->delete($server->id);
|
||||
|
||||
$this->database->commit();
|
||||
}
|
||||
}
|
195
app/Services/Servers/StartupModificationService.php
Normal file
195
app/Services/Servers/StartupModificationService.php
Normal file
|
@ -0,0 +1,195 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Services\Servers;
|
||||
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Log\Writer;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Models\Server;
|
||||
|
||||
class StartupModificationService
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $admin = false;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
||||
*/
|
||||
protected $environmentService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
||||
*/
|
||||
protected $serverVariableRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
||||
*/
|
||||
protected $validatorService;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
|
||||
/**
|
||||
* StartupModificationService constructor.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionInterface $database
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
|
||||
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
||||
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
||||
* @param \Illuminate\Log\Writer $writer
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $database,
|
||||
DaemonServerRepositoryInterface $daemonServerRepository,
|
||||
EnvironmentService $environmentService,
|
||||
ServerRepositoryInterface $repository,
|
||||
ServerVariableRepositoryInterface $serverVariableRepository,
|
||||
VariableValidatorService $validatorService,
|
||||
Writer $writer
|
||||
) {
|
||||
$this->daemonServerRepository = $daemonServerRepository;
|
||||
$this->database = $database;
|
||||
$this->environmentService = $environmentService;
|
||||
$this->repository = $repository;
|
||||
$this->serverVariableRepository = $serverVariableRepository;
|
||||
$this->validatorService = $validatorService;
|
||||
$this->writer = $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this function should run at an administrative level.
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return $this
|
||||
*/
|
||||
public function isAdmin($bool = true)
|
||||
{
|
||||
$this->admin = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process startup modification for a server.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @param array $data
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
*/
|
||||
public function handle($server, array $data)
|
||||
{
|
||||
if (! $server instanceof Server) {
|
||||
$server = $this->repository->find($server);
|
||||
}
|
||||
|
||||
if (
|
||||
$server->service_id != array_get($data, 'service_id', $server->service_id) ||
|
||||
$server->option_id != array_get($data, 'option_id', $server->option_id) ||
|
||||
$server->pack_id != array_get($data, 'pack_id', $server->pack_id)
|
||||
) {
|
||||
$hasServiceChanges = true;
|
||||
}
|
||||
|
||||
$this->database->beginTransaction();
|
||||
if (isset($data['environment'])) {
|
||||
$validator = $this->validatorService->isAdmin($this->admin)
|
||||
->setFields($data['environment'])
|
||||
->validate(array_get($data, 'option_id', $server->option_id));
|
||||
|
||||
foreach ($validator->getResults() as $result) {
|
||||
$this->serverVariableRepository->withoutFresh()->updateOrCreate([
|
||||
'server_id' => $server->id,
|
||||
'variable_id' => $result['id'],
|
||||
], [
|
||||
'variable_value' => $result['value'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$daemonData = [
|
||||
'build' => [
|
||||
'env|overwrite' => $this->environmentService->process($server),
|
||||
],
|
||||
];
|
||||
|
||||
if ($this->admin) {
|
||||
$server = $this->repository->update($server->id, [
|
||||
'installed' => 0,
|
||||
'startup' => array_get($data, 'startup', $server->startup),
|
||||
'service_id' => array_get($data, 'service_id', $server->service_id),
|
||||
'option_id' => array_get($data, 'option_id', $server->service_id),
|
||||
'pack_id' => array_get($data, 'pack_id', $server->pack_id),
|
||||
'skip_scripts' => isset($data['skip_scripts']),
|
||||
]);
|
||||
|
||||
if (isset($hasServiceChanges)) {
|
||||
$daemonData['service'] = array_merge(
|
||||
$this->repository->withColumns(['id', 'option_id', 'pack_id'])->getDaemonServiceData($server),
|
||||
['skip_scripts' => isset($data['skip_scripts'])]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($daemonData);
|
||||
$this->database->commit();
|
||||
} catch (RequestException $exception) {
|
||||
$response = $exception->getResponse();
|
||||
$this->writer->warning($exception);
|
||||
|
||||
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
|
||||
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -103,11 +103,12 @@ class VariableValidatorService
|
|||
/**
|
||||
* Set this function to be running at the administrative level.
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdmin()
|
||||
public function isAdmin($bool = true)
|
||||
{
|
||||
$this->isAdmin = true;
|
||||
$this->isAdmin = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
Reference in a new issue