Skip to content

Commit 349b36d

Browse files
committed
Added startup management, cleaned up code.
Refactored entire startup repository code block to be more efficient and cleaner. Also includes modifications to front-end to make it match backend name and design.
1 parent d51ae5e commit 349b36d

File tree

8 files changed

+249
-143
lines changed

8 files changed

+249
-143
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -475,27 +475,34 @@ public function continueDeletion(Request $request, $id, $method)
475475
return redirect()->route('admin.servers.view.delete', $id);
476476
}
477477

478-
// //
479-
// public function postUpdateServerStartup(Request $request, $id)
480-
// {
481-
// try {
482-
// $server = new ServerRepository;
483-
// $server->updateStartup($id, $request->except([
484-
// '_token',
485-
// ]), true);
486-
// Alert::success('Server startup variables were successfully updated.')->flash();
487-
// } catch (\Pterodactyl\Exceptions\DisplayException $e) {
488-
// Alert::danger($e->getMessage())->flash();
489-
// } catch (\Exception $e) {
490-
// Log::error($e);
491-
// Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
492-
// } finally {
493-
// return redirect()->route('admin.servers.view', [
494-
// 'id' => $id,
495-
// 'tab' => 'tab_startup',
496-
// ])->withInput();
497-
// }
498-
// }
478+
/**
479+
* Update the startup command as well as variables.
480+
*
481+
* @param Request $request
482+
* @param int $id
483+
* @return \Illuminate\Response\RedirectResponse
484+
*/
485+
public function saveStartup(Request $request, $id)
486+
{
487+
$repo = new ServerRepository;
488+
489+
try {
490+
$repo->updateStartup($id, $request->except('_token'), true);
491+
492+
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
493+
} catch(DisplayException $ex) {
494+
Alert::danger($ex->getMessage())->flash();
495+
} catch (TransferException $ex) {
496+
Log::warning($ex);
497+
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.')->flash();
498+
} catch (\Exception $ex) {
499+
Log::error($ex);
500+
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. This error has been logged.')->flash();
501+
}
502+
503+
return redirect()->route('admin.servers.view.startup', $id);
504+
}
505+
499506
//
500507
// public function postDatabase(Request $request, $id)
501508
// {

app/Http/Controllers/Server/ServerController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ public function postSettingsStartup(Request $request, $uuid)
309309

310310
try {
311311
$repo = new ServerRepository;
312-
$repo->updateStartup($server->id, $request->except([
313-
'_token',
314-
]));
312+
$repo->updateStartup($server->id, $request->except('_token'));
315313
Alert::success('Server startup variables were successfully updated.')->flash();
316314
} catch (DisplayException $ex) {
317315
Alert::danger($ex->getMessage())->flash();

app/Http/Routes/AdminRoutes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public function map(Router $router)
174174
'uses' => 'Admin\ServersController@viewStartup',
175175
]);
176176

