2023-09-20 13:42:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-09-27 13:48:19 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-09-20 13:42:41 +00:00
|
|
|
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 ServiceApplication extends BaseModel
|
|
|
|
|
{
|
2023-12-13 11:08:12 +00:00
|
|
|
use HasFactory, SoftDeletes;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
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) {
|
2024-01-30 08:48:51 +00:00
|
|
|
$service->update(['fqdn' => null]);
|
2023-11-06 17:04:18 +00:00
|
|
|
$service->persistentStorages()->delete();
|
|
|
|
|
$service->fileStorages()->delete();
|
|
|
|
|
});
|
2024-11-03 08:02:14 +00:00
|
|
|
static::saving(function ($service) {
|
|
|
|
|
if ($service->isDirty('status')) {
|
|
|
|
|
$service->forceFill(['last_online_at' => now()]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-11-06 17:04:18 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-25 10:33:38 +00:00
|
|
|
public function restart()
|
|
|
|
|
{
|
2024-09-23 17:51:31 +00:00
|
|
|
$container_id = $this->name.'-'.$this->service->uuid;
|
2024-03-25 10:33:38 +00:00
|
|
|
instant_remote_process(["docker restart {$container_id}"], $this->service->server);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-01 09:39:10 +00:00
|
|
|
public static function ownedByCurrentTeamAPI(int $teamId)
|
|
|
|
|
{
|
|
|
|
|
return ServiceApplication::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 10:09:38 +00:00
|
|
|
public static function ownedByCurrentTeam()
|
|
|
|
|
{
|
|
|
|
|
return ServiceApplication::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 13:35:44 +00:00
|
|
|
public function isRunning()
|
|
|
|
|
{
|
|
|
|
|
return str($this->status)->contains('running');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isExited()
|
|
|
|
|
{
|
|
|
|
|
return str($this->status)->contains('exited');
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 10:01:14 +00:00
|
|
|
public function isLogDrainEnabled()
|
2023-11-17 19:08:21 +00:00
|
|
|
{
|
|
|
|
|
return data_get($this, 'is_log_drain_enabled', false);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-04 10:01:14 +00:00
|
|
|
public function isStripprefixEnabled()
|
2024-03-04 09:46:13 +00:00
|
|
|
{
|
|
|
|
|
return data_get($this, 'is_stripprefix_enabled', true);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-04 10:01:14 +00:00
|
|
|
public function isGzipEnabled()
|
2024-02-15 19:44:01 +00:00
|
|
|
{
|
|
|
|
|
return data_get($this, 'is_gzip_enabled', true);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public function type()
|
|
|
|
|
{
|
|
|
|
|
return 'service';
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-28 12:49:03 +00: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 17:51:31 +00:00
|
|
|
return service_configuration_dir()."/{$this->service->uuid}";
|
2024-05-24 15:26:05 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-13 10:09:21 +00: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 20:43:34 +00:00
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 10:08:51 +00:00
|
|
|
public function service()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 09:23:49 +00:00
|
|
|
public function persistentStorages()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(LocalPersistentVolume::class, 'resource');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-22 19:31:47 +00:00
|
|
|
public function fileStorages()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(LocalFileVolume::class, 'resource');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-27 13:48:19 +00:00
|
|
|
public function fqdns(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn () => is_null($this->fqdn)
|
|
|
|
|
? []
|
|
|
|
|
: explode(',', $this->fqdn),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-11-06 13:30:39 +00:00
|
|
|
/**
|
|
|
|
|
* Extract port number from a given FQDN URL.
|
|
|
|
|
* Returns null if no port is specified.
|
|
|
|
|
*/
|
|
|
|
|
public static function extractPortFromUrl(string $url): ?int
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
// Ensure URL has a scheme for proper parsing
|
|
|
|
|
if (! str_starts_with($url, 'http://') && ! str_starts_with($url, 'https://')) {
|
|
|
|
|
$url = 'http://'.$url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parsed = parse_url($url);
|
|
|
|
|
$port = $parsed['port'] ?? null;
|
|
|
|
|
|
|
|
|
|
return $port ? (int) $port : null;
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if all FQDNs have a port specified.
|
|
|
|
|
*/
|
|
|
|
|
public function allFqdnsHavePort(): bool
|
|
|
|
|
{
|
|
|
|
|
if (is_null($this->fqdn) || $this->fqdn === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fqdns = explode(',', $this->fqdn);
|
|
|
|
|
|
|
|
|
|
foreach ($fqdns as $fqdn) {
|
|
|
|
|
$fqdn = trim($fqdn);
|
|
|
|
|
if (empty($fqdn)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$port = self::extractPortFromUrl($fqdn);
|
|
|
|
|
if ($port === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 16:02:32 +00:00
|
|
|
public function getFilesFromServer(bool $isInit = false)
|
2023-09-25 10:49:55 +00:00
|
|
|
{
|
2023-10-02 16:02:32 +00:00
|
|
|
getFilesystemVolumesFromServer($this, $isInit);
|
2023-09-25 10:49:55 +00:00
|
|
|
}
|
2024-10-03 18:47:02 +00:00
|
|
|
|
|
|
|
|
public function isBackupSolutionAvailable()
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-20 13:42:41 +00:00
|
|
|
}
|