Files
coolify/app/Models/ServiceDatabase.php
T

143 lines
3.5 KiB
PHP
Raw Normal View History

2023-09-20 15:42:41 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-12-13 12:08:12 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
2023-09-20 15:42:41 +02:00
class ServiceDatabase extends BaseModel
{
2023-12-13 12:08:12 +01:00
use HasFactory, SoftDeletes;
2024-06-10 20:43:34 +00:00
2023-09-20 15:42:41 +02:00
protected $guarded = [];
2023-11-06 18:04:18 +01:00
protected static function booted()
{
static::deleting(function ($service) {
$service->persistentStorages()->delete();
$service->fileStorages()->delete();
});
2024-11-03 09:02:14 +01:00
static::saving(function ($service) {
if ($service->isDirty('status')) {
$service->forceFill(['last_online_at' => now()]);
}
});
2023-11-06 18:04:18 +01:00
}
2024-06-10 20:43:34 +00:00
public static function ownedByCurrentTeamAPI(int $teamId)
{
return ServiceDatabase::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
}
public static function ownedByCurrentTeam()
{
return ServiceDatabase::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');
}
2024-03-25 11:33:38 +01:00
public function restart()
{
2024-09-23 19:51:31 +02:00
$container_id = $this->name.'-'.$this->service->uuid;
2024-03-25 11:33:38 +01:00
remote_process(["docker restart {$container_id}"], $this->service->server);
}
2024-06-10 20:43:34 +00:00
2024-09-16 15:35:44 +02:00
public function isRunning()
{
return str($this->status)->contains('running');
}
public function isExited()
{
return str($this->status)->contains('exited');
}
2024-03-04 11:01:14 +01:00
public function isLogDrainEnabled()
2023-11-17 20:08:21 +01:00
{
return data_get($this, 'is_log_drain_enabled', false);
}
2024-06-10 20:43:34 +00:00
2024-03-04 11:01:14 +01:00
public function isStripprefixEnabled()
{
2024-03-04 11:01:14 +01:00
return data_get($this, 'is_stripprefix_enabled', true);
}
2024-06-10 20:43:34 +00:00
2024-03-04 11:01:14 +01:00
public function isGzipEnabled()
{
return data_get($this, 'is_gzip_enabled', true);
}
2024-06-10 20:43:34 +00:00
2023-09-22 11:23:49 +02:00
public function type()
{
return 'service';
}
2024-06-10 20:43:34 +00:00
2023-11-13 15:19:49 +01:00
public function serviceType()
{
return null;
}
2024-06-10 20:43:34 +00:00
2023-11-07 12:11:47 +01:00
public function databaseType()
{
$image = str($this->image)->before(':');
if ($image->value() === 'postgres') {
$image = 'postgresql';
}
2024-06-10 20:43:34 +00:00
2023-11-07 12:11:47 +01:00
return "standalone-$image";
}
2024-06-10 20:43:34 +00:00
2023-11-13 15:19:49 +01:00
public function getServiceDatabaseUrl()
{
2023-11-09 14:59:38 +01:00
$port = $this->public_port;
2023-11-09 15:05:42 +01:00
$realIp = $this->service->server->ip;
2023-11-16 14:29:01 +01:00
if ($this->service->server->isLocalhost() || isDev()) {
2023-11-09 15:05:42 +01:00
$realIp = base_ip();
}
2024-06-10 20:43:34 +00:00
2024-03-25 12:13:43 +01:00
return "{$realIp}:{$port}";
2023-11-09 14:59:38 +01:00
}
2024-06-10 20:43:34 +00:00
2024-05-28 14:49:03 +02:00
public function team()
{
return data_get($this, 'environment.project.team');
}
2024-06-10 20:43:34 +00:00
public function workdir()
{
2024-09-23 19:51:31 +02:00
return service_configuration_dir()."/{$this->service->uuid}";
2024-05-24 17:26:05 +02:00
}
2024-06-10 20:43:34 +00:00
2023-09-22 12:08:51 +02:00
public function service()
{
return $this->belongsTo(Service::class);
}
2024-06-10 20:43:34 +00:00
2023-09-22 11:23:49 +02:00
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
2024-06-10 20:43:34 +00:00
2023-09-25 12:49:55 +02:00
public function fileStorages()
{
return $this->morphMany(LocalFileVolume::class, 'resource');
}
2024-06-10 20:43:34 +00:00
2023-10-02 18:02:32 +02:00
public function getFilesFromServer(bool $isInit = false)
2023-09-25 12:49:55 +02:00
{
2023-10-02 18:02:32 +02:00
getFilesystemVolumesFromServer($this, $isInit);
2023-09-25 12:49:55 +02:00
}
2024-06-10 20:43:34 +00:00
2023-11-07 12:11:47 +01:00
public function scheduledBackups()
{
return $this->morphMany(ScheduledDatabaseBackup::class, 'database');
}
public function isBackupSolutionAvailable()
{
return str($this->databaseType())->contains('mysql') ||
str($this->databaseType())->contains('postgres') ||
2024-10-03 20:47:22 +02:00
str($this->databaseType())->contains('postgis') ||
str($this->databaseType())->contains('mariadb') ||
str($this->databaseType())->contains('mongodb');
}
2023-09-20 15:42:41 +02:00
}