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-04-12 11:09:27 +00:00
|
|
|
use App\Jobs\DeployApplicationJob;
|
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-31 13:38:36 +00:00
|
|
|
public bool $hideFromOutput;
|
|
|
|
|
2023-03-31 16:26:38 +00:00
|
|
|
public bool $setStatus;
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
protected $timeStart;
|
|
|
|
|
|
|
|
protected $currentTime;
|
|
|
|
|
|
|
|
protected $lastWriteAt = 0;
|
|
|
|
|
|
|
|
protected $throttleIntervalMS = 500;
|
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
protected int $counter = 1;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*/
|
2023-03-31 16:57:37 +00:00
|
|
|
public function __construct(Activity $activity, bool $hideFromOutput = false, bool $setStatus = false)
|
2023-03-21 09:56:49 +00:00
|
|
|
{
|
2023-03-29 10:52:22 +00:00
|
|
|
|
|
|
|
if ($activity->getExtraProperty('type') !== ActivityTypes::REMOTE_PROCESS->value && $activity->getExtraProperty('type') !== ActivityTypes::DEPLOYMENT->value) {
|
2023-03-21 09:56:49 +00:00
|
|
|
throw new \RuntimeException('Incompatible Activity to run a remote command.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activity = $activity;
|
2023-03-31 13:38:36 +00:00
|
|
|
$this->hideFromOutput = $hideFromOutput;
|
2023-03-31 16:26:38 +00:00
|
|
|
$this->setStatus = $setStatus;
|
2023-03-21 09:56:49 +00:00
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
public function __invoke(): ProcessResult
|
|
|
|
{
|
2023-03-31 07:41:41 +00:00
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'status' => ProcessStatus::IN_PROGRESS,
|
|
|
|
]);
|
2023-03-21 09:08:36 +00:00
|
|
|
$this->timeStart = hrtime(true);
|
|
|
|
|
|
|
|
$processResult = Process::run($this->getCommand(), $this->handleOutput(...));
|
|
|
|
|
2023-03-31 16:26:38 +00:00
|
|
|
$status = $processResult->exitCode() != 0 ? ProcessStatus::ERROR : ($this->setStatus ? ProcessStatus::FINISHED : null);
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'exitCode' => $processResult->exitCode(),
|
2023-03-31 13:38:36 +00:00
|
|
|
'stdout' => $this->hideFromOutput || $processResult->output(),
|
2023-03-21 09:08:36 +00:00
|
|
|
'stderr' => $processResult->errorOutput(),
|
|
|
|
]);
|
2023-03-31 16:26:38 +00:00
|
|
|
if (isset($status)) {
|
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'status' => $status->value,
|
|
|
|
]);
|
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
$this->activity->save();
|
|
|
|
|
2023-04-04 12:23:10 +00:00
|
|
|
if ($processResult->exitCode() != 0 && $processResult->errorOutput()) {
|
|
|
|
throw new \RuntimeException('Remote command failed');
|
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
return $processResult;
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:09:27 +00:00
|
|
|
protected function getLatestCounter(): int
|
|
|
|
{
|
|
|
|
$description = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
|
|
|
|
if ($description === null || count($description) === 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return end($description)['order'] + 1;
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
protected function getCommand(): string
|
|
|
|
{
|
|
|
|
$user = $this->activity->getExtraProperty('user');
|
2023-03-29 10:27:02 +00:00
|
|
|
$server_ip = $this->activity->getExtraProperty('server_ip');
|
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');
|
|
|
|
|
2023-03-30 13:52:19 +00:00
|
|
|
return generateSshCommand($private_key_location, $server_ip, $user, $port, $command);
|
2023-03-21 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleOutput(string $type, string $output)
|
|
|
|
{
|
2023-03-31 13:38:36 +00:00
|
|
|
if ($this->hideFromOutput) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
$this->currentTime = $this->elapsedTime();
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-04-07 14:58:45 +00:00
|
|
|
$this->activity->description = $this->encodeOutput($type, $output);
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
if ($this->isAfterLastThrottle()) {
|
|
|
|
// Let's write to database.
|
|
|
|
DB::transaction(function () {
|
|
|
|
$this->activity->save();
|
|
|
|
$this->lastWriteAt = $this->currentTime;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
public function encodeOutput($type, $output)
|
|
|
|
{
|
2023-04-07 14:58:45 +00:00
|
|
|
$outputStack = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
|
|
|
|
|
|
|
|
$outputStack[] = [
|
|
|
|
'type' => $type,
|
|
|
|
'output' => $output,
|
2023-04-12 11:09:27 +00:00
|
|
|
'timestamp' => hrtime(true),
|
|
|
|
'batch' => DeployApplicationJob::$batch_counter,
|
|
|
|
'order' => $this->getLatestCounter(),
|
2023-04-07 14:58:45 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return json_encode($outputStack, flags: JSON_THROW_ON_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function decodeOutput(?Activity $activity = null): string
|
|
|
|
{
|
2023-04-12 11:09:27 +00:00
|
|
|
if (is_null($activity)) {
|
2023-04-07 14:58:45 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-04-12 11:09:27 +00:00
|
|
|
$decoded = json_decode(
|
|
|
|
data_get($activity, 'description'),
|
2023-04-07 14:58:45 +00:00
|
|
|
associative: true,
|
|
|
|
flags: JSON_THROW_ON_ERROR
|
|
|
|
);
|
|
|
|
} catch (\JsonException $exception) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect($decoded)
|
2023-04-12 11:09:27 +00:00
|
|
|
->sortBy(fn ($i) => $i['order'])
|
|
|
|
->map(fn ($i) => $i['output'])
|
|
|
|
->implode("");
|
2023-04-01 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|