2023-03-21 09:08:36 +00:00
|
|
|
<?php
|
|
|
|
|
2023-05-03 05:15:45 +00:00
|
|
|
namespace App\Actions\CoolifyTask;
|
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-05-24 12:26:50 +00:00
|
|
|
use App\Jobs\ApplicationDeploymentJob;
|
2023-09-15 13:34:25 +00:00
|
|
|
use App\Models\Server;
|
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-06-07 13:39:08 +00:00
|
|
|
public bool $hide_from_output;
|
2023-03-31 13:38:36 +00:00
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
public bool $ignore_errors;
|
2023-03-31 16:26:38 +00:00
|
|
|
|
2023-12-08 11:12:44 +00:00
|
|
|
public $call_event_on_finish = null;
|
|
|
|
|
2024-03-12 10:22:02 +00:00
|
|
|
public $call_event_data = null;
|
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
protected $time_start;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
protected $current_time;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
protected $last_write_at = 0;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-12-08 21:51:42 +00:00
|
|
|
protected $throttle_interval_ms = 200;
|
2023-03-21 09:08:36 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2024-03-12 10:22:02 +00:00
|
|
|
public function __construct(Activity $activity, bool $hide_from_output = false, bool $ignore_errors = false, $call_event_on_finish = null, $call_event_data = null)
|
2023-03-21 09:56:49 +00:00
|
|
|
{
|
2023-03-29 10:52:22 +00:00
|
|
|
|
2023-06-30 20:24:39 +00:00
|
|
|
if ($activity->getExtraProperty('type') !== ActivityTypes::INLINE->value) {
|
2023-03-21 09:56:49 +00:00
|
|
|
throw new \RuntimeException('Incompatible Activity to run a remote command.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activity = $activity;
|
2023-06-07 13:39:08 +00:00
|
|
|
$this->hide_from_output = $hide_from_output;
|
|
|
|
$this->ignore_errors = $ignore_errors;
|
2023-12-08 11:12:44 +00:00
|
|
|
$this->call_event_on_finish = $call_event_on_finish;
|
2024-03-12 10:22:02 +00:00
|
|
|
$this->call_event_data = $call_event_data;
|
2023-03-21 09:56:49 +00:00
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public static function decodeOutput(?Activity $activity = null): string
|
|
|
|
{
|
|
|
|
if (is_null($activity)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$decoded = json_decode(
|
|
|
|
data_get($activity, 'description'),
|
|
|
|
associative: true,
|
2024-05-23 09:58:54 +00:00
|
|
|
flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE
|
2023-08-08 09:51:36 +00:00
|
|
|
);
|
|
|
|
} catch (\JsonException $exception) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect($decoded)
|
2023-08-11 18:48:52 +00:00
|
|
|
->sortBy(fn ($i) => $i['order'])
|
|
|
|
->map(fn ($i) => $i['output'])
|
2023-08-08 09:51:36 +00:00
|
|
|
->implode("");
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:08:36 +00:00
|
|
|
public function __invoke(): ProcessResult
|
|
|
|
{
|
2023-06-07 13:39:08 +00:00
|
|
|
$this->time_start = hrtime(true);
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-04-14 19:09:38 +00:00
|
|
|
$status = ProcessStatus::IN_PROGRESS;
|
2023-12-08 21:51:42 +00:00
|
|
|
$timeout = config('constants.ssh.command_timeout');
|
|
|
|
$process = Process::timeout($timeout)->start($this->getCommand(), $this->handleOutput(...));
|
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'process_id' => $process->id(),
|
|
|
|
]);
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-12-08 21:51:42 +00:00
|
|
|
$processResult = $process->wait();
|
|
|
|
// $processResult = Process::timeout($timeout)->run($this->getCommand(), $this->handleOutput(...));
|
2023-04-14 19:09:38 +00:00
|
|
|
if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) {
|
|
|
|
$status = ProcessStatus::ERROR;
|
|
|
|
} else {
|
2023-12-08 12:07:42 +00:00
|
|
|
if ($processResult->exitCode() == 0) {
|
2023-04-14 19:09:38 +00:00
|
|
|
$status = ProcessStatus::FINISHED;
|
|
|
|
}
|
2023-06-07 13:39:08 +00:00
|
|
|
if ($processResult->exitCode() != 0 && !$this->ignore_errors) {
|
2023-04-14 19:09:38 +00:00
|
|
|
$status = ProcessStatus::ERROR;
|
|
|
|
}
|
2023-12-08 11:12:44 +00:00
|
|
|
// if (($processResult->exitCode() == 0 && $this->is_finished) || $this->activity->properties->get('status') === ProcessStatus::FINISHED->value) {
|
|
|
|
// $status = ProcessStatus::FINISHED;
|
|
|
|
// }
|
|
|
|
// if ($processResult->exitCode() != 0 && !$this->ignore_errors) {
|
|
|
|
// $status = ProcessStatus::ERROR;
|
|
|
|
// }
|
2023-04-14 19:09:38 +00:00
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
|
|
|
|
$this->activity->properties = $this->activity->properties->merge([
|
|
|
|
'exitCode' => $processResult->exitCode(),
|
2023-04-14 19:09:38 +00:00
|
|
|
'stdout' => $processResult->output(),
|
2023-03-21 09:08:36 +00:00
|
|
|
'stderr' => $processResult->errorOutput(),
|
2023-04-14 19:09:38 +00:00
|
|
|
'status' => $status->value,
|
2023-03-21 09:08:36 +00:00
|
|
|
]);
|
|
|
|
$this->activity->save();
|
2023-06-07 13:39:08 +00:00
|
|
|
if ($processResult->exitCode() != 0 && !$this->ignore_errors) {
|
2023-09-15 13:34:25 +00:00
|
|
|
throw new \RuntimeException($processResult->errorOutput(), $processResult->exitCode());
|
2023-04-04 12:23:10 +00:00
|
|
|
}
|
2023-12-08 11:12:44 +00:00
|
|
|
if ($this->call_event_on_finish) {
|
|
|
|
try {
|
2024-03-12 10:22:02 +00:00
|
|
|
if ($this->call_event_data) {
|
|
|
|
event(resolve("App\\Events\\$this->call_event_on_finish", [
|
|
|
|
"data" => $this->call_event_data,
|
|
|
|
]));
|
|
|
|
} else {
|
|
|
|
event(resolve("App\\Events\\$this->call_event_on_finish", [
|
|
|
|
'userId' => $this->activity->causer_id,
|
|
|
|
]));
|
|
|
|
}
|
2023-12-08 11:12:44 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
ray($e);
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 09:08:36 +00:00
|
|
|
return $processResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCommand(): string
|
|
|
|
{
|
2023-09-15 13:34:25 +00:00
|
|
|
$server_uuid = $this->activity->getExtraProperty('server_uuid');
|
2023-03-21 09:08:36 +00:00
|
|
|
$command = $this->activity->getExtraProperty('command');
|
2023-09-15 13:34:25 +00:00
|
|
|
$server = Server::whereUuid($server_uuid)->firstOrFail();
|
2023-03-21 09:08:36 +00:00
|
|
|
|
2023-09-15 13:34:25 +00:00
|
|
|
return generateSshCommand($server, $command);
|
2023-03-21 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleOutput(string $type, string $output)
|
|
|
|
{
|
2023-06-07 13:39:08 +00:00
|
|
|
if ($this->hide_from_output) {
|
2023-03-31 13:38:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-07 13:39:08 +00:00
|
|
|
$this->current_time = $this->elapsedTime();
|
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();
|
2023-06-07 13:39:08 +00:00
|
|
|
$this->last_write_at = $this->current_time;
|
2023-03-21 09:08:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
protected function elapsedTime(): int
|
|
|
|
{
|
|
|
|
$timeMs = (hrtime(true) - $this->time_start) / 1_000_000;
|
|
|
|
|
|
|
|
return intval($timeMs);
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
public function encodeOutput($type, $output)
|
|
|
|
{
|
2024-05-23 09:58:54 +00:00
|
|
|
$outputStack = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
|
2024-05-30 08:14:48 +00:00
|
|
|
$outputStack[] = [
|
|
|
|
'type' => $type,
|
|
|
|
'output' => $output,
|
|
|
|
'timestamp' => hrtime(true),
|
|
|
|
'batch' => ApplicationDeploymentJob::$batch_counter,
|
|
|
|
'order' => $this->getLatestCounter(),
|
|
|
|
];
|
2023-04-07 14:58:45 +00:00
|
|
|
|
2024-05-23 09:58:54 +00:00
|
|
|
return json_encode($outputStack, flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
|
2023-04-07 14:58:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
protected function getLatestCounter(): int
|
2023-04-07 14:58:45 +00:00
|
|
|
{
|
2024-05-23 09:58:54 +00:00
|
|
|
$description = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
|
2023-08-08 09:51:36 +00:00
|
|
|
if ($description === null || count($description) === 0) {
|
|
|
|
return 1;
|
2023-04-07 14:58:45 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
return end($description)['order'] + 1;
|
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.
|
2023-06-07 13:39:08 +00:00
|
|
|
if ($this->last_write_at === 0) {
|
2023-03-21 09:08:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-07 13:39:08 +00:00
|
|
|
return ($this->current_time - $this->throttle_interval_ms) > $this->last_write_at;
|
2023-03-21 09:08:36 +00:00
|
|
|
}
|
|
|
|
}
|