Skip to content

Commit

Permalink
Scope VMID check to a node
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Mar 4, 2024
1 parent 874857e commit 1b1e62c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/Http/Requests/Admin/Servers/StoreServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function rules(): array
'name' => $rules['name'],
'user_id' => $rules['user_id'],
'node_id' => $rules['node_id'],
// TODO: validation should be added for manually setting the vmid
'vmid' => 'present|nullable|numeric|min:100|max:999999999',
'hostname' => $rules['hostname'],
'limits' => 'required|array',
Expand Down
23 changes: 16 additions & 7 deletions app/Repositories/Eloquent/ServerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Convoy\Repositories\Eloquent;

use Convoy\Contracts\Repository\ServerRepositoryInterface;
use Convoy\Exceptions\Repository\RecordNotFoundException;
use Convoy\Models\Server;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Convoy\Exceptions\Repository\RecordNotFoundException;
use Convoy\Contracts\Repository\ServerRepositoryInterface;

class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
{
Expand All @@ -15,12 +15,21 @@ public function model(): string
return Server::class;
}

public function isUniqueVmId(int $nodeId, int $vmid): bool
{
return !$this->getBuilder()
->where('vmid', '=', $vmid)
->where('node_id', '=', $nodeId)
->exists();
}

/**
* Check if a given UUID and UUID-Short string are unique to a server.
*/
public function isUniqueUuidCombo(string $uuid, string $short): bool
{
return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuid_short', '=', $short)->exists();
return !$this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuid_short', '=', $short)
->exists();
}

/**
Expand All @@ -33,10 +42,10 @@ public function getByUuid(string $uuid): Server
try {
/** @var Server $model */
$model = $this->getBuilder()
->where(function (Builder $query) use ($uuid) {
$query->where('uuid_short', $uuid)->orWhere('uuid', $uuid);
})
->firstOrFail($this->getColumns());
->where(function (Builder $query) use ($uuid) {
$query->where('uuid_short', $uuid)->orWhere('uuid', $uuid);
})
->firstOrFail($this->getColumns());

return $model;
} catch (ModelNotFoundException $exception) {
Expand Down
10 changes: 6 additions & 4 deletions app/Services/Servers/ServerCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public function handle(array $data)
}
}

$nodeId = Arr::get($data, 'node_id');

$server = Server::create([
'uuid' => $uuid,
'uuid_short' => substr($uuid, 0, 8),
'status' => $shouldCreateServer ? Status::INSTALLING->value : null,
'name' => Arr::get($data, 'name'),
'user_id' => Arr::get($data, 'user_id'),
'node_id' => Arr::get($data, 'node_id'),
'vmid' => Arr::get($data, 'vmid') ?? $this->generateUniqueVmId(),
'node_id' => $nodeId,
'vmid' => Arr::get($data, 'vmid') ?? $this->generateUniqueVmId($nodeId),
'hostname' => Arr::get($data, 'hostname'),
'cpu' => Arr::get($data, 'limits.cpu'),
'memory' => Arr::get($data, 'limits.memory'),
Expand Down Expand Up @@ -78,12 +80,12 @@ public function handle(array $data)
return $server;
}

public function generateUniqueVmId(): int
public function generateUniqueVmId(int $nodeId): int
{
$vmid = random_int(100, 999999999);
$attempts = 0;

while ($this->repository->getBuilder()->where('vmid', '=', $vmid)->exists()) {
while (!$this->repository->isUniqueVmId($nodeId, $vmid)) {
$vmid = random_int(100, 999999999);

if ($attempts++ > 10) {
Expand Down

0 comments on commit 1b1e62c

Please sign in to comment.