diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 442534df6..390cea64d 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -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) { diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index e4c2a09f4..2a209e35b 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -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', diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index f29abec6d..ea83ec5dc 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -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 diff --git a/database/migrations/2016_09_17_194246_add_docker_image_column.php b/database/migrations/2016_09_17_194246_add_docker_image_column.php new file mode 100644 index 000000000..9b667828c --- /dev/null +++ b/database/migrations/2016_09_17_194246_add_docker_image_column.php @@ -0,0 +1,45 @@ +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'); + }); + } +} diff --git a/resources/views/admin/servers/view.blade.php b/resources/views/admin/servers/view.blade.php index a54ed3d8f..c698839e5 100644 --- a/resources/views/admin/servers/view.blade.php +++ b/resources/views/admin/servers/view.blade.php @@ -174,6 +174,31 @@ +
+
+
+
+
+
+ +
+ +

The docker image to use for this server. The default image for this service and option combination is {{ $server->docker_image }}.

+
+
+
+
+
+ +
+ {!! csrf_field() !!} + +
+
+
+
+
+