An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat: add deployments as activity fix: tests refactor: remoteProcess
Andras Bacsai78c434458335
diff --git a/app/Actions/RemoteProcess/DispatchRemoteProcess.php b/app/Actions/RemoteProcess/DispatchRemoteProcess.phpindex 349c847c4c..0f9d601920 100644--- a/app/Actions/RemoteProcess/DispatchRemoteProcess.php+++ b/app/Actions/RemoteProcess/DispatchRemoteProcess.php@@ -10,10 +10,23 @@ class DispatchRemoteProcess{protected Activity $activity;- public function __construct(RemoteProcessArgs $remoteProcessArgs){- $this->activity = activity()- ->withProperties($remoteProcessArgs->toArray())- ->log("");+ public function __construct(RemoteProcessArgs $remoteProcessArgs)+ {+ if ($remoteProcessArgs->model) {+ $properties = $remoteProcessArgs->toArray();+ unset($properties['model']);++ $this->activity = activity()+ ->withProperties($properties)+ ->performedOn($remoteProcessArgs->model)+ ->event('deployment')+ ->log("");+ } else {+ $this->activity = activity()+ ->withProperties($remoteProcessArgs->toArray())+ ->event('remote_process')+ ->log("");+ }}public function __invoke(): Activitydiff --git a/app/Actions/RemoteProcess/RunRemoteProcess.php b/app/Actions/RemoteProcess/RunRemoteProcess.phpindex da2eeb8bcc..14b9190e12 100644--- a/app/Actions/RemoteProcess/RunRemoteProcess.php+++ b/app/Actions/RemoteProcess/RunRemoteProcess.php@@ -31,7 +31,7 @@ class RunRemoteProcess*/public function __construct(Activity $activity){- if ($activity->getExtraProperty('type') !== ActivityTypes::COOLIFY_PROCESS->value) {+ if ($activity->getExtraProperty('type') !== ActivityTypes::REMOTE_PROCESS->value) {throw new \RuntimeException('Incompatible Activity to run a remote command.');}@@ -64,7 +64,7 @@ class RunRemoteProcessprotected function getCommand(): string{$user = $this->activity->getExtraProperty('user');- $destination = $this->activity->getExtraProperty('destination');+ $server_ip = $this->activity->getExtraProperty('server_ip');$private_key_location = $this->activity->getExtraProperty('private_key_location');$port = $this->activity->getExtraProperty('port');$command = $this->activity->getExtraProperty('command');@@ -80,7 +80,7 @@ class RunRemoteProcess. '-o LogLevel=ERROR '. '-o ControlMaster=auto -o ControlPersist=yes -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/.ssh/ssh_mux_%h_%p_%r '. "-p {$port} "- . "{$user}@{$destination} "+ . "{$user}@{$server_ip} ". " 'bash -se' << \\$delimiter" . PHP_EOL. $command . PHP_EOL. $delimiter;diff --git a/app/Data/RemoteProcessArgs.php b/app/Data/RemoteProcessArgs.phpindex 139b94c718..57c46d8134 100644--- a/app/Data/RemoteProcessArgs.php+++ b/app/Data/RemoteProcessArgs.php@@ -4,17 +4,21 @@ namespace App\Data;use App\Enums\ActivityTypes;use App\Enums\ProcessStatus;+use Illuminate\Database\Eloquent\Model;use Spatie\LaravelData\Data;class RemoteProcessArgs extends Data{public function __construct(- public string $destination,- public string $private_key_location,- public string $command,- public int $port,- public string $user,- public string $type = ActivityTypes::COOLIFY_PROCESS->value,- public string $status = ProcessStatus::HOLDING->value,- ){}+ public Model|null $model,+ public string $server_ip,+ public string $private_key_location,+ public string|null $deployment_uuid,+ public string $command,+ public int $port,+ public string $user,+ public string $type = ActivityTypes::REMOTE_PROCESS->value,+ public string $status = ProcessStatus::HOLDING->value,+ ) {+ }}diff --git a/app/Enums/ActivityTypes.php b/app/Enums/ActivityTypes.phpindex 52a35e2f8f..ad1c10c7ab 100644--- a/app/Enums/ActivityTypes.php+++ b/app/Enums/ActivityTypes.php@@ -4,5 +4,5 @@ namespace App\Enums;enum ActivityTypes: string{- case COOLIFY_PROCESS = 'coolify_process';+ case REMOTE_PROCESS = 'remote_process';}diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.phpindex b479a6d193..43b17430cd 100644--- a/app/Http/Controllers/ProjectController.php+++ b/app/Http/Controllers/ProjectController.php@@ -3,6 +3,7 @@namespace App\Http\Controllers;use Illuminate\Http\Request;+use Illuminate\Support\Facades\DB;class ProjectController extends Controller{@@ -103,7 +104,7 @@ class ProjectController extends Controllerif (!$application) {return redirect()->route('home');}- $deployment = $application->deployments->where('uuid', $deployment_uuid)->first();- return view('project.deployment', ['project' => $project, 'deployment' => $deployment]);+ $activity = $application->get_deployment($deployment_uuid);+ return view('project.deployment', ['project' => $project, 'activity' => $activity]);}}diff --git a/app/Http/Livewire/DeployApplication.php b/app/Http/Livewire/DeployApplication.phpindex 6df06cc9e5..e6889f1e46 100644--- a/app/Http/Livewire/DeployApplication.php+++ b/app/Http/Livewire/DeployApplication.php@@ -21,7 +21,7 @@ class DeployApplication extends Component}private function start_builder_container(){- $this->command[] = "docker run --pull=always -d --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-builder >/dev/null";+ $this->command[] = "docker run --pull=always -d --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/coollabsio/coolify-builder >/dev/null 2>&1";}public function deploy(){@@ -36,18 +36,17 @@ class DeployApplication extends Component$wildcard_domain = $project_wildcard_domain ?? $global_wildcard_domain ?? null;// Create Deployment ID- $this->deployment_uuid = new Cuid2(10);+ $this->deployment_uuid = new Cuid2(12);$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('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);+ $this->activity = remoteProcess($this->command, $destination->server, $this->deployment_uuid, $application);// Create DeploymentDeployment::create([diff --git a/app/Http/Livewire/PollActivity.php b/app/Http/Livewire/PollActivity.phpindex c754cca005..3a7822d8a9 100644--- a/app/Http/Livewire/PollActivity.php+++ b/app/Http/Livewire/PollActivity.php@@ -3,16 +3,12 @@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();diff --git a/app/Http/Livewire/RunCommand.php b/app/Http/Livewire/RunCommand.phpindex 3e4db9e974..5bb84a56fd 100755--- a/app/Http/Livewire/RunCommand.php+++ b/app/Http/Livewire/RunCommand.php@@ -19,11 +19,13 @@ class RunCommand extends Componentpublic $servers = [];+ protected $rules = [+ 'server' => 'required',+ ];public function mount(){- $this->servers = Server::all()->pluck('name')->toArray();- $this->server = $this->servers[0];-+ $this->servers = Server::all();+ $this->server = $this->servers[0]->uuid;}public function render(){@@ -33,25 +35,19 @@ class RunCommand extends Componentpublic function runCommand(){$this->isKeepAliveOn = true;-- $this->activity = remoteProcess($this->command, $this->server);+ $this->activity = remoteProcess([$this->command], Server::where('uuid', $this->server)->first());}public function runSleepingBeauty(){$this->isKeepAliveOn = true;-- $this->activity = remoteProcess('x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done', $this->server);+ $this->activity = remoteProcess(['x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done'], Server::where('uuid', $this->server)->first());}public function runDummyProjectBuild(){$this->isKeepAliveOn = true;-- $this->activity = remoteProcess(<<<EOT- cd projects/dummy-project- ~/.docker/cli-plugins/docker-compose build --no-cache- EOT, $this->server);+ $this->activity = remoteProcess([' cd projects/dummy-project', 'docker-compose build --no-cache'], Server::where('uuid', $this->server)->first());}public function polling()diff --git a/app/Models/Application.php b/app/Models/Application.phpindex 78d3d9c966..afd52ae90d 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -2,6 +2,8 @@namespace App\Models;+use Spatie\Activitylog\Models\Activity;+class Application extends BaseModel{public function environment()@@ -20,8 +22,9 @@ class Application extends BaseModel{return $this->morphTo();}- public function deployments()++ public function get_deployment(string $deployment_uuid){- return $this->morphMany(Deployment::class, 'type');+ return Activity::where('subject_id', $this->id)->where('properties->deployment_uuid', '=', $deployment_uuid)->first();}}diff --git a/bootstrap/helpers.php b/bootstrap/helpers.phpindex 67c650b544..0b9498831e 100644--- a/bootstrap/helpers.php+++ b/bootstrap/helpers.php@@ -3,6 +3,7 @@use App\Actions\RemoteProcess\DispatchRemoteProcess;use App\Data\RemoteProcessArgs;use App\Models\Server;+use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Storage;use Spatie\Activitylog\Contracts\Activity;@@ -13,36 +14,33 @@ if (!function_exists('remoteProcess')) {**/function remoteProcess(- string $command,- string $destination+ array $command,+ Server $server,+ string|null $deployment_uuid = null,+ Model|null $model = null,): Activity {- $found_server = checkServer($destination);- checkTeam($found_server->team_id);+ $command_string = implode("\n", $command);+ // @TODO: Check if the user has access to this server+ // checkTeam($server->team_id);- $temp_file = 'id.rsa_' . 'root' . '@' . $found_server->ip;- Storage::disk('local')->put($temp_file, $found_server->privateKey->private_key, 'private');+ $temp_file = 'id.rsa_' . 'root' . '@' . $server->ip;+ Storage::disk('local')->put($temp_file, $server->privateKey->private_key, 'private');$private_key_location = '/var/www/html/storage/app/' . $temp_file;+return resolve(DispatchRemoteProcess::class, ['remoteProcessArgs' => new RemoteProcessArgs(- destination: $found_server->ip,+ model: $model,+ server_ip: $server->ip,+ deployment_uuid: $deployment_uuid,private_key_location: $private_key_location,command: <<<EOT- {$command}+ {$command_string}EOT,- port: $found_server->port,- user: $found_server->user,+ port: $server->port,+ user: $server->user,),])();}- function checkServer(string $destination)- {- // @TODO: Use UUID instead of name- $found_server = Server::where('name', $destination)->first();- if (!$found_server) {- throw new \RuntimeException('Server not found.');- };- return $found_server;- }function checkTeam(string $team_id){$found_team = auth()->user()->teams->pluck('id')->contains($team_id);diff --git a/database/seeders/ServerSeeder.php b/database/seeders/ServerSeeder.phpindex 1a65141395..548856ba77 100644--- a/database/seeders/ServerSeeder.php+++ b/database/seeders/ServerSeeder.php@@ -37,7 +37,7 @@ class ServerSeeder extends Seeder'id' => 3,'name' => "localhost",'description' => "This is the local machine",- 'user' => 'andrasbacsai',+ 'user' => 'root','ip' => "172.17.0.1",'team_id' => $root_team->id,'private_key_id' => $private_key_1->id,diff --git a/resources/views/livewire/poll-activity.blade.php b/resources/views/livewire/poll-activity.blade.phpindex 64e8a843d3..fc3eeed34e 100644--- a/resources/views/livewire/poll-activity.blade.php+++ b/resources/views/livewire/poll-activity.blade.php@@ -1,8 +1,5 @@<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>diff --git a/resources/views/livewire/run-command.blade.php b/resources/views/livewire/run-command.blade.phpindex 6482fc2988..085bc9bb7f 100755--- a/resources/views/livewire/run-command.blade.php+++ b/resources/views/livewire/run-command.blade.php@@ -4,7 +4,7 @@<input autofocus id="command" wire:model.defer="command" type="text" wire:keydown.enter="runCommand" /><select wire:model.defer="server">@foreach ($servers as $server)- <option value="{{ $server }}">{{ $server }}</option>+ <option value="{{ $server->uuid }}">{{ $server->name }}</option>@endforeach</select></label>@@ -21,13 +21,6 @@@endif</div>@isset($activity?->id)- <div>- Activity: <span>{{ $activity?->id ?? 'waiting' }}</span>- </div><pre style="width: 100%;overflow-y: scroll;" @if ($isKeepAliveOn || $manualKeepAlive) wire:poll.750ms="polling" @endif>{{ data_get($activity, 'description') }}</pre>- {{-- <div>- <div>Details:</div>- <pre style="width: 100%;overflow-y: scroll;">{{ json_encode(data_get($activity, 'properties'), JSON_PRETTY_PRINT) }}</pre>- </div> --}}@endisset</div>diff --git a/resources/views/project/deployment.blade.php b/resources/views/project/deployment.blade.phpindex 5ca6091fa4..4d58b00ac7 100644--- a/resources/views/project/deployment.blade.php+++ b/resources/views/project/deployment.blade.php@@ -2,7 +2,6 @@<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" />++ <livewire:poll-activity :activity="$activity" /></x-layout>diff --git a/tests/Feature/DockerCommandsTest.php b/tests/Feature/DockerCommandsTest.phpindex 8fc551a04e..a8418efc1f 100644--- a/tests/Feature/DockerCommandsTest.php+++ b/tests/Feature/DockerCommandsTest.php@@ -1,5 +1,6 @@<?php+use App\Models\Server;use Tests\Support\Output;it('starts a docker container correctly', function () {@@ -10,23 +11,23 @@ it('starts a docker container correctly', function () {// Generate a known name$containerName = 'coolify_test_' . now()->format('Ymd_his');- $host = 'testing-host';+ $host = Server::where('name', 'testing-local-docker-container')->first();// Assert there's no containers start with coolify_test_*- $activity = remoteProcess($areThereCoolifyTestContainers, $host);+ $activity = remoteProcess([$areThereCoolifyTestContainers], $host);$containers = Output::containerList($activity->getExtraProperty('stdout'));expect($containers)->toBeEmpty();// start a container nginx -d --name = $containerName- $activity = remoteProcess("docker run -d --rm --name {$containerName} nginx", $host);+ $activity = remoteProcess(["docker run -d --rm --name {$containerName} nginx"], $host);expect($activity->getExtraProperty('exitCode'))->toBe(0);// docker ps name = $container- $activity = remoteProcess($areThereCoolifyTestContainers, $host);+ $activity = remoteProcess([$areThereCoolifyTestContainers], $host);$containers = Output::containerList($activity->getExtraProperty('stdout'));expect($containers->where('Names', $containerName)->count())->toBe(1);// Stop testing containers- $activity = remoteProcess("docker stop $(docker ps --filter='name={$coolifyNamePrefix}*' -q)", $host);+ $activity = remoteProcess(["docker stop $(docker ps --filter='name={$coolifyNamePrefix}*' -q)"], $host);expect($activity->getExtraProperty('exitCode'))->toBe(0);});