2023-03-21 09:08:36 +00:00
|
|
|
<?php
|
|
|
|
|
2023-03-21 09:32:16 +00:00
|
|
|
namespace App\Actions\RemoteProcess;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-03-21 09:56:49 +00:00
|
|
|
use App\Enums\ActivityTypes;
|
2023-03-21 09:32:16 +00:00
|
|
|
use App\Enums\ProcessStatus;
|
2023-03-21 09:08:36 +00:00
|
|
|
use Illuminate\Process\ProcessResult;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Process;
|
2023-03-21 10:32:38 +00:00
|
|
|
use Spatie\Activitylog\Models\Activity;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-03-21 09:56:49 +00:00
|
|
|
class RunRemoteProcess
|
2023-03-21 09:08:36 +00:00
|
|
|
{
|
2023-03-21 09:56:49 +00:00
|
|
|
public Activity $activity;
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
protected $timeStart;
|
|
|
|
|
|
|
|
protected $currentTime;
|
|
|
|
|
|
|
|
protected $lastWriteAt = 0;
|
|
|
|
|
|
|
|
protected $throttleIntervalMS = 500;
|
|
|
|
|
|
|
|
protected string $stdOutIncremental = '';
|
|
|
|
|
|
|
|
protected string $stdErrIncremental = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*/
|
2023-03-21 09:56:49 +00:00
|
|
|
public function __construct(Activity $activity)
|
|
|
|
{
|
|
|
|
if ($activity->getExtraProperty('type') !== ActivityTypes::COOLIFY_PROCESS->value) {
|
|
|
|
throw new \RuntimeException('Incompatible Activity to run a remote command.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activity = $activity;
|
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
public function __invoke(): ProcessResult
|
|
|
|
{
|
|
|
|
$this->timeStart = hrtime(true);
|
|
|
|
|
|
|
|
$processResult = Process::run($this->getCommand(), $this->handleOutput(...));
|
|
|
|
|
|
|
|
$status = match ($processResult->exitCode()) {
|
|
|
|
0 => ProcessStatus::FINISHED,
|
|
|
|
default => ProcessStatus::ERROR,
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'exitCode' => $processResult->exitCode(),
|
|
|
|
'stdout' => $processResult->output(),
|
|
|
|
'stderr' => $processResult->errorOutput(),
|
|
|
|
'status' => $status,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->activity->save();
|
|
|
|
|
|
|
|
return $processResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCommand(): string
|
|
|
|
{
|
|
|
|
$user = $this->activity->getExtraProperty('user');
|
|
|
|
$destination = $this->activity->getExtraProperty('destination');
|
2023-03-24 21:15:36 +00:00
|
|
|
$private_key_location = $this->activity->getExtraProperty('private_key_location');
|
2023-03-21 09:08:36 +00:00
|
|
|
$port = $this->activity->getExtraProperty('port');
|
|
|
|
$command = $this->activity->getExtraProperty('command');
|
|
|
|
|
|
|
|
$delimiter = 'EOF-COOLIFY-SSH';
|
|
|
|
|
2023-03-24 21:15:36 +00:00
|
|
|
$ssh_command = "ssh "
|
|
|
|
. "-i {$private_key_location} "
|
2023-03-21 09:08:36 +00:00
|
|
|
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
|
|
|
|
. '-o PasswordAuthentication=no '
|
|
|
|
. '-o RequestTTY=no '
|
|
|
|
. "-p {$port} "
|
|
|
|
. "{$user}@{$destination} "
|
|
|
|
. " 'bash -se' << \\$delimiter" . PHP_EOL
|
|
|
|
. $command . PHP_EOL
|
|
|
|
. $delimiter;
|
2023-03-24 21:15:36 +00:00
|
|
|
return $ssh_command;
|
2023-03-21 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleOutput(string $type, string $output)
|
|
|
|
{
|
|
|
|
$this->currentTime = $this->elapsedTime();
|
|
|
|
|
|
|
|
if ($type === 'out') {
|
|
|
|
$this->stdOutIncremental .= $output;
|
|
|
|
} else {
|
|
|
|
$this->stdErrIncremental .= $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activity->description .= $output;
|
|
|
|
|
|
|
|
if ($this->isAfterLastThrottle()) {
|
|
|
|
// Let's write to database.
|
|
|
|
DB::transaction(function () {
|
|
|
|
$this->activity->save();
|
|
|
|
$this->lastWriteAt = $this->currentTime;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if it's time to write again to database.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isAfterLastThrottle()
|
|
|
|
{
|
|
|
|
// If DB was never written, then we immediately decide we have to write.
|
|
|
|
if ($this->lastWriteAt === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($this->currentTime - $this->throttleIntervalMS) > $this->lastWriteAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function elapsedTime(): int
|
|
|
|
{
|
|
|
|
$timeMs = (hrtime(true) - $this->timeStart) / 1_000_000;
|
|
|
|
|
|
|
|
return intval($timeMs);
|
|
|
|
}
|
|
|
|
}
|