Refactoring: extract process handling from async job.
This commit is contained in:
parent
34c9265aa9
commit
29fb40bd16
@ -3,32 +3,16 @@
|
|||||||
namespace App\Actions\RemoteProcess;
|
namespace App\Actions\RemoteProcess;
|
||||||
|
|
||||||
use App\Data\RemoteProcessArgs;
|
use App\Data\RemoteProcessArgs;
|
||||||
use App\Enums\ActivityTypes;
|
|
||||||
use App\Enums\ProcessStatus;
|
|
||||||
use App\Jobs\ExecuteCoolifyProcess;
|
use App\Jobs\ExecuteCoolifyProcess;
|
||||||
use Spatie\Activitylog\Contracts\Activity;
|
use Spatie\Activitylog\Models\Activity;
|
||||||
|
|
||||||
class DispatchRemoteProcess
|
class DispatchRemoteProcess
|
||||||
{
|
{
|
||||||
protected Activity $activity;
|
protected Activity $activity;
|
||||||
|
|
||||||
// TODO Left 'root' as default user instead of 'coolify' because
|
public function __construct(RemoteProcessArgs $remoteProcessArgs){
|
||||||
// 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,
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->activity = activity()
|
$this->activity = activity()
|
||||||
->withProperties($arguments->toArray())
|
->withProperties($remoteProcessArgs->toArray())
|
||||||
->log("Awaiting command to start...\n\n");
|
->log("Awaiting command to start...\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,5 +26,4 @@ public function __invoke(): Activity
|
|||||||
|
|
||||||
return $this->activity;
|
return $this->activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
use Illuminate\Process\ProcessResult;
|
use Illuminate\Process\ProcessResult;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Process;
|
use Illuminate\Support\Facades\Process;
|
||||||
use Spatie\Activitylog\Contracts\Activity;
|
use Spatie\Activitylog\Models\Activity;
|
||||||
|
|
||||||
class RunRemoteProcess
|
class RunRemoteProcess
|
||||||
{
|
{
|
||||||
@ -73,10 +73,6 @@ protected function getCommand(): string
|
|||||||
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
||||||
. '-o PasswordAuthentication=no '
|
. '-o PasswordAuthentication=no '
|
||||||
. '-o RequestTTY=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} "
|
. "-p {$port} "
|
||||||
. "{$user}@{$destination} "
|
. "{$user}@{$destination} "
|
||||||
. " 'bash -se' << \\$delimiter" . PHP_EOL
|
. " 'bash -se' << \\$delimiter" . PHP_EOL
|
||||||
|
@ -9,11 +9,11 @@
|
|||||||
class RemoteProcessArgs extends Data
|
class RemoteProcessArgs extends Data
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected string $destination,
|
public string $destination,
|
||||||
protected string $command,
|
public string $command,
|
||||||
protected int $port = 22,
|
public int $port,
|
||||||
protected string $user = 'root',
|
public string $user,
|
||||||
protected string $type = ActivityTypes::COOLIFY_PROCESS->value,
|
public string $type = ActivityTypes::COOLIFY_PROCESS->value,
|
||||||
protected string $status = ProcessStatus::HOLDING->value,
|
public string $status = ProcessStatus::HOLDING->value,
|
||||||
){}
|
){}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Spatie\Activitylog\Contracts\Activity;
|
use Spatie\Activitylog\Models\Activity;
|
||||||
|
|
||||||
class ExecuteCoolifyProcess implements ShouldQueue
|
class ExecuteCoolifyProcess implements ShouldQueue
|
||||||
{
|
{
|
||||||
|
@ -1,19 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Actions\RemoteProcess\DispatchRemoteProcess;
|
use App\Actions\RemoteProcess\DispatchRemoteProcess;
|
||||||
|
use App\Data\RemoteProcessArgs;
|
||||||
use Spatie\Activitylog\Contracts\Activity;
|
use Spatie\Activitylog\Contracts\Activity;
|
||||||
|
|
||||||
if (! function_exists('remoteProcess')) {
|
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, [
|
return resolve(DispatchRemoteProcess::class, [
|
||||||
'destination' => $destination,
|
'remoteProcessArgs' => new RemoteProcessArgs(
|
||||||
'command' => $command,
|
destination: $destination,
|
||||||
|
command: $command,
|
||||||
|
port: $port,
|
||||||
|
user: $user,
|
||||||
|
),
|
||||||
])();
|
])();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,11 @@
|
|||||||
|
|
||||||
// Assert there's no containers start with coolify_test_*
|
// Assert there's no containers start with coolify_test_*
|
||||||
$activity = remoteProcess($areThereCoolifyTestContainers, $host);
|
$activity = remoteProcess($areThereCoolifyTestContainers, $host);
|
||||||
ray($activity);
|
|
||||||
$containers = Output::containerList($activity->getExtraProperty('stdout'));
|
$containers = Output::containerList($activity->getExtraProperty('stdout'));
|
||||||
expect($containers)->toBeEmpty();
|
expect($containers)->toBeEmpty();
|
||||||
|
|
||||||
// start a container nginx -d --name = $containerName
|
// 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);
|
expect($activity->getExtraProperty('exitCode'))->toBe(0);
|
||||||
|
|
||||||
// docker ps name = $container
|
// docker ps name = $container
|
||||||
|
Loading…
Reference in New Issue
Block a user