Revert this

This commit is contained in:
Lance Pioch 2024-06-15 05:44:53 -04:00
parent a97341f6f2
commit 459d90e8d1

View File

@ -2,8 +2,10 @@
namespace App\Models; namespace App\Models;
use App\Casts\EndpointCollection;
use App\Enums\ServerState; use App\Enums\ServerState;
use App\Exceptions\Http\Connection\DaemonConnectionException; use App\Exceptions\Http\Connection\DaemonConnectionException;
use App\Models\Objects\Endpoint;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
@ -128,11 +130,6 @@ class Server extends Model
'installed_at' => null, 'installed_at' => null,
]; ];
/**
* The default relationships to load for all server models.
*/
protected $with = ['allocation'];
/** /**
* Fields that are not mass assignable. * Fields that are not mass assignable.
*/ */
@ -152,7 +149,6 @@ class Server extends Model
'threads' => 'nullable|regex:/^[0-9-,]+$/', 'threads' => 'nullable|regex:/^[0-9-,]+$/',
'oom_killer' => 'sometimes|boolean', 'oom_killer' => 'sometimes|boolean',
'disk' => 'required|numeric|min:0', 'disk' => 'required|numeric|min:0',
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
'egg_id' => 'required|exists:eggs,id', 'egg_id' => 'required|exists:eggs,id',
'startup' => 'required|string', 'startup' => 'required|string',
'skip_scripts' => 'sometimes|boolean', 'skip_scripts' => 'sometimes|boolean',
@ -175,27 +171,33 @@ class Server extends Model
'io' => 'integer', 'io' => 'integer',
'cpu' => 'integer', 'cpu' => 'integer',
'oom_killer' => 'boolean', 'oom_killer' => 'boolean',
'allocation_id' => 'integer',
'egg_id' => 'integer', 'egg_id' => 'integer',
'database_limit' => 'integer', 'database_limit' => 'integer',
'allocation_limit' => 'integer', 'allocation_limit' => 'integer',
'backup_limit' => 'integer', 'backup_limit' => 'integer',
self::CREATED_AT => 'datetime',
self::UPDATED_AT => 'datetime',
'deleted_at' => 'datetime', 'deleted_at' => 'datetime',
'installed_at' => 'datetime', 'installed_at' => 'datetime',
'docker_labels' => 'array', 'docker_labels' => 'array',
'ports' => EndpointCollection::class,
]; ];
} }
/** /**
* Returns the format for server allocations when communicating with the Daemon. * Returns the format for server allocations when communicating with the Daemon.
*/ */
public function getAllocationMappings(): array public function getPortMappings(): array
{ {
return $this->allocations->where('node_id', $this->node_id)->groupBy('ip')->map(function ($item) { $defaultIp = '0.0.0.0';
return $item->pluck('port');
})->toArray(); $ports = collect($this->ports)
->map(fn ($port) => str_contains($port, ':') ? $port : "$defaultIp:$port")
->mapToGroups(function ($port) {
[$ip, $port] = explode(':', $port);
return [$ip => (int) $port];
});
return $ports->all();
} }
public function isInstalled(): bool public function isInstalled(): bool
@ -394,4 +396,17 @@ class Server extends Model
return cache()->get("servers.$this->uuid.container.status") ?? 'missing'; return cache()->get("servers.$this->uuid.container.status") ?? 'missing';
} }
public function getPrimaryEndpoint(): ?Endpoint
{
$endpoint = $this->ports->first();
$portEggVariable = $this->variables->firstWhere('env_variable', 'SERVER_PORT');
if ($portEggVariable) {
$portServerVariable = $this->serverVariables->firstWhere('variable_id', $portEggVariable->id);
$endpoint = new Endpoint($portServerVariable->variable_value);
}
return $endpoint;
}
} }