Skip to content

Commit

Permalink
📦 NEW: Add record53 creation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
caiovfernandes committed Mar 30, 2023
1 parent 3210bd9 commit b0175f5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ BEANSTALK_APP_NAME
S3_ARTIFACTS_BUCKET
S3_ARTIFACTS_OBJECT
RELEASE_HEALTH_CHECKING_PATH
SSO_PROFILE
AUTH_METHOD
HOSTED_ZONE_ID
RECORDS_LIST
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ FROM dnxsolutions/aws-v2:2.4.29-dnx1

WORKDIR /work

COPY blue_green_assets .

COPY blue_green_assets/requirements.txt .

ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python && python3 -m ensurepip && pip3 install --no-cache-dir --upgrade -r ./requirements.txt


COPY blue_green_assets .
COPY testing_env .

ENTRYPOINT [ "python", "/work/main.py" ]
8 changes: 7 additions & 1 deletion blue_green_assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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 Down Expand Up @@ -51,7 +53,7 @@ def main():
## Step 2: Swapping blue and green envs URL's.
try:
print(colored("Swapping environment URL's", "blue"))
swap_environment.main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client)
swap_environment.main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client, HOSTED_ZONE_ID, RECORDS_LIST)
print(colored("URL's swap task finished succesfully", "green"))
except Exception as err:
print(colored("Swap environment has failed.", "red"))
Expand Down Expand Up @@ -125,6 +127,10 @@ 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
42 changes: 36 additions & 6 deletions blue_green_assets/swap_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@



def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, 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')
def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client, HOSTED_ZONE_ID, RECORDS_LIST):
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')

BLUE_CNAME_CONFIG_FILE = "blue_green_assets/blue_cname.json"

blue_env_url = get_blue_env_address(BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client)
blue_env_url = get_env_address(BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client)
print("Blue env URL: " + str(blue_env_url))


green_env_info = get_environment_information(beanstalkclient, GREEN_ENV_NAME)
green_env_cname = green_env_info["Environments"][0]["CNAME"]


create_route53_record(route53_client, HOSTED_ZONE_ID, RECORDS_LIST, green_env_cname)



print("Green env CNAME: " + str(green_env_cname))

if blue_env_url == green_env_cname:
Expand All @@ -34,11 +40,10 @@ def main(BLUE_ENV_NAME, GREEN_ENV_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_
return "Ok"
else:
raise Exception("Failed to swap environments!")




def get_blue_env_address(BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client):
def get_env_address(BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client):
# Opening JSON file
file_name = BLUE_CNAME_CONFIG_FILE
data = json.loads(s3client.get_object(Bucket=S3_ARTIFACTS_BUCKET, Key=file_name)['Body'].read())
Expand Down Expand Up @@ -73,3 +78,28 @@ def swap_urls(beanstalkclient, SourceEnv, DestEnv):
return ("Successful")
else:
return ("Failure")

def create_route53_record(route53_client, HOSTED_ZONE_ID, RECORDS_LIST, green_env_url):
for record in eval(RECORDS_LIST):
route53_client.change_resource_record_sets(
HostedZoneId=HOSTED_ZONE_ID,
ChangeBatch={
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Type": "CNAME",
"Name": record,
"Region": "ap-southeast-2",
"SetIdentifier": f"{record} Identifier",
"TTL": 60,
"ResourceRecords": [
{
"Value": green_env_url,
},
],
}
},
]
}
)
49 changes: 49 additions & 0 deletions testing_env/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import boto3
import os



def create_route53_record(route53_client):
response = route53_client.get_hosted_zone(
Id='Z01998683H70F4Z45TM26',
)
print(response)
zone_id = response['HostedZone']['Id']
response = route53_client.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch={
"Comment": "Automatic DNS update",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Type": "CNAME",
"Name": "route53test.prod2.mydeal.com.au",
"Region": "ap-southeast-2",
"SetIdentifier": "Testing route53",
"TTL": 60,
"ResourceRecords": [
{
"Value": "www.google.com",
},
],
}
},
]
}
)

def get_boto_client():
print("AUTH_METHOD: " + str(os.getenv('AUTH_METHOD')))
if os.getenv('AUTH_METHOD') == 'SSO':
boto3.setup_default_session(profile_name=os.getenv('SSO_PROFILE'))
print("SSO Authentication")
else:
print("AWS KEYS authentication")
return boto3


if __name__ == '__main__':
client = get_boto_client()
route53_client = client.client('route53', region_name='ap-southeast-2')
create_route53_record(route53_client)

0 comments on commit b0175f5

Please sign in to comment.