This commit is contained in:
Andras Bacsai 2023-05-25 12:00:09 +02:00
parent 5a1a33242c
commit f766600fd8
41 changed files with 151 additions and 79 deletions

View File

@ -53,6 +53,8 @@ public function deploy(bool $force = false)
public function stop() public function stop()
{ {
dispatch(new ContainerStopJob($this->application->id, $this->destination->server)); instant_remote_process(["docker rm -f {$this->application->uuid}"], $this->application->destination->server);
$this->application->status = get_container_status(server: $this->application->destination->server, container_id: $this->application->uuid);
$this->application->save();
} }
} }

View File

@ -9,6 +9,9 @@ class Status extends Component
{ {
public Application $application; public Application $application;
protected $listeners = [
'applicationStatusChanged' => 'pollingStatus',
];
public function pollingStatus() public function pollingStatus()
{ {
$this->application->refresh(); $this->application->refresh();

View File

@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Livewire\Component; use Livewire\Component;
use LocalStorage;
class PrivateKey extends Component class PrivateKey extends Component
{ {
@ -19,8 +20,7 @@ public function setPrivateKey($private_key_id)
'private_key_id' => $private_key_id 'private_key_id' => $private_key_id
]); ]);
// Delete the old ssh mux file to force a new one to be created // Delete the old ssh mux file to force a new one to be created
Storage::disk('local')->delete(".ssh/ssh_mux_{$server->first()->ip}_{$server->first()->port}_{$server->first()->user}"); LocalStorage::ssh_mux()->delete("{$server->first()->ip}_{$server->first()->port}_{$server->first()->user}");
return redirect()->route('server.show', $this->parameters['server_uuid']); return redirect()->route('server.show', $this->parameters['server_uuid']);
} }
public function mount() public function mount()

View File

@ -2,8 +2,12 @@
namespace App\Http\Livewire\Settings; namespace App\Http\Livewire\Settings;
use App\Enums\ActivityTypes;
use App\Models\InstanceSettings as ModelsInstanceSettings; use App\Models\InstanceSettings as ModelsInstanceSettings;
use App\Models\Server;
use Livewire\Component; use Livewire\Component;
use Spatie\Url\Url;
use Symfony\Component\Yaml\Yaml;
class Form extends Component class Form extends Component
{ {
@ -44,5 +48,59 @@ public function submit()
} }
$this->validate(); $this->validate();
$this->settings->save(); $this->settings->save();
if (isset($this->settings->fqdn)) {
if (config('app.env') == 'local') {
$server = Server::findOrFail(1);
$dynamic_config_path = '/data/coolify/proxy/dynamic';
} else {
$server = Server::findOrFail(0);
$dynamic_config_path = '/traefik/dynamic';
}
$url = Url::fromString($this->settings->fqdn);
$host = $url->getHost();
$schema = $url->getScheme();
$entryPoints = [
0 => 'http',
];
if ($schema === 'https') {
$entryPoints[] = 'https';
}
$traefik_dynamic_conf = [
'http' =>
[
'routers' =>
[
'coolify' =>
[
'entryPoints' => $entryPoints,
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
],
],
'services' =>
[
'coolify' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => 'http://coolify:80',
],
],
],
],
],
],
];
$yaml = Yaml::dump($traefik_dynamic_conf);
$base64 = base64_encode($yaml);
remote_process([
"mkdir -p $dynamic_config_path",
"echo '$base64' | base64 -d > $dynamic_config_path/coolify.yaml",
], $server, ActivityTypes::INLINE->value);
}
} }
} }

View File

@ -18,6 +18,8 @@
use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Models\Activity;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LocalStorage;
use Log;
use Spatie\Url\Url; use Spatie\Url\Url;
class ApplicationDeploymentJob implements ShouldQueue class ApplicationDeploymentJob implements ShouldQueue
@ -203,7 +205,7 @@ public function handle(): void
$this->fail(); $this->fail();
} finally { } finally {
if (isset($this->docker_compose)) { if (isset($this->docker_compose)) {
Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', $this->docker_compose); LocalStorage::deployments()->put(Str::kebab($this->application->name) . '/docker-compose.yml', $this->docker_compose);
} }
$this->execute_now(["docker rm -f {$this->deployment_uuid} >/dev/null 2>&1"], hideFromOutput: true); $this->execute_now(["docker rm -f {$this->deployment_uuid} >/dev/null 2>&1"], hideFromOutput: true);
} }

View File

@ -21,7 +21,7 @@ public function __construct(
public string|null $application_id = null, public string|null $application_id = null,
) { ) {
if ($this->application_id) { if ($this->application_id) {
$this->application = Application::find($this->application_id)->first(); $this->application = Application::find($this->application_id);
} }
} }
public function uniqueId(): string public function uniqueId(): string

View File

