coolify/app/Models/ServiceApplication.php

106 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2023-09-20 15:42:41 +02:00
<?php
namespace App\Models;
2023-09-27 15:48:19 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-09-20 15:42:41 +02:00
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 ServiceApplication extends BaseModel
{
2023-12-13 12:08:12 +01:00
use HasFactory, SoftDeletes;
2024-06-10 22:43:34 +02: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->update(['fqdn' => null]);
2023-11-06 18:04:18 +01:00
$service->persistentStorages()->delete();
$service->fileStorages()->delete();
});
}
2024-06-10 22:43:34 +02:00
2024-03-25 11:33:38 +01:00
public function restart()
{
2024-06-10 22:43:34 +02:00
$container_id = $this->name.'-'.$this->service->uuid;
2024-03-25 11:33:38 +01:00
instant_remote_process(["docker restart {$container_id}"], $this->service->server);
}
2024-06-10 22:43:34 +02:00
2024-07-01 11:39:10 +02:00
public static function ownedByCurrentTeamAPI(int $teamId)
{
return ServiceApplication::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
}
public function isLogDrainEnabled()
{
return data_get($this, 'is_log_drain_enabled', false);
}
2024-06-10 22:43:34 +02:00
public function isStripprefixEnabled()
{
return data_get($this, 'is_stripprefix_enabled', true);
}
2024-06-10 22:43:34 +02:00
public function isGzipEnabled()
{
return data_get($this, 'is_gzip_enabled', true);
}
2024-06-10 22:43:34 +02:00
2023-09-22 11:23:49 +02:00
public function type()
{
return 'service';
}
2024-06-10 22:43:34 +02:00
2024-05-28 14:49:03 +02:00
public function team()
{
return data_get($this, 'environment.project.team');
}
2024-06-10 22:43:34 +02:00
public function workdir()
{
return service_configuration_dir()."/{$this->service->uuid}";
2024-05-24 17:26:05 +02:00
}
2024-06-10 22:43:34 +02:00
public function serviceType()
{
$found = str(collect(SPECIFIC_SERVICES)->filter(function ($service) {
return str($this->image)->before(':')->value() === $service;
})->first());
if ($found->isNotEmpty()) {
return $found;
}
2024-06-10 22:43:34 +02:00
return null;
}
2024-06-10 22:43:34 +02:00
2023-09-22 12:08:51 +02:00
public function service()
{
return $this->belongsTo(Service::class);
}
2024-06-10 22:43:34 +02:00
2023-09-22 11:23:49 +02:00
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
2024-06-10 22:43:34 +02:00
2023-09-22 21:31:47 +02:00
public function fileStorages()
{
return $this->morphMany(LocalFileVolume::class, 'resource');
}
2024-06-10 22:43:34 +02:00
2023-09-27 15:48:19 +02:00
public function fqdns(): Attribute
{
return Attribute::make(
get: fn () => is_null($this->fqdn)
? []
: explode(',', $this->fqdn),
);
}
2024-06-10 22:43:34 +02:00
2023-10-02 18:02:32 +02:00
public function getFilesFromServer(bool $isInit = false)
{
2023-10-02 18:02:32 +02:00
getFilesystemVolumesFromServer($this, $isInit);
}
2023-09-20 15:42:41 +02:00
}