From 65393914c3cdf0d1e530fe57c4e4f7eba5e8ad86 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 21 May 2020 15:14:09 -0600 Subject: [PATCH] Send mounts when wings fetches server information, fix issue with mount fields not being updated --- .../Controllers/Admin/MountController.php | 1 + .../Controllers/Admin/ServersController.php | 44 +++++++++++++++++++ app/Models/Mount.php | 2 +- .../ServerConfigurationStructureService.php | 12 +++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index 2badebe16..3c5f3d4b4 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -3,6 +3,7 @@ namespace Pterodactyl\Http\Controllers\Admin; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; use Pterodactyl\Models\Mount; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Exceptions\DisplayException; diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 8d527d1c1..09173d43f 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -9,12 +9,17 @@ namespace Pterodactyl\Http\Controllers\Admin; +use GuzzleHttp\Exception\RequestException; use Illuminate\Http\Request; +use Illuminate\Support\Arr; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Models\User; use Pterodactyl\Models\Server; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Repositories\Wings\DaemonServerRepository; +use Pterodactyl\Services\Servers\ServerConfigurationStructureService; use Pterodactyl\Services\Servers\SuspensionService; use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Services\Servers\ServerDeletionService; @@ -54,6 +59,11 @@ class ServersController extends Controller */ protected $config; + /** + * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository + */ + private $daemonServerRepository; + /** * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface */ @@ -104,6 +114,11 @@ class ServersController extends Controller */ protected $repository; + /** + * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService + */ + private $serverConfigurationStructureService; + /** * @var \Pterodactyl\Services\Servers\StartupModificationService */ @@ -121,6 +136,7 @@ class ServersController extends Controller * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService * @param \Illuminate\Contracts\Config\Repository $config + * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository * @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService * @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository @@ -131,6 +147,7 @@ class ServersController extends Controller * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Repositories\Eloquent\MountRepository $mountRepository * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository + * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $serverConfigurationStructureService * @param \Pterodactyl\Services\Servers\StartupModificationService $startupModificationService * @param \Pterodactyl\Services\Servers\SuspensionService $suspensionService */ @@ -139,6 +156,7 @@ class ServersController extends Controller AllocationRepositoryInterface $allocationRepository, BuildModificationService $buildModificationService, ConfigRepository $config, + DaemonServerRepository $daemonServerRepository, DatabaseManagementService $databaseManagementService, DatabasePasswordService $databasePasswordService, DatabaseRepositoryInterface $databaseRepository, @@ -149,6 +167,7 @@ class ServersController extends Controller ServerRepositoryInterface $repository, MountRepository $mountRepository, NestRepositoryInterface $nestRepository, + ServerConfigurationStructureService $serverConfigurationStructureService, StartupModificationService $startupModificationService, SuspensionService $suspensionService ) { @@ -156,6 +175,7 @@ class ServersController extends Controller $this->allocationRepository = $allocationRepository; $this->buildModificationService = $buildModificationService; $this->config = $config; + $this->daemonServerRepository = $daemonServerRepository; $this->databaseHostRepository = $databaseHostRepository; $this->databaseManagementService = $databaseManagementService; $this->databasePasswordService = $databasePasswordService; @@ -166,6 +186,7 @@ class ServersController extends Controller $this->reinstallService = $reinstallService; $this->repository = $repository; $this->mountRepository = $mountRepository; + $this->serverConfigurationStructureService = $serverConfigurationStructureService; $this->startupModificationService = $startupModificationService; $this->suspensionService = $suspensionService; } @@ -390,6 +411,16 @@ class ServersController extends Controller { $server->mounts()->attach($mount_id); + $data = $this->serverConfigurationStructureService->handle($server); + + try { + $this->daemonServerRepository + ->setServer($server) + ->update(Arr::only($data, ['mounts'])); + } catch (RequestException $exception) { + throw new DaemonConnectionException($exception); + } + $this->alert->success('Mount was added successfully.')->flash(); return redirect()->route('admin.servers.view.mounts', $server->id); @@ -401,11 +432,24 @@ class ServersController extends Controller * @param Server $server * @param int $mount_id * @return \Illuminate\Http\RedirectResponse + * + * @throws DaemonConnectionException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function deleteMount(Server $server, int $mount_id) { $server->mounts()->detach($mount_id); + $data = $this->serverConfigurationStructureService->handle($server); + + try { + $this->daemonServerRepository + ->setServer($server) + ->update(Arr::only($data, ['mounts'])); + } catch (RequestException $exception) { + throw new DaemonConnectionException($exception); + } + $this->alert->success('Mount was removed successfully.')->flash(); return redirect()->route('admin.servers.view.mounts', $server->id); diff --git a/app/Models/Mount.php b/app/Models/Mount.php index ed608c56d..ac0b5da9a 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -36,7 +36,7 @@ class Mount extends Model * * @var array */ - protected $guarded = ['id', 'uuid', 'name', 'description', 'source', 'target']; + protected $guarded = ['id', 'uuid']; /** * Default values for specific fields in the database. diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index 69950b230..fea2eaac0 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -71,6 +71,17 @@ class ServerConfigurationStructureService */ protected function returnCurrentFormat(Server $server) { + $mounts = $server->mounts; + foreach ($mounts as $mount) { + unset($mount->id); + unset($mount->uuid); + unset($mount->name); + unset($mount->description); + $mount->read_only = $mount->read_only == 1; + unset($mount->user_mountable); + unset($mount->pivot); + } + return [ 'uuid' => $server->uuid, 'suspended' => (bool) $server->suspended, @@ -101,6 +112,7 @@ class ServerConfigurationStructureService ], 'mappings' => $server->getAllocationMappings(), ], + 'mounts' => $mounts, ]; }