Skip to content

Commit 9d2d726

Browse files
committed
🎉 Finishes server creation 🎉
1 parent a7fdb96 commit 9d2d726

File tree

5 files changed

+136
-17
lines changed

5 files changed

+136
-17
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function getView(Request $request, $id)
6262
->join('locations', 'nodes.location', '=', 'locations.id')
6363
->join('services', 'servers.service', '=', 'services.id')
6464
->join('service_options', 'servers.option', '=', 'service_options.id')
65+
->where('servers.id', $id)
6566
->first();
6667

6768
return view('admin.servers.view', [

app/Repositories/ServerRepository.php

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function create(array $data)
7777

7878
// Verify IP & Port are a.) free and b.) assigned to the node.
7979
// We know the node exists because of 'exists:nodes,id' in the validation
80-
$node = Models\Node::find($data['node']);
80+
$node = Models\Node::getByID($data['node']);
8181
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
8282

8383
// Something failed in the query, either that combo doesn't exist, or it is in use.
@@ -94,6 +94,9 @@ public function create(array $data)
9494
throw new DisplayException('The requested service option does not exist for the specified service.');
9595
}
9696

97+
// Load up the Service Information
98+
$service = Models\Service::find($option->parent_service);
99+
97100
// Check those Variables
98101
$variables = Models\ServiceVariables::where('option_id', $data['option'])->get();
99102
$variableList = [];
@@ -105,12 +108,11 @@ public function create(array $data)
105108
if ($variable->required === 1) {
106109
throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.');
107110
}
108-
109111
$variableList = array_merge($variableList, [[
110-
'var_id' => $variable->id,
111-
'var_val' => $variable->default_value
112+
'id' => $variable->id,
113+
'env' => $variable->env_variable,
114+
'val' => $variable->default_value
112115
]]);
113-
114116
continue;
115117
}
116118

@@ -120,10 +122,10 @@ public function create(array $data)
120122
}
121123

122124
$variableList = array_merge($variableList, [[
123-
'var_id' => $variable->id,
124-
'var_val' => $data['env_' . $variable->env_variable]
125+
'id' => $variable->id,
126+
'env' => $variable->env_variable,
127+
'val' => $data['env_' . $variable->env_variable]
125128
]]);
126-
127129
continue;
128130
}
129131
}
@@ -188,25 +190,78 @@ public function create(array $data)
188190
$allocation->save();
189191

190192
// Add Variables
193+
$environmentVariables = [];
194+
$environmentVariables = array_merge($environmentVariables, [
195+
'STARTUP' => $data['startup']
196+
]);
191197
foreach($variableList as $item) {
198+
$environmentVariables = array_merge($environmentVariables, [
199+
$item['env'] => $item['val']
200+
]);
192201
Models\ServerVariables::create([
193202
'server_id' => $server->id,
194-
'variable_id' => $item['var_id'],
195-
'variable_value' => $item['var_val']
203+
'variable_id' => $item['id'],
204+
'variable_value' => $item['val']
196205
]);
197206
}
198207

199208
try {
200209

201-
// Add logic for communicating with Wings to make the server in here.
202-
// We should add the server regardless of the Wings response, but
203-
// handle the error and then allow the server to be re-deployed.
210+
$client = Models\Node::guzzleRequest($node->id);
211+
$client->request('POST', '/servers', [
212+
'headers' => [
213+
'X-Access-Token' => $node->daemonSecret
214+
],
215+
'json' => [
216+
'uuid' => (string) $server->uuid,
217+
'user' => $server->username,
218+
'build' => [
219+
'default' => [
220+
'ip' => $server->ip,
221+
'port' => (int) $server->port
222+
],
223+
'ports' => [
224+
(string) $server->ip => [ (int) $server->port ]
225+
],
226+
'env' => $environmentVariables,
227+
'memory' => (int) $server->memory,
228+
'swap' => (int) $server->swap,
229+
'io' => (int) $server->io,
230+
'cpu' => (int) $server->cpu,
231+
'disk' => (int) $server->disk,
232+
'image' => (isset($data['custom_image_name'])) ? $data['custom_image_name'] : $option->docker_image
233+
],
234+
'service' => [
235+
'type' => $service->file,
236+
'option' => $option->tag
237+
],
238+
'keys' => [
239+
(string) $server->daemonSecret => [
240+
's:get',
241+
's:power',
242+
's:console',
243+
's:command',
244+
's:files:get',
245+
's:files:read',
246+
's:files:post',
247+
's:files:delete',
248+
's:files:upload',
249+
's:set-password'
250+
]
251+
],
252+
'rebuild' => false
253+
]
254+
]);
204255

205256
DB::commit();
206257
return $server->id;
207-
} catch (\Exception $e) {
258+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
259+
DB::rollBack();
260+
throw new DisplayException('An error occured while attempting to update the configuration: ' . $ex->getMessage());
261+
} catch (\Exception $ex) {
208262
DB::rollBack();
209-
throw $e;
263+
Log:error($ex);
264+
throw $ex;
210265
}
211266

212267
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddServiceFile extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('services', function (Blueprint $table) {
16+
$table->string('file')->after('description');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('services', function (Blueprint $table) {
28+
$table->dropColumn('file');
29+
});
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddServiceOptionTag extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('service_options', function (Blueprint $table) {
16+
$table->string('tag')->after('description');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('service_options', function (Blueprint $table) {
28+
$table->dropColumn('tag');
29+
});
30+
}
31+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,15 @@
212212
<span class="input-group-addon" id="startupExec"></span>
213213
<input type="text" class="form-control" name="startup" value="{{ old('startup') }}" />
214214
</div>
215+
<p class="text-muted"><small>The following data replacers are avaliable for the startup command: <code>@{{SERVER_MEMORY}}</code>, <code>@{{SERVER_IP}}</code>, and <code>@{{SERVER_PORT}}</code>. They will be replaced with the allocated memory, server ip, and server port respectively.</small></p>
215216
</div>
216217
</div>
217218
<div class="row">
218219
<div class="col-md-12">
219220
<div class="alert alert-info">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.</div>
220-
<span id="serverVariables"></span>
221221
</div>
222222
</div>
223+
<div class="row" id="serverVariables"></div>
223224
</div>
224225
<div class="well">
225226
<div class="row">
@@ -402,7 +403,7 @@
402403
<input type="text" autocomplete="off" name="env_' + item.env_variable + '" class="form-control" value="' + item.default_value + '" />\
403404
<p class="text-muted"><small>' + item.description + '</small></p>\
404405
<p class="text-muted"><small>Regex Requirements for Input: <code>' + item.regex + '</code></small></p>\
405-
<p class="text-muted"><small>Access in Startup: <code>${' + item.env_variable + '}</code></small></p>\
406+
<p class="text-muted"><small>Access in Startup: <code>@{{' + item.env_variable + '}}</code></small></p>\
406407
</div>\
407408
</div>\
408409
';

0 commit comments

Comments
 (0)