Have Sail and SSU. Runs a command on a remote host.

This commit is contained in:
Joao Patricio
2023-03-20 14:04:35 +00:00
parent a613e9f113
commit bfdae4339c
15 changed files with 234 additions and 77 deletions

View File

@@ -12,7 +12,7 @@ class RunCommand extends Component
public $manualKeepAlive = false;
public $command = '';
public $command = 'ls';
public function render()
{
@@ -21,30 +21,32 @@ class RunCommand extends Component
public function runCommand()
{
// TODO Execute with Livewire Normally
$this->activity = coolifyProcess($this->command, 'testing-host');
$this->isKeepAliveOn = true;
// Override manual to experiment
// $sleepingBeauty = 'x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done';
//
// $commandString = <<<EOT
// cd projects/dummy-project
// ~/.docker/cli-plugins/docker-compose build --no-cache
// # $sleepingBeauty
// EOT;
//
// $this->activity = coolifyProcess($commandString, 'testing-host');
$override = 0;
if($override) {
// Good to play with the throttle feature
$sleepingBeauty = 'x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done';
$this->isKeepAliveOn = true;
$this->activity = coolifyProcess(<<<EOT
cd projects/dummy-project
# ~/.docker/cli-plugins/docker-compose build --no-cache
$sleepingBeauty
EOT, 'testing-host');
return;
}
$this->activity = coolifyProcess($this->command, 'testing-host');
}
public function polling()
{
$this->activity?->refresh();
if ($this->activity?->properties['status'] === 'finished') {
if (data_get($this->activity, 'properties.exitCode') !== null) {
$this->isKeepAliveOn = false;
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Jobs;
use App\Services\ProcessStatus;
use Illuminate\Support\Facades\DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
@@ -11,7 +12,6 @@ use Illuminate\Process\InvokedProcess;
use Illuminate\Process\ProcessResult;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Spatie\Activitylog\Contracts\Activity;
@@ -43,8 +43,7 @@ class ExecuteCoolifyProcess implements ShouldQueue
*/
public function handle(): ?ProcessResult
{
ray()->clearAll();
$this->timeStart = $start = hrtime(true);
$this->timeStart = hrtime(true);
$user = $this->activity->getExtraProperty('user');
$destination = $this->activity->getExtraProperty('destination');
@@ -53,24 +52,16 @@ class ExecuteCoolifyProcess implements ShouldQueue
$delimiter = 'EOF-COOLIFY-SSH';
File::chmod(base_path('coolify_id25519'), 0600);
$sshCommand = 'ssh '
. '-i ./coolify_id25519 '
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no '
. "-p {$port} "
. "{$user}@{$destination} "
. " 'bash -se' << \\$delimiter" . PHP_EOL
. $command . PHP_EOL
. $delimiter;
// $sshCommand = "whoami ; pwd ; ls ";
$process = Process::start(
$sshCommand,
$this->handleOutput(...),
);
$process = Process::start($sshCommand, $this->handleOutput(...));
$res = $process->wait();
@@ -78,13 +69,20 @@ class ExecuteCoolifyProcess implements ShouldQueue
return $res;
}
// TODO Why is this not persisting?? Immutable property??
$this->activity->properties->put('pid', $process->id());
$this->activity->properties->put('exitCode', $res->exitCode());
$this->activity->properties->put('stdout', $res->output());
$this->activity->properties->put('stderr', $res->errorOutput());
$status = match ($res->exitCode()) {
0 => ProcessStatus::FINISHED,
default => ProcessStatus::ERROR,
};
$this->activity->properties = $this->activity->properties->merge([
'exitCode' => $res->exitCode(),
'stdout' => $res->output(),
'stderr' => $res->errorOutput(),
'status' => $status,
]);
$this->activity->save();
return $res;
}

View File

@@ -19,12 +19,14 @@ class CoolifyProcess
protected ?string $user = 'root',
){
$this->activity = activity()
->withProperty('type', 'COOLIFY_PROCESS')
->withProperty('user', $this->user)
->withProperty('destination', $this->destination)
->withProperty('port', $this->port)
->withProperty('command', $this->command)
->withProperty('status', ProcessStatus::HOLDING)
->withProperties([
'type' => 'COOLIFY_PROCESS',
'user' => $this->user,
'destination' => $this->destination,
'port' => $this->port,
'command' => $this->command,
'status' => ProcessStatus::HOLDING,
])
->log("Awaiting to start command...\n\n");
}