coolify/app/Models/Server.php

74 lines
1.7 KiB
PHP
Raw Normal View History

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