From 03e0de28d9e45ee926bcb7886b28bb97454910e0 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 18 Mar 2017 13:09:30 -0400 Subject: [PATCH] Initial implementation of option scripts on panel side. --- .../Controllers/Admin/OptionController.php | 38 +++++++ .../Controllers/Daemon/OptionController.php | 56 +++++++++ app/Http/Routes/AdminRoutes.php | 8 ++ app/Http/Routes/DaemonRoutes.php | 5 + app/Models/ServiceOption.php | 1 + app/Repositories/OptionRepository.php | 31 +++++ ...03_17_223714_AddInstallAndUpgradePaths.php | 36 ++++++ .../admin/services/options/scripts.blade.php | 107 ++++++++++++++++++ .../services/options/variables.blade.php | 1 + .../admin/services/options/view.blade.php | 1 + 10 files changed, 284 insertions(+) create mode 100644 app/Http/Controllers/Daemon/OptionController.php create mode 100644 database/migrations/2017_03_17_223714_AddInstallAndUpgradePaths.php create mode 100644 resources/themes/pterodactyl/admin/services/options/scripts.blade.php diff --git a/app/Http/Controllers/Admin/OptionController.php b/app/Http/Controllers/Admin/OptionController.php index 68ddfaf9e..9300fcedb 100644 --- a/app/Http/Controllers/Admin/OptionController.php +++ b/app/Http/Controllers/Admin/OptionController.php @@ -137,6 +137,18 @@ class OptionController extends Controller return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')->findOrFail($id)]); } + /** + * Display script management page for an option. + * + * @param Request $request + * @param int $id + * @return \Illuminate\View\View + */ + public function viewScripts(Request $request, $id) + { + return view('admin.services.options.scripts', ['option' => ServiceOption::findOrFail($id)]); + } + /** * Handles POST when editing a configration for a service option. * @@ -207,4 +219,30 @@ class OptionController extends Controller return redirect()->route('admin.services.option.variables', $option); } + + /** + * Handles POST when updating scripts for a service option. + * + * @param Request $request + * @param int $id + * @return \Illuminate\Response\RedirectResponse + */ + public function updateScripts(Request $request, $id) + { + $repo = new OptionRepository; + + try { + $repo->scripts($id, $request->only([ + 'script_install', 'script_upgrade', + ])); + Alert::success('Successfully updated option scripts to be run when servers are installed or updated.')->flash(); + } catch (DisplayValidationException $ex) { + return redirect()->route('admin.services.option.scripts', $id)->withErrors(json_decode($ex->getMessage())); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash(); + } + + return redirect()->route('admin.services.option.scripts', $id); + } } diff --git a/app/Http/Controllers/Daemon/OptionController.php b/app/Http/Controllers/Daemon/OptionController.php new file mode 100644 index 000000000..8c61e42f2 --- /dev/null +++ b/app/Http/Controllers/Daemon/OptionController.php @@ -0,0 +1,56 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Http\Controllers\Daemon; + +use Illuminate\Http\Request; +use Pterodactyl\Models\Server; +use Pterodactyl\Models\ServiceOption; +use Pterodactyl\Http\Controllers\Controller; + +class OptionController extends Controller +{ + public function details(Request $request, $server) + { + $server = Server::with('allocation', 'option', 'variables.variable')->where('uuid', $server)->firstOrFail(); + + $environment = $server->variables->map(function ($item) { + return sprintf('%s=%s', $item->variable->env_variable, $item->variable_value); + }); + + return response()->json([ + 'scripts' => [ + 'install' => str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_install), + 'upgrade' => str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_upgrade), + 'privileged' => $server->option->script_is_privileged, + ], + 'env' => $environment->merge([ + 'STARTUP=' . $server->startup, + 'SERVER_MEMORY=' . $server->memory, + 'SERVER_IP=' . $server->allocation->ip, + 'SERVER_PORT=' . $server->allocation->port, + ])->toArray(), + ]); + } +} diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 41ab22482..a371bf02b 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -434,6 +434,14 @@ class AdminRoutes 'as' => 'admin.services.option.variables.edit', 'uses' => 'Admin\OptionController@editVariable', ]); + + $router->get('/option/{id}/scripts', [ + 'as' => 'admin.services.option.scripts', + 'uses' => 'Admin\OptionController@viewScripts', + ]); + + $router->post('/option/{id}/scripts', 'Admin\OptionController@updateScripts'); + }); // Service Packs diff --git a/app/Http/Routes/DaemonRoutes.php b/app/Http/Routes/DaemonRoutes.php index 7367fae5f..4458da895 100644 --- a/app/Http/Routes/DaemonRoutes.php +++ b/app/Http/Routes/DaemonRoutes.php @@ -49,6 +49,11 @@ class DaemonRoutes 'as' => 'daemon.pack.hash', 'uses' => 'Daemon\PackController@hash', ]); + + $router->get('details/option/{server}', [ + 'as' => 'daemon.pack.hash', + 'uses' => 'Daemon\OptionController@details', + ]); }); } } diff --git a/app/Models/ServiceOption.php b/app/Models/ServiceOption.php index a0f76df23..fdafed020 100644 --- a/app/Models/ServiceOption.php +++ b/app/Models/ServiceOption.php @@ -49,6 +49,7 @@ class ServiceOption extends Model */ protected $casts = [ 'service_id' => 'integer', + 'script_is_privileged' => 'boolean', ]; /** diff --git a/app/Repositories/OptionRepository.php b/app/Repositories/OptionRepository.php index d0535b575..8169c9d60 100644 --- a/app/Repositories/OptionRepository.php +++ b/app/Repositories/OptionRepository.php @@ -154,4 +154,35 @@ class OptionRepository return $option; } + + /** + * Updates a service option's scripts in the database. + * + * @param int $id + * @param array $data + * @return \Pterodactyl\Models\ServiceOption + * + * @throws \Pterodactyl\Exceptions\DisplayValidationException + */ + public function scripts($id, array $data) + { + $option = ServiceOption::findOrFail($id); + + $data['script_install'] = empty($data['script_install']) ? null : $data['script_install']; + $data['script_upgrade'] = empty($data['script_upgrade']) ? null : $data['script_upgrade']; + + $validator = Validator::make($data, [ + 'script_install' => 'sometimes|nullable|string', + 'script_upgrade' => 'sometimes|nullable|string', + 'script_is_privileged' => 'sometimes|required|boolean', + ]); + + if ($validator->fails()) { + throw new DisplayValidationException(json_encode($validator->errors())); + } + + $option->fill($data)->save(); + + return $option; + } } diff --git a/database/migrations/2017_03_17_223714_AddInstallAndUpgradePaths.php b/database/migrations/2017_03_17_223714_AddInstallAndUpgradePaths.php new file mode 100644 index 000000000..3081c3b9d --- /dev/null +++ b/database/migrations/2017_03_17_223714_AddInstallAndUpgradePaths.php @@ -0,0 +1,36 @@ +text('script_upgrade')->after('startup')->nullable(); + $table->text('script_install')->after('startup')->nullable(); + $table->boolean('script_is_privileged')->default(false)->after('startup'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('service_options', function (Blueprint $table) { + $table->dropColumn('script_upgrade'); + $table->dropColumn('script_install'); + $table->dropColumn('script_is_privileged'); + }); + } +} diff --git a/resources/themes/pterodactyl/admin/services/options/scripts.blade.php b/resources/themes/pterodactyl/admin/services/options/scripts.blade.php new file mode 100644 index 000000000..b2cee5c5c --- /dev/null +++ b/resources/themes/pterodactyl/admin/services/options/scripts.blade.php @@ -0,0 +1,107 @@ +{{-- Copyright (c) 2015 - 2017 Dane Everitt --}} + +{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}} +{{-- of this software and associated documentation files (the "Software"), to deal --}} +{{-- in the Software without restriction, including without limitation the rights --}} +{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}} +{{-- copies of the Software, and to permit persons to whom the Software is --}} +{{-- furnished to do so, subject to the following conditions: --}} + +{{-- The above copyright notice and this permission notice shall be included in all --}} +{{-- copies or substantial portions of the Software. --}} + +{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}} +{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}} +{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}} +{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}} +{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}} +{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}} +{{-- SOFTWARE. --}} +@extends('layouts.admin') + +@section('title') + Services → Option: {{ $option->name }} → Scripts +@endsection + +@section('content-header') +

