Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

wip

Andras Bacsaiec3ae7f6de7b
diff --git a/app/Actions/Proxy/CheckProxySettingsInSync.php b/app/Actions/Proxy/CheckProxySettingsInSync.php
index 038a1bb8cb..410ebcfbaa 100644
--- a/app/Actions/Proxy/CheckProxySettingsInSync.php
+++ b/app/Actions/Proxy/CheckProxySettingsInSync.php
@@ -2,34 +2,32 @@
namespace App\Actions\Proxy;
-use App\Enums\ActivityTypes;
use App\Enums\ProxyTypes;
use App\Models\Server;
-use Spatie\Activitylog\Models\Activity;
-use Symfony\Component\Yaml\Yaml;
+use Illuminate\Support\Str;
class CheckProxySettingsInSync
{
public function __invoke(Server $server)
{
- // @TODO What is the mechanism to make sure setting in sync?
- $folder_name = match ($server->extra_attributes->proxy) {
- ProxyTypes::TRAEFIK_V2->value => 'proxy',
- };
-
- $container_name = 'coolify-proxy';
-
+ $proxy_path = config('coolify.proxy_config_path');
$output = instantRemoteProcess([
- // Folder exists, in ~/projects/<folder-name>
- 'if [ -d "projects/' . $folder_name . '" ]; then echo "true"; else echo "false"; fi',
- // Container of name <container-name> is running
- <<<EOT
- [[ "$(docker inspect -f '{{.State.Running}}' $container_name 2>/dev/null)" == "true" ]] && echo "true" || echo "false"
- EOT,
- ], $server);
-
- return collect(
- explode(PHP_EOL, $output)
- )->every(fn ($output) => $output === 'true');
+ "cat $proxy_path/docker-compose.yml",
+ ], $server, false);
+ if (is_null($output)) {
+ $final_output = Str::of(getProxyConfiguration($server))->trim();
+ } else {
+ $final_output = Str::of($output)->trim();
+ }
+ $docker_compose_yml_base64 = base64_encode($final_output);
+ $server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
+ $server->save();
+ if (is_null($output)) {
+ instantRemoteProcess([
+ "mkdir -p $proxy_path",
+ "echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
+ ], $server);
+ }
+ return $final_output;
}
}
diff --git a/app/Actions/Proxy/InstallProxy.php b/app/Actions/Proxy/InstallProxy.php
index 42864b4156..2cbd4103d4 100644
--- a/app/Actions/Proxy/InstallProxy.php
+++ b/app/Actions/Proxy/InstallProxy.php
@@ -5,103 +5,63 @@ namespace App\Actions\Proxy;
use App\Enums\ActivityTypes;
use App\Enums\ProxyTypes;
use App\Models\Server;
+use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
-use Symfony\Component\Yaml\Yaml;
+use Illuminate\Support\Str;
class InstallProxy
{
+ public Collection $networks;
+
public function __invoke(Server $server): Activity
{
- $docker_compose_yml_base64 = base64_encode(
- $this->getDockerComposeContents()
- );
+ $proxy_path = config('coolify.proxy_config_path');
+
+ $networks = collect($server->standaloneDockers)->map(function ($docker) {
+ return $docker['network'];
+ })->unique();
+ if ($networks->count() === 0) {
+ $this->networks = collect(['coolify']);
+ }
+ $create_networks_command = $this->networks->map(function ($network) {
+ return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
+ });
+
+ $configuration = instantRemoteProcess([
+ "cat $proxy_path/docker-compose.yml",
+ ], $server, false);
+ if (is_null($configuration)) {
+ $configuration = Str::of(getProxyConfiguration($server))->trim();
+ } else {
+ $configuration = Str::of($configuration)->trim();
+ }
+ $docker_compose_yml_base64 = base64_encode($configuration);
+ $server->extra_attributes->last_applied_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
+ $server->save();
$env_file_base64 = base64_encode(
$this->getEnvContents()
);
-
$activity = remoteProcess([
- "docker network ls --format '{{.Name}}' | grep '^coolify$' || docker network create coolify",
- 'mkdir -p projects',
- 'mkdir -p projects/proxy',
- 'mkdir -p projects/proxy/letsencrypt',
- 'cd projects/proxy',
- "echo '$docker_compose_yml_base64' | base64 -d > docker-compose.yml",
- "echo '$env_file_base64' | base64 -d > .env",
+ ...$create_networks_command,
+ "echo 'Docker networks created...'",
+ "mkdir -p $proxy_path",
+ "cd $proxy_path",
+ "echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
+ "echo '$env_file_base64' | base64 -d > $proxy_path/.env",
+ "echo 'Docker compose file created...'",
+ "echo 'Pulling docker image...'",
+ 'docker compose pull -q',
+ "echo 'Stopping proxy...'",
+ 'docker compose down -v --remove-orphans',
+ "echo 'Starting proxy...'",
'docker compose up -d --remove-orphans',
- 'docker ps',
+ "echo 'Proxy installed successfully...'"
], $server, ActivityTypes::INLINE->value);
- // Persist to Database
- $server->extra_attributes->proxy = ProxyTypes::TRAEFIK_V2->value;
- $server->save();
-
return $activity;
}
- protected function getDockerComposeContents()
- {
- return Yaml::dump($this->getComposeData());
- }
-
- /**
- * @return array
- */
- protected function getComposeData(): array
- {
- $cwd = config('app.env') === 'local'
- ? config('proxy.project_path_on_host') . '/_testing_hosts/host_2_proxy'
- : '.';
-
- return [
- "version" => "3.7",
- "networks" => [
- "coolify" => [
- "external" => true,
- ],
- ],
- "services" => [
- "traefik" => [
- "container_name" => "coolify-proxy",
- "image" => "traefik:v2.10",
- "restart" => "always",
- "extra_hosts" => [
- "host.docker.internal:host-gateway",
- ],
- "networks" => [
- "coolify",
- ],
- "ports" => [
- "80:80",
- "443:443",
- "8080:8080",
- ],
- "volumes" => [
- "/var/run/docker.sock:/var/run/docker.sock:ro",
- "{$cwd}/letsencrypt:/letsencrypt",
- "{$cwd}/traefik.auth:/auth/traefik.auth",
- ],
- "command" => [
- "--api.dashboard=true",
- "--api.insecure=true",
- "--entrypoints.http.address=:80",
- "--entrypoints.https.address=:443",
- "--providers.docker=true",
- "--providers.docker.exposedbydefault=false",
- ],
- "labels" => [
- "traefik.enable=true",
- "traefik.http.routers.traefik.entrypoints=http",
- 'traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_HOST}`)',
- "traefik.http.routers.traefik.service=api@internal",
- "traefik.http.services.traefik.loadbalancer.server.port=8080",
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
- ],
- ],
- ],
- ];
- }
-
protected function getEnvContents()
{
$data = [
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 04e3415d5c..a636e1c5d3 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -4,6 +4,7 @@ namespace App\Console;
use App\Jobs\ContainerStatusJob;
use App\Jobs\DockerCleanupDanglingImagesJob;
+use App\Jobs\ProxyCheckJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -16,6 +17,7 @@ class Kernel extends ConsoleKernel
{
$schedule->job(new ContainerStatusJob)->everyMinute();
$schedule->job(new DockerCleanupDanglingImagesJob)->everyMinute();
+ // $schedule->job(new ProxyCheckJob)->everyMinute();
}
/**
diff --git a/app/Http/Livewire/Server/Proxy.php b/app/Http/Livewire/Server/Proxy.php
index 16e92cf483..ada0a64575 100644
--- a/app/Http/Livewire/Server/Proxy.php
+++ b/app/Http/Livewire/Server/Proxy.php
@@ -4,7 +4,8 @@ namespace App\Http\Livewire\Server;
use App\Actions\Proxy\CheckProxySettingsInSync;
use App\Actions\Proxy\InstallProxy;
-use App\Enums\ActivityTypes;
+use App\Enums\ProxyTypes;
+use Illuminate\Support\Str;
use App\Models\Server;
use Livewire\Component;
@@ -12,34 +13,60 @@ class Proxy extends Component
{
public Server $server;
- protected string $selectedProxy = '';
-
+ public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2;
public $is_proxy_installed;
public $is_check_proxy_complete = false;
- public $is_proxy_settings_in_sync = false;
-
- public function mount(Server $server)
- {
- $this->server = $server;
- }
+ public $proxy_settings = null;
- public function runInstallProxy()
+ public function installProxy()
{
+ $this->saveConfiguration($this->server);
$activity = resolve(InstallProxy::class)($this->server);
-
$this->emit('newMonitorActivity', $activity->id);
}
- public function checkProxySettingsInSync()
+ public function proxyStatus()
{
- $this->is_proxy_settings_in_sync = resolve(CheckProxySettingsInSync::class)($this->server);
-
- $this->is_check_proxy_complete = true;
+ $this->server->extra_attributes->proxy_status = checkContainerStatus(server: $this->server, container_id: 'coolify-proxy');
+ $this->server->save();
}
-
- public function render()
+ public function setProxy()
+ {
+ $this->server->extra_attributes->proxy_type = $this->selectedProxy->value;
+ $this->server->extra_attributes->proxy_status = 'exited';
+ $this->server->save();
+ }
+ public function stopProxy()
+ {
+ instantRemoteProcess([
+ "docker rm -f coolify-proxy",
+ ], $this->server);
+ $this->server->extra_attributes->proxy_status = 'exited';
+ $this->server->save();
+ }
+ public function saveConfiguration()
+ {
+ try {
+ $proxy_path = config('coolify.proxy_config_path');
+ $this->proxy_settings = Str::of($this->proxy_settings)->trim();
+ $docker_compose_yml_base64 = base64_encode($this->proxy_settings);
+ $this->server->extra_attributes->last_saved_proxy_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
+ $this->server->save();
+ instantRemoteProcess([
+ "echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
+ ], $this->server);
+ } catch (\Exception $e) {
+ return generalErrorHandler($e);
+ }
+ }
+ public function checkProxySettingsInSync()
{
- return view('livewire.server.proxy');
+ try {
+ $this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server);
+ $this->is_check_proxy_complete = true;
+ } catch (\Exception $e) {
+ return generalErrorHandler($e);
+ }
}
}
diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php
index bc05d6e0bc..de3660de4a 100644
--- a/app/Jobs/ContainerStatusJob.php
+++ b/app/Jobs/ContainerStatusJob.php
@@ -67,9 +67,7 @@ class ContainerStatusJob implements ShouldQueue
return;
}
if ($application->destination->server) {
- $container = instantRemoteProcess(["docker inspect --format '{{json .State}}' {$this->container_id}"], $application->destination->server);
- $container = formatDockerCmdOutputToJson($container);
- $application->status = $container[0]['Status'];
+ $application->status = checkContainerStatus(server: $application->destination->server, container_id: $this->container_id);
$application->save();
}
}
diff --git a/app/Jobs/DeployApplicationJob.php b/app/Jobs/DeployApplicationJob.php
index 0e1ad11b57..31159d2dfb 100644
--- a/app/Jobs/DeployApplicationJob.php
+++ b/app/Jobs/DeployApplicationJob.php
@@ -69,7 +69,6 @@ class DeployApplicationJob implements ShouldQueue
protected function stopRunningContainer()
{
$this->executeNow([
-
"echo -n 'Removing old instance... '",
$this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"),
"echo 'Done.'",
diff --git a/app/Jobs/ProxyCheckJob.php b/app/Jobs/ProxyCheckJob.php
new file mode 100755
index 0000000000..4c7ce12e64
--- /dev/null
+++ b/app/Jobs/ProxyCheckJob.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Actions\Proxy\InstallProxy;
+use App\Models\Server;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class ProxyCheckJob implements ShouldQueue
+{
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ /**
+ * Create a new job instance.
+ */
+ public function __construct()
+ {
+ }
+
+ /**
+ * Execute the job.
+ */
+ public function handle()
+ {
+ try {
+ $container_name = 'coolify-proxy';
+ $configuration_path = config('coolify.proxy_config_path');
+ $servers = Server::whereRelation('settings', 'is_validated', true)->get();
+
+ foreach ($servers as $server) {
+ $status = checkContainerStatus(server: $server, container_id: $container_name);
+ if ($status === 'running') {
+ continue;
+ }
+ resolve(InstallProxy::class)($server);
+ }
+ } catch (\Throwable $th) {
+ //throw $th;
+ }
+ }
+}
diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php
index 61996bb21f..085604cd5d 100644
--- a/bootstrap/helpers.php
+++ b/bootstrap/helpers.php
@@ -40,7 +40,7 @@ if (!function_exists('generalErrorHandler')) {
'error' => $error->getMessage(),
]);
} else {
- dump($error);
+ // dump($error);
}
}
}
@@ -165,10 +165,9 @@ if (!function_exists('instantRemoteProcess')) {
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
if (!$throwError) {
- return false;
+ return null;
}
- Log::error($process->errorOutput());
- throw new \RuntimeException('There was an error running the command.');
+ throw new \RuntimeException($process->errorOutput());
}
return $output;
}
@@ -196,6 +195,7 @@ use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token\Builder;
+use Symfony\Component\Yaml\Yaml;
if (!function_exists('generate_github_installation_token')) {
function generate_github_installation_token(GithubApp $source)
@@ -244,3 +244,73 @@ if (!function_exists('getParameters')) {
return Route::current()->parameters();
}
}
+if (!function_exists('checkContainerStatus')) {
+ function checkContainerStatus(Server $server, string $container_id, bool $throwError = false)
+ {
+ $container = instantRemoteProcess(["docker inspect --format '{{json .State}}' {$container_id}"], $server, $throwError);
+ if (!$container) {
+ return 'exited';
+ }
+ $container = formatDockerCmdOutputToJson($container);
+ return $container[0]['Status'];
+ }
+}
+if (!function_exists('getProxyConfiguration')) {
+ function getProxyConfiguration(Server $server)
+ {
+ $proxy_config_path = config('coolify.proxy_config_path');
+ $networks = collect($server->standaloneDockers)->map(function ($docker) {
+ return $docker['network'];
+ })->unique();
+ if ($networks->count() === 0) {
+ $networks = collect(['coolify']);
+ }
+ $array_of_networks = collect([]);
+ $networks->map(function ($network) use ($array_of_networks) {
+ $array_of_networks[$network] = [
+ "external" => true,
+ ];
+ });
+ return Yaml::dump([
+ "version" => "3.8",
+ "networks" => $array_of_networks->toArray(),
+ "services" => [
+ "traefik" => [
+ "container_name" => "coolify-proxy", # Do not modify this! You will break everything!
+ "image" => "traefik:v2.10",
+ "restart" => "always",
+ "extra_hosts" => [
+ "host.docker.internal:host-gateway",
+ ],
+ "networks" => $networks->toArray(), # Do not modify this! You will break everything!
+ "ports" => [
+ "80:80",
+ "443:443",
+ "8080:8080",
+ ],
+ "volumes" => [
+ "/var/run/docker.sock:/var/run/docker.sock:ro",
+ "{$proxy_config_path}/letsencrypt:/letsencrypt", # Do not modify this! You will break everything!
+ "{$proxy_config_path}/traefik.auth:/auth/traefik.auth", # Do not modify this! You will break everything!
+ ],
+ "command" => [
+ "--api.dashboard=true",
+ "--api.insecure=true",
+ "--entrypoints.http.address=:80",
+ "--entrypoints.https.address=:443",
+ "--providers.docker=true",
+ "--providers.docker.exposedbydefault=false",
+ ],
+ "labels" => [
+ "traefik.enable=true", # Do not modify this! You will break everything!
+ "traefik.http.routers.traefik.entrypoints=http",
+ 'traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_HOST}`)',
+ "traefik.http.routers.traefik.service=api@internal",
+ "traefik.http.services.traefik.loadbalancer.server.port=8080",
+ "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
+ ],
+ ],
+ ],
+ ], 4, 2);
+ }
+}
diff --git a/config/coolify.php b/config/coolify.php
index fd95a7faa6..0afb921dc9 100644
--- a/config/coolify.php
+++ b/config/coolify.php
@@ -1,9 +1,8 @@
<?php
return [
- 'version' => '4.0.0-nightly.2',
-
'mux_enabled' => env('MUX_ENABLED', true),
-
'dev_webhook' => env('SERVEO_URL'),
+ 'base_config_path' => env('BASE_CONFIG_PATH', '/data/coolify'),
+ 'proxy_config_path' => env('BASE_CONFIG_PATH', '/data/coolify') . "/proxy",
];
diff --git a/resources/views/components/proxy/options.blade.php b/resources/views/components/proxy/options.blade.php
index 5e6657c5f7..04a6f04baf 100644
--- a/resources/views/components/proxy/options.blade.php
+++ b/resources/views/components/proxy/options.blade.php
@@ -1,4 +1,4 @@
-
+@props(['proxy_settings'])
<div class="mt-4">
<label>
<div>Edit config file</div>
@@ -45,4 +45,3 @@
<button>Update</button>
</label>
</div>
-
diff --git a/resources/views/components/proxy/problems.blade.php b/resources/views/components/proxy/problems.blade.php
deleted file mode 100644
index 751f7f3708..0000000000
--- a/resources/views/components/proxy/problems.blade.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<div>
- Problems!
-</div>
diff --git a/resources/views/errors/404.blade.php b/resources/views/errors/404.blade.php
index 7549540d8d..c5c95717dc 100644
--- a/resources/views/errors/404.blade.php
+++ b/resources/views/errors/404.blade.php
@@ -1,5 +1,3 @@
-@extends('errors::minimal')
-
-@section('title', __('Not Found'))
-@section('code', '404')
-@section('message', __('Not Found'))
+<div>
+ You are lost. <a href="{{ route('dashboard') }}">Go home</a>
+</div>
diff --git a/resources/views/livewire/server/proxy.blade.php b/resources/views/livewire/server/proxy.blade.php
index 6a81549ff0..ec216d2a45 100644
--- a/resources/views/livewire/server/proxy.blade.php
+++ b/resources/views/livewire/server/proxy.blade.php
@@ -1,40 +1,55 @@
<div>
- <h2>Proxy</h2>
-
- @if ($this->server->extra_attributes->proxy)
- <div>
- <div>
- Proxy type: {{ $this->server->extra_attributes->proxy }}
- </div>
-
- <div id="proxy_options" x-init="$wire.checkProxySettingsInSync()" class="relative w-fit">
-
- {{-- Proxy is being checked against DB information --}}
- @if (!$this->is_check_proxy_complete)
- <x-proxy.loading />
- @endif
-
- @if ($this->is_check_proxy_complete && !$this->is_proxy_settings_in_sync)
- <x-proxy.problems />
- @else
- <x-proxy.options />
- @endif
-
- </div>
-
- </div>
- @else
- {{-- There is no Proxy installed --}}
-
- No proxy installed.
+ <div class="flex items-center gap-2">
+ <h2>Proxy</h2>
+ <div>{{ $this->server->extra_attributes->proxy_status }}</div>
+ </div>
+ @if ($this->server->extra_attributes->proxy_status !== 'running')
<select wire:model="selectedProxy">
<option value="{{ \App\Enums\ProxyTypes::TRAEFIK_V2 }}">
{{ \App\Enums\ProxyTypes::TRAEFIK_V2 }}
</option>
</select>
- <button wire:click="runInstallProxy">Install Proxy</button>
+ <x-inputs.button isBold wire:click="setProxy">Set Proxy</x-inputs.button>
+ @endif
+ @if ($this->server->extra_attributes->proxy_type)
+ <div wire:poll="proxyStatus">
+ @if (
+ $this->server->extra_attributes->last_applied_proxy_settings &&
+ $this->server->extra_attributes->last_saved_proxy_settings !==
+ $this->server->extra_attributes->last_applied_proxy_settings)
+ <div class="text-red-500">Configuration out of sync.</div>
+ @endif
+ @if ($this->server->extra_attributes->proxy_status !== 'running')
+ <x-inputs.button isBold wire:click="installProxy">
+ Install
+ </x-inputs.button>
+ @endif
+ <x-inputs.button isBold wire:click="stopProxy">Stop</x-inputs.button>
+ <span x-data="{ showConfiguration: false }">
+ <x-inputs.button isBold x-on:click="showConfiguration = !showConfiguration">Show Configuration
+ </x-inputs.button>
+ <div class="pt-4">
+ <livewire:activity-monitor />
+ </div>
+ <template x-if="showConfiguration">
+ <div x-init="$wire.checkProxySettingsInSync" class="pt-4">
+ <h1>Proxy Configuration</h1>
+ <div wire:loading wire:target="checkProxySettingsInSync">
+ <x-proxy.loading />
+ </div>
+ @isset($this->proxy_settings)
+ <form wire:submit.prevent='saveConfiguration'>
+ <x-inputs.button isBold>Save</x-inputs.button>
+ <x-inputs.button x-on:click="showConfiguration = false" isBold
+ wire:click.prevent="installProxy">
+ Apply
+ </x-inputs.button>
+ <textarea wire:model.defer="proxy_settings" class="w-full" rows="30"></textarea>
+ </form>
+ @endisset
+ </div>
+ </template>
+ </span>
+ </div>
@endif
-
- <livewire:activity-monitor />
-
</div>