From 79a850f3b90419744dc687109059d6a4c8169239 Mon Sep 17 00:00:00 2001 From: Joao Patricio Date: Wed, 3 May 2023 07:24:34 +0100 Subject: [PATCH] wip --- app/Actions/CoolifyTask/PrepareCoolifyTask.php | 9 +++++++-- app/Actions/CoolifyTask/RunRemoteProcess.php | 2 +- app/Data/CoolifyTaskArgs.php | 3 +++ app/Enums/ActivityTypes.php | 2 +- .../Livewire/Destination/New/StandaloneDocker.php | 2 +- app/Http/Livewire/ForceUpgrade.php | 15 ++++++++------- app/Http/Livewire/Project/Application/Deploy.php | 2 +- app/Http/Livewire/RunCommand.php | 7 ++++--- app/Http/Livewire/Server/Form.php | 8 ++++---- app/Jobs/CoolifyTask.php | 2 +- app/Jobs/DockerCleanupDanglingImagesJob.php | 2 +- app/Providers/AppServiceProvider.php | 4 ++-- bootstrap/helpers.php | 3 ++- 13 files changed, 36 insertions(+), 25 deletions(-) diff --git a/app/Actions/CoolifyTask/PrepareCoolifyTask.php b/app/Actions/CoolifyTask/PrepareCoolifyTask.php index d9a3ece2b..c67a8bcb3 100644 --- a/app/Actions/CoolifyTask/PrepareCoolifyTask.php +++ b/app/Actions/CoolifyTask/PrepareCoolifyTask.php @@ -3,9 +3,14 @@ namespace App\Actions\CoolifyTask; use App\Data\CoolifyTaskArgs; -use App\Jobs\HandleCoolifyTaskInQueue; +use App\Jobs\CoolifyTask; use Spatie\Activitylog\Models\Activity; +/** + * The initial step to run a `CoolifyTask`: a remote SSH process + * with monitoring/tracking/trace feature. Such thing is made + * possible using an Activity model and some attributes. + */ class PrepareCoolifyTask { protected Activity $activity; @@ -31,7 +36,7 @@ public function __construct(CoolifyTaskArgs $remoteProcessArgs) public function __invoke(): Activity { - $job = new HandleCoolifyTaskInQueue($this->activity); + $job = new CoolifyTask($this->activity); dispatch($job); $this->activity->refresh(); return $this->activity; diff --git a/app/Actions/CoolifyTask/RunRemoteProcess.php b/app/Actions/CoolifyTask/RunRemoteProcess.php index a8c49a7c7..979144b01 100644 --- a/app/Actions/CoolifyTask/RunRemoteProcess.php +++ b/app/Actions/CoolifyTask/RunRemoteProcess.php @@ -36,7 +36,7 @@ class RunRemoteProcess public function __construct(Activity $activity, bool $hideFromOutput = false, bool $isFinished = false, bool $ignoreErrors = false) { - if ($activity->getExtraProperty('type') !== ActivityTypes::REMOTE_PROCESS->value && $activity->getExtraProperty('type') !== ActivityTypes::DEPLOYMENT->value) { + if ($activity->getExtraProperty('type') !== ActivityTypes::INSTANT->value && $activity->getExtraProperty('type') !== ActivityTypes::DEPLOYMENT->value) { throw new \RuntimeException('Incompatible Activity to run a remote command.'); } diff --git a/app/Data/CoolifyTaskArgs.php b/app/Data/CoolifyTaskArgs.php index 803143a9a..09ab923ab 100644 --- a/app/Data/CoolifyTaskArgs.php +++ b/app/Data/CoolifyTaskArgs.php @@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Model; use Spatie\LaravelData\Data; +/** + * The parameters to execute a CoolifyTask, organized in a DTO. + */ class CoolifyTaskArgs extends Data { public function __construct( diff --git a/app/Enums/ActivityTypes.php b/app/Enums/ActivityTypes.php index 5bcab7cd7..fac79267d 100644 --- a/app/Enums/ActivityTypes.php +++ b/app/Enums/ActivityTypes.php @@ -4,6 +4,6 @@ enum ActivityTypes: string { - case REMOTE_PROCESS = 'remote_process'; + case INSTANT = 'instant'; case DEPLOYMENT = 'deployment'; } diff --git a/app/Http/Livewire/Destination/New/StandaloneDocker.php b/app/Http/Livewire/Destination/New/StandaloneDocker.php index 1d22e65fb..5abf19a90 100644 --- a/app/Http/Livewire/Destination/New/StandaloneDocker.php +++ b/app/Http/Livewire/Destination/New/StandaloneDocker.php @@ -45,7 +45,7 @@ public function submit() $server = Server::find($this->server_id); - runRemoteCommandSync($server, ['docker network create --attachable ' . $this->network], throwError: false); + instantRemoteProcess($server, ['docker network create --attachable ' . $this->network], throwError: false); return redirect()->route('destination.show', $docker->uuid); } } diff --git a/app/Http/Livewire/ForceUpgrade.php b/app/Http/Livewire/ForceUpgrade.php index ad6dd2ba4..706417014 100644 --- a/app/Http/Livewire/ForceUpgrade.php +++ b/app/Http/Livewire/ForceUpgrade.php @@ -2,25 +2,26 @@ namespace App\Http\Livewire; +use App\Enums\ActivityTypes; use App\Models\Server; -use Illuminate\Support\Facades\Http; use Livewire\Component; class ForceUpgrade extends Component { public function upgrade() { - if (env('APP_ENV') === 'local') { + //if (env('APP_ENV') === 'local') { + if (config('app.env') === 'local') { $server = Server::where('ip', 'coolify-testing-host')->first(); if (!$server) { return; } - runRemoteCommandSync($server, [ + instantRemoteProcess($server, [ "sleep 2" ]); remoteProcess([ "sleep 10" - ], $server); + ], $server, ActivityTypes::INSTANT->value); $this->emit('updateInitiated'); } else { $latestVersion = getLatestVersionOfCoolify(); @@ -31,19 +32,19 @@ public function upgrade() return; } - runRemoteCommandSync($server, [ + instantRemoteProcess($server, [ "curl -fsSL $cdn/docker-compose.yml -o /data/coolify/source/docker-compose.yml", "curl -fsSL $cdn/docker-compose.prod.yml -o /data/coolify/source/docker-compose.prod.yml", "curl -fsSL $cdn/.env.production -o /data/coolify/source/.env.production", "curl -fsSL $cdn/upgrade.sh -o /data/coolify/source/upgrade.sh", ]); - runRemoteCommandSync($server, [ + instantRemoteProcess($server, [ "docker compose -f /data/coolify/source/docker-compose.yml -f /data/coolify/source/docker-compose.prod.yml pull", ]); remoteProcess([ "bash /data/coolify/source/upgrade.sh $latestVersion" - ], $server); + ], $server, ActivityTypes::INSTANT->value); $this->emit('updateInitiated'); } diff --git a/app/Http/Livewire/Project/Application/Deploy.php b/app/Http/Livewire/Project/Application/Deploy.php index a4ac4ab2d..ba31e4fbf 100644 --- a/app/Http/Livewire/Project/Application/Deploy.php +++ b/app/Http/Livewire/Project/Application/Deploy.php @@ -73,7 +73,7 @@ public function delete() } public function stop() { - runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application->uuid}"]); + instantRemoteProcess($this->destination->server, ["docker rm -f {$this->application->uuid}"]); if ($this->application->status != 'exited') { $this->application->status = 'exited'; $this->application->save(); diff --git a/app/Http/Livewire/RunCommand.php b/app/Http/Livewire/RunCommand.php index 9eaa2bbd1..1e3f78f15 100755 --- a/app/Http/Livewire/RunCommand.php +++ b/app/Http/Livewire/RunCommand.php @@ -2,6 +2,7 @@ namespace App\Http\Livewire; +use App\Enums\ActivityTypes; use App\Models\Server; use Livewire\Component; @@ -31,19 +32,19 @@ public function mount() public function runCommand() { $this->isKeepAliveOn = true; - $this->activity = remoteProcess([$this->command], Server::where('uuid', $this->server)->first()); + $this->activity = remoteProcess([$this->command], Server::where('uuid', $this->server)->first(), ActivityTypes::INSTANT->value); } 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'], Server::where('uuid', $this->server)->first()); + $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(), ActivityTypes::INSTANT->value); } public function runDummyProjectBuild() { $this->isKeepAliveOn = true; - $this->activity = remoteProcess([' cd projects/dummy-project', 'docker-compose build --no-cache'], Server::where('uuid', $this->server)->first()); + $this->activity = remoteProcess([' cd projects/dummy-project', 'docker-compose build --no-cache'], Server::where('uuid', $this->server)->first(), ActivityTypes::INSTANT->value); } public function polling() diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php index e4d9916fd..08d2d7cf9 100644 --- a/app/Http/Livewire/Server/Form.php +++ b/app/Http/Livewire/Server/Form.php @@ -28,15 +28,15 @@ public function mount() public function installDocker() { $config = base64_encode('{ "live-restore": true }'); - runRemoteCommandSync($this->server, [ + instantRemoteProcess($this->server, [ "curl https://releases.rancher.com/install-docker/23.0.sh | sh" ]); } public function checkServer() { - $this->uptime = runRemoteCommandSync($this->server, ['uptime']); - $this->dockerVersion = runRemoteCommandSync($this->server, ['docker version|head -2|grep -i version'], false); - $this->dockerComposeVersion = runRemoteCommandSync($this->server, ['docker compose version|head -2|grep -i version'], false); + $this->uptime = instantRemoteProcess($this->server, ['uptime']); + $this->dockerVersion = instantRemoteProcess($this->server, ['docker version|head -2|grep -i version'], false); + $this->dockerComposeVersion = instantRemoteProcess($this->server, ['docker compose version|head -2|grep -i version'], false); } public function submit() { diff --git a/app/Jobs/CoolifyTask.php b/app/Jobs/CoolifyTask.php index d2236027e..d96095cc4 100755 --- a/app/Jobs/CoolifyTask.php +++ b/app/Jobs/CoolifyTask.php @@ -10,7 +10,7 @@ use Illuminate\Queue\SerializesModels; use Spatie\Activitylog\Models\Activity; -class HandleCoolifyTaskInQueue implements ShouldQueue +class CoolifyTask implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; diff --git a/app/Jobs/DockerCleanupDanglingImagesJob.php b/app/Jobs/DockerCleanupDanglingImagesJob.php index d5798ba54..0938b6bb8 100644 --- a/app/Jobs/DockerCleanupDanglingImagesJob.php +++ b/app/Jobs/DockerCleanupDanglingImagesJob.php @@ -31,7 +31,7 @@ public function handle(): void try { $servers = Server::all(); foreach ($servers as $server) { - runRemoteCommandSync($server, ['docker image prune -f']); + instantRemoteProcess($server, ['docker image prune -f']); } } catch (\Exception $e) { Log::error($e->getMessage()); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 1256de2cb..9945221c8 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,7 +2,7 @@ namespace App\Providers; -use App\Jobs\HandleCoolifyTaskInQueue; +use App\Jobs\CoolifyTask; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Queue; @@ -32,7 +32,7 @@ public function boot(): void { Queue::after(function (JobProcessed $event) { // @TODO: Remove `coolify-builder` container after the remoteProcess job is finishged and remoteProcess->type == `deployment`. - if ($event->job->resolveName() === HandleCoolifyTaskInQueue::class) { + if ($event->job->resolveName() === CoolifyTask::class) { } }); diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 2ff739570..49569d1a4 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -20,6 +20,7 @@ function remoteProcess( array $command, Server $server, + string $type, ?string $type_uuid = null, ?Model $model = null, ): Activity { @@ -40,7 +41,7 @@ function remoteProcess( EOT, port: $server->port, user: $server->user, - type: $type_uuid, + type: $type, type_uuid: $type_uuid, model: $model, ),