From 9d2d72699203963c2549b167cf74016a6518a927 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 3 Jan 2016 18:10:28 -0500 Subject: [PATCH] :tada: Finishes server creation :tada: --- .../Controllers/Admin/ServersController.php | 1 + app/Repositories/ServerRepository.php | 85 +++++++++++++++---- .../2016_01_03_204358_add_service_file.php | 31 +++++++ ...16_01_03_210418_add_service_option_tag.php | 31 +++++++ resources/views/admin/servers/new.blade.php | 5 +- 5 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 database/migrations/2016_01_03_204358_add_service_file.php create mode 100644 database/migrations/2016_01_03_210418_add_service_option_tag.php diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 3ac9dec09..c7a647924 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -62,6 +62,7 @@ class ServersController extends Controller ->join('locations', 'nodes.location', '=', 'locations.id') ->join('services', 'servers.service', '=', 'services.id') ->join('service_options', 'servers.option', '=', 'service_options.id') + ->where('servers.id', $id) ->first(); return view('admin.servers.view', [ diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index 958895a5e..4c8fdbd9a 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -77,7 +77,7 @@ class ServerRepository // Verify IP & Port are a.) free and b.) assigned to the node. // We know the node exists because of 'exists:nodes,id' in the validation - $node = Models\Node::find($data['node']); + $node = Models\Node::getByID($data['node']); $allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first(); // Something failed in the query, either that combo doesn't exist, or it is in use. @@ -94,6 +94,9 @@ class ServerRepository throw new DisplayException('The requested service option does not exist for the specified service.'); } + // Load up the Service Information + $service = Models\Service::find($option->parent_service); + // Check those Variables $variables = Models\ServiceVariables::where('option_id', $data['option'])->get(); $variableList = []; @@ -105,12 +108,11 @@ class ServerRepository if ($variable->required === 1) { throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.'); } - $variableList = array_merge($variableList, [[ - 'var_id' => $variable->id, - 'var_val' => $variable->default_value + 'id' => $variable->id, + 'env' => $variable->env_variable, + 'val' => $variable->default_value ]]); - continue; } @@ -120,10 +122,10 @@ class ServerRepository } $variableList = array_merge($variableList, [[ - 'var_id' => $variable->id, - 'var_val' => $data['env_' . $variable->env_variable] + 'id' => $variable->id, + 'env' => $variable->env_variable, + 'val' => $data['env_' . $variable->env_variable] ]]); - continue; } } @@ -188,25 +190,78 @@ class ServerRepository $allocation->save(); // Add Variables + $environmentVariables = []; + $environmentVariables = array_merge($environmentVariables, [ + 'STARTUP' => $data['startup'] + ]); foreach($variableList as $item) { + $environmentVariables = array_merge($environmentVariables, [ + $item['env'] => $item['val'] + ]); Models\ServerVariables::create([ 'server_id' => $server->id, - 'variable_id' => $item['var_id'], - 'variable_value' => $item['var_val'] + 'variable_id' => $item['id'], + 'variable_value' => $item['val'] ]); } try { - // Add logic for communicating with Wings to make the server in here. - // We should add the server regardless of the Wings response, but - // handle the error and then allow the server to be re-deployed. + $client = Models\Node::guzzleRequest($node->id); + $client->request('POST', '/servers', [ + 'headers' => [ + 'X-Access-Token' => $node->daemonSecret + ], + 'json' => [ + 'uuid' => (string) $server->uuid, + 'user' => $server->username, + 'build' => [ + 'default' => [ + 'ip' => $server->ip, + 'port' => (int) $server->port + ], + 'ports' => [ + (string) $server->ip => [ (int) $server->port ] + ], + 'env' => $environmentVariables, + 'memory' => (int) $server->memory, + 'swap' => (int) $server->swap, + 'io' => (int) $server->io, + 'cpu' => (int) $server->cpu, + 'disk' => (int) $server->disk, + 'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image + ], + 'service' => [ + 'type' => $service->file, + 'option' => $option->tag + ], + 'keys' => [ + (string) $server->daemonSecret => [ + 's:get', + 's:power', + 's:console', + 's:command', + 's:files:get', + 's:files:read', + 's:files:post', + 's:files:delete', + 's:files:upload', + 's:set-password' + ] + ], + 'rebuild' => false + ] + ]); DB::commit(); return $server->id; - } catch (\Exception $e) { + } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); - throw $e; + throw new DisplayException('An error occured while attempting to update the configuration: ' . $ex->getMessage()); + } catch (\Exception $ex) { + DB::rollBack(); + Log:error($ex); + throw $ex; } } diff --git a/database/migrations/2016_01_03_204358_add_service_file.php b/database/migrations/2016_01_03_204358_add_service_file.php new file mode 100644 index 000000000..5fff69aaf --- /dev/null +++ b/database/migrations/2016_01_03_204358_add_service_file.php @@ -0,0 +1,31 @@ +string('file')->after('description'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('services', function (Blueprint $table) { + $table->dropColumn('file'); + }); + } +} diff --git a/database/migrations/2016_01_03_210418_add_service_option_tag.php b/database/migrations/2016_01_03_210418_add_service_option_tag.php new file mode 100644 index 000000000..d565c8532 --- /dev/null +++ b/database/migrations/2016_01_03_210418_add_service_option_tag.php @@ -0,0 +1,31 @@ +string('tag')->after('description'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('service_options', function (Blueprint $table) { + $table->dropColumn('tag'); + }); + } +} diff --git a/resources/views/admin/servers/new.blade.php b/resources/views/admin/servers/new.blade.php index ae8a55156..b5c076751 100644 --- a/resources/views/admin/servers/new.blade.php +++ b/resources/views/admin/servers/new.blade.php @@ -212,14 +212,15 @@ +

The following data replacers are avaliable for the startup command: @{{SERVER_MEMORY}}, @{{SERVER_IP}}, and @{{SERVER_PORT}}. They will be replaced with the allocated memory, server ip, and server port respectively.

Some service options have additional environment variables that you can define for a given instance. They will show up below when you select a service option. If none show up, chances are that none were defined, and there is nothing to worry about.
-
+
@@ -402,7 +403,7 @@ $(document).ready(function () { \

' + item.description + '

\

Regex Requirements for Input: ' + item.regex + '

\ -

Access in Startup: ${' + item.env_variable + '}

\ +

Access in Startup: @{{' + item.env_variable + '}}

\
\
\ ';