Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Eloquent/ServerRepository.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Repositories\Eloquent;
4
5
use Pterodactyl\Models\Server;
6
use Illuminate\Support\Collection;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
11
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
12
13
class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
14
{
15
/**
16
* Return the model backing this repository.
17
*/
18
public function model(): string
19
{
20
return Server::class;
21
}
22
23
/**
24
* Load the egg relations onto the server model.
25
*/
26
public function loadEggRelations(Server $server, bool $refresh = false): Server
27
{
28
if (!$server->relationLoaded('egg') || $refresh) {
29
$server->load('egg.scriptFrom');
30
}
31
32
return $server;
33
}
34
35
/**
36
* Return a collection of servers with their associated data for rebuild operations.
37
*/
38
public function getDataForRebuild(?int $server = null, ?int $node = null): Collection
39
{
40
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);
41
42
if (!is_null($server) && is_null($node)) {
43
$instance = $instance->where('id', '=', $server);
44
} elseif (is_null($server) && !is_null($node)) {
45
$instance = $instance->where('node_id', '=', $node);
46
}
47
48
return $instance->get($this->getColumns());
49
}
50
51
/**
52
* Return a collection of servers with their associated data for reinstall operations.
53
*/
54
public function getDataForReinstall(?int $server = null, ?int $node = null): Collection
55
{
56
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);
57
58
if (!is_null($server) && is_null($node)) {
59
$instance = $instance->where('id', '=', $server);
60
} elseif (is_null($server) && !is_null($node)) {
61
$instance = $instance->where('node_id', '=', $node);
62
}
63
64
return $instance->get($this->getColumns());
65
}
66
67
/**
68
* Return a server model and all variables associated with the server.
69
*
70
* @throws RecordNotFoundException
71
*/
72
public function findWithVariables(int $id): Server
73
{
74
try {
75
return $this->getBuilder()->with('egg.variables', 'variables')
76
->where($this->getModel()->getKeyName(), '=', $id)
77
->firstOrFail($this->getColumns());
78
} catch (ModelNotFoundException) {
79
throw new RecordNotFoundException();
80
}
81
}
82
83
/**
84
* Get the primary allocation for a given server. If a model is passed into
85
* the function, load the allocation relationship onto it. Otherwise, find and
86
* return the server from the database.
87
*/
88
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server
89
{
90
if (!$server->relationLoaded('allocation') || $refresh) {
91
$server->load('allocation');
92
}
93
94
return $server;
95
}
96
97
/**
98
* Return enough data to be used for the creation of a server via the daemon.
99
*/
100
public function getDataForCreation(Server $server, bool $refresh = false): Server
101
{
102
foreach (['allocation', 'allocations', 'egg'] as $relation) {
103
if (!$server->relationLoaded($relation) || $refresh) {
104
$server->load($relation);
105
}
106
}
107
108
return $server;
109
}
110
111
/**
112
* Load associated databases onto the server model.
113
*/
114
public function loadDatabaseRelations(Server $server, bool $refresh = false): Server
115
{
116
if (!$server->relationLoaded('databases') || $refresh) {
117
$server->load('databases.host');
118
}
119
120
return $server;
121
}
122
123
/**
124
* Get data for use when updating a server on the Daemon. Returns an array of
125
* the egg which is used for build and rebuild. Only loads relations
126
* if they are missing, or refresh is set to true.
127
*/
128
public function getDaemonServiceData(Server $server, bool $refresh = false): array
129
{
130
if (!$server->relationLoaded('egg') || $refresh) {
131
$server->load('egg');
132
}
133
134
return [
135
'egg' => $server->getRelation('egg')->uuid,
136
];
137
}
138
139
/**
140
* Return a server by UUID.
141
*
142
* @throws RecordNotFoundException
143
*/
144
public function getByUuid(string $uuid): Server
145
{
146
try {
147
/** @var Server $model */
148
$model = $this->getBuilder()
149
->with('nest', 'node')
150
->where(function (Builder $query) use ($uuid) {
151
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
152
})
153
->firstOrFail($this->getColumns());
154
155
return $model;
156
} catch (ModelNotFoundException) {
157
throw new RecordNotFoundException();
158
}
159
}
160
161
/**
162
* Check if a given UUID and UUID-Short string are unique to a server.
163
*/
164
public function isUniqueUuidCombo(string $uuid, string $short): bool
165
{
166
return !$this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();
167
}
168
169
/**
170
* Returns all the servers that exist for a given node in a paginated response.
171
*/
172
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator
173
{
174
return $this->getBuilder()
175
->with(['user', 'nest', 'egg'])
176
->where('node_id', '=', $node)
177
->paginate($limit);
178
}
179
}
180
181