Refactoring: extract process handling from async job.

This commit is contained in:
Joao Patricio 2023-03-21 10:32:38 +00:00
parent 34c9265aa9
commit 29fb40bd16
6 changed files with 27 additions and 39 deletions

View File

@ -3,32 +3,16 @@
namespace App\Actions\RemoteProcess;
use App\Data\RemoteProcessArgs;
use App\Enums\ActivityTypes;
use App\Enums\ProcessStatus;
use App\Jobs\ExecuteCoolifyProcess;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Models\Activity;
class DispatchRemoteProcess
{
protected Activity $activity;
// TODO Left 'root' as default user instead of 'coolify' because
// there's a task at TODO.md to run docker without sudo
public function __construct(
protected string $destination,
protected string $command,
protected ?int $port = 22,
protected ?string $user = 'root',
){
$arguments = new RemoteProcessArgs(
destination: $this->destination,
command: $this->command,
port: $this->port,
user: $this->user,
);
public function __construct(RemoteProcessArgs $remoteProcessArgs){
$this->activity = activity()
->withProperties($arguments->toArray())
->withProperties($remoteProcessArgs->toArray())
->log("Awaiting command to start...\n\n");
}
@ -42,5 +26,4 @@ public function __invoke(): Activity
return $this->activity;
}
}

View File

@ -7,7 +7,7 @@
use Illuminate\Process\ProcessResult;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Models\Activity;
class RunRemoteProcess
{
@ -73,10 +73,6 @@ protected function getCommand(): string
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no '
. '-o RequestTTY=no '
// Quiet mode. Causes most warning and diagnostic messages to be suppressed.
// Errors are still out put. This is to silence for example, that warning
// Permanently added <host and key type> to the list of known hosts.
. '-q '
. "-p {$port} "
. "{$user}@{$destination} "
. " 'bash -se' << \\$delimiter" . PHP_EOL

View File

@ -9,11 +9,11 @@
class RemoteProcessArgs extends Data
{
public function __construct(
protected string $destination,
protected string $command,
protected int $port = 22,
protected string $user = 'root',
protected string $type = ActivityTypes::COOLIFY_PROCESS->value,
protected string $status = ProcessStatus::HOLDING->value,
public string $destination,
public string $command,
public int $port,
public string $user,
public string $type = ActivityTypes::COOLIFY_PROCESS->value,
public string $status = ProcessStatus::HOLDING->value,
){}
}

View File

@ -8,7 +8,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Models\Activity;
class ExecuteCoolifyProcess implements ShouldQueue
{

View File

@ -1,19 +1,29 @@
<?php
use App\Actions\RemoteProcess\DispatchRemoteProcess;
use App\Data\RemoteProcessArgs;
use Spatie\Activitylog\Contracts\Activity;
if (! function_exists('remoteProcess')) {
/**
* Run a Coolify Process, which SSH's into a machine to run the command(s).
* Run a Coolify Process, which SSH's asynchronously into a machine to run the command(s).
* @TODO Change 'root' to 'coolify' when it's able to run Docker commands without sudo
*
*/
function remoteProcess($command, $destination): Activity
function remoteProcess(
string $command,
string $destination,
?int $port = 22,
?string $user = 'root',
): Activity
{
return resolve(DispatchRemoteProcess::class, [
'destination' => $destination,
'command' => $command,
'remoteProcessArgs' => new RemoteProcessArgs(
destination: $destination,
command: $command,
port: $port,
user: $user,
),
])();
}
}

View File

@ -14,12 +14,11 @@
// Assert there's no containers start with coolify_test_*
$activity = remoteProcess($areThereCoolifyTestContainers, $host);
ray($activity);
$containers = Output::containerList($activity->getExtraProperty('stdout'));
expect($containers)->toBeEmpty();
// start a container nginx -d --name = $containerName
$activity = remoteProcess("docker run -d --name {$containerName} nginx", $host);
$activity = remoteProcess("docker run -d --rm --name {$containerName} nginx", $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0);
// docker ps name = $container