2019-09-22 22:30:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2020-10-31 18:14:28 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-02-24 05:20:02 +00:00
|
|
|
use Pterodactyl\Models\AuditLog;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2021-02-24 05:20:02 +00:00
|
|
|
use Illuminate\Database\Query\Builder;
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2020-04-10 19:04:11 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
2019-09-22 22:30:53 +00:00
|
|
|
use Pterodactyl\Services\Eggs\EggConfigurationService;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2020-10-31 18:14:28 +00:00
|
|
|
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
|
2019-12-22 21:28:51 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
2019-09-22 22:30:53 +00:00
|
|
|
|
2019-12-22 21:28:51 +00:00
|
|
|
class ServerDetailsController extends Controller
|
2019-09-22 22:30:53 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Eggs\EggConfigurationService
|
|
|
|
*/
|
|
|
|
private $eggConfigurationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2019-12-22 21:28:51 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
|
|
|
private $configurationStructureService;
|
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
/**
|
|
|
|
* ServerConfigurationController constructor.
|
|
|
|
*/
|
2019-12-22 21:28:51 +00:00
|
|
|
public function __construct(
|
|
|
|
ServerRepository $repository,
|
|
|
|
ServerConfigurationStructureService $configurationStructureService,
|
2020-04-10 19:04:11 +00:00
|
|
|
EggConfigurationService $eggConfigurationService,
|
|
|
|
NodeRepository $nodeRepository
|
2019-12-22 21:28:51 +00:00
|
|
|
) {
|
2019-09-22 22:30:53 +00:00
|
|
|
$this->eggConfigurationService = $eggConfigurationService;
|
|
|
|
$this->repository = $repository;
|
2019-12-22 21:28:51 +00:00
|
|
|
$this->configurationStructureService = $configurationStructureService;
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-22 21:28:51 +00:00
|
|
|
* Returns details about the server that allows Wings to self-recover and ensure
|
|
|
|
* that the state of the server matches the Panel at all times.
|
|
|
|
*
|
|
|
|
* @param string $uuid
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2019-09-22 22:30:53 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function __invoke(Request $request, $uuid)
|
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
|
2020-10-31 18:14:28 +00:00
|
|
|
return new JsonResponse([
|
2019-12-22 21:28:51 +00:00
|
|
|
'settings' => $this->configurationStructureService->handle($server),
|
|
|
|
'process_configuration' => $this->eggConfigurationService->handle($server),
|
|
|
|
]);
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|
2020-04-10 19:04:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all servers with their configurations that are assigned to the requesting node.
|
|
|
|
*
|
2020-10-31 18:14:28 +00:00
|
|
|
* @return \Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection
|
2020-04-10 19:04:11 +00:00
|
|
|
*/
|
|
|
|
public function list(Request $request)
|
|
|
|
{
|
2020-10-31 18:14:28 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
2020-04-10 23:54:50 +00:00
|
|
|
$node = $request->attributes->get('node');
|
2020-04-10 19:04:11 +00:00
|
|
|
|
2020-10-31 18:14:28 +00:00
|
|
|
// Avoid run-away N+1 SQL queries by pre-loading the relationships that are used
|
|
|
|
// within each of the services called below.
|
2020-10-31 18:28:31 +00:00
|
|
|
$servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables', 'location')
|
2020-10-31 18:14:28 +00:00
|
|
|
->where('node_id', $node->id)
|
2020-11-01 22:27:14 +00:00
|
|
|
// If you don't cast this to a string you'll end up with a stringified per_page returned in
|
|
|
|
// the metadata, and then Wings will panic crash as a result.
|
2021-01-23 20:09:16 +00:00
|
|
|
->paginate((int) $request->input('per_page', 50));
|
2020-04-10 19:04:11 +00:00
|
|
|
|
2020-10-31 18:14:28 +00:00
|
|
|
return new ServerConfigurationCollection($servers);
|
2020-04-10 19:04:11 +00:00
|
|
|
}
|
2021-02-24 05:20:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the state of all servers on the node to be normal. This is triggered
|
|
|
|
* when Wings restarts and is useful for ensuring that any servers on the node
|
|
|
|
* do not get incorrectly stuck in installing/restoring from backup states since
|
|
|
|
* a Wings reboot would completely stop those processes.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function resetState(Request $request)
|
|
|
|
{
|
|
|
|
$node = $request->attributes->get('node');
|
|
|
|
|
|
|
|
// Get all of the servers that are currently marked as restoring from a backup
|
|
|
|
// on this node that do not have a failed backup tracked in the audit logs table
|
|
|
|
// as well.
|
|
|
|
//
|
|
|
|
// For each of those servers we'll track a new audit log entry to mark them as
|
|
|
|
// failed and then update them all to be in a valid state.
|
|
|
|
/** @var \Pterodactyl\Models\Server[] $servers */
|
|
|
|
$servers = Server::query()
|
|
|
|
->select('servers.*')
|
|
|
|
->selectRaw('started.metadata->>"$.backup_uuid" as backup_uuid')
|
|
|
|
->leftJoinSub(function (Builder $builder) {
|
|
|
|
$builder->select('*')->from('audit_logs')
|
|
|
|
->where('action', AuditLog::SERVER__BACKUP_RESTORE_STARTED)
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
->limit(1);
|
|
|
|
}, 'started', 'started.server_id', '=', 'servers.id')
|
|
|
|
->leftJoin('audit_logs as completed', function (JoinClause $clause) {
|
|
|
|
$clause->whereColumn('completed.created_at', '>', 'started.created_at')
|
|
|
|
->whereIn('completed.action', [
|
|
|
|
AuditLog::SERVER__BACKUP_RESTORE_COMPLETED,
|
|
|
|
AuditLog::SERVER__BACKUP_RESTORE_FAILED,
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
->whereNotNull('started.id')
|
|
|
|
->whereNull('completed.id')
|
|
|
|
->where('servers.node_id', $node->id)
|
|
|
|
->where('servers.status', Server::STATUS_RESTORING_BACKUP)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
// Just create a new audit entry for this event and update the server state
|
|
|
|
// so that power actions, file management, and backups can resume as normal.
|
|
|
|
$server->audit(AuditLog::SERVER__BACKUP_RESTORE_FAILED, function (AuditLog $audit, Server $server) {
|
|
|
|
$audit->is_system = true;
|
|
|
|
$audit->metadata = ['backup_uuid' => $server->getAttribute('backup_uuid')];
|
|
|
|
$server->update(['status' => null]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update any server marked as installing or restoring as being in a normal state
|
|
|
|
// at this point in the process.
|
|
|
|
Server::query()->where('node_id', $node->id)
|
|
|
|
->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP])
|
|
|
|
->update(['status' => null]);
|
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
2019-09-22 22:30:53 +00:00
|
|
|
}
|