Skip to content

Commit 82e3c9b

Browse files
Merge pull request #6 from DNXLabs/feature/redeploy
Add redeploy feature
2 parents 1cbb714 + 41d01a9 commit 82e3c9b

File tree

6 files changed

+709
-18
lines changed

6 files changed

+709
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Once deployment to the blue environment is successful, and code review and code
2020
The blue environment starts serving the live traffic again, and the container terminates the temporarily created green environment. Not having to continuously run parallel environments, saving costs.
2121

2222
### Execution Diagram of `docker-beanstalk-bluegreen`
23-
![](_docs/assets/BlueGreen.png)
23+
![](_docs/assets/BlueGreen-v2.png)
2424

2525
All the steps are done using python:
2626
1. The first stage clones the blue environment, which results in a green environment.
@@ -55,6 +55,7 @@ S3_ARTIFACTS_OBJECT
5555
RELEASE_HEALTH_CHECKING_PATH
5656
AUTH_METHOD
5757
SSO_PROFILE
58+
VERSION_LABEL
5859
```
5960

6061

_docs/assets/.$BlueGreen.drawio.bkp

Lines changed: 337 additions & 0 deletions
Large diffs are not rendered by default.

_docs/assets/BlueGreen-v2.png

2.3 MB
Loading

_docs/assets/BlueGreen.drawio

Lines changed: 337 additions & 1 deletion
Large diffs are not rendered by default.

src/deploy_release.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import swap_environment
55

6-
def main(BUCKET_KEY, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client):
6+
def release_deployment(BUCKET_KEY, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client, create_app_version = True):
77
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
88

99
if "VERSION_LABEL" in os.environ:
@@ -15,11 +15,13 @@ def main(BUCKET_KEY, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME, BEANSTALK_APP_NAME, bot
1515
print("Failed to create boto3 beanstalk client.\n" + str(err))
1616
return False
1717

18-
if not create_new_version(beanstalkclient, VERSION_LABEL, BUCKET_KEY, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME):
19-
raise Exception("Failed to create beanstalk release.")
18+
if create_app_version:
19+
if not create_new_version(beanstalkclient, VERSION_LABEL, BUCKET_KEY, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME):
20+
raise Exception("Failed to create beanstalk release.")
2021

21-
wait_until_env_be_ready(beanstalkclient, BLUE_ENV_NAME)
2222
# Wait for the new version to be consistent before deploying
23+
wait_until_env_be_ready(beanstalkclient, BLUE_ENV_NAME)
24+
2325
if not deploy_new_version(beanstalkclient, BEANSTALK_APP_NAME, BLUE_ENV_NAME, VERSION_LABEL):
2426
raise Exception("Failed to deploy new version.")
2527

src/main.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
import os
23
import traceback
34
import sys
5+
46
import clone_blue_environment
57
import swap_environment
68
import deploy_release
@@ -27,11 +29,8 @@ def main():
2729
print(f"S3_ARTIFACTS_BUCKET = {S3_ARTIFACTS_BUCKET}\n")
2830
print(f"S3_ARTIFACTS_OBJECT {S3_ARTIFACTS_OBJECT}\n")
2931

30-
current_release_bucket=""
31-
current_release_key=""
32-
33-
available_execution_types = ["deploy", "cutover", "full", "rollback"]
34-
execution_type = str(sys.argv[1])
32+
available_execution_types = ["deploy", "cutover", "full", "rollback", "redeploy"]
33+
execution_type: str = str(sys.argv[1])
3534

3635
if execution_type not in available_execution_types:
3736
print("Not valid execution type argument: " + execution_type)
@@ -47,8 +46,6 @@ def main():
4746
print(f"\n Execution Type: {execution_type}\n")
4847
print("Initiating blue green deployment process")
4948

50-
51-
5249
if execution_type == "deploy" or execution_type == "full":
5350
print("\n\n\n ------------------ Stating Deployment Step 1 --------------------- \n")
5451
print("------------------ Creating Green Env --------------------- \n\n\n")
@@ -57,7 +54,7 @@ def main():
5754
try:
5855
print("Clonning the blue environment...")
5956
start_1 = time.time()
60-
previous_release_bucket, previous_release_key = clone_blue_environment.main(
57+
clone_blue_environment.main(
6158
BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client)
6259
print(f"Clonning the blue environment has finished successfully!\n\
6360
\tIt took: {time.time() - start_1} seconds\n")
@@ -104,8 +101,8 @@ def main():
104101
try:
105102
print("New release deployment initiated.")
106103
start_3 = time.time()
107-
deploy_release.main(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET,
108-
BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client)
104+
deploy_release.release_deployment(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME,
105+
BEANSTALK_APP_NAME, boto_authenticated_client)
109106
print(f"New release deployment has finished successfully!\n\
110107
\tIt took: {time.time() - start_3} seconds\n")
111108
except Exception as err:
@@ -133,7 +130,8 @@ def main():
133130

134131
# Step 5: Re-swapping the URL's and terminating the green environment.
135132
print("\n\n\n ------------------ Stating Cutover --------------------- \n")
136-
print("------------------ Re-swapping the Domains && Killing the green environment --------------------- \n\n\n")
133+
print(
134+
"------------------ Re-swapping the Domains && Killing the green environment --------------------- \n\n\n")
137135
boto_authenticated_client = aws_authentication.get_boto_client()
138136
try:
139137
print("Re-swapping the URL's and terminating the green environment.")
@@ -190,7 +188,25 @@ def main():
190188
traceback.print_exc()
191189
sys.exit(1)
192190
print("Rollback has finished successfully!")
191+
192+
# Start redeploy phase
193+
if execution_type == "redeploy":
194+
try:
195+
print("Initiating Re-Deployment of the previous version.")
196+
deploy_release.release_deployment(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME,
197+
BEANSTALK_APP_NAME, boto_authenticated_client, create_app_version=False)
198+
except Exception as err:
199+
print("Re-Deployment of the previous version has failed!")
200+
print(("Error: " + str(err)))
201+
e = sys.exc_info()[0]
202+
print(e)
203+
traceback.print_exc()
204+
sys.exit(1)
205+
193206
print("Deployment has finished successfully!")
207+
print(f"The process took: {round((time.time() - starting_time), 2)} seconds")
208+
209+
194210

195211
if __name__ == "__main__":
196212
try:
@@ -218,7 +234,6 @@ def main():
218234
if "S3_ARTIFACTS_OBJECT" not in os.environ:
219235
raise Exception(
220236
"The environment variable S3_ARTIFACTS_OBJECT wasn't exposed to the container")
221-
print("Successfully validated environment variables")
222237
except Exception as e:
223238
print("Failed to get environment variable")
224239
print(str(e))

0 commit comments

Comments
 (0)