Include default installation scripts, as well as ability to symlink a script

This commit is contained in:
Dane Everitt 2017-04-27 16:16:57 -04:00
parent 77b1a258d9
commit 30b4934013
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 346 additions and 11 deletions

View file

@ -146,7 +146,16 @@ class OptionController extends Controller
*/
public function viewScripts(Request $request, $id)
{
return view('admin.services.options.scripts', ['option' => ServiceOption::findOrFail($id)]);
$option = ServiceOption::with('copyFrom')->findOrFail($id);
return view('admin.services.options.scripts', [
'copyFromOptions' => ServiceOption::whereNull('copy_script_from')->where([
['service_id', $option->service_id],
['id', '!=', $option->id],
])->get(),
'relyOnScript' => ServiceOption::where('copy_script_from', $option->id)->get(),
'option' => $option,
]);
}
/**
@ -234,11 +243,14 @@ class OptionController extends Controller
try {
$repo->scripts($id, $request->only([
'script_install', 'script_entry', 'script_container',
'script_install', 'script_entry',
'script_container', 'copy_script_from',
]));
Alert::success('Successfully updated option scripts to be run when servers are installed.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.option.scripts', $id)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} 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();

View file

@ -40,12 +40,12 @@ class OptionController extends Controller
return response()->json([
'scripts' => [
'install' => (! $server->option->script_install) ? null : str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_install),
'install' => (! $server->option->copy_script_install) ? null : str_replace(["\r\n", "\n", "\r"], "\n", $server->option->copy_script_install),
'privileged' => $server->option->script_is_privileged,
],
'config' => [
'container' => $server->option->script_container,
'entry' => $server->option->script_entry,
'container' => $server->option->copy_script_container,
'entry' => $server->option->copy_script_entry,
],
'env' => $environment->merge([
'STARTUP=' . $server->startup,

View file

@ -63,6 +63,39 @@ class ServiceOption extends Model
return (is_null($this->startup)) ? $this->service->startup : $this->startup;
}
/**
* Returns the install script for the option; if option is copying from another
* it will return the copied script.
*
* @return string
*/
public function getCopyScriptInstallAttribute($value)
{
return (is_null($this->copy_script_from)) ? $this->script_install : $this->copyFrom->script_install;
}
/**
* Returns the entry command for the option; if option is copying from another
* it will return the copied entry command.
*
* @return string
*/
public function getCopyScriptEntryAttribute($value)
{
return (is_null($this->copy_script_from)) ? $this->script_entry : $this->copyFrom->script_entry;
}
/**
* Returns the install container for the option; if option is copying from another
* it will return the copied install container.
*
* @return string
*/
public function getCopyScriptContainerAttribute($value)
{
return (is_null($this->copy_script_from)) ? $this->script_container : $this->copyFrom->script_container;
}
/**
* Gets service associated with a service option.
*
@ -102,4 +135,9 @@ class ServiceOption extends Model
{
return $this->hasMany(Pack::class, 'option_id');
}
public function copyFrom()
{
return $this->belongsTo(ServiceOption::class, 'copy_script_from');
}
}

View file

@ -49,7 +49,7 @@ class OptionRepository
'description' => 'required|string',
'tag' => 'required|string|max:255|unique:service_options,tag',
'docker_image' => 'required|string|max:255',
'startup' => 'required|string',
'startup' => 'sometimes|nullable|string',
'config_from' => 'sometimes|required|numeric|exists:service_options,id',
'config_startup' => 'required_without:config_from|json',
'config_stop' => 'required_without:config_from|string|max:255',
@ -162,6 +162,7 @@ class OptionRepository
* @param array $data
* @return \Pterodactyl\Models\ServiceOption
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function scripts($id, array $data)
@ -175,8 +176,22 @@ class OptionRepository
'script_is_privileged' => 'sometimes|required|boolean',
'script_entry' => 'sometimes|required|string',
'script_container' => 'sometimes|required|string',
'copy_script_from' => 'sometimes|nullable|numeric',
]);
if (isset($data['copy_script_from']) && ! empty($data['copy_script_from'])) {
$select = ServiceOption::whereNull('copy_script_from')->where([
['id', $data['copy_script_from']],
['service_id', $option->service_id],
])->first();
if (! $select) {
throw new DisplayException('The service option selected to copy a script from either does not exist, or is copying from a higher level.');
}
} else {
$data['copy_script_from'] = null;
}
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}