2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-07-25 02:34:10 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-25 02:34:10 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-10-26 03:33:28 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
class StartupModificationService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $admin = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $daemonServerRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2017-09-27 03:54:34 +00:00
|
|
|
protected $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StartupModificationService constructor.
|
|
|
|
*
|
2017-09-27 03:54:34 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-07-25 02:34:10 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-09-27 03:54:34 +00:00
|
|
|
ConnectionInterface $connection,
|
2017-07-25 02:34:10 +00:00
|
|
|
DaemonServerRepositoryInterface $daemonServerRepository,
|
|
|
|
EnvironmentService $environmentService,
|
|
|
|
ServerRepositoryInterface $repository,
|
|
|
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
2017-09-27 03:54:34 +00:00
|
|
|
VariableValidatorService $validatorService
|
2017-07-25 02:34:10 +00:00
|
|
|
) {
|
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->connection = $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->environmentService = $environmentService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->serverVariableRepository = $serverVariableRepository;
|
|
|
|
$this->validatorService = $validatorService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this function should run at an administrative level.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param bool $bool
|
2017-07-25 02:34:10 +00:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function isAdmin($bool = true)
|
|
|
|
{
|
|
|
|
$this->admin = $bool;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process startup modification for a server.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param int|\Pterodactyl\Models\Server $server
|
|
|
|
* @param array $data
|
2017-07-25 02:34:10 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-13 19:55:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
public function handle($server, array $data)
|
|
|
|
{
|
|
|
|
if (! $server instanceof Server) {
|
|
|
|
$server = $this->repository->find($server);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2017-10-07 22:21:41 +00:00
|
|
|
$server->nest_id != array_get($data, 'nest_id', $server->nest_id) ||
|
|
|
|
$server->egg_id != array_get($data, 'egg_id', $server->egg_id) ||
|
2017-07-25 02:34:10 +00:00
|
|
|
$server->pack_id != array_get($data, 'pack_id', $server->pack_id)
|
|
|
|
) {
|
|
|
|
$hasServiceChanges = true;
|
|
|
|
}
|
|
|
|
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-10-26 03:33:28 +00:00
|
|
|
if (! is_null(array_get($data, 'environment'))) {
|
2017-07-25 02:34:10 +00:00
|
|
|
$validator = $this->validatorService->isAdmin($this->admin)
|
2017-10-26 03:33:28 +00:00
|
|
|
->setFields(array_get($data, 'environment', []))
|
2017-10-07 22:21:41 +00:00
|
|
|
->validate(array_get($data, 'egg_id', $server->egg_id));
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
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),
|
2017-10-07 22:21:41 +00:00
|
|
|
'nest_id' => array_get($data, 'nest_id', $server->nest_id),
|
|
|
|
'egg_id' => array_get($data, 'egg_id', $server->egg_id),
|
|
|
|
'pack_id' => array_get($data, 'pack_id', $server->pack_id) > 0 ? array_get($data, 'pack_id', $server->pack_id) : null,
|
2017-07-25 02:34:10 +00:00
|
|
|
'skip_scripts' => isset($data['skip_scripts']),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (isset($hasServiceChanges)) {
|
|
|
|
$daemonData['service'] = array_merge(
|
2017-10-07 22:21:41 +00:00
|
|
|
$this->repository->withColumns(['id', 'egg_id', 'pack_id'])->getDaemonServiceData($server->id),
|
2017-07-25 02:34:10 +00:00
|
|
|
['skip_scripts' => isset($data['skip_scripts'])]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($daemonData);
|
|
|
|
} catch (RequestException $exception) {
|
2017-10-26 03:33:28 +00:00
|
|
|
$this->connection->rollBack();
|
|
|
|
throw new DaemonConnectionException($exception);
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
2017-10-26 03:33:28 +00:00
|
|
|
|
|
|
|
$this->connection->commit();
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
}
|