2023-06-30 19:22:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
use App\Enums\ApplicationDeploymentStatus;
|
|
|
|
use App\Models\Server;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
trait ExecuteRemoteCommand
|
|
|
|
{
|
2023-09-15 13:34:25 +00:00
|
|
|
public ?string $save = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-24 14:48:23 +00:00
|
|
|
public static int $batch_counter = 0;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-30 19:22:14 +00:00
|
|
|
public function execute_remote_command(...$commands)
|
|
|
|
{
|
|
|
|
static::$batch_counter++;
|
|
|
|
if ($commands instanceof Collection) {
|
|
|
|
$commandsText = $commands;
|
|
|
|
} else {
|
|
|
|
$commandsText = collect($commands);
|
|
|
|
}
|
|
|
|
if ($this->server instanceof Server === false) {
|
|
|
|
throw new \RuntimeException('Server is not set or is not an instance of Server model');
|
|
|
|
}
|
2023-09-15 13:34:25 +00:00
|
|
|
$commandsText->each(function ($single_command) {
|
2023-06-30 19:22:14 +00:00
|
|
|
$command = data_get($single_command, 'command') ?? $single_command[0] ?? null;
|
|
|
|
if ($command === null) {
|
|
|
|
throw new \RuntimeException('Command is not set');
|
|
|
|
}
|
|
|
|
$hidden = data_get($single_command, 'hidden', false);
|
2023-11-20 12:49:10 +00:00
|
|
|
$customType = data_get($single_command, 'type');
|
2023-06-30 19:22:14 +00:00
|
|
|
$ignore_errors = data_get($single_command, 'ignore_errors', false);
|
2024-01-11 11:56:02 +00:00
|
|
|
$append = data_get($single_command, 'append', true);
|
2023-06-30 19:22:14 +00:00
|
|
|
$this->save = data_get($single_command, 'save');
|
2024-04-16 19:22:57 +00:00
|
|
|
if ($this->server->isNonRoot()) {
|
|
|
|
if (str($command)->startsWith('docker exec')) {
|
|
|
|
$command = str($command)->replace('docker exec', 'sudo docker exec');
|
|
|
|
} else {
|
|
|
|
$command = parseLineForSudo($command, $this->server);
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 13:34:25 +00:00
|
|
|
$remote_command = generateSshCommand($this->server, $command);
|
2024-01-11 11:56:02 +00:00
|
|
|
$process = Process::timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $hidden, $customType, $append) {
|
2023-08-27 20:05:37 +00:00
|
|
|
$output = Str::of($output)->trim();
|
2023-11-20 12:49:10 +00:00
|
|
|
if ($output->startsWith('╔')) {
|
2024-06-10 20:43:34 +00:00
|
|
|
$output = "\n".$output;
|
2023-11-20 12:49:10 +00:00
|
|
|
}
|
2023-06-30 19:22:14 +00:00
|
|
|
$new_log_entry = [
|
2023-11-20 12:49:10 +00:00
|
|
|
'command' => remove_iip($command),
|
|
|
|
'output' => remove_iip($output),
|
|
|
|
'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout',
|
2023-06-30 19:22:14 +00:00
|
|
|
'timestamp' => Carbon::now('UTC'),
|
|
|
|
'hidden' => $hidden,
|
|
|
|
'batch' => static::$batch_counter,
|
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $this->application_deployment_queue->logs) {
|
2023-06-30 19:22:14 +00:00
|
|
|
$new_log_entry['order'] = 1;
|
|
|
|
} else {
|
2023-11-27 10:54:55 +00:00
|
|
|
$previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR);
|
2023-06-30 19:22:14 +00:00
|
|
|
$new_log_entry['order'] = count($previous_logs) + 1;
|
|
|
|
}
|
|
|
|
$previous_logs[] = $new_log_entry;
|
2023-11-27 10:54:55 +00:00
|
|
|
$this->application_deployment_queue->logs = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR);
|
|
|
|
$this->application_deployment_queue->save();
|
2023-06-30 19:22:14 +00:00
|
|
|
|
|
|
|
if ($this->save) {
|
2024-01-11 11:56:02 +00:00
|
|
|
if (data_get($this->saved_outputs, $this->save, null) === null) {
|
|
|
|
data_set($this->saved_outputs, $this->save, str());
|
|
|
|
}
|
|
|
|
if ($append) {
|
|
|
|
$this->saved_outputs[$this->save] .= str($output)->trim();
|
|
|
|
$this->saved_outputs[$this->save] = str($this->saved_outputs[$this->save]);
|
|
|
|
} else {
|
|
|
|
$this->saved_outputs[$this->save] = str($output)->trim();
|
|
|
|
}
|
2023-06-30 19:22:14 +00:00
|
|
|
}
|
|
|
|
});
|
2023-11-27 10:54:55 +00:00
|
|
|
$this->application_deployment_queue->update([
|
2023-06-30 19:22:14 +00:00
|
|
|
'current_process_id' => $process->id(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$process_result = $process->wait();
|
|
|
|
if ($process_result->exitCode() !== 0) {
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $ignore_errors) {
|
2023-11-27 10:54:55 +00:00
|
|
|
$this->application_deployment_queue->status = ApplicationDeploymentStatus::FAILED->value;
|
|
|
|
$this->application_deployment_queue->save();
|
2023-06-30 19:22:14 +00:00
|
|
|
throw new \RuntimeException($process_result->errorOutput());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|