From d67695bb241f2725f4192dcd0abc0704c86136d3 Mon Sep 17 00:00:00 2001 From: GeoWill Date: Wed, 3 May 2023 15:06:15 +0100 Subject: [PATCH 1/2] Don't fail deploy if default asg empty We might want to set desired instances to zero on stage. This stops us from failing a deploy if that is the case, as a deploy will fail if there are no original instances to replace. --- deploy/create_deployment.py | 41 +++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/deploy/create_deployment.py b/deploy/create_deployment.py index ada9a695b1..3e0c156b2b 100644 --- a/deploy/create_deployment.py +++ b/deploy/create_deployment.py @@ -1,3 +1,4 @@ +import functools import os import sys @@ -8,26 +9,37 @@ def get_deployment_config_name(code_deploy_client): + asg_name = get_default_deployment_group_asg_name(code_deploy_client) + instance_count = get_asg_instance_count(asg_name) + + if instance_count > 1: + return "CodeDeployDefault.HalfAtATime" + return "CodeDeployDefault.AllAtOnce" + + +@functools.cache +def get_default_deployment_group_asg_name( + code_deploy_client, + app_name="WDIVCodeDeploy", + deployment_group_name="WDIVDefaultDeploymentGroup", +): deployment_group = code_deploy_client.get_deployment_group( - applicationName="WDIVCodeDeploy", - deploymentGroupName="WDIVDefaultDeploymentGroup", + applicationName=app_name, + deploymentGroupName=deployment_group_name, ) asg_name = deployment_group["deploymentGroupInfo"]["autoScalingGroups"][0]["name"] + return asg_name + + +@functools.cache +def get_asg_instance_count(asg_name): autoscale_client = session.client( "autoscaling", region_name=os.environ.get("AWS_REGION") ) - autoscale_client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg_name])[ - "AutoScalingGroups" - ][0] asg_info = autoscale_client.describe_auto_scaling_groups( AutoScalingGroupNames=[asg_name] )["AutoScalingGroups"][0] - instance_count = len( - [i for i in asg_info["Instances"] if i["LifecycleState"] == "InService"] - ) - if instance_count > 1: - return "CodeDeployDefault.HalfAtATime" - return "CodeDeployDefault.AllAtOnce" + return len([i for i in asg_info["Instances"] if i["LifecycleState"] == "InService"]) def create_deployment(): @@ -49,6 +61,11 @@ def create_deployment(): f"Another deploy ({active_deployments}) is blocking this one, waiting {WAIT_SECONDS} seconds" ) time.sleep(WAIT_SECONDS) + + if get_asg_instance_count(get_default_deployment_group_asg_name(client)) == 0: + sys.stdout.write("existing ASG is empty, so not creating deployment.") + return None + deployment = client.create_deployment( applicationName="WDIVCodeDeploy", deploymentGroupName="WDIVDefaultDeploymentGroup", @@ -143,6 +160,8 @@ def get_failed_target_info(deploy_client, deploy_id): def main(): deployment_id = create_deployment() + if not deployment_id: + return check_deployment(deployment_id=deployment_id) From 0793a75240cab0ec04ff5504e586137d897d81d5 Mon Sep 17 00:00:00 2001 From: GeoWill Date: Wed, 3 May 2023 15:45:30 +0100 Subject: [PATCH 2/2] Don't Merge: set dev to zero --- .circleci/deploy-config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/deploy-config.yml b/.circleci/deploy-config.yml index 2ea5f3ea85..870a6fa03a 100644 --- a/.circleci/deploy-config.yml +++ b/.circleci/deploy-config.yml @@ -351,9 +351,9 @@ workflows: context: [deployment-development-wdiv, slack-secrets] filters: { branches: { only: [ development ] } } dc-environment: development - min-size: 1 + min-size: 0 max-size: 2 - desired-capacity: 1 + desired-capacity: 0 - run_new_imports_post_deploy: # NB if no imports have changed this is a no-op name: "Development: Run New Imports Post Deploy"