Skip to content

Commit 8daec38

Browse files
committed
Complete base implementation of services for administrative server creation
1 parent f842aae commit 8daec38

22 files changed

+634
-142
lines changed

app/Contracts/Repository/Daemon/ServerRepositoryInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ public function update(array $data);
4747
/**
4848
* Mark a server to be reinstalled on the system.
4949
*
50+
* @param array|null $data
5051
* @return \Psr\Http\Message\ResponseInterface
5152
*/
52-
public function reinstall();
53+
public function reinstall($data = null);
5354

5455
/**
5556
* Mark a server as needing a container rebuild the next time the server is booted.
@@ -71,4 +72,11 @@ public function suspend();
7172
* @return \Psr\Http\Message\ResponseInterface
7273
*/
7374
public function unsuspend();
75+
76+
/**
77+
* Delete a server on the daemon.
78+
*
79+
* @return \Psr\Http\Message\ResponseInterface
80+
*/
81+
public function delete();
7482
}

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ public function update($id, array $fields, $validate = true, $force = false);
141141
*/
142142
public function updateWhereIn($column, array $values, array $fields);
143143

144+
/**
145+
* Update a record if it exists in the database, otherwise create it.
146+
*
147+
* @param array $where
148+
* @param array $fields
149+
* @param bool $validate
150+
* @param bool $force
151+
* @return mixed
152+
*
153+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
154+
*/
155+
public function updateOrCreate(array $where, array $fields, $validate = true, $force = false);
156+
144157
/**
145158
* Update multiple records matching the passed clauses.
146159
*

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ public function getDataForCreation($id);
7777
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
7878
*/
7979
public function getWithDatabases($id);
80+
81+
/**
82+
* Return data about the daemon service in a consumable format.
83+
*
84+
* @param int $id
85+
* @return array
86+
*
87+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
88+
*/
89+
public function getDaemonServiceData($id);
8090
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 53 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

2727
use Illuminate\Contracts\Config\Repository as ConfigRepository;
28-
use Log;
2928
use Alert;
3029
use Javascript;
3130
use Prologue\Alerts\AlertsMessageBag;
@@ -38,17 +37,17 @@
3837
use Pterodactyl\Http\Requests\Admin\ServerFormRequest;
3938
use Pterodactyl\Models\Server;
4039
use Illuminate\Http\Request;
41-
use GuzzleHttp\Exception\TransferException;
4240
use Pterodactyl\Exceptions\DisplayException;
4341
use Pterodactyl\Http\Controllers\Controller;
4442
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
45-
use Pterodactyl\Exceptions\DisplayValidationException;
46-
use Pterodactyl\Services\Database\CreationService as DatabaseCreationService;
43+
use Pterodactyl\Services\Database\DatabaseManagementService;
4744
use Pterodactyl\Services\Servers\BuildModificationService;
4845
use Pterodactyl\Services\Servers\ContainerRebuildService;
4946
use Pterodactyl\Services\Servers\CreationService;
47+
use Pterodactyl\Services\Servers\DeletionService;
5048
use Pterodactyl\Services\Servers\DetailsModificationService;
5149
use Pterodactyl\Services\Servers\ReinstallService;
50+
use Pterodactyl\Services\Servers\StartupModificationService;
5251
use Pterodactyl\Services\Servers\SuspensionService;
5352

5453
class ServersController extends Controller
@@ -84,15 +83,20 @@ class ServersController extends Controller
8483
protected $databaseRepository;
8584

8685
/**
87-
* @var \Pterodactyl\Services\Database\CreationService
86+
* @var \Pterodactyl\Services\Database\DatabaseManagementService
8887
*/
89-
protected $databaseCreationService;
88+
protected $databaseManagementService;
9089

9190
/**
9291
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
9392
*/
9493
protected $databaseHostRepository;
9594

