wip: services

This commit is contained in:
Andras Bacsai 2023-09-22 12:08:51 +02:00
parent 9c2fea4b2e
commit c91f426af3
16 changed files with 103 additions and 242 deletions

View File

@ -11,7 +11,7 @@ class StartService
public function handle(Service $service)
{
$workdir = service_configuration_dir() . "/{$service->uuid}";
$commands[] = "echo 'Starting service {$service->name} on {$service->server->name}'";
$commands[] = "echo 'Starting service {$service->name} on {$service->server->name}.'";
$commands[] = "mkdir -p $workdir";
$commands[] = "cd $workdir";
@ -22,8 +22,11 @@ public function handle(Service $service)
foreach ($envs as $env) {
$commands[] = "echo '{$env->key}={$env->value}' >> .env";
}
$commands[] = "echo 'Pulling images and starting containers...'";
$commands[] = "docker compose pull";
$commands[] = "docker compose up -d";
$commands[] = "echo 'Waiting for containers to start...'";
$commands[] = "sleep 5";
$commands[] = "docker network connect $service->uuid coolify-proxy 2>/dev/null || true";
$activity = remote_process($commands, $service->server);
return $activity;

View File

@ -2,20 +2,10 @@
namespace App\Http\Livewire\Project\New;
use App\Models\Application;
use App\Models\EnvironmentVariable;
use App\Models\GithubApp;
use App\Models\LocalPersistentVolume;
use App\Models\Project;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
class DockerCompose extends Component
{
@ -29,7 +19,7 @@ public function mount()
if (isDev()) {
$this->dockercompose = 'services:
ghost:
documentation: https://docs.ghost.org/docs/config
documentation: https://ghost.org/docs/config
image: ghost:5
volumes:
- ghost-content-data:/var/lib/ghost/content

View File

@ -33,6 +33,7 @@ public function render()
public function save() {
$this->service->save();
$this->service->parse();
$this->service->refresh();
$this->emit('refreshEnvs');
}

View File

@ -98,6 +98,8 @@ public function parse(bool $isNew = false): Collection
$services = collect($services)->map(function ($service, $serviceName) use ($composeVolumes, $composeNetworks, $definedNetwork, $envs, $volumes, $ports, $isNew) {
$container_name = "$serviceName-{$this->uuid}";
$isDatabase = false;
$serviceVariables = collect(data_get($service, 'environment', []));
// Decide if the service is a database
$image = data_get($service, 'image');
if ($image) {
@ -114,10 +116,15 @@ public function parse(bool $isNew = false): Collection
'service_id' => $this->id
]);
} 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";
}
} else {
$defaultUsableFqdn = null;
}
$savedService = ServiceApplication::create([
'name' => $serviceName,
'fqdn' => $defaultUsableFqdn,
@ -129,6 +136,16 @@ public function parse(bool $isNew = false): Collection
$savedService = $this->databases()->whereName($serviceName)->first();
} else {
$savedService = $this->applications()->whereName($serviceName)->first();
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";
}
} else {
$defaultUsableFqdn = null;
}
$savedService->fqdn = $defaultUsableFqdn;
$savedService->save();
}
}
$fqdn = data_get($savedService, 'fqdn');
@ -155,6 +172,7 @@ public function parse(bool $isNew = false): Collection
// Collect volumes
$serviceVolumes = collect(data_get($service, 'volumes', []));
if ($serviceVolumes->count() > 0) {
LocalPersistentVolume::whereResourceId($savedService->id)->whereResourceType(get_class($savedService))->delete();
foreach ($serviceVolumes as $volume) {
if (is_string($volume)) {
$volumeName = Str::before($volume, ':');
@ -189,7 +207,7 @@ public function parse(bool $isNew = false): Collection
$composeVolumes->put($volumeName, null);
LocalPersistentVolume::updateOrCreate(
[
'mount_path' => $volumePath,
'name' => $volumeName,
'resource_id' => $savedService->id,
'resource_type' => get_class($savedService)
],
@ -234,7 +252,6 @@ public function parse(bool $isNew = false): Collection
// Get variables from the service
$serviceVariables = collect(data_get($service, 'environment', []));
foreach ($serviceVariables as $variable) {
$value = Str::after($variable, '=');
if (!Str::startsWith($value, '$SERVICE_') && !Str::startsWith($value, '${SERVICE_') && Str::startsWith($value, '$')) {

View File

@ -4,6 +4,7 @@
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Symfony\Component\Yaml\Yaml;
class ServiceApplication extends BaseModel
{
@ -14,6 +15,14 @@ public function type()
{
return 'service';
}
public function documentation()
{
return data_get(Yaml::parse($this->service->docker_compose_raw), "services.{$this->name}.documentation", 'https://coolify.io/docs');
}
public function service()
{
return $this->belongsTo(Service::class);
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Symfony\Component\Yaml\Yaml;
class ServiceDatabase extends BaseModel
{
@ -13,6 +14,14 @@ public function type()
{
return 'service';
}
public function documentation()
{
return data_get(Yaml::parse($this->service->docker_compose_raw), "services.{$this->name}.documentation", 'https://coolify.io/docs');
}
public function service()
{
return $this->belongsTo(Service::class);
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');

View File

@ -0,0 +1,26 @@
<?php
namespace App\View\Components\Services;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class Explanation extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.services.explanation');
}
}

View File

@ -1,200 +1,6 @@
<?php
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\Service;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
# Application generated variables
# SERVICE_FQDN_*: FQDN coming from your application (https://coolify.io)
# SERVICE_URL_*: URL coming from your application (coolify.io)
# SERVICE_USER_*: Generated by your application, username (not encrypted)
# SERVICE_PASSWORD_*: Generated by your application, password (encrypted)
// function generateServiceFromTemplate(Service $service)
// {
// // ray()->clearAll();
// $template = data_get($service, 'docker_compose_raw');
// $network = data_get($service, 'destination.network');
// $yaml = Yaml::parse($template);
// $services = $service->parse();
// $volumes = collect(data_get($yaml, 'volumes', []));
// $composeVolumes = collect([]);
// $env = collect([]);
// $ports = collect([]);
// foreach ($services as $serviceName => $service) {
// $container_name = generateApplicationContainerName($application);
// $domain = data_get($application, "service_configurations.{$serviceName}.fqdn", null);
// if ($domain === '') {
// $domain = null;
// }
// data_forget($service, 'documentation');
// // Some default things
// data_set($service, 'restart', RESTART_MODE);
// data_set($service, 'container_name', $container_name);
// $healthcheck = data_get($service, 'healthcheck');
// if (is_null($healthcheck)) {
// $healthcheck = [
// 'test' => [
// 'CMD-SHELL',
// 'exit 0'
// ],
// 'interval' => $application->health_check_interval . 's',
// 'timeout' => $application->health_check_timeout . 's',
// 'retries' => $application->health_check_retries,
// 'start_period' => $application->health_check_start_period . 's'
// ];
// data_set($service, 'healthcheck', $healthcheck);
// }
// // Labels
// $server = data_get($application, 'destination.server');
// if ($server->proxyType() === ProxyTypes::TRAEFIK_V2->value) {
// $labels = collect(data_get($service, 'labels', []));
// $labels = collect([]);
// $labels = $labels->merge(defaultLabels($application->id, $container_name));
// if (!data_get($service, 'is_database')) {
// if ($domain) {
// $labels = $labels->merge(fqdnLabelsForTraefik($domain, $container_name, $application->settings->is_force_https_enabled));
// }
// }
// data_set($service, 'labels', $labels->toArray());
// }
// data_forget($service, 'is_database');
// // Add volumes to the volumes collection if they don't already exist
// $serviceVolumes = collect(data_get($service, 'volumes', []));
// if ($serviceVolumes->count() > 0) {
// foreach ($serviceVolumes as $volume) {
// $volumeName = Str::before($volume, ':');
// $volumePath = Str::after($volume, ':');
// if (Str::startsWith($volumeName, '/')) {
// continue;
// }
// $volumeExists = $volumes->contains(function ($_, $key) use ($volumeName) {
// return $key == $volumeName;
// });
// if ($volumeExists) {
// ray('Volume already exists');
// } else {
// $composeVolumes->put($volumeName, null);
// $volumes->put($volumeName, $volumePath);
// }
// }
// }
// // Add networks to the networks collection if they don't already exist
// $serviceNetworks = collect(data_get($service, 'networks', []));
// $networkExists = $serviceNetworks->contains(function ($_, $key) use ($network) {
// return $key == $network;
// });
// if (is_null($networkExists) || !$networkExists) {
// $serviceNetworks->push($network);
// }
// data_set($service, 'networks', $serviceNetworks->toArray());
// data_set($yaml, "services.{$serviceName}", $service);
// // Get variables from the service that does not start with SERVICE_*
// $serviceVariables = collect(data_get($service, 'environment', []));
// foreach ($serviceVariables as $variable) {
// // $key = Str::before($variable, '=');
// $value = Str::after($variable, '=');
// if (!Str::startsWith($value, '$SERVICE_') && !Str::startsWith($value, '${SERVICE_') && Str::startsWith($value, '$')) {
// if (Str::of($value)->contains(':')) {
// $nakedName = replaceVariables(Str::of($value)->before(':'));
// $nakedValue = replaceVariables(Str::of($value)->after(':'));
// }
// if (Str::of($value)->contains('-')) {
// $nakedName = replaceVariables(Str::of($value)->before('-'));
// $nakedValue = replaceVariables(Str::of($value)->after('-'));
// }
// if (Str::of($value)->contains('+')) {
// $nakedName = replaceVariables(Str::of($value)->before('+'));
// $nakedValue = replaceVariables(Str::of($value)->after('+'));
// }
// if ($nakedValue->startsWith('-')) {
// $nakedValue = Str::of($nakedValue)->after('-');
// }
// if ($nakedValue->startsWith('+')) {
// $nakedValue = Str::of($nakedValue)->after('+');
// }
// if (!$env->contains("{$nakedName->value()}={$nakedValue->value()}")) {
// $env->push("$nakedName=$nakedValue");
// }
// }
// }
// // Get ports from the service
// $servicePorts = collect(data_get($service, 'ports', []));
// foreach ($servicePorts as $port) {
// $port = Str::of($port)->before(':');
// $ports->push($port);
// }
// }
// data_set($yaml, 'networks', [
// $network => [
// 'name' => $network
// ],
// ]);
// data_set($yaml, 'volumes', $composeVolumes->toArray());
// $compose = Str::of(Yaml::dump($yaml, 10, 2));
// // Replace SERVICE_FQDN_* with the actual FQDN
// preg_match_all(collectRegex('SERVICE_FQDN_'), $compose, $fqdns);
// $fqdns = collect($fqdns)->flatten()->unique()->values();
// $generatedFqdns = collect([]);
// foreach ($fqdns as $fqdn) {
// $generatedFqdns->put("$fqdn", data_get($application, 'fqdn'));
// }
// // Replace SERVICE_URL_*
// preg_match_all(collectRegex('SERVICE_URL_'), $compose, $urls);
// $urls = collect($urls)->flatten()->unique()->values();
// $generatedUrls = collect([]);
// foreach ($urls as $url) {
// $generatedUrls->put("$url", data_get($application, 'url'));
// }
// // Generate SERVICE_USER_*
// preg_match_all(collectRegex('SERVICE_USER_'), $compose, $users);
// $users = collect($users)->flatten()->unique()->values();
// $generatedUsers = collect([]);
// foreach ($users as $user) {
// $generatedUsers->put("$user", Str::random(10));
// }
// // Generate SERVICE_PASSWORD_*
// preg_match_all(collectRegex('SERVICE_PASSWORD_'), $compose, $passwords);
// $passwords = collect($passwords)->flatten()->unique()->values();
// $generatedPasswords = collect([]);
// foreach ($passwords as $password) {
// $generatedPasswords->put("$password", Str::password(symbols: false));
// }
// // Save .env file
// foreach ($generatedFqdns as $key => $value) {
// $env->push("$key=$value");
// }
// foreach ($generatedUrls as $key => $value) {
// $env->push("$key=$value");
// }
// foreach ($generatedUsers as $key => $value) {
// $env->push("$key=$value");
// }
// foreach ($generatedPasswords as $key => $value) {
// $env->push("$key=$value");
// }
// return [
// 'dockercompose' => $compose,
// 'yaml' => Yaml::parse($compose),
// 'envs' => $env,
// 'volumes' => $volumes,
// 'ports' => $ports->values(),
// ];
// }
function replaceRegex(?string $name = null)
{

View File

@ -1,6 +1,6 @@
services:
ghost:
documentation: https://docs.ghost.org/docs/config
documentation: https://ghost.org/docs/config
image: ghost:5
volumes:
- ghost-content-data:/var/lib/ghost/content

View File

@ -0,0 +1,8 @@
<pre class="py-2 pb-4">
# You can use these variables in your Docker Compose file and Coolify will generate default values or replace them with the values you set on the UI forms.
#
# SERVICE_FQDN_*: FQDN - could be changable from the UI. (example: SERVICE_FQDN_GHOST)
# SERVICE_URL_*: URL parsed from FQDN - could be changable from the UI. (example: SERVICE_URL_GHOST)
# SERVICE_USER_*: Generated user, not encrypted in database (example: SERVICE_USER_MYSQL)
# SERVICE_PASSWORD_*: Generated password, encrypted in database (example: SERVICE_PASSWORD_MYSQL)
</pre>

View File

@ -4,22 +4,13 @@
<form wire:submit.prevent="submit">
<div class="flex gap-2 pb-1">
<h2>Docker Compose</h2>
<x-forms.button type="submit">Save</x-forms.button>
</div>
<pre>
# Application generated variables
# You can use these variables in your docker-compose.yml file and Coolify will create default values or replace them with the values you set in the application creation form.
# SERVICE_FQDN_*: FQDN coming from your application (https://coolify.io)
# SERVICE_URL_*: URL coming from your application (coolify.io)
# SERVICE_USER_*: Generated by your application, username (not encrypted)
# SERVICE_PASSWORD_*: Generated by your application, password (encrypted)
</pre>
<x-services.explanation />
<x-forms.textarea rows="20" id="dockercompose"
placeholder='services:
ghost:
documentation: https://docs.ghost.org/docs/config
documentation: https://ghost.org/docs/config
image: ghost:5
volumes:
- ghost-content-data:/var/lib/ghost/content

View File

@ -4,7 +4,7 @@
<div class="flex flex-col gap-2 pt-10">
@if ($current_step === 'type')
<ul class="pb-10 steps">
<li class="step step-secondary">Select Source Type</li>
<li class="step step-secondary">Select Resource Type</li>
<li class="step">Select a Server</li>
<li class="step">Select a Destination</li>
</ul>
@ -95,7 +95,7 @@
@endif
@if ($current_step === 'servers')
<ul class="pb-10 steps">
<li class="step step-secondary">Select Source Type</li>
<li class="step step-secondary">Select Resource Type</li>
<li class="step step-secondary">Select a Server</li>
<li class="step">Select a Destination</li>
</ul>
@ -123,7 +123,7 @@
@endif
@if ($current_step === 'destinations')
<ul class="pb-10 steps">
<li class="step step-secondary">Select Source Type</li>
<li class="step step-secondary">Select Resource Type</li>
<li class="step step-secondary">Select a Server</li>
<li class="step step-secondary">Select a Destination</li>
</ul>

View File

@ -1,14 +1,17 @@
<form wire:submit.prevent='submit'>
<div class="flex gap-2 pb-4">
<div class="flex items-center gap-2 pb-4">
@if ($application->human_name)
<h2>{{ Str::headline($application->human_name) }}</h2>
@else
<h2>{{ Str::headline($application->name) }}</h2>
@endif
<x-forms.button type="submit">Save</x-forms.button>
<a target="_blank" href="{{ $application->documentation() }}">Documentation <x-external-link /></a>
</div>
<div class="flex gap-2">
<x-forms.input label="Name" id="application.human_name" placeholder="Name"></x-forms.input>
@if (isset($application->fqdn))
<x-forms.input label="FQDN" required id="application.fqdn"></x-forms.input>
@endisset
</div>
</form>

View File

@ -1,11 +1,12 @@
<form wire:submit.prevent='submit'>
<div class="flex gap-2 pb-4">
<div class="flex items-center gap-2 pb-4">
@if ($database->human_name)
<h2>{{ Str::headline($database->human_name) }}</h2>
@else
<h2>{{ Str::headline($database->name) }}</h2>
@endif
<x-forms.button type="submit">Save</x-forms.button>
<a target="_blank" href="{{ $database->documentation() }}">Documentation <x-external-link /></a>
</div>
<div class="flex gap-2">
<x-forms.input label="Name" id="database.human_name" placeholder="Name"></x-forms.input>

View File

@ -2,10 +2,11 @@
<livewire:project.service.navbar :service="$service" :parameters="$parameters" :query="$query" />
<div class="flex h-full pt-6">
<div class="flex flex-col gap-4 min-w-fit">
<a :class="activeTab === 'service-stack' && 'text-white'"
@click.prevent="activeTab = 'service-stack'; window.location.hash = 'service-stack'" href="#">Service Stack</a>
<a :class="activeTab === 'compose' && 'text-white'"
@click.prevent="activeTab = 'compose'; window.location.hash = 'compose'" href="#">Compose File</a>
<a :class="activeTab === 'service-stack' && 'text-white'"
@click.prevent="activeTab = 'service-stack'; window.location.hash = 'service-stack'"
href="#">Service Stack</a>
<a :class="activeTab === 'environment-variables' && 'text-white'"
@click.prevent="activeTab = 'environment-variables'; window.location.hash = 'environment-variables'"
href="#">Environment
@ -57,11 +58,14 @@
</div>
</div>
<x-services.explanation />
<div x-cloak x-show="raw">
<x-forms.textarea rows="20" id="service.docker_compose_raw">
</x-forms.textarea>
</div>
<div x-cloak x-show="raw === false">
<x-forms.textarea readonly rows="20" id="service.docker_compose">
</x-forms.textarea>
</div>

View File

@ -7,21 +7,14 @@
</x-slot:modalBody>
</x-modal>
@if ($isReadOnly)
<span class="text-warning">Please modify storage layout in your Compose file.</span>
<span class="text-warning">Please modify storage layout in your <a
class="underline" href="{{ Str::of(url()->current())->beforeLast('/') }}#compose">Docker Compose</a> file.</span>
@endif
<form wire:submit.prevent='submit' class="flex flex-col gap-2 pt-4 xl:items-end xl:flex-row">
@if ($isReadOnly)
<x-forms.input id="storage.name" label="Name" required readonly />
<x-forms.input id="storage.host_path" label="Source Path" readonly />
<x-forms.input id="storage.mount_path" label="Destination Path" required readonly />
<div class="flex gap-2">
<x-forms.button type="submit" disabled>
Update
</x-forms.button>
<x-forms.button isError isModal modalId="{{ $modalId }}">
Delete
</x-forms.button>
</div>
@else
<x-forms.input id="storage.name" label="Name" required />
<x-forms.input id="storage.host_path" label="Source Path" />