def deploy(event, context): print(json.dumps(event, indent=4, sort_keys=True, default=lambda x: str(x))) deployable_bucket_name = event["Records"][0]["s3"]["bucket"]["name"] deployable_key_name = event["Records"][0]["s3"]["object"]["key"] app_metadata = util.get_app_metadata( deployable_bucket_name, deployable_key_name ) rds_version = app_metadata['Metadata']['db-version'] db_name = app_metadata['Metadata']['db-name'] db_username = app_metadata['Metadata']['db-username'] db_password = app_metadata['Metadata']['db-password'] all_db_instances = rds_client.describe_db_instances() found_db = False if len(all_db_instances['DBInstances']) > 0: for each_db_instance in all_db_instances['DBInstances']: all_tags = each_db_instance['Tags'] if "Tags" in each_db_instance else {} if "tvarit_version" in all_tags and all_tags['tvarit_version'] == rds_version: found_db = each_db_instance["Endpoint"] else: found_db = False sns_resources = util.make_base_output_map_from_cfn("SnsTopics") deploy_complete_topic = sns_resources["RdsDeployedSnsTopicOutput"] rds_already_deployed_sqs_q = sns_resources["RdsAlreadyDeployedSqsTopicOutput"] if not found_db: print("db not found!") rds_template = util.make_cfn_url("app/rds.template") group_id = app_metadata['Metadata']["group-id"] artifact_id = app_metadata['Metadata']["artifact-id"] version = app_metadata['Metadata']["version"] print("db to be created: group, artifact, version: " + group_id + " : " + artifact_id + " : " + version) network_resources = util.make_base_output_map_from_cfn("Network") print(json.dumps(network_resources, indent=4, sort_keys=True, default=lambda x: str(x))) db_subnet_group = network_resources["DbSubnetGroupOutput"] db_security_group = network_resources["DbSecurityGroupOutput"] rds_stack_parameters = [ {"ParameterKey": "DbSubnetGroupNameParam", "ParameterValue": db_subnet_group}, {"ParameterKey": "DbVersionParam", "ParameterValue": rds_version}, {"ParameterKey": "DbNameParam", "ParameterValue": db_name}, {"ParameterKey": "DbUsernameParam", "ParameterValue": db_username}, {"ParameterKey": "DbPasswordParam", "ParameterValue": db_password}, {"ParameterKey": "DbSecurityGroupParam", "ParameterValue": db_security_group} ] print("printing rds_stack_parameters") print(json.dumps(rds_stack_parameters, indent=4, sort_keys=True, default=lambda x: str(x))) stack_name = ("rds" + group_id + "-" + artifact_id + "-" + version).replace(".", "-") cfn_client.create_stack( StackName=stack_name, TemplateURL=rds_template, Parameters=rds_stack_parameters, NotificationARNs=[deploy_complete_topic], Tags=[{"Key": "app_file_object", "Value": deployable_bucket_name + "::" + deployable_key_name}] ) else: print("found the version tag and it was same as rds version " + rds_version) sqs_client.send_message(QueueUrl=rds_already_deployed_sqs_q, MessageBody=json.dumps(found_db))
def do_deploy(rds_stack_name): rds_stack = cfn_client.describe_stacks(StackName=rds_stack_name) tags_on_rds_stack = rds_stack['Stacks'][0]['Tags'] map_of_tags_on_rds_stack = util.make_map_from_list("Key", "Value", tags_on_rds_stack) app_file_object_parm = map_of_tags_on_rds_stack['app_file_object'] bucket_name = app_file_object_parm.split("::")[0] key_of_deployable = app_file_object_parm.split("::")[1] # bucket_name = "tvarit-tvarit-tomcat-plugin-test" # key_of_deployable = "deployables/tvarit/tomcat-plugin-test/1.0.1-SNAPSHOT/tomcat-plugin-test-1.0.1-SNAPSHOT.war" # util.get_app_metadata(bucket_name) # bucket_name = event["Records"][0]["s3"]["bucket"]["name"] # key_of_deployable = event["Records"][0]["s3"]["object"]["key"] all_metadata = util.get_app_metadata(bucket_name, key_of_deployable) war_info_and_metadata = { "metadata": all_metadata['Metadata'], "bucket_name": bucket_name, "key": key_of_deployable } # ensure_router_auto_scaling_group_has_instances() print("not starting router") deployable_name = key_of_deployable.split("/")[1] deployable_version = all_metadata['Metadata']['version'] tags = asg_client.describe_tags( Filters=[{ "Name": "key", "Values": ["tvarit:app:version", "tvarit:app:name"] }, { "Name": "value", "Values": [deployable_version, deployable_name] }]) if len(tags['Tags']) == 0: print("no asg found for " + key_of_deployable + " " + deployable_version) create_app_auto_scaling_group(war_info_and_metadata, rds_stack_name) modify_router_rules() else: # TODO '''
def do_deploy(rds_stack_name): rds_stack = cfn_client.describe_stacks(StackName=rds_stack_name) tags_on_rds_stack = rds_stack['Stacks'][0]['Tags'] map_of_tags_on_rds_stack = util.make_map_from_list("Key", "Value", tags_on_rds_stack) app_file_object_parm = map_of_tags_on_rds_stack['app_file_object'] bucket_name = app_file_object_parm.split("::")[0] key_of_deployable = app_file_object_parm.split("::")[1] # bucket_name = "tvarit-tvarit-tomcat-plugin-test" # key_of_deployable = "deployables/tvarit/tomcat-plugin-test/1.0.1-SNAPSHOT/tomcat-plugin-test-1.0.1-SNAPSHOT.war" # util.get_app_metadata(bucket_name) # bucket_name = event["Records"][0]["s3"]["bucket"]["name"] # key_of_deployable = event["Records"][0]["s3"]["object"]["key"] all_metadata = util.get_app_metadata(bucket_name, key_of_deployable) war_info_and_metadata = {"metadata": all_metadata['Metadata'], "bucket_name": bucket_name, "key": key_of_deployable} # ensure_router_auto_scaling_group_has_instances() print("not starting router") deployable_name = key_of_deployable.split("/")[1] deployable_version = all_metadata['Metadata']['version'] tags = asg_client.describe_tags(Filters=[ { "Name": "key", "Values": ["tvarit:app:version", "tvarit:app:name"] }, { "Name": "value", "Values": [deployable_version, deployable_name] } ]) if len(tags['Tags']) == 0: print("no asg found for " + key_of_deployable + " " + deployable_version) create_app_auto_scaling_group(war_info_and_metadata, rds_stack_name) modify_router_rules() else: # TODO '''
def deploy(event, context): print(json.dumps(event, indent=4, sort_keys=True, default=lambda x: str(x))) deployable_bucket_name = event["Records"][0]["s3"]["bucket"]["name"] deployable_key_name = event["Records"][0]["s3"]["object"]["key"] app_metadata = util.get_app_metadata(deployable_bucket_name, deployable_key_name) rds_version = app_metadata['Metadata']['db-version'] db_name = app_metadata['Metadata']['db-name'] db_username = app_metadata['Metadata']['db-username'] db_password = app_metadata['Metadata']['db-password'] all_db_instances = rds_client.describe_db_instances() found_db = False if len(all_db_instances['DBInstances']) > 0: for each_db_instance in all_db_instances['DBInstances']: all_tags = each_db_instance[ 'Tags'] if "Tags" in each_db_instance else {} if "tvarit_version" in all_tags and all_tags[ 'tvarit_version'] == rds_version: found_db = each_db_instance["Endpoint"] else: found_db = False sns_resources = util.make_base_output_map_from_cfn("SnsTopics") deploy_complete_topic = sns_resources["RdsDeployedSnsTopicOutput"] rds_already_deployed_sqs_q = sns_resources[ "RdsAlreadyDeployedSqsTopicOutput"] if not found_db: print("db not found!") rds_template = util.make_cfn_url("app/rds.template") group_id = app_metadata['Metadata']["group-id"] artifact_id = app_metadata['Metadata']["artifact-id"] version = app_metadata['Metadata']["version"] print("db to be created: group, artifact, version: " + group_id + " : " + artifact_id + " : " + version) network_resources = util.make_base_output_map_from_cfn("Network") print( json.dumps(network_resources, indent=4, sort_keys=True, default=lambda x: str(x))) db_subnet_group = network_resources["DbSubnetGroupOutput"] db_security_group = network_resources["DbSecurityGroupOutput"] rds_stack_parameters = [{ "ParameterKey": "DbSubnetGroupNameParam", "ParameterValue": db_subnet_group }, { "ParameterKey": "DbVersionParam", "ParameterValue": rds_version }, { "ParameterKey": "DbNameParam", "ParameterValue": db_name }, { "ParameterKey": "DbUsernameParam", "ParameterValue": db_username }, { "ParameterKey": "DbPasswordParam", "ParameterValue": db_password }, { "ParameterKey": "DbSecurityGroupParam", "ParameterValue": db_security_group }] print("printing rds_stack_parameters") print( json.dumps(rds_stack_parameters, indent=4, sort_keys=True, default=lambda x: str(x))) stack_name = ("rds" + group_id + "-" + artifact_id + "-" + version).replace(".", "-") cfn_client.create_stack(StackName=stack_name, TemplateURL=rds_template, Parameters=rds_stack_parameters, NotificationARNs=[deploy_complete_topic], Tags=[{ "Key": "app_file_object", "Value": deployable_bucket_name + "::" + deployable_key_name }]) else: print("found the version tag and it was same as rds version " + rds_version) sqs_client.send_message(QueueUrl=rds_already_deployed_sqs_q, MessageBody=json.dumps(found_db))