Skip to content

Commit

Permalink
Merge pull request #5 from DNXLabs/feature/rollback
Browse files Browse the repository at this point in the history
Add rollback feature
  • Loading branch information
caiovfernandes authored Jul 13, 2023
2 parents 375dfd2 + e2144eb commit 1cbb714
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 51 deletions.
8 changes: 3 additions & 5 deletions src/clone_blue_environment.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import json
import logging
import time
import os

# noqa: E502


def main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client):

beanstalkclient = boto_authenticated_client.client(
Expand Down Expand Up @@ -33,8 +31,6 @@ def main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET,
BEANSTALK_APP_NAME
)

# wait_green_be_ready(beanstalkclient, GREEN_ENV_NAME)

print("Green environment ID: " + green_env_id)
if green_env_id and did_new_env_was_created:
# Create a CNAME Config file
Expand Down Expand Up @@ -106,12 +102,14 @@ def wait_green_be_ready(beanstalkclient, GREEN_ENV_NAME):
green_env_info = get_env_info(beanstalkclient, GREEN_ENV_NAME)
while green_env_info["Environments"][0]["Status"] != "Ready":
print("Waiting the blue environment be Ready!")
time.sleep(10)
time.sleep(60)
green_env_info = get_env_info(beanstalkclient, GREEN_ENV_NAME)


def rollback_created_env(boto_authenticated_client, environment_name):
''' Terminate a beanstalk environment'''

time.sleep(10)
beanstalkclient = boto_authenticated_client.client(
'elasticbeanstalk', region_name=os.environ['AWS_DEFAULT_REGION'])

Expand Down
24 changes: 24 additions & 0 deletions src/deploy_release.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from time import strftime, sleep
from botocore.exceptions import ClientError
import os
import swap_environment

def main(BUCKET_KEY, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client):
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
Expand Down Expand Up @@ -89,3 +90,26 @@ def get_env_info(beanstalkclient, env_name):
env_name
])
return response

def rollback_release(client, application_name, environment_name):
try:
beanstalkclient = client.client('elasticbeanstalk')
except ClientError as err:
print("Failed to create boto3 beanstalk client.\n" + str(err))
return False
environment_info, client = swap_environment.get_environment_information(
beanstalkclient, environment_name)

while environment_info["Environments"][0]["Status"] != "Ready":
sleep(10)
environment_info, client = swap_environment.get_environment_information(
beanstalkclient, environment_name)

response = beanstalkclient.describe_application_versions(
ApplicationName=application_name,
MaxRecords=2
)
VERSION_LABEL = response['ApplicationVersions'][-1]['VersionLabel']

if not deploy_new_version(beanstalkclient, application_name, environment_name, VERSION_LABEL):
raise Exception("Failed to deploy new version.")
84 changes: 43 additions & 41 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def main():
if execution_type == "deploy" or execution_type == "full":
print("\n\n\n ------------------ Stating Deployment Step 1 --------------------- \n")
print("------------------ Creating Green Env --------------------- \n\n\n")

# Step 1: Cloning the blue env into green env.
try:
print("Clonning the blue environment...")
Expand All @@ -74,6 +75,7 @@ def main():
boto_authenticated_client = aws_authentication.get_boto_client()
print("\n\n\n ------------------ Stating Step 2 ---------------------\n")
print("------------------ Swapping Domains --------------------- \n\n\n")

# Step 2: Swapping blue and green envs URL's.
try:
print("Swapping environment Domains...")
Expand All @@ -89,7 +91,6 @@ def main():
boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
clone_blue_environment.rollback_created_env(
boto_authenticated_client, GREEN_ENV_NAME)

e = sys.exc_info()[0]
print(e)
traceback.print_exc()
Expand All @@ -98,7 +99,8 @@ def main():
boto_authenticated_client = aws_authentication.get_boto_client()
print("\n\n\n ------------------ Stating Step 3 --------------------- \n")
print("----------------- New release Deployment --------------------- \n\n\n")
# ## Step 3: Deploying the new release into the blue env.

