Merge pull request #1062 from coollabsio/patricio-deploy-proxy

Patricio deploy proxy
This commit is contained in:
Andras Bacsai 2023-05-03 21:35:58 +02:00 committed by GitHub
commit 186402168f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 824 additions and 148 deletions

View File

@ -5,6 +5,7 @@
# Run in your terminal: `id -u` and `id -g` and that's the results
USERID=
GROUPID=
PROJECT_PATH_ON_HOST=/Users/your-username-here/code/coollabsio/coolify
############################################################################################################
APP_NAME=Coolify

1
.gitignore vendored
View File

@ -21,6 +21,7 @@ yarn-error.log
/.bash_history
/_volumes
# Temp while developing Proxy deployment
resources/recipes
.lesshst

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,111 @@
<?php
namespace App\Actions\Proxy;
use App\Enums\ActivityTypes;
use App\Models\Server;
use Symfony\Component\Yaml\Yaml;
class InstallProxy
{
public function __invoke(Server $server)
{
$docker_compose_yml_base64 = base64_encode(
$this->getDockerComposeContents()
);
$env_file_base64 = base64_encode(
$this->getEnvContents()
);
$activity = remoteProcess([
'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",
'docker compose up -d --remove-orphans',
'docker ps',
], $server, ActivityTypes::INLINE->value);
return $activity;
}
protected function getDockerComposeContents()
{
return Yaml::dump($this->getComposeData());
}
/**
* @return array
*/
protected function getComposeData(): array
{
$cwd = config('app.env') === 'local'
? config('coolify.project_path_on_host') . '/_testing_hosts/host_2_proxy'
: '.';
ray($cwd);
return [
"version" => "3.7",
"networks" => [
"coolify" => [
"external" => true,
],
],
"services" => [
"traefik" => [
"image" => "traefik:v2.9",
"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 = [
'TRAEFIK_DASHBOARD_HOST' => '',
'LETS_ENCRYPT_EMAIL' => '',
];
return collect($data)
->map(fn ($v, $k) => "{$k}={$v}")
->push(PHP_EOL)
->implode(PHP_EOL);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Data;
use App\Enums\ProxyTypes;
use Spatie\LaravelData\Data;
class ServerMetadata extends Data
{
public function __construct(
public ?ProxyTypes $proxy,
) {}
}

10
app/Enums/ProxyTypes.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Enums;
enum ProxyTypes: string
{
case TRAEFIK_V2 = 'TRAEFIK_V2';
case NGINX = 'NGINX';
case CADDY = 'CADDY';
}

View File

@ -93,6 +93,7 @@ class ProjectController extends Controller
if (!$application) {
return redirect()->route('dashboard');
}
$activity = Activity::query()
->where('properties->type', '=', 'deployment')
->where('properties->uuid', '=', $deployment_uuid)

View File

@ -0,0 +1,16 @@
<?php
namespace App\Http\Controllers;
use App\Models\Server;
use Illuminate\Http\Request;
class ServerController extends Controller
{
public function show(Server $server)
{
return view('server.show', [
'server' => $server,
]);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
class ActivityMonitor extends Component
{
public $activityId;
public $isPollingActive = false;
protected $activity;
protected $listeners = ['newMonitorActivity'];
public function hydrateActivity()
{
$this->activity = Activity::query()
->find($this->activityId);
}
public function newMonitorActivity($activityId)
{
$this->activityId = $activityId;
$this->hydrateActivity();
$this->isPollingActive = true;
}
public function polling()
{
$this->hydrateActivity();
if (data_get($this->activity, 'properties.exitCode') !== null) {
$this->isPollingActive = false;
}
}
public function render()
{
return view('livewire.activity-monitor');
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Livewire\Server;
use App\Actions\Proxy\InstallProxy;
use App\Enums\ActivityTypes;
use App\Models\Server;
use Livewire\Component;
class Proxy extends Component
{
public Server $server;
protected string $selectedProxy = '';
public function mount(Server $server)
{
$this->server = $server;
}
public function runInstallProxy()
{
$activity = resolve(InstallProxy::class)($this->server);
$this->emit('newMonitorActivity', $activity->id);
}
public function render()
{
return view('livewire.server.proxy');
}
}

View File

@ -2,6 +2,9 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
class Server extends BaseModel
{
protected static function booted()
@ -28,10 +31,18 @@ class Server extends BaseModel
{
return $this->hasMany(SwarmDocker::class);
}
public $casts = [
'extra_attributes' => SchemalessAttributes::class,
];
public function scopeWithExtraAttributes(): Builder
{
return $this->extra_attributes->modelScope();
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);
}
public function settings()
{
return $this->hasOne(ServerSetting::class);

View File

@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.2",
"doctrine/dbal": "^3.6",
"guzzlehttp/guzzle": "^7.5.0",
"laravel/fortify": "^v1.16.0",
"laravel/framework": "^v10.7.1",
@ -18,6 +19,7 @@
"spatie/laravel-data": "^3.4.3",
"spatie/laravel-ray": "^1.32.4",
"spatie/url": "^2.2",
"spatie/laravel-schemaless-attributes": "^2.4",
"symfony/yaml": "^6.2",
"visus/cuid2": "^2.0.0"
},

655
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -2,4 +2,6 @@
return [
'version' => '4.0.0-nightly.2',
'project_path_on_host' => env('PROJECT_PATH_ON_HOST', '/var/www/html')
];

View File

@ -21,6 +21,7 @@ return new class extends Migration
$table->string('user')->default('root');
$table->foreignId('team_id');
$table->foreignId('private_key_id');
$table->schemalessAttributes('extra_attributes');
$table->timestamps();
});
}

View File

@ -2,6 +2,8 @@
namespace Database\Seeders;
use App\Data\ServerMetadata;
use App\Enums\ProxyTypes;
use App\Models\PrivateKey;
use App\Models\Server;
use App\Models\Team;
@ -16,12 +18,16 @@ class ServerSeeder extends Seeder
{
$root_team = Team::find(0);
$private_key_1 = PrivateKey::find(1);
Server::create([
'name' => "testing-local-docker-container",
'description' => "This is a test docker container",
'ip' => "coolify-testing-host",
'team_id' => $root_team->id,
'private_key_id' => $private_key_1->id,
'extra_attributes' => ServerMetadata::from([
'proxy' => ProxyTypes::TRAEFIK_V2->value
]),
]);
Server::create([
'name' => "testing-local-docker-container-2",
@ -29,6 +35,9 @@ class ServerSeeder extends Seeder
'ip' => "coolify-testing-host-2",
'team_id' => $root_team->id,
'private_key_id' => $private_key_1->id,
'extra_attributes' => ServerMetadata::from([
//
]),
]);
}
}

View File

@ -5,9 +5,6 @@ x-testing-host: &testing-host-base
build:
dockerfile: Dockerfile
context: ./docker/testing-host
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./docker/testing-host/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
networks:
- coolify
@ -52,9 +49,14 @@ services:
testing-host:
<<: *testing-host-base
container_name: coolify-testing-host
testing-host2:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
testing-host-2:
<<: *testing-host-base
container_name: coolify-testing-host-2
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- "./_testing_hosts/host_2_proxy:/root/projects/proxy"

View File

@ -0,0 +1,8 @@
<div class="mt-8">
@isset($this->activity)
<span>Activity: {{ $this->activity?->id }}</span>
<span>Status: {{ $this->activity?->properties->get('status') }}</span>
<pre class="flex flex-col-reverse w-full overflow-y-scroll"
@if ($isPollingActive) wire:poll.750ms="polling" @endif>{{ \App\Actions\CoolifyTask\RunRemoteProcess::decodeOutput($this->activity) }}</pre>
@endisset
</div>

View File

@ -0,0 +1,30 @@
<div>
<h2> Proxy </h2>
@if($this->server->extra_attributes->proxy)
<div class="mt-12">
<div>
Proxy type: {{ $this->server->extra_attributes->proxy }}
</div>
<div class="mt-12"> Features in W11.</div>
<ul>
<li>Edit config file</li>
<li>Enable dashboard (blocking port by firewall)</li>
<li>Dashboard access - login/password</li>
<li>Setup host for Traefik Dashboard</li>
<li>Visit (nav to traefik dashboard)</li>
</ul>
</div>
@else
No proxy installed.
<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>
@endif
<livewire:activity-monitor />
</div>

View File

@ -14,4 +14,7 @@
<p>Network: {{ data_get($docker, 'network') }}</p>
@endforeach
@endif
<h1> {{ $server->name }}</h1>
<livewire:server.proxy :server="$server"/>
</x-layout>

View File

@ -7,6 +7,7 @@ use App\Models\InstanceSettings;
use App\Models\PrivateKey;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use App\Http\Controllers\ServerController;
use Illuminate\Support\Facades\Route;
/*
@ -38,6 +39,15 @@ Route::middleware(['auth'])->group(function () {
'private_keys' => $private_keys->sortBy('name'),
]);
})->name('dashboard');
Route::get('/project/{project_uuid}', [ProjectController::class, 'environments'])->name('project.environments');
Route::get('/project/{project_uuid}/{environment_name}', [ProjectController::class, 'resources'])->name('project.resources');
Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}', [ProjectController::class, 'application'])->name('project.application');
Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/deployment/{deployment_uuid}', [ProjectController::class, 'deployment'])->name('project.deployment');
Route::get('/server/{server:uuid}', [ServerController::class, 'show'])->name('server.show');
Route::get('/profile', function () {
return view('profile');