2023-03-24 14:47:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
|
|
|
|
|
2023-03-24 14:47:58 +00:00
|
|
|
class Server extends BaseModel
|
|
|
|
{
|
2023-03-30 13:52:19 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($server) {
|
|
|
|
ServerSetting::create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
2023-04-26 13:38:50 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'ip',
|
|
|
|
'user',
|
|
|
|
'port',
|
|
|
|
'team_id',
|
|
|
|
'private_key_id',
|
|
|
|
];
|
2023-05-04 13:45:53 +00:00
|
|
|
|
|
|
|
public $casts = [
|
|
|
|
'extra_attributes' => SchemalessAttributes::class,
|
|
|
|
];
|
|
|
|
|
2023-05-30 13:52:17 +00:00
|
|
|
public function scopeWithExtraAttributes(): Builder
|
|
|
|
{
|
|
|
|
return $this->extra_attributes->modelScope();
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function standaloneDockers()
|
2023-04-25 12:43:35 +00:00
|
|
|
{
|
2023-05-02 10:47:52 +00:00
|
|
|
return $this->hasMany(StandaloneDocker::class);
|
|
|
|
}
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-05-02 10:47:52 +00:00
|
|
|
public function swarmDockers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(SwarmDocker::class);
|
2023-04-25 12:43:35 +00:00
|
|
|
}
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-05-30 13:52:17 +00:00
|
|
|
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-03-27 12:31:42 +00:00
|
|
|
public function privateKey()
|
2023-03-24 14:47:58 +00:00
|
|
|
{
|
2023-03-27 12:31:42 +00:00
|
|
|
return $this->belongsTo(PrivateKey::class);
|
2023-03-24 14:47:58 +00:00
|
|
|
}
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2023-03-30 13:52:19 +00:00
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
return $this->hasOne(ServerSetting::class);
|
|
|
|
}
|
2023-05-08 07:16:50 +00:00
|
|
|
|
|
|
|
static public function validated()
|
|
|
|
{
|
|
|
|
return Server::where('team_id', session('currentTeam')->id)->whereRelation('settings', 'is_validated', true)->get();
|
|
|
|
}
|
2023-05-12 11:10:09 +00:00
|
|
|
static public function destinations(string|null $server_uuid = null)
|
2023-05-11 13:20:02 +00:00
|
|
|
{
|
2023-05-12 09:59:02 +00:00
|
|
|
if ($server_uuid) {
|
|
|
|
$server = Server::where('team_id', session('currentTeam')->id)->where('uuid', $server_uuid)->firstOrFail();
|
|
|
|
$standaloneDocker = collect($server->standaloneDockers->all());
|
|
|
|
$swarmDocker = collect($server->swarmDockers->all());
|
|
|
|
return $standaloneDocker->concat($swarmDocker);
|
|
|
|
} else {
|
|
|
|
$servers = Server::where('team_id', session('currentTeam')->id)->get();
|
|
|
|
$standaloneDocker = $servers->map(function ($server) {
|
|
|
|
return $server->standaloneDockers;
|
|
|
|
})->flatten();
|
|
|
|
$swarmDocker = $servers->map(function ($server) {
|
|
|
|
return $server->swarmDockers;
|
|
|
|
})->flatten();
|
|
|
|
return $standaloneDocker->concat($swarmDocker);
|
|
|
|
}
|
2023-05-11 13:20:02 +00:00
|
|
|
}
|
2023-03-24 14:47:58 +00:00
|
|
|
}
|