Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php
10287 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
4
5
use Illuminate\Http\Request;
6
use Pterodactyl\Models\Server;
7
use Illuminate\Http\JsonResponse;
8
use Pterodactyl\Facades\Activity;
9
use Illuminate\Database\ConnectionInterface;
10
use Pterodactyl\Http\Controllers\Controller;
11
use Pterodactyl\Services\Eggs\EggConfigurationService;
12
use Pterodactyl\Repositories\Eloquent\ServerRepository;
13
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
14
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
15
16
class ServerDetailsController extends Controller
17
{
18
/**
19
* ServerConfigurationController constructor.
20
*/
21
public function __construct(
22
protected ConnectionInterface $connection,
23
private ServerRepository $repository,
24
private ServerConfigurationStructureService $configurationStructureService,
25
private EggConfigurationService $eggConfigurationService,
26
) {
27
}
28
29
/**
30
* Returns details about the server that allows Wings to self-recover and ensure
31
* that the state of the server matches the Panel at all times.
32
*
33
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
34
*/
35
public function __invoke(Request $request, string $uuid): JsonResponse
36
{
37
$server = $this->repository->getByUuid($uuid);
38
39
return new JsonResponse([
40
'settings' => $this->configurationStructureService->handle($server),
41
'process_configuration' => $this->eggConfigurationService->handle($server),
42
]);
43
}
44
45
/**
46
* Lists all servers with their configurations that are assigned to the requesting node.
47
*/
48
public function list(Request $request): ServerConfigurationCollection
49
{
50
/** @var \Pterodactyl\Models\Node $node */
51
$node = $request->attributes->get('node');
52
53
// Avoid run-away N+1 SQL queries by preloading the relationships that are used
54
// within each of the services called below.
55
$servers = Server::query()->with('allocations', 'egg', 'mounts', 'variables', 'location')
56
->where('node_id', $node->id)
57
// If you don't cast this to a string you'll end up with a stringified per_page returned in
58
// the metadata, and then Wings will panic crash as a result.
59
->paginate((int) $request->input('per_page', 50));
60
61
return new ServerConfigurationCollection($servers);
62
}
63
64
/**
65
* Resets the state of all servers on the node to be normal. This is triggered
66
* when Wings restarts and is useful for ensuring that any servers on the node
67
* do not get incorrectly stuck in installing/restoring from backup states since
68
* a Wings reboot would completely stop those processes.
69
*
70
* @throws \Throwable
71
*/
72
public function resetState(Request $request): JsonResponse
73
{
74
$node = $request->attributes->get('node');
75
76
// Get all the servers that are currently marked as restoring from a backup
77
// on this node that do not have a failed backup tracked in the audit logs table
78
// as well.
79
//
80
// For each of those servers we'll track a new audit log entry to mark them as
81
// failed and then update them all to be in a valid state.
82
$servers = Server::query()
83
->with([
84
'activity' => fn ($builder) => $builder
85
->where('activity_logs.event', 'server:backup.restore-started')
86
->latest('timestamp'),
87
])
88
->where('node_id', $node->id)
89
->where('status', Server::STATUS_RESTORING_BACKUP)
90
->get();
91
92
$this->connection->transaction(function () use ($node, $servers) {
93
/** @var Server $server */
94
foreach ($servers as $server) {
95
/** @var \Pterodactyl\Models\ActivityLog|null $activity */
96
$activity = $server->activity->first();
97
if (!is_null($activity)) {
98
if ($subject = $activity->subjects->where('subject_type', 'backup')->first()) {
99
// Just create a new audit entry for this event and update the server state
100
// so that power actions, file management, and backups can resume as normal.
101
Activity::event('server:backup.restore-failed')
102
->subject($server, $subject->subject)
103
->property('name', $subject->subject->name) // @phpstan-ignore property.notFound
104
->log();
105
}
106
}
107
}
108
109
// Update any server marked as installing or restoring as being in a normal state
110
// at this point in the process.
111
Server::query()->where('node_id', $node->id)
112
->whereIn('status', [Server::STATUS_INSTALLING, Server::STATUS_RESTORING_BACKUP])
113
->update(['status' => null]);
114
});
115
116
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
117
}
118
}
119
120