95+
/**
96+
* @var \Pterodactyl\Services\Servers\DeletionService
97+
*/
98+
protected $deletionService;
99+
96100
/**
97101
* @var \Pterodactyl\Services\Servers\DetailsModificationService
98102
*/
@@ -128,6 +132,11 @@ class ServersController extends Controller
128132
*/
129133
protected $serviceRepository;
130134

135+
/**
136+
* @var \Pterodactyl\Services\Servers\StartupModificationService
137+
*/
138+
private $startupModificationService;
139+
131140
/**
132141
* @var \Pterodactyl\Services\Servers\SuspensionService
133142
*/
@@ -142,15 +151,17 @@ class ServersController extends Controller
142151
* @param \Illuminate\Contracts\Config\Repository $config
143152
* @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService
144153
* @param \Pterodactyl\Services\Servers\CreationService $service
145-
* @param \Pterodactyl\Services\Database\CreationService $databaseCreationService
154+
* @param \Pterodactyl\Services\Database\DatabaseManagementService $databaseManagementService
146155
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
147156
* @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository
157+
* @param \Pterodactyl\Services\Servers\DeletionService $deletionService
148158
* @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService
149159
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
150160
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
151161
* @param \Pterodactyl\Services\Servers\ReinstallService $reinstallService
152162
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
153163
* @param \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface $serviceRepository
164+
* @param \Pterodactyl\Services\Servers\StartupModificationService $startupModificationService
154165
* @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService
155166
*/
156167
public function __construct(
@@ -160,32 +171,36 @@ public function __construct(
160171
ConfigRepository $config,
161172
ContainerRebuildService $containerRebuildService,
162173
CreationService $service,
163-
DatabaseCreationService $databaseCreationService,
174+
DatabaseManagementService $databaseManagementService,
164175
DatabaseRepositoryInterface $databaseRepository,
165176
DatabaseHostRepository $databaseHostRepository,
177+
DeletionService $deletionService,
166178
DetailsModificationService $detailsModificationService,
167179
LocationRepositoryInterface $locationRepository,
168180
NodeRepositoryInterface $nodeRepository,
169181
ReinstallService $reinstallService,
170182
ServerRepositoryInterface $repository,
171183
ServiceRepositoryInterface $serviceRepository,
184+
StartupModificationService $startupModificationService,
172185
SuspensionService $suspensionService
173186
) {
174187
$this->alert = $alert;
175188
$this->allocationRepository = $allocationRepository;
176189
$this->buildModificationService = $buildModificationService;
177190
$this->config = $config;
178191
$this->containerRebuildService = $containerRebuildService;
179-
$this->databaseCreationService = $databaseCreationService;
192+
$this->databaseManagementService = $databaseManagementService;
180193
$this->databaseRepository = $databaseRepository;
181194
$this->databaseHostRepository = $databaseHostRepository;
182195
$this->detailsModificationService = $detailsModificationService;
196+
$this->deletionService = $deletionService;
183197
$this->locationRepository = $locationRepository;
184198
$this->nodeRepository = $nodeRepository;
185199
$this->reinstallService = $reinstallService;
186200
$this->repository = $repository;
187201
$this->service = $service;
188202
$this->serviceRepository = $serviceRepository;
203+
$this->startupModificationService = $startupModificationService;
189204
$this->suspensionService = $suspensionService;
190205
}
191206

@@ -234,21 +249,15 @@ public function create()
234249
* @param \Pterodactyl\Http\Requests\Admin\ServerFormRequest $request
235250
* @return \Illuminate\Http\RedirectResponse
236251
*
252+
* @throws \Pterodactyl\Exceptions\DisplayException
237253
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
238254
*/
239255
public function store(ServerFormRequest $request)
240256
{
241-
try {
242-
$server = $this->service->create($request->except('_token'));
243-
244-
return redirect()->route('admin.servers.view', $server->id);
245-
} catch (TransferException $ex) {
246-
Log::warning($ex);
247-
Alert::danger('A TransferException was encountered while trying to contact the daemon, please ensure it is online and accessible. This error has been logged.')
248-
->flash();
249-
}
257+
$server = $this->service->create($request->except('_token'));
258+
$this->alert->success(trans('admin/server.alerts.server_created'))->flash();
250259

251-
return redirect()->route('admin.servers.new')->withInput();
260+
return redirect()->route('admin.servers.view', $server->id);
252261
}
253262

254263
/**
@@ -508,9 +517,9 @@ public function manageSuspension(Request $request, Server $server)
508517
*/
509518
public function updateBuild(Request $request, Server $server)
510519
{
511-
$this->buildModificationService->handle($server, $request->intersect([
512-
'allocation_id', 'add_allocations', 'remove_allocations',
513-
'memory', 'swap', 'io', 'cpu', 'disk',
520+
$this->buildModificationService->handle($server, $request->only([
521+
'allocation_id', 'add_allocations', 'remove_allocations',
522+
'memory', 'swap', 'io', 'cpu', 'disk',
514523
]));
515524
$this->alert->success(trans('admin/server.alerts.build_updated'))->flash();
516525

@@ -520,69 +529,38 @@ public function updateBuild(Request $request, Server $server)
520529
/**
521530
* Start the server deletion process.
522531
*
523-
* @param \Illuminate\Http\Request $request
524-
* @param int $id
532+
* @param \Illuminate\Http\Request $request
533+
* @param \Pterodactyl\Models\Server $server
525534
* @return \Illuminate\Http\RedirectResponse
535+
*
536+
* @throws \Pterodactyl\Exceptions\DisplayException
526537
*/
527-
public function delete(Request $request, $id)
538+
public function delete(Request $request, Server $server)
528539
{
529-
$repo = new ServerRepository;
530-
531-
try {
532-
$repo->delete($id, $request->has('force_delete'));
533-
Alert::success('Server was successfully deleted from the system.')->flash();
534-
535-
return redirect()->route('admin.servers');
536-
} catch (DisplayException $ex) {
537-
Alert::danger($ex->getMessage())->flash();
538-
} catch (TransferException $ex) {
539-
Log::warning($ex);
540-
Alert::danger('A TransferException occurred while attempting to delete this server from the daemon, please ensure it is running. This error has been logged.')
541-
->flash();
542-
} catch (\Exception $ex) {
543-
Log::error($ex);
544-
Alert::danger('An unhandled exception occured while attemping to delete this server. This error has been logged.')
545-
->flash();
546-
}
540+
$this->deletionService->withForce($request->has('force_delete'))->handle($server);
541+
$this->alert->success(trans('admin/server.alerts.server_deleted'))->flash();
547542

548-
return redirect()->route('admin.servers.view.delete', $id);
543+
return redirect()->route('admin.servers');
549544
}
550545

551546
/**
552547
* Update the startup command as well as variables.
553548
*
554-
* @param \Illuminate\Http\Request $request
555-
* @param int $id
549+
* @param \Illuminate\Http\Request $request
550+
* @param \Pterodactyl\Models\Server $server
556551
* @return \Illuminate\Http\RedirectResponse
552+
*
553+
* @throws \Pterodactyl\Exceptions\DisplayException
554+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
557555
*/
558-
public function saveStartup(Request $request, $id)
556+
public function saveStartup(Request $request, Server $server)
559557
{
560-
$repo = new ServerRepository;
561-
562-
try {
563-
if ($repo->updateStartup($id, $request->except('_token'), true)) {
564-
Alert::success('Service configuration successfully modfied for this server, reinstalling now.')
565-
->flash();
566-
567-
return redirect()->route('admin.servers.view', $id);
568-
} else {
569-
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
570-
}
571-
} catch (DisplayValidationException $ex) {
572-
return redirect()->route('admin.servers.view.startup', $id)->withErrors(json_decode($ex->getMessage()));
573-
} catch (DisplayException $ex) {
574-
Alert::danger($ex->getMessage())->flash();
575-
} catch (TransferException $ex) {
576-
Log::warning($ex);
577-
Alert::danger('A TransferException occurred while attempting to update the startup for this server, please ensure the daemon is running. This error has been logged.')
578-
->flash();
579-
} catch (\Exception $ex) {
580-
Log::error($ex);
581-
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.')
582-
->flash();
583-
}
558+
$this->startupModificationService->isAdmin()->handle(
559+
$server, $request->except('_token')
560+
);
561+
$this->alert->success(trans('admin/server.alerts.startup_changed'))->flash();
584562

585-
return redirect()->route('admin.servers.view.startup', $id);
563+
return redirect()->route('admin.servers.view.startup', $server->id);
586564
}
587565

588566
/**
@@ -598,7 +576,7 @@ public function saveStartup(Request $request, $id)
598576
*/
599577
public function newDatabase(Request $request, $server)
600578
{
601-
$this->databaseCreationService->create($server, [
579+
$this->databaseManagementService->create($server, [
602580
'database' => $request->input('database'),
603581
'remote' => $request->input('remote'),
604582
'database_host_id' => $request->input('database_host_id'),
@@ -624,7 +602,7 @@ public function resetDatabasePassword(Request $request, $server)
624602
['id', '=', $request->input('database')],
625603
]);
626604

627-
$this->databaseCreationService->changePassword($database->id, str_random(20));
605+
$this->databaseManagementService->changePassword($database->id, str_random(20));
628606

629607
return response('', 204);
630608
}
@@ -646,7 +624,7 @@ public function deleteDatabase($server, $database)
646624
['id', '=', $database],
647625
]);
648626

649-
$this->databaseCreationService->delete($database->id);
627+
$this->databaseManagementService->delete($database->id);
650628

651629
return response('', 204);
652630
}

app/Http/Requests/Admin/ServerFormRequest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public function withValidator($validator)
7575
return ! ($input->auto_deploy);
7676
});
7777

78-
if ($this->input('pack_id') !== 0) {
79-
$validator->sometimes('pack_id', [
80-
Rule::exists('packs', 'id')->where(function ($query) {
81-
$query->where('selectable', 1);
82-
$query->where('option_id', $this->input('option_id'));
83-
}),
84-
]);
85-
}
78+
$validator->sometimes('pack_id', [
79+
Rule::exists('packs', 'id')->where(function ($query) {
80+
$query->where('selectable', 1);
81+
$query->where('option_id', $this->input('option_id'));
82+
}),
83+
], function ($input) {
84+
return $input->pack_id !== 0 && $input->pack_id !== null;
85+
});
8686
});
8787
}
8888
}

