Explicitly set versions on launch templates

parent 896d566e
...@@ -53,15 +53,42 @@ class ASG(): ...@@ -53,15 +53,42 @@ class ASG():
inactive_asg = self.get_inactive_asg(asg) inactive_asg = self.get_inactive_asg(asg)
self.scale(inactive_asg, 0, 0, 0) self.scale(inactive_asg, 0, 0, 0)
def set_asg_launch_template_version(self, asg, lt_id, lt_version):
asg_client = aws_client.create_client('autoscaling', self.region, self.role_arn)
asg_client.update_auto_scaling_group(
AutoScalingGroupName = asg,
LaunchTemplate = {
"LaunchTemplateId": lt_id,
"Version": lt_version
}
)
def do_update(self, ami, asg=None, loadbalancer=None, target_group=None): def do_update(self, ami, asg=None, loadbalancer=None, target_group=None):
asg_liveness_info = self.asgs_by_liveness(asg=asg, loadbalancer=loadbalancer, target_group=target_group) asg_liveness_info = self.asgs_by_liveness(asg=asg, loadbalancer=loadbalancer, target_group=target_group)
inactive_asg = asg_liveness_info['inactive_asg'] inactive_asg = asg_liveness_info['inactive_asg']
active_asg = asg_liveness_info['active_asg'] active_asg = asg_liveness_info['active_asg']
new_ami = ami new_ami = ami
current_lt_info = self.get_launch_template_info(self.get_lt_name(inactive_asg))
logging.info("New ASG was worked out as {}. Now updating it's Launch Template".format(inactive_asg)) logging.info("New ASG was worked out as {}. Now updating it's Launch Template".format(inactive_asg))
self.update_launch_template(inactive_asg, new_ami, self.get_lt_name(inactive_asg))
# Set the current ASG to the current launch template version. Shouldn't actually be necessary,
# since it's done during the previous deploy, but including for completeness and paranoia
self.set_asg_launch_template_version(
asg=active_asg,
lt_id=current_lt_info["id"],
lt_version=current_lt_info["version"],
)
# Update the lt and set the soon to be new ASG to the new launch template version
updated_lt = self.update_launch_template(new_ami, self.get_lt_name(inactive_asg))
self.set_asg_launch_template_version(
asg=inactive_asg,
lt_id=updated_lt["id"],
lt_version=updated_lt["version"]
)
scale_to = self.get_current_scale(active_asg) scale_to = self.get_current_scale(active_asg)
...@@ -231,7 +258,9 @@ class ASG(): ...@@ -231,7 +258,9 @@ class ASG():
return "-".join(asg_parts) return "-".join(asg_parts)
def get_launch_template_id(self, lt_name): def get_launch_template_info(self, lt_name):
"""Returns the [id] and (latest) [version] number for lt_name"""
ec2_client = aws_client.create_client('ec2', self.region, self.role_arn) ec2_client = aws_client.create_client('ec2', self.region, self.role_arn)
response = ec2_client.describe_launch_templates( response = ec2_client.describe_launch_templates(
...@@ -244,23 +273,27 @@ class ASG(): ...@@ -244,23 +273,27 @@ class ASG():
] ]
) )
# This is hackish because we are assuming that there is going to be only 1 (one) launch template # FIXME: This is hackish because we are assuming that there is going to be only 1 (one) launch template
return { return {
"id": response['LaunchTemplates'][0]['LaunchTemplateId'], "id": response['LaunchTemplates'][0]['LaunchTemplateId'],
"version": response['LaunchTemplates'][0]['LatestVersionNumber'] "version": str(response['LaunchTemplates'][0]['LatestVersionNumber'])
} }
def update_launch_template(self, inactive_asg, ami, lt_name): def update_launch_template(self, ami, lt_name):
lt = self.get_launch_template_id(lt_name) """
lt_client = aws_client.create_client('ec2', self.region, self.role_arn) Creates a new template version for [lt_name] which uses [ami], and sets the default
default template version to be that version.
lt_id = lt['id'] Returns [id] and (new) [version] number
lt_source_version = str(lt['version']) """
lt = self.get_launch_template_info(lt_name)
lt_client = aws_client.create_client('ec2', self.region, self.role_arn)
response = lt_client.create_launch_template_version( response = lt_client.create_launch_template_version(
DryRun = False, DryRun = False,
LaunchTemplateId = lt_id, LaunchTemplateId = lt['id'],
SourceVersion = lt_source_version, SourceVersion = str(lt['version']),
LaunchTemplateData = { LaunchTemplateData = {
"ImageId": ami "ImageId": ami
} }
...@@ -269,12 +302,12 @@ class ASG(): ...@@ -269,12 +302,12 @@ class ASG():
launch_template_new_version = str(response['LaunchTemplateVersion']['VersionNumber']) launch_template_new_version = str(response['LaunchTemplateVersion']['VersionNumber'])
lt_client.modify_launch_template( lt_client.modify_launch_template(
LaunchTemplateId=lt_id, LaunchTemplateId=lt['id'],
DefaultVersion=launch_template_new_version DefaultVersion=launch_template_new_version
) )
return { return {
"id": lt_id, "id": lt['id'],
"version": launch_template_new_version "version": launch_template_new_version
} }
......
...@@ -8,7 +8,7 @@ with open("README.md", "r") as fh: ...@@ -8,7 +8,7 @@ with open("README.md", "r") as fh:
setuptools.setup( setuptools.setup(
name="akinaka", name="akinaka",
version="0.4.0", version="0.4.0-alpha1",
python_requires='>=3.3.0', python_requires='>=3.3.0',
author="Afraz", author="Afraz",
author_email="afraz@olindata.com", author_email="afraz@olindata.com",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment