From 8e0c1027bbea6b4523a9b7ec70acb923e7c48c20 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 28 Mar 2023 22:13:08 +0200 Subject: [PATCH] Add coolify-builder image, initials of deployments --- app/Http/Controllers/ProjectController.php | 109 ++++++++++++++++++ app/Http/Livewire/DemoDeployApplication.php | 81 ------------- app/Http/Livewire/DeployApplication.php | 72 ++++++++++++ app/Http/Livewire/PollActivity.php | 27 +++++ app/Traits/Shared.php | 14 --- database/seeders/ServiceSeeder.php | 2 +- docker/builder/Dockerfile | 1 + resources/views/home.blade.php | 48 +------- .../livewire/deploy-application.blade.php | 3 + .../views/livewire/poll-activity.blade.php | 8 ++ resources/views/project/application.blade.php | 6 + resources/views/project/database.blade.php | 5 + resources/views/project/deployment.blade.php | 8 ++ .../views/project/environments.blade.php | 11 ++ resources/views/project/resources.blade.php | 29 +++++ resources/views/project/service.blade.php | 5 + routes/web.php | 10 +- 17 files changed, 299 insertions(+), 140 deletions(-) create mode 100644 app/Http/Controllers/ProjectController.php delete mode 100644 app/Http/Livewire/DemoDeployApplication.php create mode 100644 app/Http/Livewire/DeployApplication.php create mode 100644 app/Http/Livewire/PollActivity.php delete mode 100644 app/Traits/Shared.php create mode 100644 resources/views/livewire/deploy-application.blade.php create mode 100644 resources/views/livewire/poll-activity.blade.php create mode 100644 resources/views/project/application.blade.php create mode 100644 resources/views/project/database.blade.php create mode 100644 resources/views/project/deployment.blade.php create mode 100644 resources/views/project/environments.blade.php create mode 100644 resources/views/project/resources.blade.php create mode 100644 resources/views/project/service.blade.php diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php new file mode 100644 index 000000000..b479a6d19 --- /dev/null +++ b/app/Http/Controllers/ProjectController.php @@ -0,0 +1,109 @@ +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]); + } +} diff --git a/app/Http/Livewire/DemoDeployApplication.php b/app/Http/Livewire/DemoDeployApplication.php deleted file mode 100644 index f038a72cb..000000000 --- a/app/Http/Livewire/DemoDeployApplication.php +++ /dev/null @@ -1,81 +0,0 @@ -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'); - } -} diff --git a/app/Http/Livewire/DeployApplication.php b/app/Http/Livewire/DeployApplication.php new file mode 100644 index 000000000..367b2d3e7 --- /dev/null +++ b/app/Http/Livewire/DeployApplication.php @@ -0,0 +1,72 @@ +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'); + } +} diff --git a/app/Http/Livewire/PollActivity.php b/app/Http/Livewire/PollActivity.php new file mode 100644 index 000000000..c754cca00 --- /dev/null +++ b/app/Http/Livewire/PollActivity.php @@ -0,0 +1,27 @@ +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'); + } +} diff --git a/app/Traits/Shared.php b/app/Traits/Shared.php deleted file mode 100644 index c6884a7a0..000000000 --- a/app/Traits/Shared.php +++ /dev/null @@ -1,14 +0,0 @@ - 1, - 'name'=> "My first database", + 'name'=> "My first service", 'environment_id' => $environment_1->id, 'destination_id' => $standalone_docker_1->id, 'destination_type' => StandaloneDocker::class, diff --git a/docker/builder/Dockerfile b/docker/builder/Dockerfile index 16aa2a965..32099603b 100644 --- a/docker/builder/Dockerfile +++ b/docker/builder/Dockerfile @@ -12,6 +12,7 @@ ARG PACK_VERSION=0.27.0 ARG NIXPACKS_VERSION=1.6.0 USER root +WORKDIR /artifacts RUN apk add --no-cache bash curl git git-lfs openssh-client tar tini RUN mkdir -p ~/.docker/cli-plugins RUN curl -SL https://cdn.coollabs.io/bin/$TARGETPLATFORM/docker-$DOCKER_VERSION -o /usr/bin/docker diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 030b2379b..d0477bc9e 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -1,46 +1,8 @@ -

- Coolify v4 🎉 -

Projects

- + @forelse ($projects as $project) + {{ data_get($project, 'name') }} + @empty +

No projects found.

+ @endforelse
diff --git a/resources/views/livewire/deploy-application.blade.php b/resources/views/livewire/deploy-application.blade.php new file mode 100644 index 000000000..14d29bd9c --- /dev/null +++ b/resources/views/livewire/deploy-application.blade.php @@ -0,0 +1,3 @@ +
+ +
diff --git a/resources/views/livewire/poll-activity.blade.php b/resources/views/livewire/poll-activity.blade.php new file mode 100644 index 000000000..64e8a843d --- /dev/null +++ b/resources/views/livewire/poll-activity.blade.php @@ -0,0 +1,8 @@ +
+ @isset($activity?->id) +
+ Activity: {{ $activity?->id ?? 'waiting' }} +
+
{{ data_get($activity, 'description') }}
+ @endisset +
diff --git a/resources/views/project/application.blade.php b/resources/views/project/application.blade.php new file mode 100644 index 000000000..36ab57146 --- /dev/null +++ b/resources/views/project/application.blade.php @@ -0,0 +1,6 @@ + +

Application

+

Name: {{ $project->name }}

+

UUID: {{ $project->uuid }}

+ +
diff --git a/resources/views/project/database.blade.php b/resources/views/project/database.blade.php new file mode 100644 index 000000000..f256fecb9 --- /dev/null +++ b/resources/views/project/database.blade.php @@ -0,0 +1,5 @@ + +

Database

+ + +
diff --git a/resources/views/project/deployment.blade.php b/resources/views/project/deployment.blade.php new file mode 100644 index 000000000..5ca6091fa --- /dev/null +++ b/resources/views/project/deployment.blade.php @@ -0,0 +1,8 @@ + +

Deployment

+

Name: {{ $project->name }}

+

UUID: {{ $project->uuid }}

+ +

Deployment UUID: {{ $deployment->uuid }}

+ +
diff --git a/resources/views/project/environments.blade.php b/resources/views/project/environments.blade.php new file mode 100644 index 000000000..bf9efb655 --- /dev/null +++ b/resources/views/project/environments.blade.php @@ -0,0 +1,11 @@ + +

Environments

+ + @foreach ($project->environments as $environment) +
+ + {{ $environment->name }} + +
+ @endforeach +
diff --git a/resources/views/project/resources.blade.php b/resources/views/project/resources.blade.php new file mode 100644 index 000000000..ec5976d25 --- /dev/null +++ b/resources/views/project/resources.blade.php @@ -0,0 +1,29 @@ + +

Resources

+ + @foreach ($project->environments as $environment) +
+ @foreach ($environment->applications as $application) +

+ + {{ $application->name }} + +

+ @endforeach + @foreach ($environment->databases as $database) +

+ + {{ $database->name }} + +

+ @endforeach + @foreach ($environment->services as $service) +

+ + {{ $service->name }} + +

+ @endforeach +
+ @endforeach +
diff --git a/resources/views/project/service.blade.php b/resources/views/project/service.blade.php new file mode 100644 index 000000000..04cbcd4df --- /dev/null +++ b/resources/views/project/service.blade.php @@ -0,0 +1,5 @@ + +

Service

+ + +
diff --git a/routes/web.php b/routes/web.php index 18cde30a7..875a117c8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ 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 () { return view('profile'); });