9
9
10
10
11
11
def main (argv ):
12
- helptext = 'bluegreen.py -f <path to terraform project> -a <ami> -c <command> -t <timeout> -e <environment.tfvars> -i <inactive-desired> <path> '
12
+ helptext = 'bluegreen.py -f <path to terraform project> -a <ami> -c <command> -t <timeout> -e <environment.tfvars> -i <inactive-desired> [-r <assume-role-arn>] '
13
13
14
14
try :
15
- opts , args = getopt .getopt (argv , "hsf:a:c:t:e:i:" , ["folder=" , "ami=" , "command=" , "timeout=" , "environment=" , "inactive-desired=" ])
15
+ opts , args = getopt .getopt (argv , "hsf:a:c:t:e:i:r: " , ["folder=" , "ami=" , "command=" , "timeout=" , "environment=" , "inactive-desired=" , "role-arn =" ])
16
16
except getopt .GetoptError :
17
17
print helptext
18
18
sys .exit (2 )
@@ -34,6 +34,8 @@ def main(argv):
34
34
environment = arg
35
35
elif opt in ("-i" , "--inactive-desired" ):
36
36
inactiveDesired = arg
37
+ elif opt in ("-r" , "--role-arn" ):
38
+ assumeRoleArn = arg
37
39
elif opt in ("-s" ):
38
40
stopScaling = True
39
41
else :
@@ -62,6 +64,9 @@ def main(argv):
62
64
if 'inactiveDesired' not in locals ():
63
65
inactiveDesired = 1
64
66
67
+ if 'assumeRoleArn' not in locals ():
68
+ assumeRoleArn = None
69
+
65
70
if 'stopScaling' not in locals ():
66
71
stopScaling = False
67
72
@@ -73,6 +78,10 @@ def main(argv):
73
78
agBlue = getTerraformOutput (projectPath , 'blue_asg_id' )
74
79
agGreen = getTerraformOutput (projectPath , 'green_asg_id' )
75
80
81
+ # Get a boto3 session
82
+ global awsSession
83
+ awsSession = getBotoSession (assumeRoleArn )
84
+
76
85
# Retrieve autoscaling groups information
77
86
info = getAutoscalingInfo (agBlue , agGreen )
78
87
@@ -109,6 +118,24 @@ def main(argv):
109
118
print 'Deactivating the autoscaling'
110
119
stopAutoscaling (info , active , ami , command , projectPath , environment )
111
120
121
+ def getBotoSession (assumeRoleArn ):
122
+ if assumeRoleArn :
123
+ sts_client = boto3 .client ('sts' )
124
+
125
+ # Call the assume_role method of the STSConnection object and pass the role
126
+ # ARN and a role session name.
127
+ assumed_role_object = sts_client .assume_role (
128
+ RoleArn = assumeRoleArn ,
129
+ RoleSessionName = "bluegreen"
130
+ )
131
+
132
+ return boto3 .Session (
133
+ aws_access_key_id = assumed_role_object ['Credentials' ]['AccessKeyId' ],
134
+ aws_secret_access_key = assumed_role_object ['Credentials' ]['SecretAccessKey' ],
135
+ aws_session_token = assumed_role_object ['Credentials' ]['SessionToken' ],
136
+ )
137
+ else :
138
+ return boto3 .Session ()
112
139
113
140
def getTerraformOutput (projectPath , output ):
114
141
process = subprocess .Popen ('terraform output ' + output , shell = True , cwd = projectPath , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -122,7 +149,7 @@ def getTerraformOutput(projectPath, output):
122
149
123
150
124
151
def getAutoscalingInfo (blue , green ):
125
- client = boto3 .client ('autoscaling' )
152
+ client = awsSession .client ('autoscaling' )
126
153
response = client .describe_auto_scaling_groups (
127
154
AutoScalingGroupNames = [
128
155
blue ,
@@ -141,7 +168,7 @@ def getLoadbalancers(info, type):
141
168
142
169
143
170
def getAmi (launchconfig ):
144
- client = boto3 .client ('autoscaling' )
171
+ client = awsSession .client ('autoscaling' )
145
172
response = client .describe_launch_configurations (
146
173
LaunchConfigurationNames = [
147
174
launchconfig ,
@@ -152,7 +179,7 @@ def getAmi(launchconfig):
152
179
153
180
154
181
def getLaunchconfigDate (launchconfig ):
155
- client = boto3 .client ('autoscaling' )
182
+ client = awsSession .client ('autoscaling' )
156
183
response = client .describe_launch_configurations (
157
184
LaunchConfigurationNames = [
158
185
launchconfig ,
@@ -322,7 +349,7 @@ def updateAutoscaling(command, blueMax, blueMin, blueDesired, blueAMI, greenMax,
322
349
323
350
324
351
def checkScalingStatus (elbs , albs , desiredInstanceCount ):
325
- client = boto3 .client ('elb' )
352
+ client = awsSession .client ('elb' )
326
353
for elb in elbs :
327
354
response = client .describe_instance_health (
328
355
LoadBalancerName = elb
@@ -334,7 +361,7 @@ def checkScalingStatus(elbs, albs, desiredInstanceCount):
334
361
print 'ELB: ' + state ['State' ]
335
362
if state ['State' ] != 'InService' :
336
363
return False
337
- client = boto3 .client ('elbv2' )
364
+ client = awsSession .client ('elbv2' )
338
365
for alb in albs :
339
366
response = client .describe_target_health (
340
367
TargetGroupArn = alb ,
0 commit comments