Skip to content

Commit

Permalink
📦 NEW: Get applications list per beanstalk environment
Browse files Browse the repository at this point in the history
  • Loading branch information
caiovfernandes committed Apr 4, 2023
1 parent b0175f5 commit b858674
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python && pyt
COPY blue_green_assets .
COPY testing_env .

ENTRYPOINT [ "python", "/work/main.py" ]
ENTRYPOINT [ "python", "/work/main.py" ]
#ENTRYPOINT [ "python", "/work/test.py" ]
8 changes: 1 addition & 7 deletions blue_green_assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def main():
BEANSTALK_APP_NAME = os.getenv("BEANSTALK_APP_NAME")
S3_ARTIFACTS_BUCKET = os.getenv("S3_ARTIFACTS_BUCKET")
S3_ARTIFACTS_OBJECT = os.getenv("S3_ARTIFACTS_OBJECT")
HOSTED_ZONE_ID = os.getenv("HOSTED_ZONE_ID")
RECORDS_LIST = os.getenv("RECORDS_LIST")

boto_authenticated_client = aws_authentication.get_boto_client()

Expand All @@ -41,7 +39,7 @@ def main():
## Step 1: Cloning the blue env into green env.
try:
print(colored("Clonning the blue environment", "blue"))
clone_blue_environment.main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client)
clone_blue_environment.main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME, boto_authenticated_client)
except Exception as err:
print(colored("Clonning the blue environment environment has failed!", "red"))
print(colored( ("Error: " + str(err)), "red"))
Expand Down Expand Up @@ -127,10 +125,6 @@ def main():
raise Exception("The environment variable S3_ARTIFACTS_BUCKET wasn't exposed to the container")
if "S3_ARTIFACTS_OBJECT" not in os.environ:
raise Exception("The environment variable S3_ARTIFACTS_OBJECT wasn't exposed to the container")
if "HOSTED_ZONE_ID" not in os.environ:
raise Exception("The environment variable HOSTED_ZONE_ID wasn't exposed to the container")
if "RECORDS_LIST" not in os.environ:
raise Exception("The environment variable RECORDS_LIST wasn't exposed to the container")
print(colored("Successfully validated environment variables", "green"))
except Exception as e:
print("Failed to get environment variable")
Expand Down
26 changes: 20 additions & 6 deletions blue_green_assets/swap_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@



def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client, HOSTED_ZONE_ID, RECORDS_LIST):
def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME, boto_authenticated_client):
beanstalkclient = boto_authenticated_client.client("elasticbeanstalk", region_name="ap-southeast-2")
s3client = boto_authenticated_client.client('s3', region_name='ap-southeast-2')
route53_client = boto_authenticated_client.client('route53', region_name='ap-southeast-2')
ssm_client = boto_authenticated_client.client('ssm', region_name='ap-southeast-2')

BLUE_CNAME_CONFIG_FILE = "blue_green_assets/blue_cname.json"

Expand All @@ -22,8 +23,14 @@ def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_
green_env_info = get_environment_information(beanstalkclient, GREEN_ENV_NAME)
green_env_cname = green_env_info["Environments"][0]["CNAME"]

applications_response = get_ssm_parameter(ssm_client, BEANSTALK_APP_NAME)
applications_list = applications_response['Parameter']['Value']

create_route53_record(route53_client, HOSTED_ZONE_ID, RECORDS_LIST, green_env_cname)
hosted_zone_id_ssm_response = get_ssm_parameter(ssm_client, "HostedZoneId")
hosted_zone_id = hosted_zone_id_ssm_response['Parameter']['Value']


create_route53_records(route53_client, applications_list, green_env_cname, hosted_zone_id)



Expand Down Expand Up @@ -79,10 +86,10 @@ def swap_urls(beanstalkclient, SourceEnv, DestEnv):
else:
return ("Failure")

def create_route53_record(route53_client, HOSTED_ZONE_ID, RECORDS_LIST, green_env_url):
for record in eval(RECORDS_LIST):
def create_route53_records(route53_client, applications_list, green_env_url, hosted_zone_id):
for record in eval(applications_list):
route53_client.change_resource_record_sets(
HostedZoneId=HOSTED_ZONE_ID,
HostedZoneId=hosted_zone_id,
ChangeBatch={
"Changes": [
{
Expand All @@ -102,4 +109,11 @@ def create_route53_record(route53_client, HOSTED_ZONE_ID, RECORDS_LIST, green_en
},
]
}
)
)

def get_ssm_parameter(client, parameter_name):
response = client.get_parameter(
Name=parameter_name
)

return response
35 changes: 33 additions & 2 deletions testing_env/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,38 @@ def get_boto_client():
return boto3


def get_beanstalk_environtment(beanstalk_client, application_name, template_name, environment_name):
response = beanstalk_client.describe_configuration_settings(
ApplicationName=application_name,
EnvironmentName=environment_name
)

test = response["ConfigurationSettings"][0]["OptionSettings"][36]

print(response)
print()
print(test)


def get_ssm_parameter(client, parameter_name):
response = client.get_parameter(
Name=parameter_name
)
print(response)
print(eval(response['Parameter']['Value']))
print(eval(response['Parameter']['Value'])[0])
print(eval(response['Parameter']['Value'])[1])
print(len(eval(response['Parameter']['Value'])))
return response




if __name__ == '__main__':
client = get_boto_client()
route53_client = client.client('route53', region_name='ap-southeast-2')
create_route53_record(route53_client)
# route53_client = client.client('route53', region_name='ap-southeast-2')
# beanstalk_client = client.client('elasticbeanstalk', region_name='ap-southeast-2')
ssm_client = client.client('ssm', region_name='ap-southeast-2')
# create_route53_record(route53_client)
# get_beanstalk_environtment(beanstalk_client, "MydealWeb", "template_name", "MyDealWeb-prod-green")
get_ssm_parameter(ssm_client, "MyDealWeb")

0 comments on commit b858674

Please sign in to comment.