diff --git a/app/Models/Server.php b/app/Models/Server.php index 2214c9eaf..a9ef398b4 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -10,6 +10,7 @@ use GuzzleHttp\Exception\GuzzleException; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Query\JoinClause; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Psr\Http\Message\ResponseInterface; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -103,6 +104,16 @@ use App\Exceptions\Http\Server\ServerStateConflictException; * @method static \Illuminate\Database\Eloquent\Builder|Server whereUuid($value) * @method static \Illuminate\Database\Eloquent\Builder|Server whereuuid_short($value) * + * @property array|null $docker_labels + * @property Collection|null $ports + * @property-read \Illuminate\Database\Eloquent\Collection $eggVariables + * @property-read int|null $egg_variables_count + * @property-read \Illuminate\Database\Eloquent\Collection $serverVariables + * @property-read int|null $server_variables_count + * @method static \Illuminate\Database\Eloquent\Builder|Server whereDockerLabels($value) + * @method static \Illuminate\Database\Eloquent\Builder|Server whereInstalledAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Server wherePorts($value) + * @method static \Illuminate\Database\Eloquent\Builder|Server whereUuidShort($value) * @mixin \Eloquent */ class Server extends Model diff --git a/app/Services/Allocations/FindAssignableAllocationService.php b/app/Services/Allocations/FindAssignableAllocationService.php index 6f9958a6a..a6c885207 100644 --- a/app/Services/Allocations/FindAssignableAllocationService.php +++ b/app/Services/Allocations/FindAssignableAllocationService.php @@ -2,6 +2,8 @@ namespace App\Services\Allocations; +use App\Models\Objects\Endpoint; +use Illuminate\Support\Collection; use Webmozart\Assert\Assert; use App\Models\Server; use App\Exceptions\Service\Allocation\AutoAllocationNotEnabledException; @@ -45,11 +47,10 @@ class FindAssignableAllocationService Assert::integerish($start); Assert::integerish($end); - // Get all the currently allocated ports for the node so that we can figure out which port might be available. - $ports = $server->node->allocations() - ->where('ip', $server->allocation->ip) - ->whereBetween('port', [$start, $end]) - ->pluck('port'); + $ports = $server->node->servers + ->reduce(fn (Collection $result, $value) => $result->merge($value), collect()) + ->map(fn (Endpoint $endpoint) => $endpoint->port) + ->filter(fn (int $port): bool => $port >= $start && $port <= $end); // Compute the difference of the range and the currently created ports, finding // any port that does not already exist in the database. We will then use this @@ -57,7 +58,6 @@ class FindAssignableAllocationService $available = array_diff(range($start, $end), $ports->toArray()); // Pick a random port out of the remaining available ports. - /** @var int $port */ return $available[array_rand($available)]; } } diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index 77c6a2d66..a29d80bff 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -66,8 +66,8 @@ class ServerConfigurationStructureService 'allocations' => [ 'force_outgoing_ip' => $server->egg->force_outgoing_ip, 'default' => [ - 'ip' => $server->allocation->ip, - 'port' => $server->allocation->port, + 'ip' => $server->getPrimaryEndpoint()->ip, + 'port' => $server->getPrimaryEndpoint()->port, ], 'mappings' => $server->getPortMappings(), ], diff --git a/app/Services/Servers/ServerDeletionService.php b/app/Services/Servers/ServerDeletionService.php index 97e6995c7..71e9ee19e 100644 --- a/app/Services/Servers/ServerDeletionService.php +++ b/app/Services/Servers/ServerDeletionService.php @@ -77,8 +77,6 @@ class ServerDeletionService } } - $server->allocations()->update(['server_id' => null]); - $server->delete(); }); }