Skip to content

Commit cb6b44d

Browse files
committed
Completed new server page with new theme.
1 parent 5899991 commit cb6b44d

File tree

7 files changed

+482
-162
lines changed

7 files changed

+482
-162
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ This file is a running track of new features and fixes to each version of the pa
33

44
This project follows [Semantic Versioning](http://semver.org) guidelines.
55

6+
## v0.6.0-pre.5 (Courageous Carniadactylus)
7+
### Changed
8+
* New theme applied to Admin CP. Many graphical changes were made, some data was moved around and some display data changed. Too much was changed to feasibly log it all in here. Major breaking changes or notable new features will be logged.
9+
* New server creation page now makes significantly less AJAX calls and is much quicker to respond.
10+
11+
### Added
12+
* Ability to assign multiple allocations at once when creating a new server.
13+
14+
### Deprecated
15+
* Old API calls to `Server::create` will fail due to changed data structure.
16+
617
## v0.6.0-pre.4 (Courageous Carniadactylus)
718
### Fixed
819
* `[pre.3]` — Fixes bug in cache handler that doesn't cache against the user making the request. Would have allowed for users to access servers not belonging to themselves in production.

app/Http/Controllers/Admin/ServersController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use Log;
2828
use Alert;
29+
use Javascript;
2930
use Pterodactyl\Models;
3031
use Illuminate\Http\Request;
3132
use Pterodactyl\Exceptions\DisplayException;
@@ -61,9 +62,18 @@ public function getIndex(Request $request)
6162

6263
public function getNew(Request $request)
6364
{
65+
$services = Models\Service::with('options.packs', 'options.variables')->get();
66+
Javascript::put([
67+
'services' => $services->map(function ($item) {
68+
return array_merge($item->toArray(), [
69+
'options' => $item->options->keyBy('id')->toArray(),
70+
]);
71+
})->keyBy('id'),
72+
]);
73+
6474
return view('admin.servers.new', [
6575
'locations' => Models\Location::all(),
66-
'services' => Models\Service::all(),
76+
'services' => $services,
6777
]);
6878
}
6979

@@ -119,7 +129,7 @@ public function postNewServerGetNodes(Request $request)
119129
{
120130
$nodes = Models\Node::with('allocations')->where('location_id', $request->input('location'))->get();
121131
return $nodes->map(function ($item) {
122-
$filtered = $item->allocations->map(function($map) {
132+
$filtered = $item->allocations->where('server_id', null)->map(function($map) {
123133
return collect($map)->only(['id', 'ip', 'port']);
124134
});
125135

app/Repositories/ServerRepository.php

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function create(array $data)
8383

8484
// Validate Fields
8585
$validator = Validator::make($data, [
86-
'owner' => 'bail|required',
86+
'user_id' => 'required|exists:users,id',
8787
'name' => 'required|regex:/^([\w .-]{1,200})$/',
8888
'memory' => 'required|numeric|min:0',
8989
'swap' => 'required|numeric|min:-1',
@@ -95,25 +95,20 @@ public function create(array $data)
9595
'location_id' => 'required|numeric|min:1|exists:locations,id',
9696
'pack_id' => 'sometimes|nullable|numeric|min:0',
9797
'startup' => 'string',
98-
'custom_image_name' => 'required_if:use_custom_image,on',
9998
'auto_deploy' => 'sometimes|boolean',
10099
'custom_id' => 'sometimes|required|numeric|unique:servers,id',
101100
]);
102101

103-
$validator->sometimes('node_id', 'bail|required|numeric|min:1|exists:nodes,id', function ($input) {
102+
$validator->sometimes('node_id', 'required|numeric|min:1|exists:nodes,id', function ($input) {
104103
return ! ($input->auto_deploy);
105104
});
106105

107-
$validator->sometimes('ip', 'required|ip', function ($input) {
108-
return ! $input->auto_deploy && ! $input->allocation;
109-
});
110-
111-
$validator->sometimes('port', 'required|numeric|min:1|max:65535', function ($input) {
112-
return ! $input->auto_deploy && ! $input->allocation;
106+
$validator->sometimes('allocation_id', 'required|numeric|exists:allocations,id', function ($input) {
107+
return ! ($input->auto_deploy);
113108
});
114109

115-
$validator->sometimes('allocation_id', 'numeric|exists:allocations,id', function ($input) {
116-
return ! ($input->auto_deploy || ($input->port && $input->ip));
110+
$validator->sometimes('allocation_additional.*', 'sometimes|required|numeric|exists:allocations,id', function ($input) {
111+
return ! ($input->auto_deploy);
117112
});
118113

119114
// Run validator, throw catchable and displayable exception if it fails.
@@ -122,16 +117,13 @@ public function create(array $data)
122117
throw new DisplayValidationException($validator->errors());
123118
}
124119

125-
$user = Models\User::select('id', 'email')->where((is_int($data['owner'])) ? 'id' : 'email', $data['owner'])->first();
126-
if (! $user) {
127-
throw new DisplayException('The user id or email passed to the function was not found on the system.');
128-
}
120+
$user = Models\User::findOrFail($data['user_id']);
129121

130122
$autoDeployed = false;
131-
if (isset($data['auto_deploy']) && in_array($data['auto_deploy'], [true, 1, '1'])) {
123+
if (isset($data['auto_deploy']) && $data['auto_deploy']) {
132124
// This is an auto-deployment situation
133125
// Ignore any other passed node data
134-
unset($data['node_id'], $data['ip'], $data['port'], $data['allocation_id']);
126+
unset($data['node_id'], $data['allocation_id']);
135127

136128
$autoDeployed = true;
137129
$node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location_id']);
@@ -143,17 +135,12 @@ public function create(array $data)
143135
// Verify IP & Port are a.) free and b.) assigned to the node.
144136
// We know the node exists because of 'exists:nodes,id' in the validation
145137
if (! $autoDeployed) {
146-
if (! isset($data['allocation_id'])) {
147-
$model = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port']);
148-
} else {
149-
$model = Models\Allocation::where('id', $data['allocation_id']);
150-
}
151-
$allocation = $model->where('node_id', $data['node_id'])->whereNull('server_id')->first();
138+
$allocation = Models\Allocation::where('id', $data['allocation_id'])->where('node_id', $data['node_id'])->whereNull('server_id')->first();
152139
}
153140

154141
// Something failed in the query, either that combo doesn't exist, or it is in use.
155142
if (! $allocation) {
156-
throw new DisplayException('The selected IP/Port combination or Allocation ID is either already in use, or unavaliable for this node.');
143+
throw new DisplayException('The selected Allocation ID is either already in use, or unavaliable for this node.');
157144
}
158145

159146
// Validate those Service Option Variables
@@ -166,11 +153,9 @@ public function create(array $data)
166153
}
167154

168155
// Validate the Pack
169-
if ($data['pack_id'] == 0) {
156+
if (! isset($data['pack_id']) || (int) $data['pack_id'] < 1) {
170157
$data['pack_id'] = null;
171-
}
172-
173-
if (! is_null($data['pack_id'])) {
158+
} else {
174159
$pack = Models\ServicePack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
175160
if (! $pack) {
176161
throw new DisplayException('The requested service pack does not seem to exist for this combination.');
@@ -188,7 +173,7 @@ public function create(array $data)
188173

189174
// Is the variable required?
190175
if (! isset($data['env_' . $variable->env_variable])) {
191-
if ($variable->required === 1) {
176+
if ($variable->required) {
192177
throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.');
193178
}
194179
$variableList[] = [
@@ -271,7 +256,7 @@ public function create(array $data)
271256
'pack_id' => $data['pack_id'],
272257
'startup' => $data['startup'],
273258
'daemonSecret' => $uuid->generate('servers', 'daemonSecret'),
274-
'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image,
259+
'image' => (isset($data['custom_container'])) ? $data['custom_container'] : $option->docker_image,
275260
'username' => $this->generateSFTPUsername($data['name'], $genShortUuid),
276261
'sftp_password' => Crypt::encrypt('not set'),
277262
]);
@@ -281,6 +266,19 @@ public function create(array $data)
281266
$allocation->server_id = $server->id;
282267
$allocation->save();
283268

269+
// Add Additional Allocations
270+
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
271+
foreach($data['allocation_additional'] as $allocation) {
272+
$model = Models\Allocation::where('id', $allocation)->where('node_id', $data['node_id'])->whereNull('server_id')->first();
273+
if (! $model) {
274+
continue;
275+
}
276+
277+
$model->server_id = $server->id;
278+
$model->save();
279+
}
280+
}
281+
284282
// Add Variables
285283
$environmentVariables = [
286284
'STARTUP' => $data['startup'],
@@ -296,25 +294,26 @@ public function create(array $data)
296294
]);
297295
}
298296

297+
$server->load('allocation', 'allocations');
299298
$node->guzzleClient(['X-Access-Token' => $node->daemonSecret])->request('POST', '/servers', [
300299
'json' => [
301300
'uuid' => (string) $server->uuid,
302301
'user' => $server->username,
303302
'build' => [
304303
'default' => [
305-
'ip' => $allocation->ip,
306-
'port' => (int) $allocation->port,
307-
],
308-
'ports' => [
309-
(string) $allocation->ip => [(int) $allocation->port],
304+
'ip' => $server->allocation->ip,
305+
'port' => $server->allocation->port,
310306
],
307+
'ports' => $server->allocations->groupBy('ip')->map(function ($item) {
308+
return $item->pluck('port');
309+
})->toArray(),
311310
'env' => $environmentVariables,
312311
'memory' => (int) $server->memory,
313312
'swap' => (int) $server->swap,
314313
'io' => (int) $server->io,
315314
'cpu' => (int) $server->cpu,
316315
'disk' => (int) $server->disk,
317-
'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image,
316+
'image' => (isset($data['custom_container'])) ? $data['custom_container'] : $option->docker_image,
318317
],
319318
'service' => [
320319
'type' => $service->file,

config/javascript.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
'bind_js_vars_to_this_view' => [
1717
'layouts.master',
18+
'layouts.admin',
1819
],
1920

2021
/*

0 commit comments

Comments
 (0)