diff --git a/app/Helpers/Aws/Ecs.php b/app/Helpers/Aws/Ecs.php index 67fb374..d09aba5 100644 --- a/app/Helpers/Aws/Ecs.php +++ b/app/Helpers/Aws/Ecs.php @@ -4,6 +4,7 @@ use App\Helpers\Aws; use App\Helpers\NetsellsFile; +use App\Exceptions\ProcessFailed; use Symfony\Component\Process\Process; use LaravelZero\Framework\Commands\Command; @@ -22,39 +23,28 @@ public function authenticateDocker(Command $command): bool $awsAccountId = $this->aws->helpers->console()->handleOverridesAndFallbacks($command->option('aws-account-id'), NetsellsFile::DOCKER_AWS_ACCOUNT_ID, Aws::DEFAULT_ACCOUNT_ID); $awsRegion = $this->aws->helpers->console()->handleOverridesAndFallbacks($command->option('aws-region'), NetsellsFile::DOCKER_AWS_REGION, Aws::DEFAULT_REGION); - $process = $this->aws->newProcess($command, [ - 'ecr', 'get-login-password', - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } - + try { + $processOutput = $this->aws->newProcess($command, [ + 'ecr', 'get-login-password', + ]) + ->echoLineByLineOutput(false) + ->run(); + } catch (ProcessFailed $e) { $command->error("Unable to get docker password from AWS."); return false; } - $password = $process->getOutput(); - - $process = new Process([ - 'docker', 'login', - "--username=AWS", - "--password={$password}", - "{$awsAccountId}.dkr.ecr.{$awsRegion}.amazonaws.com" - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } - + $password = $processOutput; + + try { + $this->aws->helpers->process()->withCommand([ + 'docker', 'login', + "--username=AWS", + "--password={$password}", + "{$awsAccountId}.dkr.ecr.{$awsRegion}.amazonaws.com" + ]) + ->echoLineByLineOutput(false); + } catch (ProcessFailed $e) { $command->error("Unable to login to docker."); return false; } @@ -64,68 +54,53 @@ public function authenticateDocker(Command $command): bool public function getTaskDefinition(Command $command, $name): ?array { - $process = $this->aws->newProcess($command, [ - 'ecs', 'describe-task-definition', "--task-definition={$name}", - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } - + try { + $processOutput = $this->aws->newProcess($command, [ + 'ecs', 'describe-task-definition', "--task-definition={$name}", + ]) + ->echoLineByLineOutput(false) + ->run(); + } catch (ProcessFailed $e) { $command->error("Unable to get task definition [{$name}] from AWS."); return null; } - return json_decode($process->getOutput(), true); + return json_decode($processOutput, true); } public function registerTaskDefinition(Command $command, string $taskDefinitionJson): ?array { - $process = $this->aws->newProcess($command, [ - 'ecs', 'register-task-definition', "--cli-input-json", $taskDefinitionJson, - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } - + try { + $processOutput = $this->aws->newProcess($command, [ + 'ecs', 'register-task-definition', "--cli-input-json", $taskDefinitionJson, + ]) + ->echoLineByLineOutput(false) + ->run(); + } catch (ProcessFailed $e) { $command->error("Unable to register task definition in AWS."); return null; } - return json_decode($process->getOutput(), true); + return json_decode($processOutput, true); } public function updateService(Command $command, string $clusterName, string $serviceName, string $taskDefinition): ?array { - $process = $this->aws->newProcess($command, [ - 'ecs', 'update-service', - "--cluster={$clusterName}", - "--service={$serviceName}", - "--task-definition={$taskDefinition}", - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } - + try { + $processOutput = $this->aws->newProcess($command, [ + 'ecs', 'update-service', + "--cluster={$clusterName}", + "--service={$serviceName}", + "--task-definition={$taskDefinition}", + ]) + ->echoLineByLineOutput(false) + ->run(); + } catch (ProcessFailed $e) { $command->error("Unable to update service in AWS."); return null; } - return json_decode($process->getOutput(), true); + return json_decode($processOutput, true); } public function runTaskWithCommand(Command $command, string $clusterName, string $taskDefinition, array $migrateCommand, string $container): void @@ -139,21 +114,17 @@ public function runTaskWithCommand(Command $command, string $clusterName, string ] ]); - $process = $this->aws->newProcess($command, [ - 'ecs', 'run-task', - "--cluster={$clusterName}", - "--overrides={$overrides}", - "--task-definition={$taskDefinition}", - ]); - - $process->start(); - $process->wait(); - - if ($process->getExitCode() !== 0) { - foreach ($process as $data) { - echo $data; - } + try { + $this->aws->newProcess($command, [ + 'ecs', 'run-task', + "--cluster={$clusterName}", + "--overrides={$overrides}", + "--task-definition={$taskDefinition}", + ]) + ->echoLineByLineOutput(false) + ->run(); + } catch (ProcessFailed $e) { $command->error("Unable to start migration task in AWS."); return; }