From b9a028d450c59a605326d04241d1cae8e230225c Mon Sep 17 00:00:00 2001 From: korikuzma Date: Mon, 29 Mar 2021 14:59:17 -0400 Subject: [PATCH] Add codebuild files --- codebuild/deploy_eb_env.py | 52 +++++++++++++++++++++++++++++++++++ codebuild/terminate_eb_env.py | 30 ++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 codebuild/deploy_eb_env.py create mode 100644 codebuild/terminate_eb_env.py diff --git a/codebuild/deploy_eb_env.py b/codebuild/deploy_eb_env.py new file mode 100644 index 00000000..b5c81d1c --- /dev/null +++ b/codebuild/deploy_eb_env.py @@ -0,0 +1,52 @@ +"""Module for deploying staging EB environment.""" +import boto3 +import time + +elasticbeanstalk = boto3.client('elasticbeanstalk') +servicecatalog = boto3.client('servicecatalog') +terminate_time = 12 +eb_app_name = "GeneNormalization" +eb_env_name = "GeneNormalization-staging-env" +sc_product_id = "prod-mmw6ymv2ntzl2" +print(f'Launching new Service Catalog Product for staging environment:' + f' {eb_app_name}') +sc_product_artifacts = \ + servicecatalog.list_provisioning_artifacts(ProductId=sc_product_id) +for artifact in sc_product_artifacts['ProvisioningArtifactDetails']: + if artifact['Active']: + provisioning_artifact_id = artifact['Id'] +try: + eb_provisioned_product = servicecatalog.provision_product( + ProductId=sc_product_id, + ProvisioningArtifactId=provisioning_artifact_id, + ProvisionedProductName=eb_env_name, + ProvisioningParameters=[ + { + 'Key': 'Env', + 'Value': eb_app_name + }, + { + 'Key': 'EnvType', + 'Value': 'staging' + }, + { + 'Key': 'TerminateTime', + 'Value': str(terminate_time) + } + ] + ) + eb_provisioned_product_Id = \ + eb_provisioned_product['RecordDetail']['ProvisionedProductId'] + product_status = servicecatalog.describe_provisioned_product( + Id=eb_provisioned_product_Id) + eb_provisioned_product_status = \ + product_status['ProvisionedProductDetail']['Status'] + while eb_provisioned_product_status == "UNDER_CHANGE": + time.sleep(10) + product_status = servicecatalog.describe_provisioned_product( + Id=eb_provisioned_product_Id) + eb_provisioned_product_status = \ + product_status['ProvisionedProductDetail']['Status'] + print(eb_provisioned_product_status) +except: # noqa: E722 + print("The EB environment is already running...") diff --git a/codebuild/terminate_eb_env.py b/codebuild/terminate_eb_env.py new file mode 100644 index 00000000..f65d5dd3 --- /dev/null +++ b/codebuild/terminate_eb_env.py @@ -0,0 +1,30 @@ +"""Module for terminating staging EB environment.""" +import boto3 +import json +import time + +client = boto3.client('lambda') +servicecatalog = boto3.client('servicecatalog') +eb_env_name = "GeneNormalization-staging-env" +data = {"sc_provisioned_name": eb_env_name} +client.invoke(FunctionName='igm-inf-terminate-provisioned-product', + Payload=json.dumps(data)) +time.sleep(10) +provisioned_product = \ + servicecatalog.describe_provisioned_product(Name=eb_env_name) +eb_provisioned_product_Id =\ + provisioned_product['ProvisionedProductDetail']['Id'] +product_status = servicecatalog.describe_provisioned_product( + Id=eb_provisioned_product_Id) +eb_provisioned_product_status =\ + product_status['ProvisionedProductDetail']['Status'] +while eb_provisioned_product_status == "UNDER_CHANGE": + time.sleep(10) + try: + product_status = servicecatalog.describe_provisioned_product( + Id=eb_provisioned_product_Id) + eb_provisioned_product_status = \ + product_status['ProvisionedProductDetail']['Status'] + except: # noqa: E722 + eb_provisioned_product_status = "PRODUCT NOT FOUND" + print(eb_provisioned_product_status)