## Step 3: Deploying the new release into the blue env.
try:
print("New release deployment initiated.")
start_3 = time.time()
Expand Down Expand Up @@ -148,46 +150,47 @@ def main():
print(e)
traceback.print_exc()
sys.exit(1)
# if execution_type == "rollback":
# try:
# print("Rolling back the blue environment to the previous version.")
# deploy_release.main(previous_release_key, previous_release_bucket,
# BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client)
#
# except Exception as err:
# print("Rolling back the blue environment has failed!")
# print(("Error: " + str(err)))
# e = sys.exc_info()[0]
# print(e)
# traceback.print_exc()
# sys.exit(1)
# try:
# print("Re-swapping the URL's and terminating the green environment.")
# swap_environment.re_swap_dns(
# boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
# except Exception as err:
# print(
# "Re-swapping the URL's and terminating the green environment has failed!")
# print(("Error: " + str(err)))
# e = sys.exc_info()[0]
# print(e)
# traceback.print_exc()
# sys.exit(1)
# try:
# print("Rolling back the blue environment.")
# clone_blue_environment.rollback_created_env(
# boto_authenticated_client, GREEN_ENV_NAME
# )
# except Exception as err:
# print("Rolling back the blue environment has failed!")
# print(("Error: " + str(err)))
# e = sys.exc_info()[0]
# print(e)
# traceback.print_exc()
# sys.exit(1)

# Start rollback phase
if execution_type == "rollback":
try:
print("Rolling back the blue environment to the previous version.")
deploy_release.rollback_release(boto_authenticated_client, BEANSTALK_APP_NAME, BLUE_ENV_NAME)
except Exception as err:
print("Rolling back the blue environment has failed!")
print(("Error: " + str(err)))
e = sys.exc_info()[0]
print(e)
traceback.print_exc()
sys.exit(1)

try:
print("Re-swapping the URL's and terminating the green environment.")
swap_environment.re_swap_dns(
boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
except Exception as err:
print(
"Re-swapping the URL's ...")
print(("Error: " + str(err)))
e = sys.exc_info()[0]
print(e)
traceback.print_exc()
sys.exit(1)

try:
print("Rolling back the blue environment.")
clone_blue_environment.rollback_created_env(
boto_authenticated_client, GREEN_ENV_NAME
)
except Exception as err:
print("Rolling back the blue environment has failed!")
print(("Error: " + str(err)))
e = sys.exc_info()[0]
print(e)
traceback.print_exc()
sys.exit(1)
print("Rollback has finished successfully!")
print("Deployment has finished successfully!")

if __name__ == "__main__":
try:
Expand Down Expand Up @@ -222,5 +225,4 @@ def main():
e = sys.exc_info()[0]
traceback.print_exc()
sys.exit(1)
main()

main()
16 changes: 11 additions & 5 deletions src/swap_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def get_ssm_parameter(client, parameter_name):
def re_swap_dns(boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME):
'''Re-swap beanstalk environments Domains applying the rollback'''

time.sleep(10)
beanstalkclient = boto_authenticated_client.client(
"elasticbeanstalk", region_name=os.environ['AWS_DEFAULT_REGION'])
s3client = boto_authenticated_client.client(
Expand All @@ -143,21 +144,26 @@ def re_swap_dns(boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME,
BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client
)

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

blue_env_info, client = get_environment_information(
beanstalkclient, BLUE_ENV_NAME)

if blue_env_url != green_env_cname:
print("Nothing to re-swap")
return "Nothing to Swap!"

else:
while green_env_info["Environments"][0]["Status"] != "Ready":
while green_env_info["Environments"][0]["Status"] != "Ready" and blue_env_info["Environments"][0]["Status"] != "Ready" :
time.sleep(10)
green_env_info = get_environment_information(
green_env_info, client = get_environment_information(
beanstalkclient, GREEN_ENV_NAME)
blue_env_info, client = get_environment_information(
beanstalkclient, BLUE_ENV_NAME)
swap_response = swap_urls(
beanstalkclient, GREEN_ENV_NAME, BLUE_ENV_NAME)
if swap_response == "Successful":
return "Ok"
else:
raise Exception("Failed to re-swap environments!")
raise Exception("Failed to re-swap environments!")

0 comments on commit 1cbb714

Please sign in to comment.