Skip to content

Commit 03e0de2

Browse files
committed
Initial implementation of option scripts on panel side.
1 parent 5d990dc commit 03e0de2

File tree

10 files changed

+284
-0
lines changed

10 files changed

+284
-0
lines changed

app/Http/Controllers/Admin/OptionController.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ public function viewVariables(Request $request, $id)
137137
return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')->findOrFail($id)]);
138138
}
139139

140+
/**
141+
* Display script management page for an option.
142+
*
143+
* @param Request $request
144+
* @param int $id
145+
* @return \Illuminate\View\View
146+
*/
147+
public function viewScripts(Request $request, $id)
148+
{
149+
return view('admin.services.options.scripts', ['option' => ServiceOption::findOrFail($id)]);
150+
}
151+
140152
/**
141153
* Handles POST when editing a configration for a service option.
142154
*
@@ -207,4 +219,30 @@ public function editVariable(Request $request, $option, $variable)
207219

208220
return redirect()->route('admin.services.option.variables', $option);
209221
}
222+
223+
/**
224+
* Handles POST when updating scripts for a service option.
225+
*
226+
* @param Request $request
227+
* @param int $id
228+
* @return \Illuminate\Response\RedirectResponse
229+
*/
230+
public function updateScripts(Request $request, $id)
231+
{
232+
$repo = new OptionRepository;
233+
234+
try {
235+
$repo->scripts($id, $request->only([
236+
'script_install', 'script_upgrade',
237+
]));
238+
Alert::success('Successfully updated option scripts to be run when servers are installed or updated.')->flash();
239+
} catch (DisplayValidationException $ex) {
240+
return redirect()->route('admin.services.option.scripts', $id)->withErrors(json_decode($ex->getMessage()));
241+
} catch (\Exception $ex) {
242+
Log::error($ex);
243+
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
244+
}
245+
246+
return redirect()->route('admin.services.option.scripts', $id);
247+
}
210248
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Http\Controllers\Daemon;
26+
27+
use Illuminate\Http\Request;
28+
use Pterodactyl\Models\Server;
29+
use Pterodactyl\Models\ServiceOption;
30+
use Pterodactyl\Http\Controllers\Controller;
31+
32+
class OptionController extends Controller
33+
{
34+
public function details(Request $request, $server)
35+
{
36+
$server = Server::with('allocation', 'option', 'variables.variable')->where('uuid', $server)->firstOrFail();
37+
38+
$environment = $server->variables->map(function ($item) {
39+
return sprintf('%s=%s', $item->variable->env_variable, $item->variable_value);
40+
});
41+
42+
return response()->json([
43+
'scripts' => [
44+
'install' => str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_install),
45+
'upgrade' => str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_upgrade),
46+
'privileged' => $server->option->script_is_privileged,
47+
],
48+
'env' => $environment->merge([
49+
'STARTUP=' . $server->startup,
50+
'SERVER_MEMORY=' . $server->memory,
51+
'SERVER_IP=' . $server->allocation->ip,
52+
'SERVER_PORT=' . $server->allocation->port,
53+
])->toArray(),
54+
]);
55+
}
56+
}

app/Http/Routes/AdminRoutes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ public function map(Router $router)
434434
'as' => 'admin.services.option.variables.edit',
435435
'uses' => 'Admin\OptionController@editVariable',
436436
]);
437+
438+
$router->get('/option/{id}/scripts', [
439+
'as' => 'admin.services.option.scripts',
440+
'uses' => 'Admin\OptionController@viewScripts',
441+
]);
442+
443+
$router->post('/option/{id}/scripts', 'Admin\OptionController@updateScripts');
444+
437445
});
438446

439447
// Service Packs

app/Http/Routes/DaemonRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function map(Router $router)
4949
'as' => 'daemon.pack.hash',
5050
'uses' => 'Daemon\PackController@hash',
5151
]);
52+
53+
$router->get('details/option/{server}', [
54+
'as' => 'daemon.pack.hash',
55+
'uses' => 'Daemon\OptionController@details',
56+
]);
5257
});
5358
}
5459
}

