2023-04-03 11:37:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-05-05 10:08:38 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-06-15 11:28:16 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2025-10-03 14:39:57 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2023-05-05 10:08:38 +00:00
|
|
|
|
2023-06-15 11:28:16 +00:00
|
|
|
class LocalPersistentVolume extends Model
|
2023-04-03 11:37:53 +00:00
|
|
|
{
|
2023-08-07 20:14:21 +00:00
|
|
|
protected $guarded = [];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-04-03 11:37:53 +00:00
|
|
|
public function application()
|
|
|
|
|
{
|
2023-09-25 15:14:19 +00:00
|
|
|
return $this->morphTo('resource');
|
2023-04-03 11:37:53 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-20 13:42:41 +00:00
|
|
|
public function service()
|
|
|
|
|
{
|
2023-09-25 15:14:19 +00:00
|
|
|
return $this->morphTo('resource');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-25 15:14:19 +00:00
|
|
|
public function database()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo('resource');
|
2023-09-20 13:42:41 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-08-19 10:14:48 +00:00
|
|
|
protected function customizeName($value)
|
2023-05-05 10:08:38 +00:00
|
|
|
{
|
2025-08-19 10:14:48 +00:00
|
|
|
return str($value)->trim()->value;
|
2023-05-05 10:08:38 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-05 10:08:38 +00:00
|
|
|
protected function mountPath(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2024-06-25 08:37:10 +00:00
|
|
|
set: fn (string $value) => str($value)->trim()->start('/')->value
|
2023-05-05 10:08:38 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-05 10:08:38 +00:00
|
|
|
protected function hostPath(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2024-06-10 20:43:34 +00:00
|
|
|
set: function (?string $value) {
|
2023-05-05 10:08:38 +00:00
|
|
|
if ($value) {
|
2024-06-25 08:37:10 +00:00
|
|
|
return str($value)->trim()->start('/')->value;
|
2025-01-07 14:31:43 +00:00
|
|
|
} else {
|
|
|
|
|
return $value;
|
2023-05-05 10:08:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-10-03 14:39:57 +00:00
|
|
|
|
|
|
|
|
// Check if this volume is read-only by parsing the docker-compose content
|
|
|
|
|
public function isReadOnlyVolume(): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
// Get the resource (can be application, service, or database)
|
|
|
|
|
$resource = $this->resource;
|
|
|
|
|
if (! $resource) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only check for services
|
|
|
|
|
if (! method_exists($resource, 'service')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$actualService = $resource->service;
|
|
|
|
|
if (! $actualService || ! $actualService->docker_compose_raw) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the docker-compose content
|
|
|
|
|
$compose = Yaml::parse($actualService->docker_compose_raw);
|
|
|
|
|
if (! isset($compose['services'])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the service that this volume belongs to
|
|
|
|
|
$serviceName = $resource->name;
|
|
|
|
|
if (! isset($compose['services'][$serviceName]['volumes'])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$volumes = $compose['services'][$serviceName]['volumes'];
|
|
|
|
|
|
|
|
|
|
// Check each volume to find a match
|
|
|
|
|
foreach ($volumes as $volume) {
|
|
|
|
|
// Volume can be string like "host:container:ro" or "host:container"
|
|
|
|
|
if (is_string($volume)) {
|
|
|
|
|
$parts = explode(':', $volume);
|
|
|
|
|
|
|
|
|
|
// Check if this volume matches our mount_path
|
|
|
|
|
if (count($parts) >= 2) {
|
|
|
|
|
$containerPath = $parts[1];
|
|
|
|
|
$options = $parts[2] ?? null;
|
|
|
|
|
|
|
|
|
|
// Match based on mount_path
|
|
|
|
|
// Remove leading slash from mount_path if present for comparison
|
|
|
|
|
$mountPath = str($this->mount_path)->ltrim('/')->toString();
|
|
|
|
|
$containerPathClean = str($containerPath)->ltrim('/')->toString();
|
|
|
|
|
|
|
|
|
|
if ($mountPath === $containerPathClean || $this->mount_path === $containerPath) {
|
|
|
|
|
return $options === 'ro';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
ray($e->getMessage(), 'Error checking read-only persistent volume');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-03 11:37:53 +00:00
|
|
|
}
|