{{ $option->name }}Manage install and upgrade scripts for this service option.

+ +@endsection + +@section('content') +
+
+ +
+
+
+
+
+
+
+

Install Script

+
+
+
{{ $option->script_install }}
+
+
+
+
+
+
+

Upgrade Script

+
+
+
{{ $option->script_upgrade }}
+
+ +
+
+
+
+@endsection + +@section('footer-scripts') + @parent + {!! Theme::js('js/vendor/ace/ace.js') !!} + {!! Theme::js('js/vendor/ace/ext-modelist.js') !!} + +@endsection diff --git a/resources/themes/pterodactyl/admin/services/options/variables.blade.php b/resources/themes/pterodactyl/admin/services/options/variables.blade.php index 830bec458..dcc8882ca 100644 --- a/resources/themes/pterodactyl/admin/services/options/variables.blade.php +++ b/resources/themes/pterodactyl/admin/services/options/variables.blade.php @@ -42,6 +42,7 @@
  • Configuration
  • Variables
  • New Variable
  • +
  • Scripts
  • diff --git a/resources/themes/pterodactyl/admin/services/options/view.blade.php b/resources/themes/pterodactyl/admin/services/options/view.blade.php index 6450fdef3..b61f79f9d 100644 --- a/resources/themes/pterodactyl/admin/services/options/view.blade.php +++ b/resources/themes/pterodactyl/admin/services/options/view.blade.php @@ -40,6 +40,7 @@