This commit is contained in:
Joao Patricio
2023-05-03 07:24:34 +01:00
parent ce9fb38055
commit 79a850f3b9
13 changed files with 36 additions and 25 deletions

View File

@@ -3,9 +3,14 @@
namespace App\Actions\CoolifyTask; namespace App\Actions\CoolifyTask;
use App\Data\CoolifyTaskArgs; use App\Data\CoolifyTaskArgs;
use App\Jobs\HandleCoolifyTaskInQueue; use App\Jobs\CoolifyTask;
use Spatie\Activitylog\Models\Activity; 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 class PrepareCoolifyTask
{ {
protected Activity $activity; protected Activity $activity;
@@ -31,7 +36,7 @@ class PrepareCoolifyTask
public function __invoke(): Activity public function __invoke(): Activity
{ {
$job = new HandleCoolifyTaskInQueue($this->activity); $job = new CoolifyTask($this->activity);
dispatch($job); dispatch($job);
$this->activity->refresh(); $this->activity->refresh();
return $this->activity; return $this->activity;

View File

@@ -36,7 +36,7 @@ class RunRemoteProcess
public function __construct(Activity $activity, bool $hideFromOutput = false, bool $isFinished = false, bool $ignoreErrors = false) 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.'); throw new \RuntimeException('Incompatible Activity to run a remote command.');
} }

View File

@@ -6,6 +6,9 @@ use App\Enums\ProcessStatus;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Spatie\LaravelData\Data; use Spatie\LaravelData\Data;
/**
* The parameters to execute a CoolifyTask, organized in a DTO.
*/
class CoolifyTaskArgs extends Data class CoolifyTaskArgs extends Data
{ {
public function __construct( public function __construct(

View File

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

View File

@@ -45,7 +45,7 @@ class StandaloneDocker extends Component
$server = Server::find($this->server_id); $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); return redirect()->route('destination.show', $docker->uuid);
} }
} }

View File

@@ -2,25 +2,26 @@
namespace App\Http\Livewire; namespace App\Http\Livewire;
use App\Enums\ActivityTypes;
use App\Models\Server; use App\Models\Server;
use Illuminate\Support\Facades\Http;
use Livewire\Component; use Livewire\Component;
class ForceUpgrade extends Component class ForceUpgrade extends Component
{ {
public function upgrade() 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(); $server = Server::where('ip', 'coolify-testing-host')->first();
if (!$server) { if (!$server) {
return; return;
} }
runRemoteCommandSync($server, [ instantRemoteProcess($server, [
"sleep 2" "sleep 2"
]); ]);
remoteProcess([ remoteProcess([
"sleep 10" "sleep 10"
], $server); ], $server, ActivityTypes::INSTANT->value);
$this->emit('updateInitiated'); $this->emit('updateInitiated');
} else { } else {
$latestVersion = getLatestVersionOfCoolify(); $latestVersion = getLatestVersionOfCoolify();
@@ -31,19 +32,19 @@ class ForceUpgrade extends Component
return; return;
} }
runRemoteCommandSync($server, [ instantRemoteProcess($server, [
"curl -fsSL $cdn/docker-compose.yml -o /data/coolify/source/docker-compose.yml", "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/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/.env.production -o /data/coolify/source/.env.production",
"curl -fsSL $cdn/upgrade.sh -o /data/coolify/source/upgrade.sh", "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", "docker compose -f /data/coolify/source/docker-compose.yml -f /data/coolify/source/docker-compose.prod.yml pull",
]); ]);
remoteProcess([ remoteProcess([
"bash /data/coolify/source/upgrade.sh $latestVersion" "bash /data/coolify/source/upgrade.sh $latestVersion"
], $server); ], $server, ActivityTypes::INSTANT->value);
$this->emit('updateInitiated'); $this->emit('updateInitiated');
} }

View File

@@ -73,7 +73,7 @@ class Deploy extends Component
} }
public function stop() 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') { if ($this->application->status != 'exited') {
$this->application->status = 'exited'; $this->application->status = 'exited';
$this->application->save(); $this->application->save();

View File

@@ -2,6 +2,7 @@
namespace App\Http\Livewire; namespace App\Http\Livewire;
use App\Enums\ActivityTypes;
use App\Models\Server; use App\Models\Server;
use Livewire\Component; use Livewire\Component;
@@ -31,19 +32,19 @@ class RunCommand extends Component
public function runCommand() public function runCommand()
{ {
$this->isKeepAliveOn = true; $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() public function runSleepingBeauty()
{ {
$this->isKeepAliveOn = true; $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() public function runDummyProjectBuild()
{ {
$this->isKeepAliveOn = true; $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() public function polling()

View File

@@ -28,15 +28,15 @@ class Form extends Component
public function installDocker() public function installDocker()
{ {
$config = base64_encode('{ "live-restore": true }'); $config = base64_encode('{ "live-restore": true }');
runRemoteCommandSync($this->server, [ instantRemoteProcess($this->server, [
"curl https://releases.rancher.com/install-docker/23.0.sh | sh" "curl https://releases.rancher.com/install-docker/23.0.sh | sh"
]); ]);
} }
public function checkServer() public function checkServer()
{ {
$this->uptime = runRemoteCommandSync($this->server, ['uptime']); $this->uptime = instantRemoteProcess($this->server, ['uptime']);
$this->dockerVersion = runRemoteCommandSync($this->server, ['docker version|head -2|grep -i version'], false); $this->dockerVersion = instantRemoteProcess($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->dockerComposeVersion = instantRemoteProcess($this->server, ['docker compose version|head -2|grep -i version'], false);
} }
public function submit() public function submit()
{ {

View File

@@ -10,7 +10,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Models\Activity;
class HandleCoolifyTaskInQueue implements ShouldQueue class CoolifyTask implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

View File

@@ -31,7 +31,7 @@ class DockerCleanupDanglingImagesJob implements ShouldQueue
try { try {
$servers = Server::all(); $servers = Server::all();
foreach ($servers as $server) { foreach ($servers as $server) {
runRemoteCommandSync($server, ['docker image prune -f']); instantRemoteProcess($server, ['docker image prune -f']);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error($e->getMessage()); Log::error($e->getMessage());

View File

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

View File

@@ -20,6 +20,7 @@ if (!function_exists('remoteProcess')) {
function remoteProcess( function remoteProcess(
array $command, array $command,
Server $server, Server $server,
string $type,
?string $type_uuid = null, ?string $type_uuid = null,
?Model $model = null, ?Model $model = null,
): Activity { ): Activity {
@@ -40,7 +41,7 @@ if (!function_exists('remoteProcess')) {
EOT, EOT,
port: $server->port, port: $server->port,
user: $server->user, user: $server->user,
type: $type_uuid, type: $type,
type_uuid: $type_uuid, type_uuid: $type_uuid,
model: $model, model: $model,
), ),