Initial migration (#1)

This will be needed to make the initial tags. Once merged to main, I
will tag with the same module revisions as what exists in the Fleet
repo. We can then test the example module using the new settings.

Once validated, we'll then update the main fleet repo to redirect here
for /terraform and remove everything there. Anything referring to old
tags will still work in-place. Additionally any website references will
be updated as well to reflect these changes.
This commit is contained in:
Robert Fairburn authored and GitHub committed 2025-02-04 10:07:51 -06:00
1 parent ab9ac9862e
commit f378f62db2
186 files changed
+9900 -3

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# Migrations addon
This addon enables automatic migrations for Fleet.
Due to limitations in the AWS provider for Terraform, only Linux platforms are supported.
This module uses the local-exec provisioner to call aws-cli to complete migrations.
Due to this, the following commands must be available to the shell:
- aws
+1
View File
@@ -0,0 +1 @@
header-from: .header.md
+45
View File
@@ -0,0 +1,45 @@
# Migrations addon
This addon enables automatic migrations for Fleet.
Due to limitations in the AWS provider for Terraform, only Linux platforms are supported.
This module uses the local-exec provisioner to call aws-cli to complete migrations.
Due to this, the following commands must be available to the shell:
- aws
## Requirements
No requirements.
## Providers
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.31.0 |
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.2 |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [null_resource.main](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_desired_count"></a> [desired\_count](#input\_desired\_count) | n/a | `number` | n/a | yes |
| <a name="input_ecs_cluster"></a> [ecs\_cluster](#input\_ecs\_cluster) | n/a | `string` | n/a | yes |
| <a name="input_ecs_service"></a> [ecs\_service](#input\_ecs\_service) | n/a | `string` | n/a | yes |
| <a name="input_min_capacity"></a> [min\_capacity](#input\_min\_capacity) | n/a | `number` | n/a | yes |
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | n/a | `list(string)` | n/a | yes |
| <a name="input_subnets"></a> [subnets](#input\_subnets) | n/a | `list(string)` | n/a | yes |
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | n/a | `string` | n/a | yes |
| <a name="input_task_definition_revision"></a> [task\_definition\_revision](#input\_task\_definition\_revision) | n/a | `number` | n/a | yes |
## Outputs
No outputs.
+10
View File
@@ -0,0 +1,10 @@
data "aws_region" "current" {}
resource "null_resource" "main" {
triggers = {
task_definition_revision = var.task_definition_revision
}
provisioner "local-exec" {
command = "/bin/bash ${path.module}/migrate.sh REGION=${data.aws_region.current.name} VULN_SERVICE=${var.vuln_service} ECS_CLUSTER=${var.ecs_cluster} TASK_DEFINITION=${var.task_definition} TASK_DEFINITION_REVISION=${var.task_definition_revision} SUBNETS=${jsonencode(var.subnets)} SECURITY_GROUPS=${jsonencode(var.security_groups)} ECS_SERVICE=${var.ecs_service} MIN_CAPACITY=${var.min_capacity} DESIRED_COUNT=${var.desired_count}"
}
}
+62
View File
@@ -0,0 +1,62 @@
#!/bin/bash
set -e
function scale_services(){
UP_DOWN="${1:?}"
SERVICE_NAME="${2:?}" # Take service name as an argument
ADJUST_AUTOSCALING="${3:-}"
COUNT="${4:-1}"
# Set the minimum capacity and desired count in the cluster to 0 to scale down or to the original size to scale back to normal.
# This is a bit hacky, but the update-service has to happen first when scaling up and second when scaling down.
# Assume scaling down unless "up".
CAPACITY=0
if [ "${UP_DOWN:?}" = "up" ]; then
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count "${COUNT:?}"
CAPACITY="${MIN_CAPACITY:?}"
fi
if [ -n "${ADJUST_AUTOSCALING}" ]; then
aws application-autoscaling register-scalable-target --region "${REGION:?}" --service-namespace ecs --resource-id "service/${ECS_CLUSTER:?}/${SERVICE_NAME:?}" --scalable-dimension "ecs:service:DesiredCount" --min-capacity "${CAPACITY:?}"
fi
# We are scaling down, make it 0
if [ "${UP_DOWN:?}" != "up" ]; then
aws ecs update-service --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --service "${SERVICE_NAME:?}" --desired-count 0
fi
# The first task definition might never get stable because it never had initial migrations so don't wait before continuing
if [ "${TASK_DEFINITION_REVISION}" != "1" ]; then
# Wait for scale-down to succeed
aws ecs wait services-stable --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --services "${SERVICE_NAME:?}"
fi
}
for ARGUMENT in "$@"
do
KEY=$(echo $ARGUMENT | cut -f1 -d=)
KEY_LENGTH=${#KEY}
VALUE="${ARGUMENT:$KEY_LENGTH+1}"
export "$KEY"="$VALUE"
done
scale_services down "${ECS_SERVICE:?}" true "${DESIRED_COUNT}"
if [ -n "${VULN_SERVICE}" ]; then
scale_services down "${VULN_SERVICE:?}"
fi
# Call aws ecs run-task
TASK_ARN="$(aws ecs run-task --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --task-definition "${TASK_DEFINITION:?}":"${TASK_DEFINITION_REVISION:?}" --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets="${SUBNETS:?}",securityGroups="${SECURITY_GROUPS:?}"}" --query 'tasks[].taskArn' --overrides '{"containerOverrides": [{"name": "fleet", "command": ["fleet", "prepare", "db"]}]}' --output text | rev | cut -d'/' -f1 | rev)"
# Wait for completion
aws ecs wait tasks-stopped --region "${REGION:?}" --cluster="${ECS_CLUSTER:?}" --tasks="${TASK_ARN:?}"
scale_services up "${ECS_SERVICE:?}" true "${DESIRED_COUNT}"
if [ -n "${VULN_SERVICE}" ]; then
scale_services up "${VULN_SERVICE:?}"
fi
# Exit with task's exit code
TASK_EXIT_CODE=$(aws ecs describe-tasks --region "${REGION:?}" --cluster "${ECS_CLUSTER:?}" --tasks "${TASK_ARN:?}" --query "tasks[0].containers[?name=='fleet'].exitCode" --output text)
exit "${TASK_EXIT_CODE}"
Binary file not shown.
View File
Whitespace-only changes.
+44
View File
@@ -0,0 +1,44 @@
variable "ecs_cluster" {
type = string
nullable = false
}
variable "ecs_service" {
type = string
nullable = false
}
variable "min_capacity" {
type = number
nullable = false
}
variable "desired_count" {
type = number
nullable = false
}
variable "task_definition" {
type = string
nullable = false
}
variable "task_definition_revision" {
type = number
nullable = false
}
variable "subnets" {
type = list(string)
nullable = false
}
variable "security_groups" {
type = list(string)
nullable = false
}
variable "vuln_service" {
default = ""
}