diff --git a/app/Traits/ExecuteRemoteCommand.php b/app/Traits/ExecuteRemoteCommand.php index 60dcf32afc..509a898d10 100644 --- a/app/Traits/ExecuteRemoteCommand.php +++ b/app/Traits/ExecuteRemoteCommand.php @@ -49,45 +49,44 @@ public function execute_remote_command(...$commands) } $remote_command = SshMultiplexingHelper::generateSshCommand($this->server, $command); $process = Process::timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($command, $secrets, $hidden, $customType, $append) { - $output = str($output)->trim(); - if (count($secrets) > 0) { - $output = $this->maskSecrets($output, $secrets); - } - if (str($output)->startsWith('╔')) { - $output = "\n" . $output; - } + $output = str($output)->trim(); + if (count($secrets) > 0) { + $output = $this->maskSecrets($output, $secrets); + $command = $this->maskSecrets($command, $secrets); + } + if (str($output)->startsWith('╔')) { + $output = "\n" . $output; + } + $new_log_entry = [ + 'command' => remove_iip($command), + 'output' => remove_iip($output), + 'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout', + 'timestamp' => Carbon::now('UTC'), + 'hidden' => $hidden, + 'batch' => static::$batch_counter, + ]; + if (! $this->application_deployment_queue->logs) { + $new_log_entry['order'] = 1; + } else { + $previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR); + $new_log_entry['order'] = count($previous_logs) + 1; + } + $previous_logs[] = $new_log_entry; + $this->application_deployment_queue->logs = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR); + $this->application_deployment_queue->save(); - // Prepare log entry with masked command and output - $new_log_entry = [ - 'command' => remove_iip($this->maskSecrets($this->interpolateCommand($command, $secrets), $secrets)), - 'output' => remove_iip($output), - 'type' => $customType ?? $type === 'err' ? 'stderr' : 'stdout', - 'timestamp' => Carbon::now('UTC'), - 'hidden' => $hidden, - 'batch' => static::$batch_counter, - ]; - if (! $this->application_deployment_queue->logs) { - $new_log_entry['order'] = 1; - } else { - $previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR); - $new_log_entry['order'] = count($previous_logs) + 1; + if ($this->save) { + if (data_get($this->saved_outputs, $this->save, null) === null) { + data_set($this->saved_outputs, $this->save, str()); } - $previous_logs[] = $new_log_entry; - $this->application_deployment_queue->logs = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR); - $this->application_deployment_queue->save(); - - if ($this->save) { - 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(); + 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(); - } + } else { + $this->saved_outputs[$this->save] = str($output)->trim(); } - }); + } + }); $this->application_deployment_queue->update([ 'current_process_id' => $process->id(), ]); @@ -117,7 +116,7 @@ private function interpolateCommand(string $command, array $secrets): string private function maskSecrets(string $text, array $secrets): string { // Sort secrets by length descending to prevent partial masking - usort($secrets, function($a, $b) { + usort($secrets, function ($a, $b) { return strlen($b) - strlen($a); }); @@ -127,4 +126,4 @@ private function maskSecrets(string $text, array $secrets): string } return $text; } -} \ No newline at end of file +}