Skip to content

Commit

Permalink
Adds ability to specify a migration command for AWS ECS Update
Browse files Browse the repository at this point in the history
  • Loading branch information
spamoom committed Jul 14, 2020
1 parent adb69f8 commit e0f828b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ docker:
cluster: traefik
service: netsells-api
task-definition: netsells-api
```

# If set, will spin up a migrate task to run the command during an update
migrate:
container: php
command: LARAVEL_DATABASE_MIGRATIONS # Using a constant
# command: ["php", "artisan", "migrate", "--force"] # List syntax, same as dockerfile - https://docs.docker.com/engine/reference/builder/#cmd
```
35 changes: 35 additions & 0 deletions app/Commands/DeployEcsServiceUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function configure()
new InputOption('ecs-service', null, InputOption::VALUE_OPTIONAL, 'The ECS service name'),
new InputOption('ecs-cluster', null, InputOption::VALUE_OPTIONAL, 'The ECS cluster name'),
new InputOption('ecs-task-definition', null, InputOption::VALUE_OPTIONAL, 'The ECS task definition name'),
new InputOption('migrate-container', null, InputOption::VALUE_OPTIONAL, 'The container to run the migration on'),
new InputOption('migrate-command', null, InputOption::VALUE_OPTIONAL, 'The migration command to run'),
], $this->helpers->aws()->commonConsoleOptions()));
}

Expand Down Expand Up @@ -91,9 +93,42 @@ public function handle()
$this->helpers->aws()->ecs()->updateService($this, $this->clusterName, $this->serviceName, $newTaskDefinitionString);
$this->line("Service updated to task definition {$newTaskDefinitionString}");

$migrateCommand = $this->helpers->console()->handleOverridesAndFallbacks($this->option('migrate-command'), NetsellsFile::DOCKER_ECS_MIGRATE_COMMAND);
$migrateContainer = $this->helpers->console()->handleOverridesAndFallbacks($this->option('migrate-container'), NetsellsFile::DOCKER_ECS_MIGRATE_CONTAINER);

if ($migrateCommand && $migrateContainer) {
$this->line("Migrate command detected, running as a one-off task.");

$this->runMigrateCommand($migrateCommand, $newTaskDefinitionString, $migrateContainer);
}

$this->info("Successfully deployed to ECS, deployment can be seen at " . $this->generateDeploymentUrl());
}

protected function runMigrateCommand($migrateCommand, string $newTaskDefinitionString, string $container): void
{
$consts = [
'LARAVEL_DATABASE_MIGRATIONS' => ['php', 'artisan', 'migrate', '--force'],
];

if (is_string($migrateCommand)) {
foreach ($consts as $const => $value) {
if ($migrateCommand === $const) {
$migrateCommand = $value;
continue;
}
}
}

$this->helpers->aws()->ecs()->runTaskWithCommand(
$this,
$this->clusterName,
$newTaskDefinitionString,
$migrateCommand,
$container
);
}

protected function prepareNewTaskDefinitionRevisionString($newTaskDefinition): string
{
$newTaskDefinitionRevision = data_get($newTaskDefinition, 'taskDefinition.revision');
Expand Down
35 changes: 34 additions & 1 deletion app/Helpers/Aws/Ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,37 @@ public function updateService(Command $command, string $clusterName, string $ser

return json_decode($process->getOutput(), true);
}
}

public function runTaskWithCommand(Command $command, string $clusterName, string $taskDefinition, array $migrateCommand, string $container): void
{
$overrides = json_encode([
'containerOverrides' => [
[
'name' => $container,
'command' => $migrateCommand,
]
]
]);

$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;
}

$command->error("Unable to start migration task in AWS.");
return;
}

return;
}
}
4 changes: 3 additions & 1 deletion app/Helpers/NetsellsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class NetsellsFile
public const DOCKER_ECS_SERVICE = 'docker.aws.ecs.service';
public const DOCKER_ECS_CLUSTER = 'docker.aws.ecs.cluster';
public const DOCKER_ECS_TASK_DEFINITION = 'docker.aws.ecs.task-definition';
public const DOCKER_ECS_MIGRATE_COMMAND = 'docker.aws.ecs.migrate.command';
public const DOCKER_ECS_MIGRATE_CONTAINER = 'docker.aws.ecs.migrate.container';

protected $fileName = '.netsells.yml';
protected $fileData = null;
Expand Down Expand Up @@ -47,4 +49,4 @@ public function get($keyPath, $default = null)
{
return Arr::get($this->fileData, $keyPath, $default);
}
}
}

0 comments on commit e0f828b

Please sign in to comment.