add ability to change servers docker image

This commit is contained in:
Dane Everitt 2016-09-17 20:14:36 -04:00
parent 7b7bbdf576
commit 812b869be8
5 changed files with 156 additions and 10 deletions

View file

@ -83,6 +83,7 @@ class ServersController extends Controller
'locations.long as a_locationName',
'services.name as a_serviceName',
DB::raw('IFNULL(service_options.executable, services.executable) as a_serviceExecutable'),
'service_options.docker_image',
'service_options.name as a_servceOptionName',
'allocations.ip',
'allocations.port',
@ -248,10 +249,6 @@ class ServersController extends Controller
]);
Alert::success('Server details were successfully updated.')->flash();
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
]);
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
'id' => $id,
@ -259,19 +256,40 @@ class ServersController extends Controller
])->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
])->withInput();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
Alert::danger('An unhandled exception occured while attemping to update this server. Please try again.')->flash();
}
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
])->withInput();
}
public function postUpdateContainerDetails(Request $request, $id) {
try {
$server = new ServerRepository;
$server->updateContainer($id, [
'image' => $request->input('docker_image')
]);
Alert::success('Successfully updated this server\'s docker image.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
])->withInput();
])->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. Please try again.')->flash();
}
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
]);
}
public function postUpdateServerToggleBuild(Request $request, $id) {

View file

@ -163,6 +163,12 @@ class AdminRoutes {
'uses' => 'Admin\ServersController@postUpdateServerDetails'
]);
// Change Server Details
$router->post('/view/{id}/container', [
'as' => 'admin.servers.post.container',
'uses' => 'Admin\ServersController@postUpdateContainerDetails'
]);
// Change Server Details
$router->post('/view/{id}/startup', [
'as' => 'admin.servers.post.startup',

View file

@ -387,6 +387,58 @@ class ServerRepository
}
/**
* [updateContainer description]
* @param int $id
* @param array $data
* @return bool
*/
public function updateContainer($id, array $data)
{
$validator = Validator::make($data, [
'image' => 'required|string'
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ($validator->fails()) {
throw new DisplayValidationException($validator->errors());
}
DB::beginTransaction();
try {
$server = Models\Server::findOrFail($id);
$server->image = $data['image'];
$server->save();
$node = Models\Node::getByID($server->node);
$client = Models\Node::guzzleRequest($server->node);
$client->request('PATCH', '/server', [
'headers' => [
'X-Access-Server' => $server->uuid,
'X-Access-Token' => $node->daemonSecret
],
'json' => [
'build' => [
'image' => $server->image
]
]
]);
DB::commit();
return true;
} catch (\GuzzleHttp\Exception\TransferException $ex) {
DB::rollBack();
throw new DisplayException('An error occured while attempting to update the container image.', $ex);
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}
/**
* [changeBuild description]
* @param integer $id

View file

@ -0,0 +1,45 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Pterodactyl\Models\Server;
class AddDockerImageColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('servers', function (Blueprint $table) {
$table->string('image')->after('daemonSecret');
});
// Populate the column
$servers = Server::select(
'servers.id',
'service_options.docker_image as s_optionImage'
)->join('service_options', 'service_options.id', '=', 'servers.option')->get();
foreach ($servers as $server) {
$server->image = $server->s_optionImage;
$server->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('servers', function (Blueprint $table) {
$table->dropColumn('image');
});
}
}

View file

@ -174,6 +174,31 @@
</div>
</form>
</div>
<div class="panel-heading" style="border-top: 1px solid #ddd;"></div>
<div class="panel-body">
<form action="{{ route('admin.servers.post.container', $server->id) }}" method="POST">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name" class="control-label">Docker Container Image</label>
<div>
<input type="text" name="docker_image" value="{{ $server->image }}" class="form-control" />
<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>
</div>
</div>
</div>
<div class="col-md-6 text-center">
<div class="form-group">
<label for="name" class="control-label">&nbsp;</label>
<div>
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm btn-primary" value="Update Docker Image" />
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="tab-pane" id="tab_build">