coolify/app/Models/StandaloneDocker.php

121 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2023-03-27 12:31:42 +00:00
<?php
namespace App\Models;
use App\Jobs\ConnectProxyToNetworksJob;
use App\Traits\HasSafeStringAttribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-27 12:31:42 +00:00
class StandaloneDocker extends BaseModel
{
use HasFactory;
use HasSafeStringAttribute;
2023-08-11 14:13:53 +00:00
protected $guarded = [];
2025-01-12 16:26:28 +00:00
protected static function boot()
{
parent::boot();
static::created(function ($newStandaloneDocker) {
$server = $newStandaloneDocker->server;
instant_remote_process([
"docker network inspect $newStandaloneDocker->network >/dev/null 2>&1 || docker network create --driver overlay --attachable $newStandaloneDocker->network >/dev/null",
], $server, false);
ConnectProxyToNetworksJob::dispatchSync($server);
2025-01-12 16:26:28 +00:00
});
}
2023-03-27 12:31:42 +00:00
public function applications()
{
return $this->morphMany(Application::class, 'destination');
}
2023-08-07 16:46:40 +00:00
public function postgresqls()
2023-05-16 09:02:51 +00:00
{
2023-08-07 20:14:21 +00:00
return $this->morphMany(StandalonePostgresql::class, 'destination');
2023-05-16 09:02:51 +00:00
}
2023-10-19 11:32:03 +00:00
2023-10-12 15:18:33 +00:00
public function redis()
{
return $this->morphMany(StandaloneRedis::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2023-10-19 11:32:03 +00:00
public function mongodbs()
{
return $this->morphMany(StandaloneMongodb::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
public function mysqls()
{
return $this->morphMany(StandaloneMysql::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
public function mariadbs()
{
return $this->morphMany(StandaloneMariadb::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function keydbs()
{
return $this->morphMany(StandaloneKeydb::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function dragonflies()
{
return $this->morphMany(StandaloneDragonfly::class, 'destination');
}
2024-06-10 20:43:34 +00:00
2024-04-10 13:00:46 +00:00
public function clickhouses()
{
return $this->morphMany(StandaloneClickhouse::class, 'destination');
}
2023-03-28 13:47:37 +00:00
public function server()
{
return $this->belongsTo(Server::class);
}
/**
* Get the server attribute using identity map caching.
* This intercepts lazy-loading to use cached Server lookups.
*/
public function getServerAttribute(): ?Server
{
// Use eager loaded data if available
if ($this->relationLoaded('server')) {
return $this->getRelation('server');
}
// Use identity map for lazy loading
$server = Server::findCached($this->server_id);
// Cache in relation for future access on this instance
if ($server) {
$this->setRelation('server', $server);
}
return $server;
}
2023-09-21 15:48:31 +00:00
public function services()
2023-09-20 13:42:41 +00:00
{
2023-09-21 15:48:31 +00:00
return $this->morphMany(Service::class, 'destination');
2023-09-20 13:42:41 +00:00
}
2023-10-24 12:31:28 +00:00
public function databases()
{
$postgresqls = $this->postgresqls;
$redis = $this->redis;
$mongodbs = $this->mongodbs;
$mysqls = $this->mysqls;
$mariadbs = $this->mariadbs;
2024-06-10 20:43:34 +00:00
2023-10-24 12:31:28 +00:00
return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs);
}
2023-05-16 09:02:51 +00:00
public function attachedTo()
{
return $this->applications?->count() > 0 || $this->databases()->count() > 0;
2023-05-16 09:02:51 +00:00
}
2023-08-07 20:14:21 +00:00
}