Skip to content

Commit 812b869

Browse files
committed
add ability to change servers docker image
1 parent 7b7bbdf commit 812b869

File tree

5 files changed

+155
-9
lines changed

5 files changed

+155
-9
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function getView(Request $request, $id)
8383
'locations.long as a_locationName',
8484
'services.name as a_serviceName',
8585
DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'),
86+
'service_options.docker_image',
8687
'service_options.name as a_servceOptionName',
8788
'allocations.ip',
8889
'allocations.port',
@@ -248,30 +249,47 @@ public function postUpdateServerDetails(Request $request, $id)
248249
]);
249250

250251
Alert::success('Server details were successfully updated.')->flash();
252+
} catch (DisplayValidationException $ex) {
251253
return redirect()->route('admin.servers.view', [
252254
'id' => $id,
253255
'tab' => 'tab_details'
256+
])->withErrors(json_decode($ex->getMessage()))->withInput();
257+
} catch (DisplayException $ex) {
258+
Alert::danger($ex->getMessage())->flash();
259+
} catch (\Exception $ex) {
260+
Log::error($ex);
261+
Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash();
262+
}
263+
264+
return redirect()->route('admin.servers.view', [
265+
'id' => $id,
266+
'tab' => 'tab_details'
267+
])->withInput();
268+
}
269+
270+
public function postUpdateContainerDetails(Request $request, $id) {
271+
try {
272+
$server = new ServerRepository;
273+
$server->updateContainer($id, [
274+
'image' => $request->input('docker_image')
254275
]);
276+
Alert::success('Successfully updated this server\'s docker image.')->flash();
255277
} catch (DisplayValidationException $ex) {
256278
return redirect()->route('admin.servers.view', [
257279
'id' => $id,
258280
'tab' => 'tab_details'
259281
])->withErrors(json_decode($ex->getMessage()))->withInput();
260282
} catch (DisplayException $ex) {
261283
Alert::danger($ex->getMessage())->flash();
262-
return redirect()->route('admin.servers.view', [
263-
'id' => $id,
264-
'tab' => 'tab_details'
265-
])->withInput();
266284
} catch (\Exception $ex) {
267285
Log::error($ex);
268-
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
269-
return redirect()->route('admin.servers.view', [
270-
'id' => $id,
271-
'tab' => 'tab_details'
272-
])->withInput();
286+
Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash();
273287
}
274288

289+
return redirect()->route('admin.servers.view', [
290+
'id' => $id,
291+
'tab' => 'tab_details'
292+
]);
275293
}
276294

277295
public function postUpdateServerToggleBuild(Request $request, $id) {

app/Http/Routes/AdminRoutes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ public function map(Router $router) {
163163
'uses' => 'Admin\ServersController@postUpdateServerDetails'
164164
]);
165165

166+
// Change Server Details
167+
$router->post('/view/{id}/container', [
168+
'as' => 'admin.servers.post.container',
169+
'uses' => 'Admin\ServersController@postUpdateContainerDetails'
170+
]);
171+
166172
// Change Server Details
167173
$router->post('/view/{id}/startup', [
168174
'as' => 'admin.servers.post.startup',

app/Repositories/ServerRepository.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,58 @@ public function updateDetails($id, array $data)
387387

388388
}
389389

390+
/**
391+
* [updateContainer description]
392+
* @param int $id
393+
* @param array $data
394+
* @return bool
395+
*/
396+
public function updateContainer($id, array $data)
397+
{
398+
$validator = Validator::make($data, [
399+
'image' => 'required|string'
400+
]);
401+
402+
// Run validator, throw catchable and displayable exception if it fails.
403+
// Exception includes a JSON result of failed validation rules.
404+
if ($validator->fails()) {
405+
throw new DisplayValidationException($validator->errors());
406+
}
407+
408+
DB::beginTransaction();
409+
try {
410+
$server = Models\Server::findOrFail($id);
411+
412+
$server->image = $data['image'];
413+
$server->save();
414+
415+
$node = Models\Node::getByID($server->node);
416+
$client = Models\Node::guzzleRequest($server->node);
417+
418+
$client->request('PATCH', '/server', [
419+
'headers' => [
420+
'X-Access-Server' => $server->uuid,
421+
'X-Access-Token' => $node->daemonSecret
422+
],
423+
'json' => [
424+
'build' => [
425+
'image' => $server->image
426+
]
427+
]
428+
]);
429+
430+
DB::commit();
431+
return true;
432+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
433+
DB::rollBack();
434+
throw new DisplayException('An error occured while attempting to update the container image.', $ex);
435+
} catch (\Exception $ex) {
436+
DB::rollBack();
437+
throw $ex;
438+
}
439+
440+
}
441+
390442
/**
391443
* [changeBuild description]
392444
* @param integer $id
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
use Pterodactyl\Models\Server;
8+
9+
class AddDockerImageColumn extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::table('servers', function (Blueprint $table) {
19+
$table->string('image')->after('daemonSecret');
20+
});
21+
22+
// Populate the column
23+
$servers = Server::select(
24+
'servers.id',
25+
'service_options.docker_image as s_optionImage'
26+
)->join('service_options', 'service_options.id', '=', 'servers.option')->get();
27+
28+
foreach ($servers as $server) {
29+
$server->image = $server->s_optionImage;
30+
$server->save();
31+
}
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::table('servers', function (Blueprint $table) {
42+
$table->dropColumn('image');
43+
});
44+
}
45+
}

resources/views/admin/servers/view.blade.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,31 @@
174174
</div>
175175
</form>
176176
</div>
177+
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
178+
<div class="panel-body">
179+
<form action="{{ route('admin.servers.post.container', $server->id) }}" method="POST">
180+
<div class="row">
181+
<div class="col-md-6">
182+
<div class="form-group">
183+
<label for="name" class="control-label">Docker Container Image</label>
184+
<div>
185+
<input type="text" name="docker_image" value="{{ $server->image }}" class="form-control" />
186+
<p class="text-muted"><small>The docker image to use for this server. The default image for this service and option combination is <code>{{ $server->docker_image }}</code>.</small></p>
187+
</div>
188+
</div>
189+
</div>
190+
<div class="col-md-6 text-center">
191+
<div class="form-group">
192+
<label for="name" class="control-label">&nbsp;</label>
193+
<div>
194+
{!! csrf_field() !!}
195+
<input type="submit" class="btn btn-sm btn-primary" value="Update Docker Image" />
196+
</div>
197+
</div>
198+
</div>
199+
</div>
200+
</form>
201+
</div>
177202
</div>
178203
</div>
179204
<div class="tab-pane" id="tab_build">

0 commit comments

Comments
 (0)