This commit is contained in:
Andras Bacsai
2023-06-30 21:22:14 +02:00
parent 55d5b1e8da
commit b10ce8ea2d
6 changed files with 589 additions and 110 deletions

View File

@@ -128,7 +128,7 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
}
try {
$decoded = json_decode(
data_get($application_deployment_queue, 'log'),
data_get($application_deployment_queue, 'logs'),
associative: true,
flags: JSON_THROW_ON_ERROR
);
@@ -137,10 +137,10 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
}
$formatted = collect($decoded);
if (!$is_debug_enabled) {
$formatted = $formatted->filter(fn ($i) => $i['show_in_output'] ?? true);
$formatted = $formatted->filter(fn ($i) => $i['hidden'] === false ?? false);
}
$formatted = $formatted->sortBy(fn ($i) => $i['order'])
$formatted = $formatted
->sortBy(fn ($i) => $i['order'])
->map(function ($i) {
$i['timestamp'] = Carbon::parse($i['timestamp'])->format('Y-M-d H:i:s.u');
return $i;
@@ -148,51 +148,3 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
return $formatted;
}
function execute_remote_command(array|Collection $commands, Server $server, ApplicationDeploymentQueue $queue, bool $show_in_output = true, bool $ignore_errors = false)
{
if ($commands instanceof Collection) {
$commandsText = $commands;
} else {
$commandsText = collect($commands);
}
$ip = data_get($server, 'ip');
$user = data_get($server, 'user');
$port = data_get($server, 'port');
$private_key_location = get_private_key_for_server($server);
$commandsText->each(function ($command) use ($queue, $private_key_location, $ip, $user, $port, $show_in_output, $ignore_errors) {
$remote_command = generate_ssh_command($private_key_location, $ip, $user, $port, $command);
$process = Process::timeout(3600)->idleTimeout(3600)->start($remote_command, function (string $type, string $output) use ($queue, $command, $show_in_output) {
$new_log_entry = [
'command' => $command,
'output' => $output,
'type' => $type === 'err' ? 'stderr' : 'stdout',
'timestamp' => Carbon::now('UTC'),
'show_in_output' => $show_in_output,
];
if (!$queue->log) {
$new_log_entry['order'] = 1;
} else {
$previous_logs = json_decode($queue->log, associative: true, flags: JSON_THROW_ON_ERROR);
$new_log_entry['order'] = count($previous_logs) + 1;
}
$previous_logs[] = $new_log_entry;
$queue->log = json_encode($previous_logs, flags: JSON_THROW_ON_ERROR);;
$queue->save();
});
$queue->update([
'current_process_id' => $process->id(),
]);
$process_result = $process->wait();
if ($process_result->exitCode() !== 0) {
if (!$ignore_errors) {
$status = ApplicationDeploymentStatus::FAILED->value;
$queue->status = $status;
$queue->save();
throw new \RuntimeException($process_result->errorOutput());
}
}
});
}