From 47d3ac3ef546a1807c8376971274ef5484960eae Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 31 Mar 2023 09:42:05 +0200 Subject: [PATCH] feat: add runRemoteCommandSync --- app/Jobs/ContainerStatusJob.php | 9 +++------ bootstrap/helpers.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index 2f1fece58..b3432d3ec 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -32,10 +32,7 @@ class ContainerStatusJob implements ShouldQueue $not_found_applications = $applications; $containers = collect(); foreach ($servers as $server) { - $private_key_location = savePrivateKey($server); - $ssh_command = generateSshCommand($private_key_location, $server->ip, $server->user, $server->port, 'docker ps -a -q --format \'{{json .}}\''); - $process = Process::run($ssh_command); - $output = trim($process->output()); + $output = runRemoteCommandSync($server, ['docker ps -a -q --format \'{{json .}}\'']); $containers = $containers->concat(formatDockerCmdOutputToJson($output)); } foreach ($containers as $container) { @@ -48,13 +45,13 @@ class ContainerStatusJob implements ShouldQueue }); $found_application->status = $container['State']; $found_application->save(); - Log::info('Found application: ' . $found_application->uuid . '.Set status to: ' . $found_application->status); + Log::info('Found application: ' . $found_application->uuid . '. Set status to: ' . $found_application->status); } } foreach ($not_found_applications as $not_found_application) { $not_found_application->status = 'exited'; $not_found_application->save(); - Log::info('Not found application: ' . $not_found_application->uuid . '.Set status to: ' . $not_found_application->status); + Log::info('Not found application: ' . $not_found_application->uuid . '. Set status to: ' . $not_found_application->status); } } catch (\Exception $e) { Log::error($e->getMessage()); diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index dad126963..b613cf109 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -7,6 +7,7 @@ use App\Models\Server; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Storage; use Spatie\Activitylog\Contracts\Activity; @@ -113,3 +114,18 @@ if (!function_exists('formatDockerLabelsToJson')) { })[0]; } } +if (!function_exists('runRemoteCommandSync')) { + function runRemoteCommandSync($server, array $command) { + $command_string = implode("\n", $command); + $private_key_location = savePrivateKey($server); + $ssh_command = generateSshCommand($private_key_location, $server->ip, $server->user, $server->port, $command_string); + $process = Process::run($ssh_command); + $output = trim($process->output()); + $exitCode = $process->exitCode(); + if ($exitCode !== 0) { + Log::error($output); + throw new \RuntimeException('There was an error running the command.'); + } + return $output; + } +}