Skip to content

Commit abaf294

Browse files
committed
Only send updated data to daemon; makes better use of on-the-fly container updates
Now we only send information that would trigger a container rebuild if it actually changes from current settings.
1 parent 06c680e commit abaf294

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

app/Repositories/ServerRepository.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ public function changeBuild($id, array $data)
401401
'string',
402402
'regex:/^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])):(\d{1,5})$/'
403403
],
404-
'add_additional' => 'array',
405-
'remove_additional' => 'array',
404+
'add_additional' => 'nullable|array',
405+
'remove_additional' => 'nullable|array',
406406
'memory' => 'integer|min:0',
407407
'swap' => 'integer|min:-1',
408408
'io' => 'integer|min:10|max:1000',
@@ -422,23 +422,31 @@ public function changeBuild($id, array $data)
422422
$server = Models\Server::findOrFail($id);
423423
$allocation = Models\Allocation::findOrFail($server->allocation);
424424

425+
$newBuild = [];
426+
425427
if (isset($data['default'])) {
426428
list($ip, $port) = explode(':', $data['default']);
427-
if ($ip !== $allocation->ip || $port !== $allocation->port) {
429+
if ($ip !== $allocation->ip || (int) $port !== $allocation->port) {
428430
$selection = Models\Allocation::where('ip', $ip)->where('port', $port)->where('assigned_to', $server->id)->first();
429431
if (!$selection) {
430432
throw new DisplayException('The requested default connection (' . $ip . ':' . $port . ') is not allocated to this server.');
431433
}
432434

433435
$server->allocation = $selection->id;
436+
$newBuild['default'] = [
437+
'ip' => $ip,
438+
'port' => (int) $port
439+
];
434440

435441
// Re-Run to keep updated for rest of function
436442
$allocation = Models\Allocation::findOrFail($server->allocation);
437443
}
438444
}
439445

446+
$newPorts = false;
440447
// Remove Assignments
441448
if (isset($data['remove_additional'])) {
449+
$newPorts = true;
442450
foreach ($data['remove_additional'] as $id => $combo) {
443451
list($ip, $port) = explode(':', $combo);
444452
// Invalid, not worth killing the whole thing, we'll just skip over it.
@@ -459,6 +467,7 @@ public function changeBuild($id, array $data)
459467

460468
// Add Assignments
461469
if (isset($data['add_additional'])) {
470+
$newPorts = true;
462471
foreach ($data['add_additional'] as $id => $combo) {
463472
list($ip, $port) = explode(':', $combo);
464473
// Invalid, not worth killing the whole thing, we'll just skip over it.
@@ -488,6 +497,10 @@ public function changeBuild($id, array $data)
488497
}
489498
}
490499

500+
if ($newPorts === true) {
501+
$newBuild['ports|overwrite'] = $additionalAssignments;
502+
}
503+
491504
// @TODO: verify that server can be set to this much memory without
492505
// going over node limits.
493506
if (isset($data['memory'])) {
@@ -516,6 +529,12 @@ public function changeBuild($id, array $data)
516529
// This won't be committed unless the HTTP request succeedes anyways
517530
$server->save();
518531

532+
$newBuild['memory'] = (int) $server->memory;
533+
$newBuild['swap'] = (int) $server->swap;
534+
$newBuild['io'] = (int) $server->io;
535+
$newBuild['cpu'] = (int) $server->cpu;
536+
$newBuild['disk'] = (int) $server->disk;
537+
519538
$node = Models\Node::getByID($server->node);
520539
$client = Models\Node::guzzleRequest($server->node);
521540

@@ -525,18 +544,7 @@ public function changeBuild($id, array $data)
525544
'X-Access-Token' => $node->daemonSecret
526545
],
527546
'json' => [
528-
'build' => [
529-
'default' => [
530-
'ip' => $allocation->ip,
531-
'port' => (int) $allocation->port
532-
],
533-
'ports|overwrite' => $additionalAssignments,
534-
'memory' => (int) $server->memory,
535-
'swap' => (int) $server->swap,
536-
'io' => (int) $server->io,
537-
'cpu' => (int) $server->cpu,
538-
'disk' => (int) $server->disk
539-
]
547+
'build' => $newBuild
540548
]
541549
]);
542550

0 commit comments

Comments
 (0)