-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodedeploy.py
More file actions
107 lines (98 loc) · 3.39 KB
/
codedeploy.py
File metadata and controls
107 lines (98 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from __future__ import print_function
import os
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError
from botocore.config import config
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
BUCKET_KEY = os.getenv('APPLICATION_NAME') + '/' + VERSION_LABEL + \
'-bitbucket_builds.zip'
DEPLOYMENT_BACKOFF_SECS = 30
BOTO3_CONFIG = Config(
retries = dict(
max_attempts = 10
)
)
def upload_to_s3(artifact):
"""
Uploads an artifact to Amazon S3
"""
try:
client = boto3.client('s3')
except ClientError as err:
print("Failed to create boto3 client.\n" + str(err))
return False
try:
client.put_object(
Body=open(artifact, 'rb'),
Bucket=os.getenv('S3_BUCKET'),
Key=BUCKET_KEY
)
except ClientError as err:
print("Failed to upload artifact to S3.\n" + str(err))
return False
except IOError as err:
print("Failed to access artifact.zip in this directory.\n" + str(err))
return False
return True
def deploy_new_revision():
"""
Deploy a new application revision to AWS CodeDeploy Deployment Group
"""
try:
client = boto3.client('codedeploy', config=BOTO3_CONFIG))
except ClientError as err:
print("Failed to create boto3 client.\n" + str(err))
return False
try:
response = client.create_deployment(
applicationName=str(os.getenv('APPLICATION_NAME')),
deploymentGroupName=str(os.getenv('DEPLOYMENT_GROUP_NAME')),
revision={
'revisionType': 'S3',
's3Location': {
'bucket': os.getenv('S3_BUCKET'),
'key': BUCKET_KEY,
'bundleType': 'zip'
}
},
deploymentConfigName=str(os.getenv('DEPLOYMENT_CONFIG')),
description='New deployment from BitBucket',
ignoreApplicationStopFailures=True
)
except ClientError as err:
print("Failed to deploy application revision.\n" + str(err))
return False
"""
Wait for deployment to complete
"""
while 1:
try:
deploymentResponse = client.get_deployment(
deploymentId=str(response['deploymentId'])
)
deploymentStatus=deploymentResponse['deploymentInfo']['status']
if deploymentStatus == 'Succeeded':
print ("Deployment Succeeded")
return True
elif (deploymentStatus == 'Failed') or (deploymentStatus == 'Stopped') :
print ("Deployment Failed")
return False
elif (deploymentStatus == 'InProgress') or (deploymentStatus == 'Queued') or (deploymentStatus == 'Created'):
deploymentCounter += 1
deploymentDelay = (deploymentCounter * DEPLOYMENT_BACKOFF_SECS)
print("Deployment " + deploymentStatus + " (Exponential back off " + str(deploymentDelay) + "s)")
sleep(deploymentDelay)
continue
except ClientError as err:
print("Failed to deploy application revision.\n" + str(err))
return False
return True
def main():
if not upload_to_s3('/tmp/artifact.zip'):
sys.exit(1)
if not deploy_new_revision():
sys.exit(1)
if __name__ == "__main__":
main()