Add coolify-builder image, initials of deployments

This commit is contained in:
Andras Bacsai 2023-03-28 22:13:08 +02:00
parent 593f1acf10
commit 8e0c1027bb
17 changed files with 299 additions and 140 deletions

View File

@ -0,0 +1,109 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProjectController extends Controller
{
public function environments()
{
$project_uuid = request()->route('project_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
return view('project.environments', ['project' => $project]);
}
public function resources()
{
$project_uuid = request()->route('project_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
return view('project.resources', ['project' => $project]);
}
public function application()
{
$project_uuid = request()->route('project_uuid');
$environment_name = request()->route('environment_name');
$application_uuid = request()->route('application_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
$environment = $project->environments->where('name', $environment_name)->first();
if (!$environment) {
return redirect()->route('home');
}
$application = $environment->applications->where('uuid', $application_uuid)->first();
if (!$application) {
return redirect()->route('home');
}
return view('project.application', ['project' => $project, 'application' => $application]);
}
public function database()
{
$project_uuid = request()->route('project_uuid');
$environment_name = request()->route('environment_name');
$database_uuid = request()->route('database_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
$environment = $project->environments->where('name', $environment_name)->first();
if (!$environment) {
return redirect()->route('home');
}
$database = $environment->databases->where('uuid', $database_uuid)->first();
if (!$database) {
return redirect()->route('home');
}
return view('project.database', ['project' => $project, 'database' => $database]);
}
public function service()
{
$project_uuid = request()->route('project_uuid');
$environment_name = request()->route('environment_name');
$service_uuid = request()->route('service_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
$environment = $project->environments->where('name', $environment_name)->first();
if (!$environment) {
return redirect()->route('home');
}
$service = $environment->services->where('uuid', $service_uuid)->first();
if (!$service) {
return redirect()->route('home');
}
return view('project.service', ['project' => $project, 'service' => $service]);
}
public function deployment()
{
$project_uuid = request()->route('project_uuid');
$environment_name = request()->route('environment_name');
$application_uuid = request()->route('application_uuid');
$deployment_uuid = request()->route('deployment_uuid');
$project = session('currentTeam')->projects->where('uuid', $project_uuid)->first();
if (!$project) {
return redirect()->route('home');
}
$environment = $project->environments->where('name', $environment_name)->first();
if (!$environment) {
return redirect()->route('home');
}
$application = $environment->applications->where('uuid', $application_uuid)->first();
if (!$application) {
return redirect()->route('home');
}
$deployment = $application->deployments->where('uuid', $deployment_uuid)->first();
return view('project.deployment', ['project' => $project, 'deployment' => $deployment]);
}
}

View File

@ -1,81 +0,0 @@
<?php
namespace App\Http\Livewire;
use App\Models\Application;
use App\Models\CoolifyInstanceSettings;
use App\Models\Deployment;
use App\Traits\Shared;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class DemoDeployApplication extends Component
{
use Shared;
public $activity;
public $isKeepAliveOn = false;
public $application_uuid;
public Application $application;
public $destination;
public string $deployment_id;
public string $workdir;
public CoolifyInstanceSettings $coolify_instance_settings;
public $wildcard_domain;
protected $command;
private function dockerPreCommand($command)
{
return $this->command[] = "docker exec {$this->deployment_id} sh -c '{$command}'";
}
public function deploy()
{
$this->isKeepAliveOn = true;
$this->coolify_instance_settings = CoolifyInstanceSettings::find(1);
$this->application = Application::where('uuid', $this->application_uuid)->first();
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
$project_wildcard_domain = data_get($this->application, 'environment.project.settings.wildcard_domain');
$global_wildcard_domain = data_get($this->coolify_instance_settings, 'wildcard_domain');
$this->wildcard_domain = $project_wildcard_domain ?? $global_wildcard_domain ?? null;
$source = $this->application->source->getMorphClass()::where('id', $this->application->source->id)->first();
$this->deployment_id = new Cuid2(10);
$this->workdir = "/tmp/{$this->deployment_id}";
$this->command[] = "echo 'Starting deployment of {$this->application->name} ({$this->application->uuid})'";
$this->command[] = "docker run -d --name {$this->deployment_id} --rm -v /var/run/docker.sock:/var/run/docker.sock coolify-builder >/dev/null";
$this->dockerPreCommand('hostname');
$this->dockerPreCommand("mkdir -p {$this->workdir}");
$this->dockerPreCommand("ls -ld {$this->workdir}");
$this->dockerPreCommand("git clone -b {$this->application->git_branch} {$source->html_url}/{$this->application->git_repository}.git {$this->workdir}");
$this->dockerPreCommand("ls -l {$this->workdir}");
$this->command[] = "docker stop -t 0 {$this->deployment_id} >/dev/null";
$this->activity = remoteProcess(implode("\n", $this->command), $this->destination->server->name);
Deployment::create([
'uuid' => $this->deployment_id,
'type_id' => $this->application->id,
'type_type' => Application::class,
'activity_log_id' => $this->activity->id,
]);
}
public function polling()
{
$this->activity?->refresh();
if (data_get($this->activity, 'properties.exitCode') !== null) {
$this->isKeepAliveOn = false;
}
}
public function render()
{
return view('livewire.demo-deploy-application');
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Http\Livewire;
use App\Models\Application;
use App\Models\CoolifyInstanceSettings;
use App\Models\Deployment;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class DeployApplication extends Component
{
public string $application_uuid;
public $activity;
protected string $deployment_uuid;
protected array $command = [];
private function execute_in_builder(string $command)
{
return $this->command[] = "docker exec {$this->deployment_uuid} sh -c '{$command}'";
}
private function start_builder_container()
{
// @TODO: Add --pull=always if the container is published to ghcr.io
$this->command[] = "docker run -d --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock coolify-builder >/dev/null";
}
public function deploy()
{
$coolify_instance_settings = CoolifyInstanceSettings::find(1);
$application = Application::where('uuid', $this->application_uuid)->first();
$destination = $application->destination->getMorphClass()::where('id', $application->destination->id)->first();
$source = $application->source->getMorphClass()::where('id', $application->source->id)->first();
// Get Wildcard Domain
$project_wildcard_domain = data_get($application, 'environment.project.settings.wildcard_domain');
$global_wildcard_domain = data_get($coolify_instance_settings, 'wildcard_domain');
$wildcard_domain = $project_wildcard_domain ?? $global_wildcard_domain ?? null;
// Create Deployment ID
$this->deployment_uuid = new Cuid2(10);
$workdir = "/artifacts/{$this->deployment_uuid}";
// Start build process
$this->command[] = "echo 'Starting deployment of {$application->name} ({$application->uuid})'";
$this->start_builder_container();
$this->execute_in_builder('hostname');
$this->execute_in_builder("git clone -b {$application->git_branch} {$source->html_url}/{$application->git_repository}.git {$workdir}");
$this->execute_in_builder("ls -l {$workdir}");
$this->command[] = "docker stop -t 0 {$this->deployment_uuid} >/dev/null";
$this->activity = remoteProcess(implode("\n", $this->command), $destination->server->name);
// Create Deployment
Deployment::create([
'uuid' => $this->deployment_uuid,
'type_id' => $application->id,
'type_type' => Application::class,
'activity_log_id' => $this->activity->id,
]);
// Redirect to deployment page
return redirect()->route('project.deployment', [
"deployment_uuid" => $this->deployment_uuid,
"project_uuid" => $application->environment->project->uuid,
"environment_name" => $application->environment->name,
"application_uuid" => $application->uuid
]);
}
public function render()
{
return view('livewire.deploy-application');
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
class PollActivity extends Component
{
public $activity;
public $activity_log_id;
public $isKeepAliveOn = true;
public function mount() {
$this->activity = Activity::find($this->activity_log_id);
}
public function polling()
{
$this->activity?->refresh();
if (data_get($this->activity, 'properties.exitCode') !== null) {
$this->isKeepAliveOn = false;
}
}
public function render()
{
return view('livewire.poll-activity');
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Process;
trait Shared
{
public function get_workdir(string $type, string $resource_id, string $deployment_id)
{
$workdir = "/tmp/coolify/$type/{$resource_id}/{$deployment_id}/";
return $workdir;
}
}

View File

@ -18,7 +18,7 @@ public function run(): void
$standalone_docker_1 = StandaloneDocker::find(1); $standalone_docker_1 = StandaloneDocker::find(1);
Service::create([ Service::create([
'id' => 1, 'id' => 1,
'name'=> "My first database", 'name'=> "My first service",
'environment_id' => $environment_1->id, 'environment_id' => $environment_1->id,
'destination_id' => $standalone_docker_1->id, 'destination_id' => $standalone_docker_1->id,
'destination_type' => StandaloneDocker::class, 'destination_type' => StandaloneDocker::class,

View File

@ -12,6 +12,7 @@ ARG PACK_VERSION=0.27.0
ARG NIXPACKS_VERSION=1.6.0 ARG NIXPACKS_VERSION=1.6.0
USER root USER root
WORKDIR /artifacts
RUN apk add --no-cache bash curl git git-lfs openssh-client tar tini RUN apk add --no-cache bash curl git git-lfs openssh-client tar tini
RUN mkdir -p ~/.docker/cli-plugins RUN mkdir -p ~/.docker/cli-plugins
RUN curl -SL https://cdn.coollabs.io/bin/$TARGETPLATFORM/docker-$DOCKER_VERSION -o /usr/bin/docker RUN curl -SL https://cdn.coollabs.io/bin/$TARGETPLATFORM/docker-$DOCKER_VERSION -o /usr/bin/docker

View File

@ -1,46 +1,8 @@
<x-layout> <x-layout>
<h1>
Coolify v4 🎉
</h1>
<h1>Projects</h1> <h1>Projects</h1>
<ul>
@forelse ($projects as $project) @forelse ($projects as $project)
<h2>{{ $project->name }}</h2> <a href="{{ route('project.environments', [$project->uuid]) }}">{{ data_get($project, 'name') }}</a>
<p>Project Settings:{{ $project->settings }}</p>
<h2>Environments</h2>
@forelse ($project->environments as $environment)
<h1>Environment: {{ $environment->name }}</h1>
<h2>Applications</h2>
@forelse ($environment->applications as $application)
<h3>{{ $application->name }}</h3>
<p>Application: {{ $application->settings }}</p>
<p>Destination Class: {{ $application->destination->getMorphClass() }}</p>
<p>Source Class: {{ $application->source->getMorphClass() }}</p>
<livewire:demo-deploy-application :application_uuid="$application->uuid" />
@empty @empty
<li>No application found</li> <p>No projects found.</p>
@endforelse @endforelse
<h2>Databases</h2>
@forelse ($environment->databases as $database)
<h3>{{ $database->name }}</h3>
<p>Database: {{ $database }}</p>
<p>Destination Class: {{ $database->destination->getMorphClass() }}</p>
@empty
<li>No database found</li>
@endforelse
<h2>Services</h2>
@forelse ($environment->services as $service)
<h3>{{ $service->name }}</h3>
<p>Service: {{ $service }}</p>
<p>Destination Class: {{ $service->destination->getMorphClass() }}</p>
@empty
<li>No service found</li>
@endforelse
@empty
<p>No environments found</p>
@endforelse
@empty
<li>No projects found</li>
@endforelse
</ul>
</x-layout> </x-layout>

View File

@ -0,0 +1,3 @@
<div>
<button wire:click='deploy'>Deploy</button>
</div>

View File

@ -0,0 +1,8 @@
<div>
@isset($activity?->id)
<div>
Activity: <span>{{ $activity?->id ?? 'waiting' }}</span>
</div>
<pre style="width: 100%;overflow-y: scroll;" @if ($isKeepAliveOn) wire:poll.750ms="polling" @endif>{{ data_get($activity, 'description') }}</pre>
@endisset
</div>

View File

@ -0,0 +1,6 @@
<x-layout>
<h1>Application</h1>
<p>Name: {{ $project->name }}</p>
<p>UUID: {{ $project->uuid }}</p>
<livewire:deploy-application :application_uuid="$application->uuid" />
</x-layout>

View File

@ -0,0 +1,5 @@
<x-layout>
<h1>Database</h1>
</x-layout>

View File

@ -0,0 +1,8 @@
<x-layout>
<h1>Deployment</h1>
<p>Name: {{ $project->name }}</p>
<p>UUID: {{ $project->uuid }}</p>
<p>Deployment UUID: {{ $deployment->uuid }}</p>
<livewire:poll-activity :activity_log_id="$deployment->activity_log_id" />
</x-layout>

View File

@ -0,0 +1,11 @@
<x-layout>
<h1>Environments</h1>
@foreach ($project->environments as $environment)
<div>
<a href="{{ route('project.resources', [$project->uuid, $environment->name]) }}">
{{ $environment->name }}
</a>
</div>
@endforeach
</x-layout>

View File

@ -0,0 +1,29 @@
<x-layout>
<h1>Resources</h1>
@foreach ($project->environments as $environment)
<div>
@foreach ($environment->applications as $application)
<p>
<a href="{{ route('project.application', [$project->uuid, $environment->name, $application->uuid]) }}">
{{ $application->name }}
</a>
</p>
@endforeach
@foreach ($environment->databases as $database)
<p>
<a href="{{ route('project.database', [$project->uuid, $environment->name, $database->uuid]) }}">
{{ $database->name }}
</a>
</p>
@endforeach
@foreach ($environment->services as $service)
<p>
<a href="{{ route('project.service', [$project->uuid, $environment->name, $service->uuid]) }}">
{{ $service->name }}
</a>
</p>
@endforeach
</div>
@endforeach
</x-layout>

View File

@ -0,0 +1,5 @@
<x-layout>
<h1>Service</h1>
</x-layout>

View File

@ -1,6 +1,7 @@
<?php <?php
use App\Http\Controllers\HomeController; use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProjectController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
/* /*
@ -17,7 +18,14 @@
Route::middleware(['auth'])->group(function () { Route::middleware(['auth'])->group(function () {
Route::get('/', [HomeController::class, 'show']); Route::get('/', [HomeController::class, 'show'])->name('home');
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('/project/{project_uuid}/{environment_name}/database/{database_uuid}', [ProjectController::class, 'database'])->name('project.database');
Route::get('/project/{project_uuid}/{environment_name}/service/{service_uuid}', [ProjectController::class, 'service'])->name('project.service');
Route::get('/profile', function () { Route::get('/profile', function () {
return view('profile'); return view('profile');
}); });