Skip to content

Commit

Permalink
Merge pull request #6 from DNXLabs/feature/redeploy
Browse files Browse the repository at this point in the history
Add redeploy feature
  • Loading branch information
caiovfernandes authored Jul 23, 2023
2 parents 1cbb714 + 41d01a9 commit 82e3c9b
Show file tree
Hide file tree
Showing 6 changed files with 709 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Once deployment to the blue environment is successful, and code review and code
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.

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

All the steps are done using python:
1. The first stage clones the blue environment, which results in a green environment.
Expand Down Expand Up @@ -55,6 +55,7 @@ S3_ARTIFACTS_OBJECT
RELEASE_HEALTH_CHECKING_PATH
AUTH_METHOD
SSO_PROFILE
VERSION_LABEL
```


Expand Down
337 changes: 337 additions & 0 deletions _docs/assets/.$BlueGreen.drawio.bkp

Large diffs are not rendered by default.

Binary file added _docs/assets/BlueGreen-v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
338 changes: 337 additions & 1 deletion _docs/assets/BlueGreen.drawio

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/deploy_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import swap_environment

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

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

if not create_new_version(beanstalkclient, VERSION_LABEL, BUCKET_KEY, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME):
raise Exception("Failed to create beanstalk release.")
if create_app_version:
if not create_new_version(beanstalkclient, VERSION_LABEL, BUCKET_KEY, S3_ARTIFACTS_BUCKET, BEANSTALK_APP_NAME):
raise Exception("Failed to create beanstalk release.")

wait_until_env_be_ready(beanstalkclient, BLUE_ENV_NAME)
# Wait for the new version to be consistent before deploying
wait_until_env_be_ready(beanstalkclient, BLUE_ENV_NAME)

if not deploy_new_version(beanstalkclient, BEANSTALK_APP_NAME, BLUE_ENV_NAME, VERSION_LABEL):
raise Exception("Failed to deploy new version.")

Expand Down
39 changes: 27 additions & 12 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import os
import traceback
import sys

import clone_blue_environment
import swap_environment
import deploy_release
Expand All @@ -27,11 +29,8 @@ def main():
print(f"S3_ARTIFACTS_BUCKET = {S3_ARTIFACTS_BUCKET}\n")
print(f"S3_ARTIFACTS_OBJECT {S3_ARTIFACTS_OBJECT}\n")

current_release_bucket=""
current_release_key=""

available_execution_types = ["deploy", "cutover", "full", "rollback"]
execution_type = str(sys.argv[1])
available_execution_types = ["deploy", "cutover", "full", "rollback", "redeploy"]
execution_type: str = str(sys.argv[1])

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



if execution_type == "deploy" or execution_type == "full":
print("\n\n\n ------------------ Stating Deployment Step 1 --------------------- \n")
print("------------------ Creating Green Env --------------------- \n\n\n")
Expand All @@ -57,7 +54,7 @@ def main():
try:
print("Clonning the blue environment...")
start_1 = time.time()
previous_release_bucket, previous_release_key = clone_blue_environment.main(
clone_blue_environment.main(
BLUE_ENV_NAME, GREEN_ENV_NAME, BEANSTALK_APP_NAME, S3_ARTIFACTS_BUCKET, boto_authenticated_client)
print(f"Clonning the blue environment has finished successfully!\n\
\tIt took: {time.time() - start_1} seconds\n")
Expand Down Expand Up @@ -104,8 +101,8 @@ def main():
try:
print("New release deployment initiated.")
start_3 = time.time()
deploy_release.main(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET,
BLUE_ENV_NAME, BEANSTALK_APP_NAME, boto_authenticated_client)
deploy_release.release_deployment(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME,
BEANSTALK_APP_NAME, boto_authenticated_client)
print(f"New release deployment has finished successfully!\n\
\tIt took: {time.time() - start_3} seconds\n")
except Exception as err:
Expand Down Expand Up @@ -133,7 +130,8 @@ def main():

# Step 5: Re-swapping the URL's and terminating the green environment.
print("\n\n\n ------------------ Stating Cutover --------------------- \n")
print("------------------ Re-swapping the Domains && Killing the green environment --------------------- \n\n\n")
print(
"------------------ Re-swapping the Domains && Killing the green environment --------------------- \n\n\n")
boto_authenticated_client = aws_authentication.get_boto_client()
try:
print("Re-swapping the URL's and terminating the green environment.")
Expand Down Expand Up @@ -190,7 +188,25 @@ def main():
traceback.print_exc()
sys.exit(1)
print("Rollback has finished successfully!")

# Start redeploy phase
if execution_type == "redeploy":
try:
print("Initiating Re-Deployment of the previous version.")
deploy_release.release_deployment(S3_ARTIFACTS_OBJECT, S3_ARTIFACTS_BUCKET, BLUE_ENV_NAME,
BEANSTALK_APP_NAME, boto_authenticated_client, create_app_version=False)
except Exception as err:
print("Re-Deployment of the previous version has failed!")
print(("Error: " + str(err)))
e = sys.exc_info()[0]
print(e)
traceback.print_exc()
sys.exit(1)

print("Deployment has finished successfully!")
print(f"The process took: {round((time.time() - starting_time), 2)} seconds")



if __name__ == "__main__":
try:
Expand Down Expand Up @@ -218,7 +234,6 @@ def main():
if "S3_ARTIFACTS_OBJECT" not in os.environ:
raise Exception(
"The environment variable S3_ARTIFACTS_OBJECT wasn't exposed to the container")
print("Successfully validated environment variables")
except Exception as e:
print("Failed to get environment variable")
print(str(e))
Expand Down

0 comments on commit 82e3c9b

Please sign in to comment.