lasthourcloud/app/Models/Service.php

518 lines
26 KiB
PHP
Raw Normal View History

2023-09-20 13:42:41 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
2023-09-22 19:31:47 +00:00
use Spatie\Url\Url;
2023-09-20 13:42:41 +00:00
class Service extends BaseModel
{
use HasFactory;
protected $guarded = [];
2023-09-22 09:23:49 +00:00
protected static function booted()
{
static::deleted(function ($service) {
$storagesToDelete = collect([]);
foreach ($service->applications()->get() as $application) {
$storages = $application->persistentStorages()->get();
foreach ($storages as $storage) {
$storagesToDelete->push($storage);
}
2023-09-22 09:23:49 +00:00
$application->persistentStorages()->delete();
}
foreach ($service->databases()->get() as $database) {
$storages = $database->persistentStorages()->get();
foreach ($storages as $storage) {
$storagesToDelete->push($storage);
}
2023-09-22 09:23:49 +00:00
$database->persistentStorages()->delete();
}
$service->environment_variables()->delete();
$service->applications()->delete();
$service->databases()->delete();
if ($storagesToDelete->count() > 0) {
$storagesToDelete->each(function ($storage) use ($service) {
instant_remote_process(["docker volume rm -f $storage->name"], $service->server, false);
});
}
2023-09-22 09:23:49 +00:00
});
2023-09-20 13:42:41 +00:00
}
2023-09-22 09:23:49 +00:00
public function type()
2023-09-20 13:42:41 +00:00
{
2023-09-22 09:23:49 +00:00
return 'service';
2023-09-20 13:42:41 +00:00
}
2023-09-22 09:23:49 +00:00
2023-09-20 13:42:41 +00:00
public function applications()
{
return $this->hasMany(ServiceApplication::class);
}
public function databases()
{
return $this->hasMany(ServiceDatabase::class);
}
2023-09-21 15:48:31 +00:00
public function environment()
{
return $this->belongsTo(Environment::class);
}
2023-09-22 09:23:49 +00:00
public function server()
{
2023-09-21 15:48:31 +00:00
return $this->belongsTo(Server::class);
}
public function byName(string $name)
{
$app = $this->applications()->whereName($name)->first();
if ($app) {
return $app;
}
$db = $this->databases()->whereName($name)->first();
if ($db) {
return $db;
}
return null;
}
2023-09-20 13:42:41 +00:00
public function environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->orderBy('key', 'asc');
}
public function workdir() {
return service_configuration_dir() . "/{$this->uuid}";
}
public function saveComposeConfigs()
{
$workdir = $this->workdir();
$commands[] = "mkdir -p $workdir";
$commands[] = "cd $workdir";
$docker_compose_base64 = base64_encode($this->docker_compose);
$commands[] = "echo $docker_compose_base64 | base64 -d > docker-compose.yml";
$envs = $this->environment_variables()->get();
$commands[] = "rm -f .env || true";
foreach ($envs as $env) {
$commands[] = "echo '{$env->key}={$env->value}' >> .env";
}
instant_remote_process($commands, $this->server);
}
2023-09-21 15:48:31 +00:00
public function parse(bool $isNew = false): Collection
2023-09-20 13:42:41 +00:00
{
ray('parsing');
2023-09-21 15:48:31 +00:00
// ray()->clearAll();
2023-09-20 13:42:41 +00:00
if ($this->docker_compose_raw) {
2023-09-22 19:31:47 +00:00
try {
$yaml = Yaml::parse($this->docker_compose_raw);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
2023-09-20 13:42:41 +00:00
$composeVolumes = collect(data_get($yaml, 'volumes', []));
$composeNetworks = collect(data_get($yaml, 'networks', []));
2023-09-21 15:48:31 +00:00
$dockerComposeVersion = data_get($yaml, 'version') ?? '3.8';
2023-09-20 13:42:41 +00:00
$services = data_get($yaml, 'services');
2023-09-21 15:48:31 +00:00
$definedNetwork = $this->uuid;
2023-09-20 13:42:41 +00:00
$volumes = collect([]);
$envs = collect([]);
$ports = collect([]);
2023-09-21 15:48:31 +00:00
$services = collect($services)->map(function ($service, $serviceName) use ($composeVolumes, $composeNetworks, $definedNetwork, $envs, $volumes, $ports, $isNew) {
$container_name = "$serviceName-{$this->uuid}";
2023-09-20 13:42:41 +00:00
$isDatabase = false;
2023-09-22 10:08:51 +00:00
$serviceVariables = collect(data_get($service, 'environment', []));
2023-09-20 13:42:41 +00:00
// Decide if the service is a database
$image = data_get($service, 'image');
if ($image) {
$imageName = Str::of($image)->before(':');
if (collect(DATABASE_DOCKER_IMAGES)->contains($imageName)) {
$isDatabase = true;
data_set($service, 'is_database', true);
}
}
2023-09-21 15:48:31 +00:00
if ($isNew) {
2023-09-20 13:42:41 +00:00
if ($isDatabase) {
$savedService = ServiceDatabase::create([
'name' => $serviceName,
'service_id' => $this->id
]);
} else {
2023-09-22 18:39:56 +00:00
$defaultUsableFqdn = "http://$serviceName-{$this->uuid}.{$this->server->ip}.sslip.io";
if (isDev()) {
$defaultUsableFqdn = "http://$serviceName-{$this->uuid}.127.0.0.1.sslip.io";
2023-09-21 15:48:31 +00:00
}
2023-09-20 13:42:41 +00:00
$savedService = ServiceApplication::create([
'name' => $serviceName,
2023-09-21 15:48:31 +00:00
'fqdn' => $defaultUsableFqdn,
2023-09-20 13:42:41 +00:00
'service_id' => $this->id
]);
}
2023-09-21 15:48:31 +00:00
} else {
if ($isDatabase) {
$savedService = $this->databases()->whereName($serviceName)->first();
} else {
$savedService = $this->applications()->whereName($serviceName)->first();
if (data_get($savedService, 'fqdn')) {
$defaultUsableFqdn = data_get($savedService, 'fqdn', null);
2023-09-22 10:08:51 +00:00
} else {
if (Str::of($serviceVariables)->contains('SERVICE_FQDN') || Str::of($serviceVariables)->contains('SERVICE_URL')) {
$defaultUsableFqdn = "http://$serviceName-{$this->uuid}.{$this->server->ip}.sslip.io";
if (isDev()) {
$defaultUsableFqdn = "http://$serviceName-{$this->uuid}.127.0.0.1.sslip.io";
}
}
2023-09-22 10:08:51 +00:00
}
$savedService->fqdn = $defaultUsableFqdn ?? null;
2023-09-22 10:08:51 +00:00
$savedService->save();
2023-09-21 15:48:31 +00:00
}
2023-09-20 13:42:41 +00:00
}
$fqdns = data_get($savedService, 'fqdn');
if ($fqdns) {
$fqdns = collect(Str::of($fqdns)->explode(','));
}
2023-09-20 13:42:41 +00:00
// Collect ports
$servicePorts = collect(data_get($service, 'ports', []));
$ports->put($serviceName, $servicePorts);
$collectedPorts = collect([]);
if ($servicePorts->count() > 0) {
foreach ($servicePorts as $sport) {
if (is_string($sport) || is_numeric($sport)) {
$collectedPorts->push($sport);
}
if (is_array($sport)) {
$target = data_get($sport, 'target');
$published = data_get($sport, 'published');
$collectedPorts->push("$target:$published");
2023-09-21 15:48:31 +00:00
}
}
2023-09-20 13:42:41 +00:00
}
$savedService->ports = $collectedPorts->implode(',');
$savedService->save();
2023-09-20 13:42:41 +00:00
// Collect volumes
$serviceVolumes = collect(data_get($service, 'volumes', []));
if ($serviceVolumes->count() > 0) {
2023-09-22 10:08:51 +00:00
LocalPersistentVolume::whereResourceId($savedService->id)->whereResourceType(get_class($savedService))->delete();
2023-09-20 13:42:41 +00:00
foreach ($serviceVolumes as $volume) {
if (is_string($volume)) {
if (Str::startsWith($volume, './')) {
$fsPath = Str::before($volume, ':');
$volumePath = Str::of($volume)->after(':')->beforeLast(':');
LocalFileVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
[
'fs_path' => $fsPath,
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]
);
$savedService->saveFileVolumes();
continue;
}
2023-09-20 13:42:41 +00:00
$volumeName = Str::before($volume, ':');
$volumePath = Str::after($volume, ':');
}
if (is_array($volume)) {
$volumeName = data_get($volume, 'source');
$volumePath = data_get($volume, 'target');
$volumeContent = data_get($volume, 'content');
if (Str::startsWith($volumeName, './')) {
$payload = [
'fs_path' => $volumeName,
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
];
if ($volumeContent) {
$payload['content'] = $volumeContent;
}
LocalFileVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
$payload
);
if ($volumeContent) {
$volume = data_forget($volume, 'content');
}
$savedService->saveFileVolumes();
continue;
}
2023-09-20 13:42:41 +00:00
}
$volumeExists = $serviceVolumes->contains(function ($_, $key) use ($volumeName) {
return $key == $volumeName;
});
if (!$volumeExists) {
2023-09-22 09:23:49 +00:00
if (Str::startsWith($volumeName, '/')) {
$volumes->put($volumeName, $volumePath);
LocalPersistentVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
[
'name' => Str::slug($volumeName, '-'),
'mount_path' => $volumePath,
'host_path' => $volumeName,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]
);
} else {
2023-09-20 13:42:41 +00:00
$composeVolumes->put($volumeName, null);
2023-09-22 09:23:49 +00:00
LocalPersistentVolume::updateOrCreate(
[
2023-09-22 10:08:51 +00:00
'name' => $volumeName,
2023-09-22 09:23:49 +00:00
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
[
'name' => $volumeName,
'mount_path' => $volumePath,
'host_path' => null,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
]
);
2023-09-20 13:42:41 +00:00
}
}
}
}
// Collect and add networks
$serviceNetworks = collect(data_get($service, 'networks', []));
if ($serviceNetworks->count() > 0) {
foreach ($serviceNetworks as $networkName => $networkDetails) {
$networkExists = $composeNetworks->contains(function ($value, $key) use ($networkName) {
return $value == $networkName || $key == $networkName;
});
if (!$networkExists) {
2023-09-21 15:48:31 +00:00
$composeNetworks->put($networkDetails, null);
2023-09-20 13:42:41 +00:00
}
}
}
// Add Coolify specific networks
$definedNetworkExists = $composeNetworks->contains(function ($value, $_) use ($definedNetwork) {
return $value == $definedNetwork;
});
if (!$definedNetworkExists) {
$composeNetworks->put($definedNetwork, [
2023-09-21 15:48:31 +00:00
'name' => $definedNetwork,
'external' => false
2023-09-20 13:42:41 +00:00
]);
}
2023-09-21 15:48:31 +00:00
$networks = $serviceNetworks->toArray();
$networks = array_merge($networks, [$definedNetwork]);
data_set($service, 'networks', $networks);
2023-09-20 13:42:41 +00:00
2023-09-21 15:48:31 +00:00
// Get variables from the service
2023-09-20 13:42:41 +00:00
foreach ($serviceVariables as $variable) {
$value = Str::after($variable, '=');
if (!Str::startsWith($value, '$SERVICE_') && !Str::startsWith($value, '${SERVICE_') && Str::startsWith($value, '$')) {
$value = Str::of(replaceVariables(Str::of($value)));
if ($value->contains(':')) {
$nakedName = $value->before(':');
$nakedValue = $value->after(':');
} else if ($value->contains('-')) {
$nakedName = $value->before('-');
$nakedValue = $value->after('-');
} else if ($value->contains('+')) {
$nakedName = $value->before('+');
$nakedValue = $value->after('+');
} else {
$nakedName = $value;
}
if (isset($nakedName)) {
if (isset($nakedValue)) {
if ($nakedValue->startsWith('-')) {
$nakedValue = Str::of($nakedValue)->after('-');
}
if ($nakedValue->startsWith('+')) {
$nakedValue = Str::of($nakedValue)->after('+');
}
if (!$envs->has($nakedName->value())) {
$envs->put($nakedName->value(), $nakedValue->value());
2023-09-22 09:23:49 +00:00
EnvironmentVariable::updateOrCreate([
'key' => $nakedName->value(),
'service_id' => $this->id,
], [
'value' => $nakedValue->value(),
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
2023-09-20 13:42:41 +00:00
}
} else {
if (!$envs->has($nakedName->value())) {
$envs->put($nakedName->value(), null);
2023-09-22 09:23:49 +00:00
$envExists = EnvironmentVariable::where('service_id', $this->id)->where('key', $nakedName->value())->exists();
if (!$envExists) {
2023-09-20 13:42:41 +00:00
EnvironmentVariable::create([
'key' => $nakedName->value(),
'value' => null,
'service_id' => $this->id,
2023-09-22 09:23:49 +00:00
'is_build_time' => false,
2023-09-20 13:42:41 +00:00
'is_preview' => false,
]);
}
}
}
}
} else {
2023-09-21 15:48:31 +00:00
$variableName = Str::of(replaceVariables(Str::of($value)));
2023-09-20 13:42:41 +00:00
$generatedValue = null;
2023-09-21 15:48:31 +00:00
if ($variableName->startsWith('SERVICE_USER')) {
$variableDefined = EnvironmentVariable::whereServiceId($this->id)->where('key', $variableName->value())->first();
if (!$variableDefined) {
$generatedValue = Str::random(10);
} else {
$generatedValue = $variableDefined->value;
}
2023-09-22 09:23:49 +00:00
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::updateOrCreate([
'key' => $variableName->value(),
'service_id' => $this->id,
], [
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
2023-09-20 13:42:41 +00:00
}
2023-09-21 15:48:31 +00:00
} else if ($variableName->startsWith('SERVICE_PASSWORD')) {
$variableDefined = EnvironmentVariable::whereServiceId($this->id)->where('key', $variableName->value())->first();
if (!$variableDefined) {
$generatedValue = Str::password(symbols: false);
} else {
$generatedValue = $variableDefined->value;
}
2023-09-22 09:23:49 +00:00
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::updateOrCreate([
'key' => $variableName->value(),
'service_id' => $this->id,
], [
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
2023-09-20 13:42:41 +00:00
}
} else if ($variableName->startsWith('SERVICE_BASE64')) {
$variableDefined = EnvironmentVariable::whereServiceId($this->id)->where('key', $variableName->value())->first();
$length = Str::of($variableName)->after('SERVICE_BASE64_')->beforeLast('_')->value();
if (is_numeric($length)) {
$length = (int) $length;
} else {
$length = 1;
}
if (!$variableDefined) {
$generatedValue = base64_encode(Str::password(length: $length, symbols: false));
} else {
$generatedValue = $variableDefined->value;
}
if (!$envs->has($variableName->value())) {
$envs->put($variableName->value(), $generatedValue);
EnvironmentVariable::updateOrCreate([
'key' => $variableName->value(),
'service_id' => $this->id,
], [
'value' => $generatedValue,
'is_build_time' => false,
'service_id' => $this->id,
'is_preview' => false,
]);
}
2023-09-21 15:48:31 +00:00
} else if ($variableName->startsWith('SERVICE_FQDN')) {
if ($fqdns) {
$number = Str::of($variableName)->after('SERVICE_FQDN')->afterLast('_')->value();
if (is_numeric($number)) {
$number = (int) $number - 1;
} else {
$number = 0;
}
2023-09-24 15:39:12 +00:00
$fqdn = getFqdnWithoutPort(data_get($fqdns, $number, $fqdns->first()));
2023-09-21 15:48:31 +00:00
$environments = collect(data_get($service, 'environment'));
$environments = $environments->map(function ($envValue) use ($value, $fqdn) {
$envValue = Str::of($envValue)->replace($value, $fqdn);
return $envValue->value();
});
$service['environment'] = $environments->toArray();
}
} else if ($variableName->startsWith('SERVICE_URL')) {
if ($fqdns) {
2023-09-22 19:31:47 +00:00
$number = Str::of($variableName)->after('SERVICE_URL')->afterLast('_')->value();
if (is_numeric($number)) {
$number = (int) $number - 1;
} else {
$number = 0;
}
2023-09-24 15:39:12 +00:00
$fqdn = getFqdnWithoutPort(data_get($fqdns, $number, $fqdns->first()));
2023-09-22 19:31:47 +00:00
$url = Url::fromString($fqdn)->getHost();
2023-09-21 15:48:31 +00:00
$environments = collect(data_get($service, 'environment'));
$environments = $environments->map(function ($envValue) use ($value, $url) {
$envValue = Str::of($envValue)->replace($value, $url);
return $envValue->value();
});
$service['environment'] = $environments->toArray();
}
2023-09-20 13:42:41 +00:00
}
}
}
2023-09-25 07:17:42 +00:00
// Add labels to the service
$labels = collect(data_get($service, 'labels', []));
$labels = collect([]);
$labels = $labels->merge(defaultLabels($this->id, $container_name, type: 'service'));
if (!$isDatabase) {
if ($fqdns) {
$labels = $labels->merge(fqdnLabelsForTraefik($fqdns, $container_name, true));
2023-09-21 15:48:31 +00:00
}
}
2023-09-25 07:17:42 +00:00
data_set($service, 'labels', $labels->toArray());
2023-09-20 13:42:41 +00:00
data_forget($service, 'is_database');
2023-09-21 15:48:31 +00:00
data_set($service, 'restart', RESTART_MODE);
data_set($service, 'container_name', $container_name);
2023-09-20 13:42:41 +00:00
data_forget($service, 'documentation');
data_forget($service, 'volumes.*.content');
2023-09-20 13:42:41 +00:00
return $service;
});
2023-09-21 15:48:31 +00:00
$finalServices = [
'version' => $dockerComposeVersion,
'services' => $services->toArray(),
'volumes' => $composeVolumes->toArray(),
'networks' => $composeNetworks->toArray(),
];
data_forget($yaml, 'services.*.volumes.*.content');
$this->docker_compose_raw = Yaml::dump($yaml, 10, 2);
2023-09-21 15:48:31 +00:00
$this->docker_compose = Yaml::dump($finalServices, 10, 2);
$this->save();
$this->saveComposeConfigs();
2023-09-20 13:42:41 +00:00
$shouldBeDefined = collect([
'envs' => $envs,
'volumes' => $volumes,
'ports' => $ports
]);
$parsedCompose = collect([
2023-09-21 15:48:31 +00:00
'dockerCompose' => $finalServices,
2023-09-20 13:42:41 +00:00
'shouldBeDefined' => $shouldBeDefined
]);
return $parsedCompose;
} else {
return collect([]);
}
}
}