Skip to content
Open
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
55 changes: 32 additions & 23 deletions tf_from_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@

DEFAULT_TEMPLATE = '''
module "{{ MODULE_NAME }}" {
source = "../modules/autoscaling/qw_asg"
source = "../../../../terraform/modules/autoscaling/qw_asg"

key_name = "${var.key_name}"
key_name = "{{ KEY_NAME }}"
asg_cluster = "{{ ASG_CLUSTER }}"
zones = "${var.default_zones}"
asg_queue = "{{ QUEUE_NAME }}"
asg_name = "{{ ASG_NAME }}"
worker_security_group = ["${var.worker_security_group}"]
consumer_config = "{{ CONSUMER_CONFIG }}"
r53_zone = "${var.r53_zone}"
hosted_domain = "${var.hosted_domain}"
vpc_subnet_ids = ["${split(",", join(",", var.vpc_public_subnet_ids))}"]
vpc_subnet_ids = ["${local.c3_supported_subnets}"]
name_tag = "${var.name_tag}"
zookeeper_dns = "${module.zookeeper.zookeeper_dns}"
zookeeper_dns = "${var.zookeeper_dns}"
env = "${var.env}"
app_branch = "${var.app_branch}"
asg_desired = "{{ ASG_DESIRED }}"
asg_max = "{{ ASG_MAX }}"
asg_min = "{{ ASG_MIN }}"
instance_type = "{{ INSTANCE_TYPE }}"
iam_role_name = "AutoscalingEc2InstanceRole"
deregistration_arn = "{{ DEREGISTRATION_ARN }}"
lifecycle_hook_arn = "{{ LIFECYCLE_HOOK_ARN }}"
}
'''

# TODO: They only differ in the final piece of the name, templatize better
LC_TEMPLATE = 'terraform import module.{}.aws_launch_configuration.qw-asg-launch-config {}'
ASG_TEMPLATE = 'terraform import module.{}.aws_autoscaling_group.qw-asg {}'
LC_TEMPLATE = 'terraform import module.{}.aws_launch_configuration.qw-asg-launch-config {}\n'
ASG_TEMPLATE = 'terraform import module.{}.aws_autoscaling_group.qw-asg {}\n'

DEREGISTRATION_ARN = 'arn:aws:sns:us-east-1:368154587575:qw_autoscale_events'
LIFECYCLE_HOOK_ARN = 'arn:aws:iam::368154587575:role/AutoScalingNotificationAccessRole'
Expand Down Expand Up @@ -89,16 +90,20 @@ def get_autoscaling_information(autoscaling_client, asg_name_prefix):
for response in asg_iterator:
for asg_response in response['AutoScalingGroups']:
if asg_response['AutoScalingGroupName'].startswith(asg_name_prefix):
lifecycle_hook_response = autoscaling_client.describe_lifecycle_hooks(
AutoScalingGroupName=asg_response['AutoScalingGroupName']
)
launch_config_response = autoscaling_client.describe_launch_configurations(
LaunchConfigurationNames=[
asg_response['LaunchConfigurationName']
]
)['LaunchConfigurations'][0]
asgs_to_process[asg_response['AutoScalingGroupName']] = {
'name' : asg_response['AutoScalingGroupName'],
'tags' : asg_response['Tags'],
'lc_name' : asg_response['LaunchConfigurationName'],
'asg_min' : asg_response['MinSize'],
'asg_max' : asg_response['MaxSize'],
'asg_desired' : asg_response['DesiredCapacity'],
'instance_type' : launch_config_response['InstanceType'],
'key_name' : launch_config_response['KeyName'],
}

lc_to_asg_name = {asg['lc_name'] : asg['name'] for asg in asgs_to_process.values()}
Expand Down Expand Up @@ -129,17 +134,21 @@ def generate_tf_for_asg(asg_info, template):
# TODO: Think about whether or not we can make this more reusable - right now it's heavily tied to the current qw module implementation
dns_cluster_name = get_dns_safe_cluster_name(asg_info)
cluster_name = get_cluster_name(asg_info)
asg_context = {'MODULE_NAME' : cluster_name,
'ASG_CLUSTER' : dns_cluster_name,
'CONSUMER_CONFIG' : asg_info['lc_info'],
'ASG_NAME' : asg_info['name'],
'QUEUE_NAME' : get_queue_from_info(asg_info),
'ASG_MIN' : asg_info['asg_min'],
'ASG_MAX' : asg_info['asg_max'],
'ASG_DESIRED' : asg_info['asg_desired'],
'DEREGISTRATION_ARN' : DEREGISTRATION_ARN,
'LIFECYCLE_HOOK_ARN' : LIFECYCLE_HOOK_ARN
}
asg_context = {
'MODULE_NAME' : cluster_name,
'ASG_CLUSTER' : dns_cluster_name,
'CONSUMER_CONFIG' : asg_info['lc_info'],
'ASG_NAME' : asg_info['name'],
'QUEUE_NAME' : get_queue_from_info(asg_info),
'ASG_MIN' : asg_info['asg_min'],
'ASG_MAX' : asg_info['asg_max'],
'ASG_DESIRED' : asg_info['asg_desired'],
'DEREGISTRATION_ARN' : DEREGISTRATION_ARN,
'LIFECYCLE_HOOK_ARN' : LIFECYCLE_HOOK_ARN,
'INSTANCE_TYPE' : asg_info['instance_type'],
'KEY_NAME' : asg_info['key_name'],
}

return template.render(**asg_context)

def get_queue_from_info(asg_info):
Expand All @@ -149,12 +158,12 @@ def get_queue_from_info(asg_info):

def import_statements_from_asg(asg_name, asg_info):
# asg_info is asg_name -> dictionary of collected infor
cluster_name = get_dns_safe_cluster_name(asg_info)
cluster_name = get_cluster_name(asg_info)
return [LC_TEMPLATE.format(cluster_name, asg_info['lc_name']),
ASG_TEMPLATE.format(cluster_name, asg_name)]

def get_dns_safe_cluster_name(asg_info):
return asg_info['name'].replace('/', '-').replace('.', '-')
return asg_info['name'].replace('/', '-').replace('.', '-').replace('_', '-')

def get_cluster_name(asg_info):
return asg_info['name'].replace('/', '_').replace('.', '_')
Expand Down