app/Repositories/Daemon/ServerRepository.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function create($id, $overrides = [], $start = false)
5959
'io' => (int) $server->io,
6060
'cpu' => (int) $server->cpu,
6161
'disk' => (int) $server->disk,
62-
'image' => (int) $server->image,
62+
'image' => $server->image,
6363
],
6464
'service' => [
6565
'type' => $server->option->service->folder,
@@ -97,9 +97,15 @@ public function update(array $data)
9797
/**
9898
* {@inheritdoc}
9999
*/
100-
public function reinstall()
100+
public function reinstall($data = null)
101101
{
102-
return $this->getHttpClient()->request('POST', '/server/reinstall');
102+
if (is_null($data)) {
103+
return $this->getHttpClient()->request('POST', '/server/reinstall');
104+
}
105+
106+
return $this->getHttpClient()->request('POST', '/server/reinstall', [
107+
'json' => $data,
108+
]);
103109
}
104110

105111
/**
@@ -125,4 +131,12 @@ public function unsuspend()
125131
{
126132
return $this->getHttpClient()->request('POST', '/server/unsuspend');
127133
}
134+
135+
/**
136+
* {@inheritdoc}
137+
*/
138+
public function delete()
139+
{
140+
return $this->getHttpClient()->request('DELETE', '/servers');
141+
}
128142
}

0 commit comments

Comments
 (0)