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;
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;

View File

@ -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.');
}

View File

@ -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(

View File

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

View File

@ -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);
}
}

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::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');
}

View File

@ -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();

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 @@ 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()

View File

@ -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()
{

View File

@ -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;

View File

@ -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());

View File

@ -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) {
}
});

View File

@ -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,
),