Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 20, 2024
1 parent 87ef966 commit 4f33c5c
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ecspresso/reviewapps/dk-2419/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e -o pipefail
cd $(dirname $0)

find . -name "ecspresso.yml" | xargs -I{} -P10 ecspresso --config={} delete --force --terminate ||:
sleep 10 # wait for ECS Services to be deleted
aws servicediscovery get-service --id srv-dd7axudjrtpvkdho &>/dev/null && aws servicediscovery delete-service --id srv-dd7axudjrtpvkdho
aws servicediscovery get-service --id srv-fm4s4u4qfga3b2va &>/dev/null && aws servicediscovery delete-service --id srv-fm4s4u4qfga3b2va
aws elbv2 describe-rules --rule-arn arn:aws:elasticloadbalancing:us-west-2:607167088920:listener-rule/app/dreamkast-dev/122c5b4a47b64f9d/bc86e7b2e4bca8f5/3dc35c00f5be46a0 &>/dev/null && aws elbv2 delete-rule --rule-arn arn:aws:elasticloadbalancing:us-west-2:607167088920:listener-rule/app/dreamkast-dev/122c5b4a47b64f9d/bc86e7b2e4bca8f5/3dc35c00f5be46a0
aws elbv2 describe-target-groups --target-group-arn arn:aws:elasticloadbalancing:us-west-2:607167088920:targetgroup/dev-dk-2419/627d14921fe18ebe &>/dev/null && aws elbv2 delete-target-group --target-group-arn arn:aws:elasticloadbalancing:us-west-2:607167088920:targetgroup/dev-dk-2419/627d14921fe18ebe
:
45 changes: 45 additions & 0 deletions ecspresso/reviewapps/dk-2419/const.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
PR_NAME: 'dk-2419',
externalEndpoints: {
dkApi: 'https://api.dev.cloudnativedays.jp',
dkWeaver: 'https://dkw.dev.cloudnativedays.jp',
},
imageTags: {
dreamkast_ecs: 'c4b43b17443ffc9de5ac17004764d2e6659eaf05',
mysql: '8.0.33',
redis: '6.0',
},
internalEndpoints: {
rdb: 'mysql-dk-2419.development.local',
redis: 'redis://redis-dk-2419.development.local',
},
publicSubnetIDs: [
'subnet-00709135a42bf907e',
'subnet-0d07831c8fc073511',
'subnet-033491d41490494b6',
],
region: 'us-west-2',
s3: {
dreamkast: {
name: 'dreamkast-dev-bucket',
region: 'us-west-2',
},
},
secretManager: {
dk: 'dreamkast/reviewapp-env-yGJKrj',
railsApp: 'dreamkast/rails-app-secret-SqidNC',
},
sentry: {
dsn: 'TODO',
},
serviceDiscovery: {
mysql: 'srv-dd7axudjrtpvkdho',
redis: 'srv-fm4s4u4qfga3b2va',
},
sqs: {
fifo: 'dreamkast-stg-fifo-queue',
},
targetGroupArn: {
dk: 'arn:aws:elasticloadbalancing:us-west-2:607167088920:targetgroup/dev-dk-2419/627d14921fe18ebe',
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
region: us-west-2
cluster: dreamkast-dev
service: dk-2419-dreamkast-fifo-worker
service_definition: service-def.jsonnet
task_definition: task-def.jsonnet
timeout: 10m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local dreamkast_fifo_worker = import '../../../base/dreamkast-fifo-worker.libsonnet';
local const = import '../const.libsonnet';

dreamkast_fifo_worker.serviceDef(
region=const.region,
subnetIDs=const.publicSubnetIDs,
securityGroupID='sg-0140d2aeaaa5d6d07', // dreamkast-dev-ecs-dreamkast-fifo-worker
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local dreamkast_fifo_worker = import '../../../base/dreamkast-fifo-worker.libsonnet';
local const = import '../const.libsonnet';

dreamkast_fifo_worker.taskDef(
family='dreamkast-dev-%s-fifo-worker' % [const.PR_NAME],
taskRoleName='dreamkast-dev-ecs-dreamkast-fifo-worker',
imageTag=const.imageTags.dreamkast_ecs,

region=const.region,
rdbInternalEndpoint=const.internalEndpoints.rdb,
redisInternalEndpoint=const.internalEndpoints.redis,

s3BucketName=const.s3.dreamkast.name,
s3BucketRegion=const.s3.dreamkast.region,

sqsFifoQueueName=const.sqs.fifo,

sentryDsn=const.sentry.dsn,

railsAppSecretManagerName=const.secretManager.railsApp,
rdsSecretManagerName=const.secretManager.rds,
dreamkastSecretManagerName=const.secretManager.dk,

enableLogging=true,
reviewapp=true,
)
6 changes: 6 additions & 0 deletions ecspresso/reviewapps/dk-2419/dreamkast/ecspresso.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
region: us-west-2
cluster: dreamkast-dev
service: dk-2419-dreamkast
service_definition: service-def.jsonnet
task_definition: task-def.jsonnet
timeout: 10m
9 changes: 9 additions & 0 deletions ecspresso/reviewapps/dk-2419/dreamkast/service-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local dreamkast_dk = import '../../../base/dreamkast.libsonnet';
local const = import '../const.libsonnet';

dreamkast_dk.serviceDef(
region=const.region,
subnetIDs=const.publicSubnetIDs,
securityGroupID='sg-00e734fea020b954d', // dreamkast-dev-ecs-dreamkast
targetGroupArn=const.targetGroupArn.dk,
)
28 changes: 28 additions & 0 deletions ecspresso/reviewapps/dk-2419/dreamkast/task-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local dreamkast = import '../../../base/dreamkast.libsonnet';
local const = import '../const.libsonnet';

dreamkast.taskDef(
family='dreamkast-dev-%s-dk' % [const.PR_NAME],
taskRoleName='dreamkast-dev-ecs-dreamkast',
imageTag=const.imageTags.dreamkast_ecs,

region=const.region,
dkApiEndpoint=const.externalEndpoints.dkApi,
dkWeaverEndpoint=const.externalEndpoints.dkWeaver,
rdbInternalEndpoint=const.internalEndpoints.rdb,
redisInternalEndpoint=const.internalEndpoints.redis,

s3BucketName=const.s3.dreamkast.name,
s3BucketRegion=const.s3.dreamkast.region,

sqsFifoQueueName=const.sqs.fifo,

sentryDsn=const.sentry.dsn,

railsAppSecretManagerName=const.secretManager.railsApp,
rdsSecretManagerName=const.secretManager.rds,
dreamkastSecretManagerName=const.secretManager.dk,

enableLogging=true,
reviewapp=true,
)
101 changes: 101 additions & 0 deletions ecspresso/reviewapps/dk-2419/initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash
set -e -o pipefail
cd $(dirname $0)

# required the following commands:
# * aws
# * jq
# * jsonnet
# * jsonnetfmt

# variables
PR_NAME=${PR_NAME:?"PR_NAME must be specified"}
PR_NUMBER=${PR_NUMBER:?"PR_NUMBER must be specified"}
IMAGE_TAG=${IMAGE_TAG:?"IMAGE_TAG must be specified"}
LISTENER_RULE_PRIORITY_BASE=30000
LISTENER_RULE_PRIORITY=$(( LISTENER_RULE_PRIORITY_BASE + PR_NUMBER ))

VPC_ID="vpc-0f0d012967c635f34"
LISTENER_ARN="arn:aws:elasticloadbalancing:us-west-2:607167088920:listener/app/dreamkast-dev/122c5b4a47b64f9d/bc86e7b2e4bca8f5"
SERVICE_DISCOVERY_NAMESPACE="ns-53ijjrlltqf5r2gm"


if [ ! -f "cleanup.sh" ]; then
# create TargetGroup
TARGET_GROUP_ARN=$(aws elbv2 create-target-group \
--name "dev-${PR_NAME}" \
--target-type ip \
--protocol HTTP \
--port 3000 \
--vpc-id ${VPC_ID} \
--ip-address-type ipv4 \
| jq -r ".TargetGroups[0].TargetGroupArn")

# create ALB ListenerRule
LISTENER_RULE_ARN=$(aws elbv2 create-rule --listener-arn ${LISTENER_ARN} \
--priority ${LISTENER_RULE_PRIORITY} \
--conditions Field=host-header,Values="dreamkast-${PR_NAME}.dev.cloudnativedays.jp" \
--actions Type=forward,TargetGroupArn=${TARGET_GROUP_ARN} \
| jq -r ".Rules[] | select(.Priority == \"${LISTENER_RULE_PRIORITY}\") | .RuleArn")

# create ServiceDiscovery Services
SERVICE_ID_MYSQL=$(aws servicediscovery create-service \
--name "mysql-${PR_NAME}" \
--dns-config "NamespaceId="${SERVICE_DISCOVERY_NAMESPACE}",DnsRecords=[{Type="A",TTL="10"}]" \
--health-check-custom-config FailureThreshold=1 \
| jq -r ".Service.Id")

SERVICE_ID_REDIS=$(aws servicediscovery create-service \
--name "redis-${PR_NAME}" \
--dns-config "NamespaceId="${SERVICE_DISCOVERY_NAMESPACE}",DnsRecords=[{Type="A",TTL="10"}]" \
--health-check-custom-config FailureThreshold=1 \
| jq -r ".Service.Id")

# replace variables in each ecspresso.yml
find . -name ecspresso.yml | xargs -I{} sed -i -e 's/__PR_NAME__/'${PR_NAME}'/g' {}

# replace variables in const.libsonnet
cat << _EOL_ | jsonnet - > ./const.libsonnet.tmp
local const = import './const.libsonnet';
const + {
PR_NAME: "${PR_NAME}",
targetGroupArn: {
dk: "${TARGET_GROUP_ARN}",
},
serviceDiscovery: {
mysql: "${SERVICE_ID_MYSQL}",
redis: "${SERVICE_ID_REDIS}",
},
imageTags: const.imageTags + {
dreamkast_ecs: "${IMAGE_TAG}",
},
}
_EOL_
mv const.libsonnet.tmp const.libsonnet

# create cleanup.sh
cat << _EOF_ > ./cleanup.sh
#!/usr/bin/env bash
set -e -o pipefail
cd \$(dirname \$0)
find . -name "ecspresso.yml" | xargs -I{} -P10 ecspresso --config={} delete --force --terminate ||:
sleep 10 # wait for ECS Services to be deleted
aws servicediscovery get-service --id ${SERVICE_ID_MYSQL} &>/dev/null && aws servicediscovery delete-service --id ${SERVICE_ID_MYSQL}
aws servicediscovery get-service --id ${SERVICE_ID_REDIS} &>/dev/null && aws servicediscovery delete-service --id ${SERVICE_ID_REDIS}
aws elbv2 describe-rules --rule-arn ${LISTENER_RULE_ARN} &>/dev/null && aws elbv2 delete-rule --rule-arn ${LISTENER_RULE_ARN}
aws elbv2 describe-target-groups --target-group-arn ${TARGET_GROUP_ARN} &>/dev/null && aws elbv2 delete-target-group --target-group-arn ${TARGET_GROUP_ARN}
:
_EOF_

else
# update imageTags
jsonnet const.libsonnet \
| jq ".imageTags.dreamkast_ecs|=\"${IMAGE_TAG}\"" \
> const.libsonnet.tmp
mv const.libsonnet.tmp const.libsonnet

fi

jsonnetfmt -i const.libsonnet
6 changes: 6 additions & 0 deletions ecspresso/reviewapps/dk-2419/mysql/ecspresso.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
region: us-west-2
cluster: dreamkast-dev
service: dk-2419-mysql
service_definition: service-def.jsonnet
task_definition: task-def.jsonnet
timeout: 10m
9 changes: 9 additions & 0 deletions ecspresso/reviewapps/dk-2419/mysql/service-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local redis = import '../../../base/mysql.libsonnet';
local const = import '../const.libsonnet';

redis.serviceDef(
region=const.region,
subnetIDs=const.publicSubnetIDs,
securityGroupID='sg-0e0029eb49f4d0455', // dreamkast-dev-ecs-mysql
serviceDiscoveryID=const.serviceDiscovery.mysql,
)
12 changes: 12 additions & 0 deletions ecspresso/reviewapps/dk-2419/mysql/task-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local mysql = import '../../../base/mysql.libsonnet';
local const = import '../const.libsonnet';

mysql.taskDef(
family='dreamkast-dev-%s-mysql' % [const.PR_NAME],
taskRoleName='dreamkast-dev-ecs-mysql',
imageTag=const.imageTags.mysql,

region=const.region,

enableLogging=false,
)
6 changes: 6 additions & 0 deletions ecspresso/reviewapps/dk-2419/redis/ecspresso.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
region: us-west-2
cluster: dreamkast-dev
service: dk-2419-redis
service_definition: service-def.jsonnet
task_definition: task-def.jsonnet
timeout: 10m
9 changes: 9 additions & 0 deletions ecspresso/reviewapps/dk-2419/redis/service-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local redis = import '../../../base/redis.libsonnet';
local const = import '../const.libsonnet';

redis.serviceDef(
region=const.region,
subnetIDs=const.publicSubnetIDs,
securityGroupID='sg-0ab649652e2dd6c9c', // dreamkast-dev-ecs-redis
serviceDiscoveryID=const.serviceDiscovery.redis,
)
12 changes: 12 additions & 0 deletions ecspresso/reviewapps/dk-2419/redis/task-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local redis = import '../../../base/redis.libsonnet';
local const = import '../const.libsonnet';

redis.taskDef(
family='dreamkast-dev-%s-redis' % [const.PR_NAME],
taskRoleName='dreamkast-dev-ecs-redis',
imageTag=const.imageTags.redis,

region=const.region,

enableLogging=false,
)

0 comments on commit 4f33c5c

Please sign in to comment.