Skip to content

Commit

Permalink
Swapped ecs to use new process style
Browse files Browse the repository at this point in the history
  • Loading branch information
spamoom committed Jul 27, 2020
1 parent e71dfbb commit a9a818d
Showing 1 changed file with 56 additions and 85 deletions.
141 changes: 56 additions & 85 deletions app/Helpers/Aws/Ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
}
Expand Down

0 comments on commit a9a818d

Please sign in to comment.