lasthourcloud/app/Models/ServiceDatabase.php

90 lines
2.4 KiB
PHP
Raw Normal View History

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