Skip to content

Commit 1cbb714

Browse files
Merge pull request #5 from DNXLabs/feature/rollback
Add rollback feature
2 parents 375dfd2 + e2144eb commit 1cbb714

File tree

4 files changed

+81
-51
lines changed

4 files changed

+81
-51
lines changed

src/clone_blue_environment.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import json
2-
import logging
32
import time
43
import os
54

65
# noqa: E502
76

8-
97
def main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client):
108

119
beanstalkclient = boto_authenticated_client.client(
@@ -33,8 +31,6 @@ def main(BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET,
3331
BEANSTALK_APP_NAME
3432
)
3533

36-
# wait_green_be_ready(beanstalkclient, GREEN_ENV_NAME)
37-
3834
print("Green environment ID: " + green_env_id)
3935
if green_env_id and did_new_env_was_created:
4036
# Create a CNAME Config file
@@ -106,12 +102,14 @@ def wait_green_be_ready(beanstalkclient, GREEN_ENV_NAME):
106102
green_env_info = get_env_info(beanstalkclient, GREEN_ENV_NAME)
107103
while green_env_info["Environments"][0]["Status"] != "Ready":
108104
print("Waiting the blue environment be Ready!")
109-
time.sleep(10)
105+
time.sleep(60)
110106
green_env_info = get_env_info(beanstalkclient, GREEN_ENV_NAME)
111107

112108

113109
def rollback_created_env(boto_authenticated_client, environment_name):
114110
''' Terminate a beanstalk environment'''
111+
112+
time.sleep(10)
115113
beanstalkclient = boto_authenticated_client.client(
116114
'elasticbeanstalk', region_name=os.environ['AWS_DEFAULT_REGION'])
117115

src/deploy_release.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from time import strftime, sleep
22
from botocore.exceptions import ClientError
33
import os
4+
import swap_environment
45

56
def main(BUCKET_KEY, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client):
67
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
@@ -89,3 +90,26 @@ def get_env_info(beanstalkclient, env_name):
8990
env_name
9091
])
9192
return response
93+
94+
def rollback_release(client, application_name, environment_name):
95+
try:
96+
beanstalkclient = client.client('elasticbeanstalk')
97+
except ClientError as err:
98+
print("Failed to create boto3 beanstalk client.\n" + str(err))
99+
return False
100+
environment_info, client = swap_environment.get_environment_information(
101+
beanstalkclient, environment_name)
102+
103+
while environment_info["Environments"][0]["Status"] != "Ready":
104+
sleep(10)
105+
environment_info, client = swap_environment.get_environment_information(
106+
beanstalkclient, environment_name)
107+
108+
response = beanstalkclient.describe_application_versions(
109+
ApplicationName=application_name,
110+
MaxRecords=2
111+
)
112+
VERSION_LABEL = response['ApplicationVersions'][-1]['VersionLabel']
113+
114+
if not deploy_new_version(beanstalkclient, application_name, environment_name, VERSION_LABEL):
115+
raise Exception("Failed to deploy new version.")

src/main.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def main():
5252
if execution_type == "deploy" or execution_type == "full":
5353
print("\n\n\n ------------------ Stating Deployment Step 1 --------------------- \n")
5454
print("------------------ Creating Green Env --------------------- \n\n\n")
55+
5556
# Step 1: Cloning the blue env into green env.
5657
try:
5758
print("Clonning the blue environment...")
@@ -74,6 +75,7 @@ def main():
7475
boto_authenticated_client = aws_authentication.get_boto_client()
7576
print("\n\n\n ------------------ Stating Step 2 ---------------------\n")
7677
print("------------------ Swapping Domains --------------------- \n\n\n")
78+
7779
# Step 2: Swapping blue and green envs URL's.
7880
try:
7981
print("Swapping environment Domains...")
@@ -89,7 +91,6 @@ def main():
8991
boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
9092
clone_blue_environment.rollback_created_env(
9193
boto_authenticated_client, GREEN_ENV_NAME)
92-
9394
e = sys.exc_info()[0]
9495
print(e)
9596
traceback.print_exc()
@@ -98,7 +99,8 @@ def main():
9899
boto_authenticated_client = aws_authentication.get_boto_client()
99100
print("\n\n\n ------------------ Stating Step 3 --------------------- \n")
100101
print("----------------- New release Deployment --------------------- \n\n\n")
101-
# ## Step 3: Deploying the new release into the blue env.
102+
103+
## Step 3: Deploying the new release into the blue env.
102104
try:
103105
print("New release deployment initiated.")
104106
start_3 = time.time()
@@ -148,46 +150,47 @@ def main():
148150
print(e)
149151
traceback.print_exc()
150152
sys.exit(1)
151-
# if execution_type == "rollback":
152-
# try:
153-
# print("Rolling back the blue environment to the previous version.")
154-
# deploy_release.main(previous_release_key, previous_release_bucket,
155-
# BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client)
156-
#
157-
# except Exception as err:
158-
# print("Rolling back the blue environment has failed!")
159-
# print(("Error: " + str(err)))
160-
# e = sys.exc_info()[0]
161-
# print(e)
162-
# traceback.print_exc()
163-
# sys.exit(1)
164-
# try:
165-
# print("Re-swapping the URL's and terminating the green environment.")
166-
# swap_environment.re_swap_dns(
167-
# boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
168-
# except Exception as err:
169-
# print(
170-
# "Re-swapping the URL's and terminating the green environment has failed!")
171-
# print(("Error: " + str(err)))
172-
# e = sys.exc_info()[0]
173-
# print(e)
174-
# traceback.print_exc()
175-
# sys.exit(1)
176-
# try:
177-
# print("Rolling back the blue environment.")
178-
# clone_blue_environment.rollback_created_env(
179-
# boto_authenticated_client, GREEN_ENV_NAME
180-
# )
181-
# except Exception as err:
182-
# print("Rolling back the blue environment has failed!")
183-
# print(("Error: " + str(err)))
184-
# e = sys.exc_info()[0]
185-
# print(e)
186-
# traceback.print_exc()
187-
# sys.exit(1)
188153