@ -1,39 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\Application;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class ContainerStopJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public int $application_id,
public Server $server,
) {
}
public function uniqueId(): int
{
return $this->application_id;
}
public function handle(): void
{
try {
$application = Application::find($this->application_id)->first();
instant_remote_process(["docker rm -f {$application->uuid}"], $this->server);
$application->status = get_container_status(server: $application->destination->server, container_id: $application->uuid);
$application->save();
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}

View File

@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LocalStorage;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {

View File

@ -7,6 +7,9 @@
function getProxyConfiguration(Server $server) function getProxyConfiguration(Server $server)
{ {
$proxy_path = config('coolify.proxy_config_path'); $proxy_path = config('coolify.proxy_config_path');
if (config('app.env') === 'local') {
$proxy_path = $proxy_path . '/testing-host-1/';
}
$networks = collect($server->standaloneDockers)->map(function ($docker) { $networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network']; return $docker['network'];
})->unique(); })->unique();

View File

@ -46,19 +46,20 @@ function remote_process(
function save_private_key_for_server(Server $server) function save_private_key_for_server(Server $server)
{ {
$temp_file = "id.root@{$server->ip}"; $temp_file = "id.root@{$server->ip}";
Storage::disk('ssh-keys')->put($temp_file, $server->privateKey->private_key); LocalStorage::ssh_keys()->put($temp_file, $server->privateKey->private_key);
return '/var/www/html/storage/app/ssh-keys/' . $temp_file; return '/var/www/html/storage/app/private/ssh/keys/' . $temp_file;
} }
function generate_ssh_command(string $private_key_location, string $server_ip, string $user, string $port, string $command, bool $isMux = true) function generate_ssh_command(string $private_key_location, string $server_ip, string $user, string $port, string $command, bool $isMux = true)
{ {
Storage::disk('local')->makeDirectory('.ssh'); LocalStorage::ssh_keys();
LocalStorage::ssh_mux();
$delimiter = 'EOF-COOLIFY-SSH'; $delimiter = 'EOF-COOLIFY-SSH';
$ssh_command = "ssh "; $ssh_command = "ssh ";
if ($isMux && config('coolify.mux_enabled')) { if ($isMux && config('coolify.mux_enabled')) {
$ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/.ssh/ssh_mux_%h_%p_%r '; $ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/private/ssh/mux/%h_%p_%r ';
} }
$ssh_command .= "-i {$private_key_location} " $ssh_command .= "-i {$private_key_location} "
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
@ -85,6 +86,7 @@ function instant_remote_process(array $command, Server $server, $throwError = tr
$output = trim($process->output()); $output = trim($process->output());
$exitCode = $process->exitCode(); $exitCode = $process->exitCode();
if ($exitCode !== 0) { if ($exitCode !== 0) {
Log::info($process->errorOutput());
if (!$throwError) { if (!$throwError) {
return null; return null;
} }

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Storage;
class LocalStorage extends Facade
{
public static function deployments()
{
$storage = Storage::build([
'driver' => 'local',
'root' => storage_path("app/private/deployments"),
'visibility' => 'private',
]);
$storage->makeDirectory('.');
return $storage;
}
public static function ssh_keys()
{
$storage = Storage::build([
'driver' => 'local',
'root' => storage_path("app/private/ssh/keys"),
'visibility' => 'private'
]);
$storage->makeDirectory('.');
return $storage;
}
public static function ssh_mux()
{
$storage = Storage::build([
'driver' => 'local',
'root' => storage_path("app/private/ssh/mux"),
'visibility' => 'private',
]);
$storage->makeDirectory('.');
return $storage;
}
}

View File

@ -29,7 +29,6 @@
*/ */
'disks' => [ 'disks' => [
'local' => [ 'local' => [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path('app'), 'root' => storage_path('app'),

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->boolean('is_root_user')->default(false); $table->boolean('is_root_user')->default(false);
$table->string('name')->default('Your Name Here'); $table->string('name')->default('Your Name Here');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('personal_access_tokens', function (Blueprint $table) { Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->morphs('tokenable'); $table->morphs('tokenable');
$table->string('name'); $table->string('name');
$table->string('token', 64)->unique(); $table->string('token', 64)->unique();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('teams', function (Blueprint $table) { Schema::create('teams', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');
$table->boolean('personal_team')->default(false); $table->boolean('personal_team')->default(false);

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('team_user', function (Blueprint $table) { Schema::create('team_user', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->foreignId('team_id'); $table->foreignId('team_id');
$table->foreignId('user_id'); $table->foreignId('user_id');
$table->string('role')->nullable(); $table->string('role')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('instance_settings', function (Blueprint $table) { Schema::create('instance_settings', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('fqdn')->nullable(); $table->string('fqdn')->nullable();
$table->string('wildcard_domain')->nullable(); $table->string('wildcard_domain')->nullable();
$table->string('redirect_url')->nullable(); $table->string('redirect_url')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('servers', function (Blueprint $table) { Schema::create('servers', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');
$table->string('description')->nullable(); $table->string('description')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('server_settings', function (Blueprint $table) { Schema::create('server_settings', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->boolean('is_part_of_swarm')->default(false); $table->boolean('is_part_of_swarm')->default(false);

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('private_keys', function (Blueprint $table) { Schema::create('private_keys', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');
$table->string('description')->nullable(); $table->string('description')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('projects', function (Blueprint $table) { Schema::create('projects', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');
$table->string('description')->nullable(); $table->string('description')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('project_settings', function (Blueprint $table) { Schema::create('project_settings', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('wildcard_domain')->nullable(); $table->string('wildcard_domain')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('environments', function (Blueprint $table) { Schema::create('environments', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('name'); $table->string('name');
$table->foreignId('project_id'); $table->foreignId('project_id');
$table->timestamps(); $table->timestamps();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('applications', function (Blueprint $table) { Schema::create('applications', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->integer('repository_project_id')->nullable(); $table->integer('repository_project_id')->nullable();
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('application_settings', function (Blueprint $table) { Schema::create('application_settings', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->boolean('is_static')->default(false); $table->boolean('is_static')->default(false);
$table->boolean('is_git_submodules_allowed')->default(true); $table->boolean('is_git_submodules_allowed')->default(true);
$table->boolean('is_git_lfs_allowed')->default(true); $table->boolean('is_git_lfs_allowed')->default(true);

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('databases', function (Blueprint $table) { Schema::create('databases', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('services', function (Blueprint $table) { Schema::create('services', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('standalone_dockers', function (Blueprint $table) { Schema::create('standalone_dockers', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('name'); $table->string('name');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('network'); $table->string('network');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('swarm_dockers', function (Blueprint $table) { Schema::create('swarm_dockers', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('name'); $table->string('name');
$table->string('uuid')->unique(); $table->string('uuid')->unique();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('kubernetes', function (Blueprint $table) { Schema::create('kubernetes', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->timestamps(); $table->timestamps();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('gits', function (Blueprint $table) { Schema::create('gits', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->enum('type', ['github', 'gitlab', 'bitbucket', 'custom']); $table->enum('type', ['github', 'gitlab', 'bitbucket', 'custom']);
$table->string('api_url'); $table->string('api_url');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('github_apps', function (Blueprint $table) { Schema::create('github_apps', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('gitlab_apps', function (Blueprint $table) { Schema::create('gitlab_apps', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('local_persistent_volumes', function (Blueprint $table) { Schema::create('local_persistent_volumes', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->string('name'); $table->string('name');
$table->string('mount_path'); $table->string('mount_path');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('environment_variables', function (Blueprint $table) { Schema::create('environment_variables', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('key'); $table->string('key');
$table->string('value')->nullable(); $table->string('value')->nullable();

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('failed_jobs', function (Blueprint $table) { Schema::create('failed_jobs', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('uuid')->unique(); $table->string('uuid')->unique();
$table->text('connection'); $table->text('connection');
$table->text('queue'); $table->text('queue');

View File

@ -12,7 +12,7 @@
public function up(): void public function up(): void
{ {
Schema::create('application_deployment_queues', function (Blueprint $table) { Schema::create('application_deployment_queues', function (Blueprint $table) {
$table->id(); $table->bigIncrements('id');
$table->string('application_id'); $table->string('application_id');
$table->integer('pull_request_id')->default(0); $table->integer('pull_request_id')->default(0);
$table->schemalessAttributes('metadata'); $table->schemalessAttributes('metadata');

View File

@ -14,6 +14,7 @@
use App\Models\Team; use App\Models\Team;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use LocalStorage;
class ProductionSeeder extends Seeder class ProductionSeeder extends Seeder
{ {
@ -56,7 +57,7 @@ public function run(): void
// Save SSH Keys for the Coolify Host // Save SSH Keys for the Coolify Host
$coolify_key_name = "id.root@host.docker.internal"; $coolify_key_name = "id.root@host.docker.internal";
$coolify_key = Storage::disk('local')->get("ssh-keys/{$coolify_key_name}"); $coolify_key = LocalStorage::ssh_keys()->get("{$coolify_key_name}");
if ($coolify_key) { if ($coolify_key) {
$private_key = PrivateKey::find(0); $private_key = PrivateKey::find(0);

View File

@ -7,9 +7,7 @@ services:
source: /data/coolify/source/.env source: /data/coolify/source/.env
target: /var/www/html/.env target: /var/www/html/.env
read_only: true read_only: true
- /data/coolify/deployments:/var/www/html/storage/app/deployments - /data/coolify:/var/www/html/storage/app/
- /data/coolify/ssh-keys:/var/www/html/storage/app/ssh-keys
- /data/coolify/proxy:/var/www/html/storage/app/proxy
environment: environment:
- APP_ENV=production - APP_ENV=production
- APP_DEBUG - APP_DEBUG

View File

@ -10,6 +10,7 @@ services:
- coolify - coolify
depends_on: depends_on:
- postgres - postgres
- redis
postgres: postgres:
image: postgres:15-alpine image: postgres:15-alpine

View File

@ -60,6 +60,9 @@ function coolify {
function coolify:root { function coolify:root {
bash vendor/bin/spin exec coolify bash bash vendor/bin/spin exec coolify bash
} }
function coolify:proxy {
docker exec -ti coolify-proxy sh
}
function redis { function redis {
docker exec -ti coolify-redis redis-cli docker exec -ti coolify-redis redis-cli