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;
}
}