Merge pull request #1061 from coollabsio/patricio-wip-11

Refactor for CoolifyTask
This commit is contained in:
Andras Bacsai 2023-05-03 08:57:33 +02:00 committed by GitHub
commit a6fd3f102d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 131 additions and 91 deletions

View File

@ -1,10 +1,14 @@
name: Docker Image CI
on:
# push:
# branches: [ "main" ]
# pull_request:
# branches: [ "*" ]
push:
branches: [ "main" ]
branches: ["this-does-not-exist"]
pull_request:
branches: [ "*" ]
branches: ["this-does-not-exist"]
jobs:
build:

3
.gitignore vendored
View File

@ -21,5 +21,8 @@ yarn-error.log
/.bash_history
/_volumes
resources/recipes
.lesshst
psysh_history
.psql_history

View File

@ -1,16 +1,21 @@
<?php
namespace App\Actions\RemoteProcess;
namespace App\Actions\CoolifyTask;
use App\Data\RemoteProcessArgs;
use App\Jobs\ExecuteRemoteProcess;
use App\Data\CoolifyTaskArgs;
use App\Jobs\CoolifyTask;
use Spatie\Activitylog\Models\Activity;
class DispatchRemoteProcess
/**
* 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;
public function __construct(RemoteProcessArgs $remoteProcessArgs)
public function __construct(CoolifyTaskArgs $remoteProcessArgs)
{
if ($remoteProcessArgs->model) {
$properties = $remoteProcessArgs->toArray();
@ -31,7 +36,7 @@ class DispatchRemoteProcess
public function __invoke(): Activity
{
$job = new ExecuteRemoteProcess($this->activity);
$job = new CoolifyTask($this->activity);
dispatch($job);
$this->activity->refresh();
return $this->activity;

View File

@ -1,6 +1,6 @@
<?php
namespace App\Actions\RemoteProcess;
namespace App\Actions\CoolifyTask;
use App\Enums\ActivityTypes;
use App\Enums\ProcessStatus;
@ -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::INLINE->value && $activity->getExtraProperty('type') !== ActivityTypes::DEPLOYMENT->value) {
throw new \RuntimeException('Incompatible Activity to run a remote command.');
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Data;
use App\Enums\ProcessStatus;
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(
public string $server_ip,
public string $private_key_location,
public string $command,
public int $port,
public string $user,
public string $type,
public ?string $type_uuid = null,
public ?Model $model = null,
public string $status = ProcessStatus::HOLDING->value,
) {
}
}

View File

@ -1,24 +0,0 @@
<?php
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 $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,
public ?Model $model = null,
) {
}
}

View File

@ -4,6 +4,6 @@ namespace App\Enums;
enum ActivityTypes: string
{
case REMOTE_PROCESS = 'remote_process';
case INLINE = 'inline';
case DEPLOYMENT = 'deployment';
}

View File

@ -56,8 +56,14 @@ class ApplicationController extends Controller
if (!$application) {
return redirect()->route('dashboard');
}
$activity = Activity::where('properties->deployment_uuid', '=', $deployment_uuid)->first();
$activity = Activity::where('properties->type_uuid', '=', $deployment_uuid)->first();
if (!$activity) {
return redirect()->route('project.application.deployments', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'application_uuid' => $application->uuid,
]);
}
return view('project.application.deployment', [
'application' => $application,
'activity' => $activity,

View File

@ -93,7 +93,10 @@ class ProjectController extends Controller
if (!$application) {
return redirect()->route('dashboard');
}
$activity = Activity::where('properties->deployment_uuid', '=', $deployment_uuid)->first();
$activity = Activity::query()
->where('properties->type', '=', 'deployment')
->where('properties->uuid', '=', $deployment_uuid)
->first();
return view('project.application.deployment', [
'application' => $application,

View File

@ -45,7 +45,7 @@ class StandaloneDocker extends Component
$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);
}
}

View File

@ -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::INLINE->value);
$this->emit('updateInitiated');
} else {
$latestVersion = getLatestVersionOfCoolify();
@ -31,19 +32,19 @@ class ForceUpgrade extends Component
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::INLINE->value);
$this->emit('updateInitiated');
}

View File

@ -73,7 +73,7 @@ class Deploy extends Component
}
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();

View File

@ -12,7 +12,7 @@ class GetDeployments extends Component
public string $status;
public function polling()
{
$activity = Activity::where('properties->deployment_uuid', '=', $this->deployment_uuid)->first();
$activity = Activity::where('properties->type_uuid', '=', $this->deployment_uuid)->first();
$this->created_at = $activity->created_at;
$this->status = data_get($activity, 'properties.status');
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Livewire\Project\Application;
use App\Enums\ActivityTypes;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
@ -14,7 +15,9 @@ class PollDeployment extends Component
public function polling()
{
if ( is_null($this->activity) && isset($this->deployment_uuid)) {
$this->activity = Activity::where('properties->deployment_uuid', '=', $this->deployment_uuid)
$this->activity = Activity::query()
->where('properties->type', '=', ActivityTypes::DEPLOYMENT->value)
->where('properties->type_uuid', '=', $this->deployment_uuid)
->first();
} else {
$this->activity?->refresh();

View File

@ -38,7 +38,7 @@ class PublicGitRepository extends Component
];
public function mount()
{
if (env('APP_ENV') === 'local') {
if (config('app.env') === 'local') {
$this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
$this->port = 3000;
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Livewire;
use App\Enums\ActivityTypes;
use App\Models\Server;
use Livewire\Component;
@ -31,19 +32,19 @@ class RunCommand extends Component
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::INLINE->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::INLINE->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::INLINE->value);
}
public function polling()

View File

@ -28,15 +28,15 @@ class Form extends Component
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()
{

View File

@ -38,7 +38,7 @@ class ContainerStatusJob implements ShouldQueue
$not_found_applications = $applications;
$containers = collect();
foreach ($servers as $server) {
$output = runRemoteCommandSync($server, ['docker ps -a -q --format \'{{json .}}\'']);
$output = instantRemoteProcess($server, ['docker ps -a -q --format \'{{json .}}\'']);
$containers = $containers->concat(formatDockerCmdOutputToJson($output));
}
foreach ($containers as $container) {
@ -67,7 +67,7 @@ class ContainerStatusJob implements ShouldQueue
return;
}
if ($application->destination->server) {
$container = runRemoteCommandSync($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]);
$container = instantRemoteProcess($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]);
$container = formatDockerCmdOutputToJson($container);
$application->status = $container[0]['Status'];
$application->save();

View File

@ -2,7 +2,7 @@
namespace App\Jobs;
use App\Actions\RemoteProcess\RunRemoteProcess;
use App\Actions\CoolifyTask\RunRemoteProcess;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -10,7 +10,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Spatie\Activitylog\Models\Activity;
class ExecuteRemoteProcess implements ShouldQueue
class CoolifyTask implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

View File

@ -2,8 +2,8 @@
namespace App\Jobs;
use App\Actions\RemoteProcess\RunRemoteProcess;
use App\Data\RemoteProcessArgs;
use App\Actions\CoolifyTask\RunRemoteProcess;
use App\Data\CoolifyTaskArgs;
use App\Enums\ActivityTypes;
use App\Models\Application;
use App\Models\InstanceSettings;
@ -55,14 +55,14 @@ class DeployApplicationJob implements ShouldQueue
$private_key_location = savePrivateKeyForServer($server);
$remoteProcessArgs = new RemoteProcessArgs(
$remoteProcessArgs = new CoolifyTaskArgs(
server_ip: $server->ip,
private_key_location: $private_key_location,
deployment_uuid: $this->deployment_uuid,
command: 'overwritten-later',
port: $server->port,
user: $server->user,
type: ActivityTypes::DEPLOYMENT->value,
type_uuid: $this->deployment_uuid,
);
$this->activity = activity()

View File

@ -31,7 +31,7 @@ class DockerCleanupDanglingImagesJob implements ShouldQueue
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());

View File

@ -94,13 +94,12 @@ class Application extends BaseModel
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function deployments()
{
return Activity::where('subject_id', $this->id)->where('properties->deployment_uuid', '!=', null)->orderBy('created_at', 'desc')->get();
return Activity::where('subject_id', $this->id)->where('properties->type', '=', 'deployment')->orderBy('created_at', 'desc')->get();
}
public function get_deployment(string $deployment_uuid)
{
return Activity::where('subject_id', $this->id)->where('properties->deployment_uuid', '=', $deployment_uuid)->first();
return Activity::where('subject_id', $this->id)->where('properties->type_uuid', '=', $deployment_uuid)->first();
}
}

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use App\Jobs\CoolifyTask;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Queue;
@ -31,7 +32,7 @@ class AppServiceProvider extends ServiceProvider
{
Queue::after(function (JobProcessed $event) {
// @TODO: Remove `coolify-builder` container after the remoteProcess job is finishged and remoteProcess->type == `deployment`.
if ($event->job->resolveName() === 'App\Jobs\ExecuteRemoteProcess') {
if ($event->job->resolveName() === CoolifyTask::class) {
}
});

View File

@ -1,8 +1,7 @@
<?php
use App\Actions\RemoteProcess\DispatchRemoteProcess;
use App\Data\RemoteProcessArgs;
use App\Enums\ActivityTypes;
use App\Actions\CoolifyTask\PrepareCoolifyTask;
use App\Data\CoolifyTaskArgs;
use App\Models\Server;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
@ -19,10 +18,11 @@ if (!function_exists('remoteProcess')) {
*
*/
function remoteProcess(
array $command,
Server $server,
?string $deployment_uuid = null,
?Model $model = null,
array $command,
Server $server,
string $type,
?string $type_uuid = null,
?Model $model = null,
): Activity {
$command_string = implode("\n", $command);
@ -32,17 +32,17 @@ if (!function_exists('remoteProcess')) {
$private_key_location = savePrivateKeyForServer($server);
return resolve(DispatchRemoteProcess::class, [
'remoteProcessArgs' => new RemoteProcessArgs(
return resolve(PrepareCoolifyTask::class, [
'remoteProcessArgs' => new CoolifyTaskArgs(
server_ip: $server->ip,
private_key_location: $private_key_location,
deployment_uuid: $deployment_uuid,
command: <<<EOT
{$command_string}
EOT,
port: $server->port,
user: $server->user,
type: $deployment_uuid ? ActivityTypes::DEPLOYMENT->value : ActivityTypes::REMOTE_PROCESS->value,
type: $type,
type_uuid: $type_uuid,
model: $model,
),
])();
@ -119,8 +119,8 @@ if (!function_exists('formatDockerLabelsToJson')) {
})[0];
}
}
if (!function_exists('runRemoteCommandSync')) {
function runRemoteCommandSync(Server $server, array $command, $throwError = true)
if (!function_exists('instantRemoteProcess')) {
function instantRemoteProcess(Server $server, array $command, $throwError = true)
{
$command_string = implode("\n", $command);
$private_key_location = savePrivateKeyForServer($server);

View File

@ -18,7 +18,7 @@
@if (data_get($application, 'ports_mappings_array'))
@foreach ($application->ports_mappings_array as $port)
@if (env('APP_ENV') === 'local')
@if (config('app.env') === 'local')
<a target="_blank" href="http://localhost:{{ explode(':', $port)[0] }}">Open
{{ explode(':', $port)[0] }}</a>
@else

View File

@ -1,3 +1,6 @@
<div>
<pre style="width: 100%;overflow-y: scroll;" @if ($isKeepAliveOn) wire:poll.750ms="polling" @endif>{{ \App\Actions\RemoteProcess\RunRemoteProcess::decodeOutput($activity) }}</pre>
<pre
style="width: 100%;overflow-y: scroll;"
@if ($isKeepAliveOn) wire:poll.750ms="polling" @endif
>{{ \App\Actions\CoolifyTask\RunRemoteProcess::decodeOutput($activity) }}</pre>
</div>

View File

@ -1,7 +1,7 @@
<div>
<div>
<label for="command">
<input autofocus id="command" wire:model.defer="command" type="text" wire:keydown.enter="runCommand" />
<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->uuid }}">{{ $server->name }}</option>
@ -21,6 +21,9 @@
@endif
</div>
@isset($activity?->id)
<pre style="width: 100%;overflow-y: scroll;" @if ($isKeepAliveOn || $manualKeepAlive) wire:poll.750ms="polling" @endif>{{ data_get($activity, 'description') }}</pre>
<pre
style="width: 100%;overflow-y: scroll;"
@if ($isKeepAliveOn) wire:poll.750ms="polling" @endif
>{{ \App\Actions\CoolifyTask\RunRemoteProcess::decodeOutput($activity) }}</pre>
@endisset
</div>

View File

@ -3,7 +3,7 @@
<x-applications.navbar :applicationId="$application->id" />
<div class="pt-2">
@forelse ($deployments as $deployment)
<livewire:project.application.get-deployments :deployment_uuid="data_get($deployment->properties, 'deployment_uuid')" :created_at="data_get($deployment, 'created_at')" :status="data_get($deployment->properties, 'status')" />
<livewire:project.application.get-deployments :deployment_uuid="data_get($deployment->properties, 'type_uuid')" :created_at="data_get($deployment, 'created_at')" :status="data_get($deployment->properties, 'status')" />
@empty
<p>No deployments found.</p>
@endforelse

View File

@ -48,6 +48,12 @@ function vite {
function build-builder {
act -W .github/workflows/coolify-builder.yml --secret-file .env.secrets
}
function tinker {
bash vendor/bin/spin exec -u webuser coolify php artisan tinker
}
function db {
bash vendor/bin/spin exec -u webuser coolify php artisan db
}
function default {
help
}

View File

@ -1,7 +1,7 @@
<?php
use App\Actions\RemoteProcess\RunRemoteProcess;
use App\Actions\RemoteProcess\TidyOutput;
use App\Actions\CoolifyTask\RunRemoteProcess;
use App\Actions\CoolifyTask\TidyOutput;
use App\Models\User;
use App\Models\Server;
use Database\Seeders\DatabaseSeeder;

View File

@ -1,6 +1,6 @@
<?php
use App\Actions\RemoteProcess\RunRemoteProcess;
use App\Actions\CoolifyTask\RunRemoteProcess;
use App\Models\Server;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\DatabaseMigrations;