Skip to content

Commit b3e5565

Browse files
committed
Cleanup auto-deployment functions substantially
Also cleans up ServerRepository to use named models more clearly.
1 parent c59cfce commit b3e5565

File tree

4 files changed

+266
-108
lines changed

4 files changed

+266
-108
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions;
26+
27+
class AutoDeploymentException extends \Exception
28+
{
29+
//
30+
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Pterodactyl\Http\Controllers\Controller;
3535
use Pterodactyl\Repositories\ServerRepository;
3636
use Pterodactyl\Repositories\DatabaseRepository;
37+
use Pterodactyl\Exceptions\AutoDeploymentException;
3738
use Pterodactyl\Exceptions\DisplayValidationException;
3839

3940
class ServersController extends Controller
@@ -97,6 +98,8 @@ public function store(Request $request)
9798
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
9899
} catch (DisplayException $ex) {
99100
Alert::danger($ex->getMessage())->flash();
101+
} catch (AutoDeploymentException $ex) {
102+
Alert::danger('Auto-Deployment Exception: ' . $ex->getMessage())->flash();
100103
} catch (TransferException $ex) {
101104
Log::warning($ex);
102105
Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')->flash();

app/Repositories/ServerRepository.php

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@
2727
use DB;
2828
use Crypt;
2929
use Validator;
30-
use Pterodactyl\Models;
30+
use Pterodactyl\Models\Pack;
31+
use Pterodactyl\Models\User;
32+
use Pterodactyl\Models\Node;
33+
use Pterodactyl\Models\Server;
34+
use Pterodactyl\Models\Service;
35+
use Pterodactyl\Models\Allocation;
3136
use Pterodactyl\Services\UuidService;
37+
use Pterodactyl\Models\ServiceOption;
38+
use Pterodactyl\Models\ServerVariable;
39+
use Pterodactyl\Models\ServiceVariable;
3240
use GuzzleHttp\Exception\ClientException;
3341
use GuzzleHttp\Exception\TransferException;
3442
use Pterodactyl\Services\DeploymentService;
@@ -80,12 +88,11 @@ protected function generateSFTPUsername($name, $identifier = null)
8088
* @return \Pterodactyl\Models\Server
8189
*
8290
* @throws \Pterodactyl\Exceptions\DisplayException
91+
* @throws \Pterodactyl\Exceptions\AutoDeploymentException
8392
* @throws \Pterodactyl\Exceptions\DisplayValidationException
8493
*/
8594
public function create(array $data)
8695
{
87-
88-
// Validate Fields
8996
$validator = Validator::make($data, [
9097
'user_id' => 'required|exists:users,id',
9198
'name' => 'required|regex:/^([\w .-]{1,200})$/',
@@ -124,25 +131,27 @@ public function create(array $data)
124131
throw new DisplayValidationException(json_encode($validator->errors()));
125132
}
126133

127-
$user = Models\User::findOrFail($data['user_id']);
134+
$user = User::findOrFail($data['user_id']);
128135

129-
$autoDeployed = false;
130-
if (isset($data['auto_deploy']) && $data['auto_deploy']) {
131-
// This is an auto-deployment situation
132-
// Ignore any other passed node data
133-
unset($data['node_id'], $data['allocation_id']);
136+
$deployment = false;
137+
if (isset($data['auto_deploy'])) {
138+
$deployment = new DeploymentService;
134139

135-
$autoDeployed = true;
136-
$node = DeploymentService::smartRandomNode($data['memory'], $data['disk'], $data['location_id']);
137-
$allocation = DeploymentService::randomAllocation($node->id);
138-
} else {
139-
$node = Models\Node::findOrFail($data['node_id']);
140+
if (isset($data['location_id'])) {
141+
$deployment->setLocation($data['location_id']);
142+
}
143+
144+
$deployment->setMemory($data['memory'])->setDisk($data['disk'])->select();
140145
}
141146

147+
$node = (! $deployment) ? Node::findOrFail($data['node_id']) : $deployment->node();
148+
142149
// Verify IP & Port are a.) free and b.) assigned to the node.
143150
// We know the node exists because of 'exists:nodes,id' in the validation
144-
if (! $autoDeployed) {
145-
$allocation = Models\Allocation::where('id', $data['allocation_id'])->where('node_id', $data['node_id'])->whereNull('server_id')->first();
151+
if (! $deployment) {
152+
$allocation = Allocation::where('id', $data['allocation_id'])->where('node_id', $data['node_id'])->whereNull('server_id')->first();
153+
} else {
154+
$allocation = $deployment->allocation();
146155
}
147156

148157
// Something failed in the query, either that combo doesn't exist, or it is in use.
@@ -154,7 +163,7 @@ public function create(array $data)
154163
// We know the service and option exists because of the validation.
155164
// We need to verify that the option exists for the service, and then check for
156165
// any required variable fields. (fields are labeled env_<env_variable>)
157-
$option = Models\ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first();
166+
$option = ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first();
158167
if (! $option) {
159168
throw new DisplayException('The requested service option does not exist for the specified service.');
160169
}
@@ -163,17 +172,17 @@ public function create(array $data)
163172
if (! isset($data['pack_id']) || (int) $data['pack_id'] < 1) {
164173
$data['pack_id'] = null;
165174
} else {
166-
$pack = Models\Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
175+
$pack = Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
167176
if (! $pack) {
168177
throw new DisplayException('The requested service pack does not seem to exist for this combination.');
169178
}
170179
}
171180

172181
// Load up the Service Information
173-
$service = Models\Service::find($option->service_id);
182+
$service = Service::find($option->service_id);
174183

175184
// Check those Variables
176-
$variables = Models\ServiceVariable::where('option_id', $data['option_id'])->get();
185+
$variables = ServiceVariable::where('option_id', $data['option_id'])->get();
177186
$variableList = [];
178187
if ($variables) {
179188
foreach ($variables as $variable) {
@@ -206,9 +215,9 @@ public function create(array $data)
206215
}
207216

208217
// Check Overallocation
209-
if (! $autoDeployed) {
218+
if (! $deployment) {
210219
if (is_numeric($node->memory_overallocate) || is_numeric($node->disk_overallocate)) {
211-
$totals = Models\Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first();
220+
$totals = Server::select(DB::raw('SUM(memory) as memory, SUM(disk) as disk'))->where('node_id', $node->id)->first();
212221

213222
// Check memory limits
214223
if (is_numeric($node->memory_overallocate)) {
@@ -236,7 +245,7 @@ public function create(array $data)
236245
$uuid = new UuidService;
237246

238247
// Add Server to the Database
239-
$server = new Models\Server;
248+
$server = new Server;
240249
$genUuid = $uuid->generate('servers', 'uuid');
241250
$genShortUuid = $uuid->generateShort('servers', 'uuidShort', $genUuid);
242251

@@ -278,7 +287,7 @@ public function create(array $data)
278287
// Add Additional Allocations
279288
if (isset($data['allocation_additional']) && is_array($data['allocation_additional'])) {
280289
foreach ($data['allocation_additional'] as $allocation) {
281-
$model = Models\Allocation::where('id', $allocation)->where('node_id', $data['node_id'])->whereNull('server_id')->first();
290+
$model = Allocation::where('id', $allocation)->where('node_id', $data['node_id'])->whereNull('server_id')->first();
282291
if (! $model) {
283292
continue;
284293
}
@@ -296,7 +305,7 @@ public function create(array $data)
296305
foreach ($variableList as $item) {
297306
$environmentVariables[$item['env']] = $item['val'];
298307

299-
Models\ServerVariable::create([
308+
ServerVariable::create([
300309
'server_id' => $server->id,
301310
'variable_id' => $item['id'],
302311
'variable_value' => $item['val'],
@@ -379,7 +388,7 @@ public function updateDetails($id, array $data)
379388
DB::beginTransaction();
380389

381390
try {
382-
$server = Models\Server::with('user')->findOrFail($id);
391+
$server = Server::with('user')->findOrFail($id);
383392

384393
// Update daemon secret if it was passed.
385394
if (isset($data['reset_token']) || (isset($data['owner_id']) && (int) $data['owner_id'] !== $server->user->id)) {
@@ -445,7 +454,7 @@ public function updateContainer($id, array $data)
445454

446455
DB::beginTransaction();
447456
try {
448-
$server = Models\Server::findOrFail($id);
457+
$server = Server::findOrFail($id);
449458

450459
$server->image = $data['docker_image'];
451460
$server->save();
@@ -502,7 +511,7 @@ public function changeBuild($id, array $data)
502511
DB::beginTransaction();
503512

504513
try {
505-
$server = Models\Server::with('allocation', 'allocations')->findOrFail($id);
514+
$server = Server::with('allocation', 'allocations')->findOrFail($id);
506515
$newBuild = [];
507516
$newAllocations = [];
508517

@@ -525,7 +534,7 @@ public function changeBuild($id, array $data)
525534
// Add Assignments
526535
if (isset($data['add_allocations'])) {
527536
foreach ($data['add_allocations'] as $allocation) {
528-
$model = Models\Allocation::where('id', $allocation)->whereNull('server_id')->first();
537+
$model = Allocation::where('id', $allocation)->whereNull('server_id')->first();
529538
if (! $model) {
530539
continue;
531540
}
@@ -555,7 +564,7 @@ public function changeBuild($id, array $data)
555564
}
556565

557566
$newPorts = true;
558-
Models\Allocation::where('id', $allocation)->where('server_id', $server->id)->update([
567+
Allocation::where('id', $allocation)->where('server_id', $server->id)->update([
559568
'server_id' => null,
560569
]);
561570
}
@@ -637,7 +646,7 @@ protected function changeService($id, array $data)
637646
{
638647
}
639648

640-
protected function processVariables(Models\Server $server, $data, $admin = false)
649+
protected function processVariables(Server $server, $data, $admin = false)
641650
{
642651
$server->load('option.variables');
643652

@@ -671,7 +680,7 @@ protected function processVariables(Models\Server $server, $data, $admin = false
671680
));
672681
}
673682

674-
$svar = Models\ServerVariable::firstOrNew([
683+
$svar = ServerVariable::firstOrNew([
675684
'server_id' => $server->id,
676685
'variable_id' => $variable->id,
677686
]);
@@ -716,7 +725,7 @@ protected function processVariables(Models\Server $server, $data, $admin = false
716725
*/
717726
public function updateStartup($id, array $data, $admin = false)
718727
{
719-
$server = Models\Server::with('variables', 'option.variables')->findOrFail($id);
728+
$server = Server::with('variables', 'option.variables')->findOrFail($id);
720729
$hasServiceChanges = false;
721730

722731
if ($admin) {
@@ -771,7 +780,7 @@ public function updateStartup($id, array $data, $admin = false)
771780
// We know the service and option exists because of the validation.
772781
// We need to verify that the option exists for the service, and then check for
773782
// any required variable fields. (fields are labeled env_<env_variable>)
774-
$option = Models\ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first();
783+
$option = ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first();
775784
if (! $option) {
776785
throw new DisplayException('The requested service option does not exist for the specified service.');
777786
}
@@ -780,7 +789,7 @@ public function updateStartup($id, array $data, $admin = false)
780789
if (! isset($data['pack_id']) || (int) $data['pack_id'] < 1) {
781790
$data['pack_id'] = null;
782791
} else {
783-
$pack = Models\Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
792+
$pack = Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
784793
if (! $pack) {
785794
throw new DisplayException('The requested service pack does not seem to exist for this combination.');
786795
}
@@ -833,7 +842,7 @@ public function updateStartup($id, array $data, $admin = false)
833842
*/
834843
public function delete($id, $force = false)
835844
{
836-
$server = Models\Server::with('node', 'allocations', 'variables')->findOrFail($id);
845+
$server = Server::with('node', 'allocations', 'variables')->findOrFail($id);
837846

838847
// Due to MySQL lockouts if the daemon response fails, we need to
839848
// delete the server from the daemon first. If it succeedes and then
@@ -903,7 +912,7 @@ public function delete($id, $force = false)
903912
*/
904913
public function toggleInstall($id)
905914
{
906-
$server = Models\Server::findOrFail($id);
915+
$server = Server::findOrFail($id);
907916
if ($server->installed > 1) {
908917
throw new DisplayException('This server was marked as having a failed install or being deleted, you cannot override this.');
909918
}
@@ -921,7 +930,7 @@ public function toggleInstall($id)
921930
*/
922931
public function toggleAccess($id, $unsuspend = true)
923932
{
924-
$server = Models\Server::with('node')->findOrFail($id);
933+
$server = Server::with('node')->findOrFail($id);
925934

926935
DB::transaction(function () use ($server, $unsuspend) {
927936
if (
@@ -952,7 +961,7 @@ public function toggleAccess($id, $unsuspend = true)
952961
*/
953962
public function updateSFTPPassword($id, $password)
954963
{
955-
$server = Models\Server::with('node')->findOrFail($id);
964+
$server = Server::with('node')->findOrFail($id);
956965

957966
$validator = Validator::make(['password' => $password], [
958967
'password' => 'required|regex:/^((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})$/',
@@ -983,7 +992,7 @@ public function updateSFTPPassword($id, $password)
983992
*/
984993
public function reinstall($id)
985994
{
986-
$server = Models\Server::with('node')->findOrFail($id);
995+
$server = Server::with('node')->findOrFail($id);
987996

988997
DB::transaction(function () use ($server) {
989998
$server->installed = 0;

0 commit comments

Comments
 (0)