154+
# Start rollback phase
155+
if execution_type == "rollback":
156+
try:
157+
print("Rolling back the blue environment to the previous version.")
158+
deploy_release.rollback_release(boto_authenticated_client, BEANSTALK_APP_NAME, BLUE_ENV_NAME)
159+
except Exception as err:
160+
print("Rolling back the blue environment has failed!")
161+
print(("Error: " + str(err)))
162+
e = sys.exc_info()[0]
163+
print(e)
164+
traceback.print_exc()
165+
sys.exit(1)
189166

167+
try:
168+
print("Re-swapping the URL's and terminating the green environment.")
169+
swap_environment.re_swap_dns(
170+
boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME)
171+
except Exception as err:
172+
print(
173+
"Re-swapping the URL's ...")
174+
print(("Error: " + str(err)))
175+
e = sys.exc_info()[0]
176+
print(e)
177+
traceback.print_exc()
178+
sys.exit(1)
190179

180+
try:
181+
print("Rolling back the blue environment.")
182+
clone_blue_environment.rollback_created_env(
183+
boto_authenticated_client, GREEN_ENV_NAME
184+
)
185+
except Exception as err:
186+
print("Rolling back the blue environment has failed!")
187+
print(("Error: " + str(err)))
188+
e = sys.exc_info()[0]
189+
print(e)
190+
traceback.print_exc()
191+
sys.exit(1)
192+
print("Rollback has finished successfully!")
193+
print("Deployment has finished successfully!")
191194

192195
if __name__ == "__main__":
193196
try:
@@ -222,5 +225,4 @@ def main():
222225
e = sys.exc_info()[0]
223226
traceback.print_exc()
224227
sys.exit(1)
225-
main()
226-
228+
main()

src/swap_environment.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def get_ssm_parameter(client, parameter_name):
134134
def re_swap_dns(boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME, BLUE_ENV_NAME):
135135
'''Re-swap beanstalk environments Domains applying the rollback'''
136136

137+
time.sleep(10)
137138
beanstalkclient = boto_authenticated_client.client(
138139
"elasticbeanstalk", region_name=os.environ['AWS_DEFAULT_REGION'])
139140
s3client = boto_authenticated_client.client(
@@ -143,21 +144,26 @@ def re_swap_dns(boto_authenticated_client, S3_ARTIFACTS_BUCKET, GREEN_ENV_NAME,
143144
BLUE_CNAME_CONFIG_FILE, S3_ARTIFACTS_BUCKET, s3client
144145
)
145146

146-
green_env_info, beanstalkclient = get_environment_information(
147+
green_env_info, client = get_environment_information(
147148
beanstalkclient, GREEN_ENV_NAME)
148149
green_env_cname = green_env_info["Environments"][0]["CNAME"]
149150

151+
blue_env_info, client = get_environment_information(
152+
beanstalkclient, BLUE_ENV_NAME)
153+
150154
if blue_env_url != green_env_cname:
155+
print("Nothing to re-swap")
151156
return "Nothing to Swap!"
152-
153157
else:
154-
while green_env_info["Environments"][0]["Status"] != "Ready":
158+
while green_env_info["Environments"][0]["Status"] != "Ready" and blue_env_info["Environments"][0]["Status"] != "Ready" :
155159
time.sleep(10)
156-
green_env_info = get_environment_information(
160+
green_env_info, client = get_environment_information(
157161
beanstalkclient, GREEN_ENV_NAME)
162+
blue_env_info, client = get_environment_information(
163+
beanstalkclient, BLUE_ENV_NAME)
158164
swap_response = swap_urls(
159165
beanstalkclient, GREEN_ENV_NAME, BLUE_ENV_NAME)
160166
if swap_response == "Successful":
161167
return "Ok"
162168
else:
163-
raise Exception("Failed to re-swap environments!")
169+
raise Exception("Failed to re-swap environments!")

0 commit comments

Comments
 (0)