Implement server startup stuff

This commit is contained in:
Dane Everitt 2016-01-22 19:40:48 -05:00
parent 401d72a884
commit 4719b20a27
3 changed files with 129 additions and 26 deletions

View file

@ -29,11 +29,7 @@ use Uuid;
use Alert;
use Log;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Download;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Repositories\Daemon\FileRepository;
@ -57,10 +53,10 @@ class ServerController extends Controller
public function getJavascript(Request $request, $uuid, $file)
{
$server = Server::getByUUID($uuid);
$server = Models\Server::getByUUID($uuid);
return response()->view('server.js.' . $server->a_serviceFile . '.' . basename($file, '.js'), [
'server' => $server,
'node' => Node::find($server->node)
'node' => Models\Node::find($server->node)
])->header('Content-Type', 'application/javascript');
}
@ -72,11 +68,11 @@ class ServerController extends Controller
*/
public function getIndex(Request $request)
{
$server = Server::getByUUID($request->route()->server);
$server = Models\Server::getByUUID($request->route()->server);
return view('server.index', [
'server' => $server,
'allocations' => Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
'node' => Node::find($server->node)
'allocations' => Models\Allocation::where('assigned_to', $server->id)->orderBy('ip', 'asc')->orderBy('port', 'asc')->get(),
'node' => Models\Node::find($server->node)
]);
}
@ -89,12 +85,12 @@ class ServerController extends Controller
public function getFiles(Request $request)
{
$server = Server::getByUUID($request->route()->server);
$server = Models\Server::getByUUID($request->route()->server);
$this->authorize('list-files', $server);
return view('server.files.index', [
'server' => $server,
'node' => Node::find($server->node)
'node' => Models\Node::find($server->node)
]);
}
@ -107,12 +103,12 @@ class ServerController extends Controller
public function getAddFile(Request $request)
{
$server = Server::getByUUID($request->route()->server);
$server = Models\Server::getByUUID($request->route()->server);
$this->authorize('add-files', $server);
return view('server.files.add', [
'server' => $server,
'node' => Node::find($server->node),
'node' => Models\Node::find($server->node),
'directory' => (in_array($request->get('dir'), [null, '/', ''])) ? '' : trim($request->get('dir'), '/') . '/'
]);
}
@ -128,7 +124,7 @@ class ServerController extends Controller
public function getEditFile(Request $request, $uuid, $file)
{
$server = Server::getByUUID($uuid);
$server = Models\Server::getByUUID($uuid);
$this->authorize('edit-files', $server);
$fileInfo = (object) pathinfo($file);
@ -152,7 +148,7 @@ class ServerController extends Controller
return view('server.files.edit', [
'server' => $server,
'node' => Node::find($server->node),
'node' => Models\Node::find($server->node),
'file' => $file,
'contents' => $fileContent->content,
'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/',
@ -172,8 +168,8 @@ class ServerController extends Controller
public function getDownloadFile(Request $request, $uuid, $file)
{
$server = Server::getByUUID($uuid);
$node = Node::find($server->node);
$server = Models\Server::getByUUID($uuid);
$node = Models\Node::find($server->node);
$this->authorize('download-files', $server);
@ -197,16 +193,38 @@ class ServerController extends Controller
*/
public function getSettings(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
$server = Models\Server::getByUUID($uuid);
$variables = Models\ServiceVariables::select('service_variables.*', 'server_variables.variable_value as a_serverValue')
->join('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
->where('service_variables.option_id', $server->option)
->where('server_variables.server_id', $server->id)
->get();
$service = Models\Service::findOrFail($server->service);
$serverVariables = [
'{{SERVER_MEMORY}}' => $server->memory,
'{{SERVER_IP}}' => $server->ip,
'{{SERVER_PORT}}' => $server->port,
];
$processed = str_replace(array_keys($serverVariables), array_values($serverVariables), $server->startup);
foreach($variables as &$variable) {
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '**';
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
}
return view('server.settings', [
'server' => $server,
'node' => Node::find($server->node)
'node' => Models\Node::find($server->node),
'variables' => $variables,
'service' => $service,
'processedStartup' => $processed,
]);
}
public function postSettingsSFTP(Request $request, $uuid)
{
$server = Server::getByUUID($uuid);
$server = Models\Server::getByUUID($uuid);
$this->authorize('reset-sftp', $server);
try {
@ -224,4 +242,28 @@ class ServerController extends Controller
return redirect()->route('server.settings', $uuid);
}
public function postSettingsStartup(Request $request, $uuid)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('edit-startup', $server);
try {
$repo = new ServerRepository;
$repo->updateStartup($server->id, $request->except([
'_token'
]));
Alert::success('Server startup variables were successfully updated.')->flash();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch(\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
} finally {
return redirect()->route('server.settings', [
'uuid' => $uuid,
'tab' => 'tab_startup'
])->withInput();
}
}
}

View file

@ -53,6 +53,11 @@ class ServerRoutes {
'uses' => 'Server\ServerController@postSettingsSFTP'
]);
$router->post('/settings/startup', [
'as' => 'server.settings.startup',
'uses' => 'Server\ServerController@postSettingsStartup'
]);
// File Manager Routes
$router->get('/files', [
'as' => 'files.index',

View file

@ -78,12 +78,56 @@
@endcan
@can('view-startup', $server)
<div class="tab-pane" id="tab_startup">
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body">
Startup
<form action="{{ route('server.settings.startup', $server->uuidShort) }}" method="POST">
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body">
<div class="row">
<div class="form-group col-md-12">
<label class="control-label">Startup Command:</label>
<div class="input-group">
<span class="input-group-addon">{{ $service->executable }}</span>
<input type="text" class="form-control" readonly="readonly" value="{{ $processedStartup }}" />
</div>
</div>
</div>
</div>
@can('edit-startup', $server)
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<div class="row">
@foreach($variables as $item)
<div class="form-group col-md-6">
<label class="control-label">
@if($item->required === 1)<span class="label label-primary">Required</span> @endif
{{ $item->name }}
</label>
<div>
<input type="text"
@if($item->user_editable === 1)
name="{{ $item->env_variable }}"
@else
readonly="readonly"
@endif
class="form-control" value="{{ old($item->env_variable, $item->a_serverValue) }}" data-action="matchRegex" data-regex="{{ $item->regex }}" />
</div>
<p class="text-muted"><small>{{ $item->description }}<br />Regex: <code>{{ $item->regex }}</code><br />Access as: <code>&#123;&#123;{{$item->env_variable}}&#125;&#125;</code></small></p>
</div>
@endforeach
</div>
</div>
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<div class="row">
<div class="col-md-12">
{!! csrf_field() !!}
<input type="submit" class="btn btn-primary btn-sm" value="Update Startup Arguments" />
</div>
</div>
</div>
@endcan
</div>
</div>
</form>
</div>
@endcan
</div>
@ -91,6 +135,18 @@
<script>
$(document).ready(function () {
$('.server-settings').addClass('active');
$('[data-action="matchRegex"]').keyup(function (event) {
if (!$(this).data('regex')) return;
var input = $(this).val();
console.log(escapeRegExp($(this).data('regex')));
var regex = new RegExp(escapeRegExp($(this).data('regex')));
console.log(regex);
if (!regex.test(input)) {
$(this).parent().parent().removeClass('has-success').addClass('has-error');
} else {
$(this).parent().parent().removeClass('has-error').addClass('has-success');
}
});
});
</script>
@endsection