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-06-20 18:19:31 +00:00
|
|
|
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2023-03-24 14:47:58 +00:00
|
|
|
class Server extends BaseModel
|
|
|
|
{
|
2023-06-20 18:19:31 +00:00
|
|
|
use SchemalessAttributesTrait;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-23 08:14:39 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($server) {
|
|
|
|
ServerSetting::create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
StandaloneDocker::create([
|
|
|
|
'name' => 'coolify',
|
|
|
|
'network' => 'coolify',
|
|
|
|
'server_id' => $server->id,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
static::deleting(function ($server) {
|
|
|
|
$server->settings()->delete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public $casts = [
|
|
|
|
'proxy' => SchemalessAttributes::class,
|
|
|
|
];
|
2023-06-20 18:19:31 +00:00
|
|
|
protected $schemalessAttributes = [
|
|
|
|
'proxy',
|
|
|
|
];
|
2023-08-23 08:14:39 +00:00
|
|
|
protected $guarded = [];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
static public function isReachable()
|
|
|
|
{
|
|
|
|
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function ownedByCurrentTeam(array $select = ['*'])
|
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
$teamId = currentTeam()->id;
|
2023-08-08 09:51:36 +00:00
|
|
|
$selectArray = collect($select)->concat(['id']);
|
2023-08-21 08:18:11 +00:00
|
|
|
return Server::whereTeamId($teamId)->with('settings')->select($selectArray->all())->orderBy('name');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static public function isUsable()
|
|
|
|
{
|
|
|
|
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function destinationsByServer(string $server_id)
|
|
|
|
{
|
|
|
|
$server = Server::ownedByCurrentTeam()->get()->where('id', $server_id)->firstOrFail();
|
|
|
|
$standaloneDocker = collect($server->standaloneDockers->all());
|
|
|
|
$swarmDocker = collect($server->swarmDockers->all());
|
|
|
|
return $standaloneDocker->concat($swarmDocker);
|
|
|
|
}
|
|
|
|
|
2023-08-23 08:14:39 +00:00
|
|
|
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
return $this->hasOne(ServerSetting::class);
|
|
|
|
}
|
2023-06-20 18:19:31 +00:00
|
|
|
|
|
|
|
public function scopeWithProxy(): Builder
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
2023-06-20 18:19:31 +00:00
|
|
|
return $this->proxy->modelScope();
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-15 13:15:27 +00:00
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
if ($this->applications()->count() === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-15 13:15:27 +00:00
|
|
|
public function applications()
|
|
|
|
{
|
|
|
|
return $this->destinations()->map(function ($standaloneDocker) {
|
|
|
|
return $standaloneDocker->applications;
|
|
|
|
})->flatten();
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-13 08:02:58 +00:00
|
|
|
public function destinations()
|
|
|
|
{
|
|
|
|
$standalone_docker = $this->hasMany(StandaloneDocker::class)->get();
|
|
|
|
$swarm_docker = $this->hasMany(SwarmDocker::class)->get();
|
|
|
|
return $standalone_docker->concat($swarm_docker);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
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-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-06-16 11:13:09 +00:00
|
|
|
public function muxFilename()
|
|
|
|
{
|
|
|
|
return "{$this->ip}_{$this->port}_{$this->user}";
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-07-07 19:35:29 +00:00
|
|
|
public function team()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Team::class);
|
|
|
|
}
|
2023-07-14 11:38:24 +00:00
|
|
|
}
|