Skip to content

Commit 8dc2447

Browse files
committed
Add reinstall abilities and cleanup process for new servers
1 parent 3fe5d16 commit 8dc2447

File tree

9 files changed

+91
-3
lines changed

9 files changed

+91
-3
lines changed

app/Http/Controllers/Admin/OptionController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function createVariable(Request $request, $id)
9595
$repo = new VariableRepository;
9696

9797
try {
98-
$variable = $repo->create($id, $request->only([
98+
$variable = $repo->create($id, $request->intersect([
9999
'name', 'description', 'env_variable',
100100
'default_value', 'options', 'rules',
101101
]));
@@ -200,7 +200,7 @@ public function editVariable(Request $request, $option, $variable)
200200

201201
try {
202202
if ($request->input('action') !== 'delete') {
203-
$variable = $repo->update($variable, $request->only([
203+
$variable = $repo->update($variable, $request->intersect([
204204
'name', 'description', 'env_variable',
205205
'default_value', 'options', 'rules',
206206
]));
@@ -233,7 +233,9 @@ public function updateScripts(Request $request, $id)
233233
$repo = new OptionRepository;
234234

235235
try {
236-
$repo->scripts($id, $request->only('script_install'));
236+
$repo->scripts($id, $request->only([
237+
'script_install', 'script_entry', 'script_container',
238+
]));
237239
Alert::success('Successfully updated option scripts to be run when servers are installed.')->flash();
238240
} catch (DisplayValidationException $ex) {
239241
return redirect()->route('admin.services.option.scripts', $id)->withErrors(json_decode($ex->getMessage()));

app/Http/Controllers/Admin/ServersController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,30 @@ public function toggleInstall(Request $request, $id)
322322
return redirect()->route('admin.servers.view.manage', $id);
323323
}
324324

325+
/**
326+
* Reinstalls the server with the currently assigned pack and service.
327+
*
328+
* @param \Illuminate\Http\Request $request
329+
* @param int $id
330+
* @return \Illuminate\Http\RedirectResponse
331+
*/
332+
public function reinstallServer(Request $request, $id)
333+
{
334+
$repo = new ServerRepository;
335+
try {
336+
$repo->reinstall($id);
337+
338+
Alert::success('Server successfully marked for reinstallation.')->flash();
339+
} catch (DisplayException $ex) {
340+
Alert::danger($ex->getMessage())->flash();
341+
} catch (\Exception $ex) {
342+
Log::error($ex);
343+
Alert::danger('An unhandled exception occured while attemping to perform this reinstallation. This error has been logged.')->flash();
344+
}
345+
346+
return redirect()->route('admin.servers.view.manage', $id);
347+
}
348+
325349
/**
326350
* Setup a server to have a container rebuild.
327351
*

app/Http/Controllers/Daemon/OptionController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function details(Request $request, $server)
4444
'install' => (! $server->option->script_install) ? null : str_replace(["\r\n", "\n", "\r"], "\n", $server->option->script_install),
4545
'privileged' => $server->option->script_is_privileged,
4646
],
47+
'config' => [
48+
'container' => $server->option->script_container,
49+
'entry' => $server->option->script_entry,
50+
],
4751
'env' => $environment->merge([
4852
'STARTUP=' . $server->startup,
4953
'SERVER_MEMORY=' . $server->memory,

app/Repositories/OptionRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public function scripts($id, array $data)
173173
$validator = Validator::make($data, [
174174
'script_install' => 'sometimes|nullable|string',
175175
'script_is_privileged' => 'sometimes|required|boolean',
176+
'script_entry' => 'sometimes|required|string',
177+
'script_container' => 'sometimes|required|string',
176178
]);
177179

178180
if ($validator->fails()) {

app/Repositories/ServerRepository.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,4 +864,25 @@ public function updateSFTPPassword($id, $password)
864864
]);
865865
});
866866
}
867+
868+
/**
869+
* Marks a server for reinstallation on the node.
870+
*
871+
* @param int $id
872+
* @return void
873+
*/
874+
public function reinstall($id)
875+
{
876+
$server = Models\Server::with('node')->findOrFail($id);
877+
878+
DB::transaction(function () use ($server) {
879+
$server->installed = 0;
880+
$server->save();
881+
882+
$server->node->guzzleClient([
883+
'X-Access-Token' => $server->node->daemonSecret,
884+
'X-Access-Server' => $server->uuid,
885+
])->request('POST', '/server/reinstall');
886+
});
887+
}
867888
}

database/migrations/2017_04_20_171943_AddScriptsToServiceOptions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function up()
1616
Schema::table('service_options', function (Blueprint $table) {
1717
$table->text('script_install')->after('startup')->nullable();
1818
$table->boolean('script_is_privileged')->default(true)->after('startup');
19+
$table->text('script_entry')->default('ash')->after('startup');
20+
$table->text('script_container')->default('alpine:3.4')->after('startup');
1921
});
2022
}
2123

@@ -29,6 +31,8 @@ public function down()
2931
Schema::table('service_options', function (Blueprint $table) {
3032
$table->dropColumn('script_install');
3133
$table->dropColumn('script_is_privileged');
34+
$table->dropColumn('script_entry');
35+
$table->dropColumn('script_container');
3236
});
3337
}
3438
}

resources/themes/pterodactyl/admin/servers/view/manage.blade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@
5353
</div>
5454
</div>
5555
<div class="row">
56+
<div class="col-sm-4">
57+
<div class="box box-primary">
58+
<div class="box-header with-border">
59+
<h3 class="box-title">Reinstall Server</h3>
60+
</div>
61+
<div class="box-body">
62+
<p>This will reinstall the server with the assigned pack and service scripts. <strong>Danger!</strong> This could overwrite server data.</p>
63+
</div>
64+
<div class="box-footer">
65+
<form action="{{ route('admin.servers.view.manage.reinstall', $server->id) }}" method="POST">
66+
{!! csrf_field() !!}
67+
<button type="submit" class="btn btn-danger">Reinstall Server</button>
68+
</form>
69+
</div>
70+
</div>
71+
</div>
5672
<div class="col-sm-4">
5773
<div class="box box-primary">
5874
<div class="box-header with-border">

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
<div class="box-body no-padding">
5656
<div id="editor_install"style="height:300px">{{ $option->script_install }}</div>
5757
</div>
58+
<div class="box-body">
59+
<div class="row">
60+
<div class="form-group col-sm-6">
61+
<label class="control-label">Script Container</label>
62+
<input type="text" name="script_container" class="form-control" value="{{ $option->script_container }}" />
63+
<p class="text-muted small">Docker container to use when running this script for the server.</p>
64+
</div>
65+
<div class="form-group col-sm-6">
66+
<label class="control-label">Script Entrypoint Command</label>
67+
<input type="text" name="script_entry" class="form-control" value="{{ $option->script_entry }}" />
68+
<p class="text-muted small">The entrypoint command to use for this script.</p>
69+
</div>
70+
</div>
71+
</div>
5872
<div class="box-footer">
5973
{!! csrf_field() !!}
6074
<textarea name="script_install" class="hidden"></textarea>

routes/admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
Route::post('/view/{id}/manage/toggle', 'ServersController@toggleInstall')->name('admin.servers.view.manage.toggle');
119119
Route::post('/view/{id}/manage/rebuild', 'ServersController@rebuildContainer')->name('admin.servers.view.manage.rebuild');
120120
Route::post('/view/{id}/manage/suspension', 'ServersController@manageSuspension')->name('admin.servers.view.manage.suspension');
121+
Route::post('/view/{id}/manage/reinstall', 'ServersController@reinstallServer')->name('admin.servers.view.manage.reinstall');
121122
Route::post('/view/{id}/delete', 'ServersController@delete');
122123

123124
Route::patch('/view/{id}/database', 'ServersController@resetDatabasePassword');

0 commit comments

Comments
 (0)