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

Don't fail deploy if default asg empty #5723

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions .circleci/deploy-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 30 additions & 11 deletions deploy/create_deployment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import os
import sys

Expand All @@ -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():
Expand All @@ -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",
Expand Down Expand Up @@ -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)


Expand Down