177+
$router->post('/view/{id}/startup', [
178+
'uses' => 'Admin\ServersController@saveStartup',
179+
]);
180+
177181
$router->get('/view/{id}/database', [
178182
'as' => 'admin.servers.view.database',
179183
'uses' => 'Admin\ServersController@viewDatabase',

app/Repositories/ServerRepository.php

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -617,109 +617,75 @@ public function updateStartup($id, array $data, $admin = false)
617617
{
618618
$server = Models\Server::with('variables', 'option.variables')->findOrFail($id);
619619

620-
DB::beginTransaction();
621-
622-
try {
623-
// Check the startup
620+
DB::transaction(function () use ($admin, $data, $server) {
624621
if (isset($data['startup']) && $admin) {
625622
$server->startup = $data['startup'];
626623
$server->save();
627624
}
628625

629-
// Check those Variables
630-
$server->option->variables->transform(function ($item, $key) use ($server) {
631-
$displayValue = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
632-
$item->server_value = (! is_null($displayValue)) ? $displayValue : $item->default_value;
633-
634-
return $item;
635-
});
636-
637-
$variableList = [];
638626
if ($server->option->variables) {
639-
foreach ($server->option->variables as &$variable) {
640-
// Move on if the new data wasn't even sent
641-
if (! isset($data[$variable->env_variable])) {
642-
$variableList[] = [
643-
'id' => $variable->id,
644-
'env' => $variable->env_variable,
645-
'val' => $variable->server_value,
646-
];
647-
continue;
627+
foreach($server->option->variables as &$variable) {
628+
$set = isset($data['env_' . $variable->id]);
629+
630+
// Variable is required but was not passed into the function.
631+
if ($variable->required && ! $set) {
632+
throw new DisplayException('A required variable (' . $variable->env_variable . ') was not passed in the request.');
648633
}
649634

650-
// Update Empty but skip validation
651-
if (empty($data[$variable->env_variable])) {
652-
$variableList[] = [
653-
'id' => $variable->id,
654-
'env' => $variable->env_variable,
655-
'val' => null,
656-
];
635+
// If user is not an admin and are trying to edit a non-editable field
636+
// or an invisible field just silently skip the variable.
637+
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
657638
continue;
658639
}
659640

660-
// Is the variable required?
661-
// @TODO: is this even logical to perform this check?
662-
if (isset($data[$variable->env_variable]) && empty($data[$variable->env_variable])) {
663-
if ($variable->required) {
664-
throw new DisplayException('A required service option variable field (' . $variable->env_variable . ') was included in this request but was left blank.');
641+
// Confirm value is valid when compared aganist regex.
642+
// @TODO: switch to Laravel validation rules.
643+
if ($set && ! is_null($variable->regex)) {
644+
if (! preg_match($variable->regex, $data['env_' . $variable->id])) {
645+
throw new DisplayException('The value passed for a variable (' . $variable->env_variable . ') could not be matched aganist the regex for that field (' . $variable->regex . ').');
665646
}
666647
}
667648

668-
// Variable hidden and/or not user editable
669-
if ((! $variable->user_viewable || ! $variable->user_editable) && ! $admin) {
670-
throw new DisplayException('A service option variable field (' . $variable->env_variable . ') does not exist or you do not have permission to edit it.');
671-
}
649+
$svar = Models\ServerVariable::firstOrNew([
650+
'server_id' => $server->id,
651+
'variable_id' => $variable->id,
652+
]);
653+
654+
// Set the value; if one was not passed set it to the default value
655+
if ($set) {
656+
$svar->variable_value = $data['env_' . $variable->id];
672657

673-
// Check aganist Regex Pattern
674-
if (! is_null($variable->regex) && ! preg_match($variable->regex, $data[$variable->env_variable])) {
675-
throw new DisplayException('Failed to validate service option variable field (' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').');
658+
// Not passed, check if this record exists if so keep value, otherwise set default
659+
} else {
660+
$svar->variable_value = ($svar->exists) ? $svar->variable_value : $variable->default_value;
676661
}
677662

678-
$variableList[] = [
679-
'id' => $variable->id,
680-
'env' => $variable->env_variable,
681-
'val' => $data[$variable->env_variable],
682-
];
663+
$svar->save();
683664
}
684665
}
685666

686-
// Add Variables
687-
$environmentVariables = [
688-
'STARTUP' => $server->startup,
689-
];
690-
foreach ($variableList as $item) {
691-
$environmentVariables[$item['env']] = $item['val'];
667+
// Reload Variables
668+
$server->load('variables');
669+
$environment = $server->option->variables->map(function ($item, $key) use ($server) {
670+
$display = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
692671

693-
// Update model or make a new record if it doesn't exist.
694-
$model = Models\ServerVariable::firstOrNew([
695-
'variable_id' => $item['id'],
696-
'server_id' => $server->id,
697-
]);
698-
$model->variable_value = $item['val'];
699-
$model->save();
700-
}
672+
return [
673+
'variable' => $item->env_variable,
674+
'value' => (! is_null($display)) ? $display : $item->default_value,
675+
];
676+
});
701677

702678
$server->node->guzzleClient([
703679
'X-Access-Server' => $server->uuid,
704680
'X-Access-Token' => $server->node->daemonSecret,
705681
])->request('PATCH', '/server', [
706682
'json' => [
707683
'build' => [
708-
'env|overwrite' => $environmentVariables,
684+
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup]),
709685
],
710686
],
711687
]);
712-
713-
DB::commit();
714-
715-
return true;
716-
} catch (TransferException $ex) {
717-
DB::rollBack();
718-
throw new DisplayException('An error occured while attempting to update the server configuration.', $ex);
719-
} catch (\Exception $ex) {
720-
DB::rollBack();
721-
throw $ex;
722-
}
688+
});
723689
}
724690

725691
public function queueDeletion($id, $force = false)

resources/lang/en/server.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@
233233
'command' => 'Startup Command',
234234
'edit_params' => 'Edit Parameters',
235235
'update' => 'Update Startup Parameters',
236+
'startup_var' => 'Startup Command Variable',
237+
'startup_regex' => 'Verification Regex',
236238
],
237239
'sftp' => [
238240
'header' => 'SFTP Configuration',

resources/lang/en/strings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@
6363
'2fa' => '2FA',
6464
'logout' => 'Logout',
6565
'admin_cp' => 'Admin Control Panel',
66+
'optional' => 'Optional',
67+
'read_only' => 'Read Only',
6668
];
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
Server — {{ $server->name }}: Startup
24+
@endsection
25+
26+
@section('content-header')
27+
<h1>{{ $server->name }}<small>Control startup command as well as variables.</small></h1>
28+
<ol class="breadcrumb">
29+
<li><a href="{{ route('admin.index') }}">Admin</a></li>
30+
<li><a href="{{ route('admin.servers') }}">Servers</a></li>
31+
<li><a href="{{ route('admin.servers.view', $server->id) }}">{{ $server->name }}</a></li>
32+
<li class="active">Startup</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.servers.view', $server->id) }}">About</a></li>
42+
@if(! $server->trashed() && $server->installed === 1)
43+
<li><a href="{{ route('admin.servers.view.details', $server->id) }}">Details</a></li>
44+
<li><a href="{{ route('admin.servers.view.build', $server->id) }}">Build Configuration</a></li>
45+
<li class="active"><a href="{{ route('admin.servers.view.startup', $server->id) }}">Startup</a></li>
46+
<li><a href="{{ route('admin.servers.view.database', $server->id) }}">Database</a></li>
47+
@endif
48+
@if(! $server->trashed())
49+
<li><a href="{{ route('admin.servers.view.manage', $server->id) }}">Manage</a></li>
50+
@endif
51+
<li class="tab-danger"><a href="{{ route('admin.servers.view.delete', $server->id) }}">Delete</a></li>
52+
</ul>
53+
</div>
54+
</div>
55+
</div>
56+
<form action="{{ route('admin.servers.view.startup', $server->id) }}" method="POST">
57+
<div class="row">
58+
<div class="col-xs-12">
59+
<div class="box box-primary">
60+
<div class="box-header with-border">
61+
<h3 class="box-title">Startup Command Modification</h3>
62+
</div>
63+
<div class="box-body">
64+
<label for="pStartup" class="form-label">Startup Command</label>
65+
<div class="input-group">
66+
<span class="input-group-addon bg-gray">{{ $server->option->display_executable }}</span>
67+
<input id="pStartup" name="startup" class="form-control" type="text" value="{{ old('startup', $server->startup) }}" />
68+
</div>
69+
<p class="small text-muted">Edit your server's startup command here. The following variables are available by default: <code>@{{SERVER_MEMORY}}</code>, <code>@{{SERVER_IP}}</code>, and <code>@{{SERVER_PORT}}</code>.</p>
70+
</div>
71+
<div class="box-footer">
72+
{!! csrf_field() !!}
73+
<button type="submit" class="btn btn-primary btn-sm pull-right">Save Modifications</button>
74+
</div>
75+
</div>
76+
</div>
77+
@foreach($server->option->variables as $variable)
78+
<div class="col-xs-12 col-md-4 col-sm-6">
79+
<div class="box">
80+
<div class="box-header with-border">
81+
<h3 class="box-title">{{ $variable->name }}</h3>
82+
</div>
83+
<div class="box-body">
84+
<input data-action="match-regex" data-regex="{{ $variable->regex }}" name="env_{{ $variable->id }}" class="form-control" type="text" value="{{ old('env_' . $variable->id, $variable->server_value) }}" />
85+
<p class="no-margin small text-muted">{{ $variable->description }}</p>
86+
<p class="no-margin">
87+
@if($variable->required)<span class="label label-danger">Required</span>@else<span class="label label-default">Optional</span>@endif
88+
@if($variable->user_viewable)<span class="label label-success">Visible</span>@else<span class="label label-primary">Hidden</span>@endif
89+
@if($variable->user_editable)<span class="label label-success">Editable</span>@else<span class="label label-primary">Locked</span>@endif
90+
</p>
91+
</div>
92+
<div class="box-footer">
93+
<p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>{{ $variable->env_variable }}</code></p>
94+
<p class="no-margin text-muted small"><strong>Verification Regex:</strong> <code>{{ $variable->regex }}</code></p>
95+
</div>
96+
</div>
97+
</div>
98+
@endforeach
99+
</div>
100+
</form>
101+
@endsection
102+
103+
@section('footer-scripts')
104+
@parent
105+
<script>
106+
$('input[data-action="match-regex"]').on('keyup', function (event) {
107+
if (! $(this).data('regex')) return;
108+
109+
var input = $(this).val();
110+
var regex = new RegExp($(this).data('regex').replace(/^\/|\/$/g, ''));
111+
112+
$(this).parent().parent().removeClass('has-success has-error').addClass((! regex.test(input)) ? 'has-error' : 'has-success');
113+
});
114+
</script>
115+
@endsection

0 commit comments

Comments
 (0)