coolify/app/Models/Server.php

160 lines
4.5 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;
2023-06-20 20:19:31 +02:00
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
2023-05-03 07:23:45 +02:00
class Server extends BaseModel
{
2023-06-20 20:19:31 +02:00
use SchemalessAttributesTrait;
2023-08-23 10:14:39 +02:00
protected static function booted()
{
static::created(function ($server) {
ServerSetting::create([
'server_id' => $server->id,
]);
2023-08-23 16:40:59 +02:00
if ($server->id === 0) {
StandaloneDocker::create([
'id' => 0,
'name' => 'coolify',
'network' => 'coolify',
'server_id' => $server->id,
]);
} else {
StandaloneDocker::create([
'name' => 'coolify',
'network' => 'coolify',
'server_id' => $server->id,
]);
}
2023-08-23 10:14:39 +02:00
});
static::deleting(function ($server) {
2023-08-29 15:51:30 +02:00
$server->destinations()->each(function ($destination) {
$destination->delete();
});
2023-08-23 10:14:39 +02:00
$server->settings()->delete();
});
}
public $casts = [
'proxy' => SchemalessAttributes::class,
];
2023-06-20 20:19:31 +02:00
protected $schemalessAttributes = [
'proxy',
];
2023-08-23 10:14:39 +02:00
protected $guarded = [];
static public function isReachable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
}
static public function ownedByCurrentTeam(array $select = ['*'])
{
2023-08-22 17:44:49 +02:00
$teamId = currentTeam()->id;
$selectArray = collect($select)->concat(['id']);
2023-08-21 10:18:11 +02:00
return Server::whereTeamId($teamId)->with('settings')->select($selectArray->all())->orderBy('name');
}
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);
}
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
2023-06-20 20:19:31 +02:00
public function scopeWithProxy(): Builder
2023-05-30 15:52:17 +02:00
{
2023-06-20 20:19:31 +02:00
return $this->proxy->modelScope();
2023-05-30 15:52:17 +02:00
}
2023-06-15 15:15:27 +02:00
public function isEmpty()
{
2023-08-29 15:51:30 +02:00
$applications = $this->applications()->count() === 0;
$databases = $this->databases()->count() === 0;
if ($applications && $databases) {
2023-06-15 15:15:27 +02:00
return true;
}
return false;
}
2023-09-11 22:29:34 +02:00
public function databases()
{
2023-08-29 15:51:30 +02:00
return $this->destinations()->map(function ($standaloneDocker) {
$postgresqls = $standaloneDocker->postgresqls;
return $postgresqls?->concat([]) ?? collect([]);
})->flatten();
}
2023-06-15 15:15:27 +02:00
public function applications()
{
return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications;
})->flatten();
}
2023-06-13 10:02:58 +02: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-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-06-16 13:13:09 +02:00
public function muxFilename()
{
return "{$this->ip}_{$this->port}_{$this->user}";
}
2023-07-07 21:35:29 +02:00
public function team()
{
return $this->belongsTo(Team::class);
}
2023-09-11 22:29:34 +02:00
public function isProxyShouldRun()
{
$shouldRun = false;
foreach ($this->applications() as $application) {
if (data_get($application, 'fqdn')) {
$shouldRun = true;
break;
}
}
if ($this->id === 0) {
$settings = InstanceSettings::get();
if (data_get($settings, 'fqdn')) {
$shouldRun = true;
}
}
return $shouldRun;
}
2023-09-12 13:14:01 +02:00
public function isFunctional() {
return $this->settings->is_reachable && $this->settings->is_usable;
}
2023-07-14 13:38:24 +02:00
}