Add ability to change service implementation for a server.

This commit is contained in:
Dane Everitt 2017-04-21 17:52:44 -04:00
parent e0958414d7
commit a5a1ea3165
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
5 changed files with 383 additions and 85 deletions

View file

@ -198,7 +198,24 @@ class ServersController extends Controller
return $item;
});
return view('admin.servers.view.startup', ['server' => $server]);
$services = Models\Service::with('options.packs', 'options.variables')->get();
Javascript::put([
'services' => $services->map(function ($item) {
return array_merge($item->toArray(), [
'options' => $item->options->keyBy('id')->toArray(),
]);
})->keyBy('id'),
'server_variables' => $server->variables->mapWithKeys(function ($item) {
return ['env_' . $item->variable_id => [
'value' => $item->variable_value,
]];
})->toArray(),
]);
return view('admin.servers.view.startup', [
'server' => $server,
'services' => $services,
]);
}
/**
@ -479,9 +496,13 @@ class ServersController extends Controller
$repo = new ServerRepository;
try {
$repo->updateStartup($id, $request->except('_token'), true);
if ($repo->updateStartup($id, $request->except('_token'), true)) {
Alert::success('Service configuration successfully modfied for this server, reinstalling now.')->flash();
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
return redirect()->route('admin.servers.view', $id);
} else {
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
}
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view.startup', $id)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) {

View file

@ -72,6 +72,7 @@ class Server extends Model
*/
protected $casts = [
'node_id' => 'integer',
'skip_scripts' => 'boolean',
'suspended' => 'integer',
'owner_id' => 'integer',
'memory' => 'integer',

View file

@ -103,7 +103,7 @@ class ServerRepository
'startup' => 'string',
'auto_deploy' => 'sometimes|required|accepted',
'custom_id' => 'sometimes|required|numeric|unique:servers,id',
'skip_scripting' => 'sometimes|required|boolean',
'skip_scripts' => 'sometimes|required|boolean',
]);
$validator->sometimes('node_id', 'required|numeric|min:1|exists:nodes,id', function ($input) {
@ -250,14 +250,15 @@ class ServerRepository
'node_id' => $node->id,
'name' => $data['name'],
'description' => $data['description'],
'suspended' => 0,
'skip_scripts' => isset($data['skip_scripts']),
'suspended' => false,
'owner_id' => $user->id,
'memory' => $data['memory'],
'swap' => $data['swap'],
'disk' => $data['disk'],
'io' => $data['io'],
'cpu' => $data['cpu'],
'oom_disabled' => (isset($data['oom_disabled'])) ? true : false,
'oom_disabled' => isset($data['oom_disabled']),
'allocation_id' => $allocation->id,
'service_id' => $data['service_id'],
'option_id' => $data['option_id'],
@ -327,7 +328,7 @@ class ServerRepository
'type' => $service->folder,
'option' => $option->tag,
'pack' => (isset($pack)) ? $pack->uuid : null,
'skip_scripting' => isset($data['skip_scripting']),
'skip_scripts' => $server->skip_scripts,
],
'keys' => [
(string) $server->daemonSecret => $this->daemonPermissions,
@ -621,13 +622,93 @@ class ServerRepository
}
}
/**
* Update the service configuration for a server.
*
* @param int $id
* @param array $data
* @return void
*
* @throws \GuzzleHttp\Exception\RequestException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
protected function changeService($id, array $data)
{
}
protected function processVariables(Models\Server $server, $data, $admin = false)
{
$server->load('option.variables');
if ($admin) {
$server->startup = $data['startup'];
$server->save();
}
if ($server->option->variables) {
foreach ($server->option->variables as &$variable) {
$set = isset($data['env_' . $variable->id]);
// If user is not an admin and are trying to edit a non-editable field
// or an invisible field just silently skip the variable.
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
continue;
}
// Perform Field Validation
$validator = Validator::make([
'variable_value' => ($set) ? $data['env_' . $variable->id] : null,
], [
'variable_value' => $variable->rules,
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode(
collect([
'notice' => ['There was a validation error with the `' . $variable->name . '` variable.'],
])->merge($validator->errors()->toArray())
));
}
$svar = Models\ServerVariable::firstOrNew([
'server_id' => $server->id,
'variable_id' => $variable->id,
]);
// Set the value; if one was not passed set it to the default value
if ($set) {
$svar->variable_value = $data['env_' . $variable->id];
// Not passed, check if this record exists if so keep value, otherwise set default
} else {
$svar->variable_value = ($svar->exists) ? $svar->variable_value : $variable->default_value;
}
$svar->save();
}
}
// Reload Variables
$server->load('variables');
return $server->option->variables->map(function ($item, $key) use ($server) {
$display = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
return [
'variable' => $item->env_variable,
'value' => (! is_null($display)) ? $display : $item->default_value,
];
});
}
/**
* Update the startup details for a server.
*
* @param int $id
* @param array $data
* @param bool $admin
* @return void
* @return bool
*
* @throws \GuzzleHttp\Exception\RequestException
* @throws \Pterodactyl\Exceptions\DisplayException
@ -636,78 +717,110 @@ class ServerRepository
public function updateStartup($id, array $data, $admin = false)
{
$server = Models\Server::with('variables', 'option.variables')->findOrFail($id);
$hasServiceChanges = false;
DB::transaction(function () use ($admin, $data, $server) {
if (isset($data['startup']) && $admin) {
$server->startup = $data['startup'];
$server->save();
if ($admin) {
// User is an admin, lots of things to do here.
$validator = Validator::make($data, [
'startup' => 'required|string',
'skip_scripts' => 'sometimes|required|boolean',
'service_id' => 'required|numeric|min:1|exists:services,id',
'option_id' => 'required|numeric|min:1|exists:service_options,id',
'pack_id' => 'sometimes|nullable|numeric|min:0',
]);
if ((int) $data['pack_id'] < 1) {
$data['pack_id'] = null;
}
if ($server->option->variables) {
foreach ($server->option->variables as &$variable) {
$set = isset($data['env_' . $variable->id]);
// If user is not an admin and are trying to edit a non-editable field
// or an invisible field just silently skip the variable.
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
continue;
}
// Perform Field Validation
$validator = Validator::make([
'variable_value' => ($set) ? $data['env_' . $variable->id] : null,
], [
'variable_value' => $variable->rules,
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode(
collect([
'notice' => ['There was a validation error with the `' . $variable->name . '` variable.'],
])->merge($validator->errors()->toArray())
));
}
$svar = Models\ServerVariable::firstOrNew([
'server_id' => $server->id,
'variable_id' => $variable->id,
]);
// Set the value; if one was not passed set it to the default value
if ($set) {
$svar->variable_value = $data['env_' . $variable->id];
// Not passed, check if this record exists if so keep value, otherwise set default
} else {
$svar->variable_value = ($svar->exists) ? $svar->variable_value : $variable->default_value;
}
$svar->save();
}
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
// Reload Variables
$server->load('variables');
$environment = $server->option->variables->map(function ($item, $key) use ($server) {
$display = $server->variables->where('variable_id', $item->id)->pluck('variable_value')->first();
if (
$server->service_id != $data['service_id'] ||
$server->option_id != $data['option_id'] ||
$server->pack_id != $data['pack_id']
) {
$hasServiceChanges = true;
}
}
return [
'variable' => $item->env_variable,
'value' => (! is_null($display)) ? $display : $item->default_value,
];
// If user isn't an administrator, this function is being access from the front-end
// Just try to update specific variables.
if (! $admin || ! $hasServiceChanges) {
return DB::transaction(function () use ($admin, $data, $server) {
$environment = $this->processVariables($server, $data, $admin);
$server->node->guzzleClient([
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $server->node->daemonSecret,
])->request('PATCH', '/server', [
'json' => [
'build' => [
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup])->toArray(),
],
],
]);
return false;
});
}
// Validate those Service Option Variables
// We know the service and option exists because of the validation.
// We need to verify that the option exists for the service, and then check for
// any required variable fields. (fields are labeled env_<env_variable>)
$option = Models\ServiceOption::where('id', $data['option_id'])->where('service_id', $data['service_id'])->first();
if (! $option) {
throw new DisplayException('The requested service option does not exist for the specified service.');
}
// Validate the Pack
if (! isset($data['pack_id']) || (int) $data['pack_id'] < 1) {
$data['pack_id'] = null;
} else {
$pack = Models\Pack::where('id', $data['pack_id'])->where('option_id', $data['option_id'])->first();
if (! $pack) {
throw new DisplayException('The requested service pack does not seem to exist for this combination.');
}
}
return DB::transaction(function () use ($admin, $data, $server) {
$server->installed = 0;
$server->service_id = $data['service_id'];
$server->option_id = $data['option_id'];
$server->pack_id = $data['pack_id'];
$server->skip_scripts = isset($data['skip_scripts']);
$server->save();
$server->variables->each->delete();
$server->load('service', 'pack');
// Send New Environment
$environment = $this->processVariables($server, $data, $admin);
$server->node->guzzleClient([
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $server->node->daemonSecret,
])->request('PATCH', '/server', [
])->request('POST', '/server/reinstall', [
'json' => [
'build' => [
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup]),
'env|overwrite' => $environment->pluck('value', 'variable')->merge(['STARTUP' => $server->startup])->toArray(),
],
'service' => [
'type' => $server->option->service->folder,
'option' => $server->option->tag,
'pack' => (! is_null($server->pack_id)) ? $server->pack->uuid : null,
'skip_scripts' => $server->skip_scripts,
],
],
]);
return true;
});
}
/**

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddServiceScriptTrackingToServers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('servers', function (Blueprint $table) {
$table->boolean('skip_scripts')->default(false)->after('description');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('servers', function (Blueprint $table) {
$table->dropColumn('skip_scripts');
});
}
}

View file

@ -64,48 +64,179 @@
<input id="pStartup" name="startup" class="form-control" type="text" value="{{ old('startup', $server->startup) }}" />
<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>
</div>
<div class="box-body">
<label for="pDefaultStartupCommand" class="form-label">Default Service Start Command</label>
<input id="pDefaultStartupCommand" class="form-control" type="text" readonly />
</div>
<div class="box-footer">
{!! csrf_field() !!}
<button type="submit" class="btn btn-primary btn-sm pull-right">Save Modifications</button>
</div>
</div>
</div>
@foreach($server->option->variables as $variable)
<div class="col-xs-12 col-md-4 col-sm-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ $variable->name }}</h3>
</div>
<div class="box-body">
<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) }}" />
<p class="no-margin small text-muted">{{ $variable->description }}</p>
<p class="no-margin">
@if($variable->required)<span class="label label-danger">Required</span>@else<span class="label label-default">Optional</span>@endif
@if($variable->user_viewable)<span class="label label-success">Visible</span>@else<span class="label label-primary">Hidden</span>@endif
@if($variable->user_editable)<span class="label label-success">Editable</span>@else<span class="label label-primary">Locked</span>@endif
</div>
<div class="row">
<div class="col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Service Configuration</h3>
</div>
<div class="box-body row">
<div class="col-xs-12">
<p class="small text-danger">
Changing any of the below values will result in the server processing a re-install command. The server will be stopped and will then proceede.
If you are changing the pack, exisiting data <em>may</em> be overwritten. If you would like the service scripts to not run, ensure the box is checked at the bottom.
</p>
<p class="small text-danger">
<strong>This is a destructive operation in many cases. This server will be stopped immediately in order for this action to proceede.</strong>
</p>
</div>
<div class="box-footer">
<p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>{{ $variable->env_variable }}</code></p>
<p class="no-margin text-muted small"><strong>Input Rules:</strong> <code>{{ $variable->rules }}</code></p>
<div class="form-group col-xs-12">
<label for="pServiceId">Service</label>
<select name="service_id" id="pServiceId" class="form-control">
@foreach($services as $service)
<option value="{{ $service->id }}"
@if($service->id === $server->service_id)
selected="selected"
@endif
>{{ $service->name }}</option>
@endforeach
</select>
<p class="small text-muted no-margin">Select the type of service that this server will be running.</p>
</div>
<div class="form-group col-xs-12">
<label for="pOptionId">Option</label>
<select name="option_id" id="pOptionId" class="form-control"></select>
<p class="small text-muted no-margin">Select the type of sub-service that this server will be running.</p>
</div>
<div class="form-group col-xs-12">
<label for="pPackId">Service Pack</label>
<select name="pack_id" id="pPackId" class="form-control"></select>
<p class="small text-muted no-margin">Select a service pack to be automatically installed on this server when first created.</p>
</div>
<div class="form-group col-xs-12">
<div class="checkbox checkbox-primary no-margin-bottom">
<input id="pSkipScripting" name="skip_scripting" type="checkbox" value="1" @if($server->skip_scripts) checked @endif />
<label for="pSkipScripting" class="strong">Skip Service Option Install Script</label>
</div>
<p class="small text-muted no-margin">If the selected <code>Option</code> has an install script attached to it, the script will run during install after the pack is installed. If you would like to skip this step, check this box.</p>
</div>
</div>
</div>
@endforeach
</div>
<div class="col-md-6">
<div class="row" id="appendVariablesTo">
@foreach($server->option->variables as $variable)
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ $variable->name }}</h3>
</div>
<div class="box-body">
<input data-action="match-regex" name="env_{{ $variable->id }}" class="form-control" type="text" value="{{ old('env_' . $variable->id, $variable->server_value) }}" />
<p class="no-margin small text-muted">{{ $variable->description }}</p>
<p class="no-margin">
@if($variable->required)<span class="label label-danger">Required</span>@else<span class="label label-default">Optional</span>@endif
@if($variable->user_viewable)<span class="label label-success">Visible</span>@else<span class="label label-primary">Hidden</span>@endif
@if($variable->user_editable)<span class="label label-success">Editable</span>@else<span class="label label-primary">Locked</span>@endif
</p>
</div>
<div class="box-footer">
<p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>{{ $variable->env_variable }}</code></p>
<p class="no-margin text-muted small"><strong>Input Rules:</strong> <code>{{ $variable->rules }}</code></p>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</form>
@endsection
@section('footer-scripts')
@parent
{!! Theme::js('vendor/lodash/lodash.js') !!}
<script>
$('input[data-action="match-regex"]').on('keyup', function (event) {
if (! $(this).data('regex')) return;
$(document).ready(function () {
$('#pServiceId').select2({
placeholder: 'Select a Service',
}).change();
$('#pOptionId').select2({
placeholder: 'Select a Service Option',
});
$('#pPackId').select2({
placeholder: 'Select a Service Pack',
});
var input = $(this).val();
var regex = new RegExp($(this).data('regex').replace(/^\/|\/$/g, ''));
$('input[data-action="match-regex"]').on('keyup', function (event) {
if (! $(this).data('regex')) return;
$(this).parent().parent().removeClass('has-success has-error').addClass((! regex.test(input)) ? 'has-error' : 'has-success');
var input = $(this).val();
var regex = new RegExp($(this).data('regex').replace(/^\/|\/$/g, ''));
$(this).parent().parent().removeClass('has-success has-error').addClass((! regex.test(input)) ? 'has-error' : 'has-success');
});
});
</script>
<script>
$('#pServiceId').on('change', function (event) {
$('#pOptionId').html('').select2({
data: $.map(_.get(Pterodactyl.services, $(this).val() + '.options', []), function (item) {
console.log(item);
return {
id: item.id,
text: item.name,
};
}),
}).val('{{ $server->option_id }}').change();
});
$('#pOptionId').on('change', function (event) {
var parentChain = _.get(Pterodactyl.services, $('#pServiceId').val(), null);
var objectChain = _.get(parentChain, 'options.' + $(this).val(), null);
$('#pDefaultContainer').val(_.get(objectChain, 'docker_image', 'not defined!'));
if (!_.get(objectChain, 'startup', false)) {
$('#pDefaultStartupCommand').val(_.get(parentChain, 'startup', 'ERROR: Startup Not Defined!'));
} else {
$('#pDefaultStartupCommand').val(_.get(objectChain, 'startup'));
}
$('#pPackId').html('').select2({
data: [{ id: 0, text: 'No Service Pack' }].concat(
$.map(_.get(objectChain, 'packs', []), function (item, i) {
return {
id: item.id,
text: item.name + ' (' + item.version + ')',
};
})
),
}).val('{{ is_null($server->pack_id) ? 0 : $server->pack_id }}');
$('#appendVariablesTo').html('');
$.each(_.get(objectChain, 'variables', []), function (i, item) {
var setValue = _.get(Pterodactyl.server_variables, 'env_' + item.id + '.value', item.default_value);
var isRequired = (item.required === 1) ? '<span class="label label-danger">Required</span> ' : '';
var dataAppend = ' \
<div class="col-xs-12"> \
<div class="box"> \
<div class="box-header with-border"> \
<h3 class="box-title">' + isRequired + item.name + '</h3> \
</div> \
<div class="box-body"> \
<input data-action="match-regex" name="env_' + item.id + '" class="form-control" type="text" value="' + setValue + '" /> \
<p class="no-margin small text-muted">' + item.description + '</p> \
</div> \
<div class="box-footer"> \
<p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>' + item.env_variable + '</code></p> \
<p class="no-margin text-muted small"><strong>Input Rules:</strong> <code>' + item.rules + '</code></p> \
</div> \
</div> \
</div>';
$('#appendVariablesTo').append(dataAppend);
});
});
</script>
@endsection