Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix first deploy cluster creation #76

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,10 @@ if [[ $deploy_process_type != "scheduledtasks" && ( -z "$ECS_SERVICE_TASK_PROCES
PORT=3000
fi

deploy_json_workload_resource_tags=$(jq --raw-input --raw-output '[ split(",") | .[] | "key=" + split("=")[0] + ",value=" + split("=")[1] ] | join(" ")' <<<"$WORKLOAD_RESOURCE_TAGS")
deploy_json_workload_resource_tags_captalized=$(jq --raw-input --raw-output '[ split(",") | .[] | "Key=" + split("=")[0] + ",Value=" + split("=")[1] ] | join(" ")' <<<"$WORKLOAD_RESOURCE_TAGS")

echo "----> Deploying service $deploy_process_type"

if [ -z "$DEPLOYMENT_CIRCUIT_BREAKER_RULE" ]; then
DEPLOYMENT_CIRCUIT_BREAKER_RULE='enable=true,rollback=true'
fi

deploy_cluster_status=$(aws ecs describe-clusters \
--cluster $deploy_cluster_id --query 'clusters[?status==`INACTIVE`].status' --output text
)
deploy_cluster_failure_reason=$(aws ecs describe-clusters \
--cluster $deploy_cluster_id --query 'failures[?reason==`MISSING`].reason' --output text
)

if [ "$deploy_cluster_status" == "INACTIVE" ] || [ ! -z "$deploy_cluster_failure_reason" ]; then
deploy_create_cluster=$(aws ecs create-cluster \
--cluster-name $deploy_cluster_id \
--tags $deploy_json_workload_resource_tags \
--capacity-provider FARGATE FARGATE_SPOT \
--default-capacity-provider-strategy capacityProvider=FARGATE_SPOT,weight=1
)
echo "----> First deployment detected. Provisioning cluster $deploy_cluster_id"
fi

if [ ! -z "$deploy_create_service" ]; then
deploy_ecs_output=$(aws ecs create-service \
--cluster $deploy_cluster_id \
Expand Down
3 changes: 2 additions & 1 deletion main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ while IFS= read -r line; do

provision.sh --process-type $line \
--repository-slug $REPO_SLUG \
--branch-name $BRANCH
--branch-name $BRANCH \
--cluster-id $ECS_CLUSTER_ID

deploy.sh --process-type $line \
--service-name $REPO_SLUG-$BRANCH-$line \
Expand Down
33 changes: 29 additions & 4 deletions provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eo pipefail

VALID_ARGS=$(getopt -o b:p:r: --long branch-name:,process-type:,repository-slug: -n 'provision.sh' -- "$@")
VALID_ARGS=$(getopt -o b:p:r:i: --long branch-name:,process-type:,repository-slug:,cluster-id: -n 'provision.sh' -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi
Expand All @@ -12,19 +12,20 @@ while [ : ]; do
case "$1" in
-b | --branch-name)
provision_branch_name=$2
#echo "Branch name is '$2'"
shift 2
;;
-p | --process-type)
provision_process_type=$2
#echo "Process Type is '$2'"
shift 2
;;
-r | --repository-slug)
provision_repository_slug=$2
#echo "Repository slug is '$2'"
shift 2
;;
-i | --cluster-id)
provision_cluster_id=$2
shift 2
;;
--) shift;
break
;;
Expand All @@ -46,6 +47,11 @@ if [ -z "$provision_repository_slug" ]; then
exit 1
fi

if [ -z "$provision_cluster_id" ]; then
echo "Error: Missing required parameter --cluster-id"
exit 1
fi

provision_log_group_name="/ecs/$provision_repository_slug-$provision_branch_name-$provision_process_type"
provision_log_group_exists=$(aws logs describe-log-groups --log-group-name-prefix $provision_log_group_name | jq '.logGroups | length')
if [ "$provision_log_group_exists" -eq "0" ]; then
Expand All @@ -55,6 +61,25 @@ if [ "$provision_log_group_exists" -eq "0" ]; then
echo "----> Created missing log group for $provision_process_type"
fi

provision_json_workload_resource_tags=$(jq --raw-input --raw-output '[ split(",") | .[] | "key=" + split("=")[0] + ",value=" + split("=")[1] ] | join(" ")' <<<"$WORKLOAD_RESOURCE_TAGS")
provision_json_workload_resource_tags_captalized=$(jq --raw-input --raw-output '[ split(",") | .[] | "Key=" + split("=")[0] + ",Value=" + split("=")[1] ] | join(" ")' <<<"$WORKLOAD_RESOURCE_TAGS")

provision_cluster_status=$(aws ecs describe-clusters \
--cluster $provision_cluster_id --query 'clusters[?status==`INACTIVE`].status' --output text
)
provision_cluster_failure_reason=$(aws ecs describe-clusters \
--cluster $provision_cluster_id --query 'failures[?reason==`MISSING`].reason' --output text
)
if [ "$provision_cluster_status" == "INACTIVE" ] || [ ! -z "$provision_cluster_failure_reason" ]; then
provision_create_cluster=$(aws ecs create-cluster \
--cluster-name $provision_cluster_id \
--tags $provision_json_workload_resource_tags \
--capacity-provider FARGATE FARGATE_SPOT \
--default-capacity-provider-strategy capacityProvider=FARGATE_SPOT,weight=1
)
echo "----> First deployment detected. Provisioning cluster $provision_cluster_id"
fi

provision_json_workload_resource_tags=$(jq --raw-input --raw-output '[ split(",") | .[] | "Key=" + split("=")[0] + ",Value=" + split("=")[1] ] | join(" ")' <<<"$WORKLOAD_RESOURCE_TAGS")
if [ "$provision_process_type" = "web" ]; then
provision_alb_name=$provision_repository_slug-$provision_branch_name
Expand Down