app/Models/ServiceOption.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ServiceOption extends Model
4949
*/
5050
protected $casts = [
5151
'service_id' => 'integer',
52+
'script_is_privileged' => 'boolean',
5253
];
5354

5455
/**

app/Repositories/OptionRepository.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,35 @@ public function update($id, array $data)
154154

155155
return $option;
156156
}
157+
158+
/**
159+
* Updates a service option's scripts in the database.
160+
*
161+
* @param int $id
162+
* @param array $data
163+
* @return \Pterodactyl\Models\ServiceOption
164+
*
165+
* @throws \Pterodactyl\Exceptions\DisplayValidationException
166+
*/
167+
public function scripts($id, array $data)
168+
{
169+
$option = ServiceOption::findOrFail($id);
170+
171+
$data['script_install'] = empty($data['script_install']) ? null : $data['script_install'];
172+
$data['script_upgrade'] = empty($data['script_upgrade']) ? null : $data['script_upgrade'];
173+
174+
$validator = Validator::make($data, [
175+
'script_install' => 'sometimes|nullable|string',
176+
'script_upgrade' => 'sometimes|nullable|string',
177+
'script_is_privileged' => 'sometimes|required|boolean',
178+
]);
179+
180+
if ($validator->fails()) {
181+
throw new DisplayValidationException(json_encode($validator->errors()));
182+
}
183+
184+
$option->fill($data)->save();
185+
186+
return $option;
187+
}
157188
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddInstallAndUpgradePaths extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('service_options', function (Blueprint $table) {
17+
$table->text('script_upgrade')->after('startup')->nullable();
18+
$table->text('script_install')->after('startup')->nullable();
19+
$table->boolean('script_is_privileged')->default(false)->after('startup');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('service_options', function (Blueprint $table) {
31+
$table->dropColumn('script_upgrade');
32+
$table->dropColumn('script_install');
33+
$table->dropColumn('script_is_privileged');
34+
});
35+
}
36+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{{-- Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com> --}}
2+
3+
{{-- Permission is hereby granted, free of charge, to any person obtaining a copy --}}
4+
{{-- of this software and associated documentation files (the "Software"), to deal --}}
5+
{{-- in the Software without restriction, including without limitation the rights --}}
6+
{{-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --}}
7+
{{-- copies of the Software, and to permit persons to whom the Software is --}}
8+
{{-- furnished to do so, subject to the following conditions: --}}
9+
10+
{{-- The above copyright notice and this permission notice shall be included in all --}}
11+
{{-- copies or substantial portions of the Software. --}}
12+
13+
{{-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --}}
14+
{{-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --}}
15+
{{-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --}}
16+
{{-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --}}
17+
{{-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --}}
18+
{{-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --}}
19+
{{-- SOFTWARE. --}}
20+
@extends('layouts.admin')
21+
22+
@section('title')
23+
Services &rarr; Option: {{ $option->name }} &rarr; Scripts
24+
@endsection
25+
26+
@section('content-header')
27+
<h1>{{ $option->name }}<small>Manage install and upgrade scripts for this service option.</small></h1>
28+
<ol class="breadcrumb">
29+
<li><a href="{{ route('admin.index') }}">Admin</a></li>
30+
<li><a href="{{ route('admin.services') }}">Services</a></li>
31+
<li><a href="{{ route('admin.services.view', $option->service->id) }}">{{ $option->service->name }}</a></li>
32+
<li class="active">{{ $option->name }}</li>
33+
</ol>
34+
@endsection
35+
36+
@section('content')
37+
<div class="row">
38+
<div class="col-xs-12">
39+
<div class="nav-tabs-custom nav-tabs-floating">
40+
<ul class="nav nav-tabs">
41+
<li><a href="{{ route('admin.services.option.view', $option->id) }}">Configuration</a></li>
42+
<li><a href="{{ route('admin.services.option.variables', $option->id) }}">Variables</a></li>
43+
<li class="active"><a href="{{ route('admin.services.option.scripts', $option->id) }}">Scripts</a></li>
44+
</ul>
45+
</div>
46+
</div>
47+
</div>
48+
<form action="{{ route('admin.services.option.scripts', $option->id) }}" method="POST">
49+
<div class="row">
50+
<div class="col-xs-12">
51+
<div class="box">
52+
<div class="box-header with-border">
53+
<h3 class="box-title">Install Script</h3>
54+
</div>
55+
<div class="box-body no-padding">
56+
<div id="editor_install"style="height:300px">{{ $option->script_install }}</div>
57+
</div>
58+
</div>
59+
</div>
60+
<div class="col-xs-12">
61+
<div class="box">
62+
<div class="box-header with-border">
63+
<h3 class="box-title">Upgrade Script</h3>
64+
</div>
65+
<div class="box-body no-padding">
66+
<div id="editor_upgrade"style="height:300px">{{ $option->script_upgrade }}</div>
67+
</div>
68+
<div class="box-footer">
69+
{!! csrf_field() !!}
70+
<textarea name="script_install" class="hidden"></textarea>
71+
<textarea name="script_upgrade" class="hidden"></textarea>
72+
<button type="submit" class="btn btn-primary btn-sm pull-right">Save Scripts</button>
73+
</div>
74+
</div>
75+
</div>
76+
</div>
77+
</form>
78+
@endsection
79+
80+
@section('footer-scripts')
81+
@parent
82+
{!! Theme::js('js/vendor/ace/ace.js') !!}
83+
{!! Theme::js('js/vendor/ace/ext-modelist.js') !!}
84+
<script>
85+
$(document).ready(function () {
86+
const InstallEditor = ace.edit('editor_install');
87+
const UpgradeEditor = ace.edit('editor_upgrade');
88+
89+
const Modelist = ace.require('ace/ext/modelist')
90+
91+
InstallEditor.setTheme('ace/theme/chrome');
92+
InstallEditor.getSession().setMode('ace/mode/sh');
93+
InstallEditor.getSession().setUseWrapMode(true);
94+
InstallEditor.setShowPrintMargin(false);
95+
96+
UpgradeEditor.setTheme('ace/theme/chrome');
97+
UpgradeEditor.getSession().setMode('ace/mode/sh');
98+
UpgradeEditor.getSession().setUseWrapMode(true);
99+
UpgradeEditor.setShowPrintMargin(false);
100+
101+
$('form').on('submit', function (e) {
102+
$('textarea[name="script_install"]').val(InstallEditor.getValue());
103+
$('textarea[name="script_upgrade"]').val(UpgradeEditor.getValue());
104+
});
105+
});
106+
</script>
107+
@endsection

resources/themes/pterodactyl/admin/services/options/variables.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<li><a href="{{ route('admin.services.option.view', $option->id) }}">Configuration</a></li>
4343
<li class="active"><a href="{{ route('admin.services.option.variables', $option->id) }}">Variables</a></li>
4444
<li class="tab-success"><a href="#modal" data-toggle="modal" data-target="#newVariableModal">New Variable</a></li>
45+
<li><a href="{{ route('admin.services.option.scripts', $option->id) }}">Scripts</a></li>
4546
</ul>
4647
</div>
4748
</div>

resources/themes/pterodactyl/admin/services/options/view.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<ul class="nav nav-tabs">
4141
<li class="active"><a href="{{ route('admin.services.option.view', $option->id) }}">Configuration</a></li>
4242
<li><a href="{{ route('admin.services.option.variables', $option->id) }}">Variables</a></li>
43+
<li><a href="{{ route('admin.services.option.scripts', $option->id) }}">Scripts</a></li>
4344
</ul>
4445
</div>
4546
</div>

0 commit comments

Comments
 (0)