blob: 6a9c8ae518c78c588423c821ccb834f3256e5a81 [file] [log] [blame]
Tomáš Kukráldf540c42017-05-22 15:12:21 +02001package com.mirantis.mk
2
Tomáš Kukráldf540c42017-05-22 15:12:21 +02003/**
4 *
5 * AWS function functions
6 *
7 */
8
9def setupVirtualEnv(venv_path = 'aws_venv') {
10 def python = new com.mirantis.mk.Python()
11
12 def requirements = [
13 'awscli'
14 ]
15
16 python.setupVirtualenv(venv_path, 'python2', requirements)
17}
18
Tomáš Kukrál36a2dcd2017-09-07 10:02:28 +020019def getEnvVars(credentials, region = 'us-west-2') {
Tomáš Kukráldf540c42017-05-22 15:12:21 +020020 def common = new com.mirantis.mk.Common()
21
Tomáš Kukrál36a2dcd2017-09-07 10:02:28 +020022 def creds
23 def username
24 def password
25
26 if (credentials.contains(':')) {
27 // we have key and secret in string (delimited by :)
Tomáš Kukrále364fe12017-09-07 10:16:13 +020028 creds = credentials.tokenize(':')
Tomáš Kukrál36a2dcd2017-09-07 10:02:28 +020029 username = creds[0]
Tomáš Kukrálee4adfb2017-09-07 10:19:14 +020030 password = creds[1]
Tomáš Kukrál36a2dcd2017-09-07 10:02:28 +020031 } else {
32 // we have creadentials_id
33 creds = common.getCredentials(credentials)
34 username = creds.username
35 password = creds.password
36 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +020037
38 return [
Tomáš Kukrál36a2dcd2017-09-07 10:02:28 +020039 "AWS_ACCESS_KEY_ID=${username}",
40 "AWS_SECRET_ACCESS_KEY=${password}",
Tomáš Kukráldf540c42017-05-22 15:12:21 +020041 "AWS_DEFAULT_REGION=${region}"
42 ]
43}
44
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +020045
46/**
47 *
Tomáš Kukrál692c9772017-06-06 16:28:38 +020048 * CloudFormation stacks (cloudformation)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +020049 *
50 */
51
Tomáš Kukráldf540c42017-05-22 15:12:21 +020052def createStack(venv_path, env_vars, template_file, stack_name, parameters = []) {
53 def python = new com.mirantis.mk.Python()
54
Tomáš Kukrál2a0f88d2017-09-05 11:12:33 +020055 def cmd = "aws cloudformation create-stack --stack-name ${stack_name} --template-body file://template/${template_file} --capabilities CAPABILITY_NAMED_IAM"
Tomáš Kukráldf540c42017-05-22 15:12:21 +020056
57 if (parameters != null && parameters.size() > 0) {
58 cmd = "${cmd} --parameters"
59
60 for (int i=0; i<parameters.size(); i++) {
61 cmd = "${cmd} ${parameters[i]}"
62 }
63 }
64
65 withEnv(env_vars) {
66 def out = python.runVirtualenvCommand(venv_path, cmd)
67 }
68}
69
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020070def deleteStack(venv_path, env_vars, stack_name) {
71 def python = new com.mirantis.mk.Python()
72
73 def cmd = "aws cloudformation delete-stack --stack-name ${stack_name}"
74
75 withEnv(env_vars) {
76 def out = python.runVirtualenvCommand(venv_path, cmd)
77 }
78}
79
Tomáš Kukráldf540c42017-05-22 15:12:21 +020080def describeStack(venv_path, env_vars, stack_name) {
81 def python = new com.mirantis.mk.Python()
82 def common = new com.mirantis.mk.Common()
83
84 def cmd = "aws cloudformation describe-stacks --stack-name ${stack_name}"
85
86 withEnv(env_vars) {
87 def out = python.runVirtualenvCommand(venv_path, cmd)
88 def out_json = common.parseJSON(out)
Tomáš Kukrál3db21052017-08-29 09:55:13 +020089 def resources = out_json['Stacks'][0]
90 common.prettyPrint(resources)
Tomáš Kukráldf540c42017-05-22 15:12:21 +020091
Tomáš Kukrál3db21052017-08-29 09:55:13 +020092 return resources
93 }
94}
95
96def describeStackResources(venv_path, env_vars, stack_name) {
97 def python = new com.mirantis.mk.Python()
98 def common = new com.mirantis.mk.Common()
99
100 def cmd = "aws cloudformation describe-stack-resources --stack-name ${stack_name}"
101
102 withEnv(env_vars) {
103 def out = python.runVirtualenvCommand(venv_path, cmd)
104 def out_json = common.parseJSON(out)
105 def resources = out_json['StackResources']
106 common.prettyPrint(resources)
107
108 return resources
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200109 }
110}
111
Tomáš Kukrál987c0ff2017-11-29 09:53:49 +0100112def waitForStatus(venv_path, env_vars, stack_name, state, state_failed = ['ROLLBACK_COMPLETE'], max_timeout = 1200, loop_sleep = 30) {
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200113 def aws = new com.mirantis.mk.Aws()
114 def common = new com.mirantis.mk.Common()
115 def python = new com.mirantis.mk.Python()
116
Tomáš Kukrál735d2072017-05-25 16:17:33 +0200117 timeout(time: max_timeout, unit: 'SECONDS') {
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200118 withEnv(env_vars) {
119 while (true) {
120 // get stack state
121 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
122 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200123
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200124 // check for desired state
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200125 if (stack_info['StackStatus'] == state) {
126 common.successMsg("Stack ${stack_name} in in state ${state}")
127 common.prettyPrint(stack_info)
128 break
129 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200130
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200131 // check for failed state
Tomáš Kukrál47d1f232017-06-01 15:37:41 +0200132 if (state_failed.contains(stack_info['StackStatus'])) {
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200133 throw new Exception("Stack ${stack_name} in in failed state")
134 }
135
Tomáš Kukrál3db21052017-08-29 09:55:13 +0200136 // print stack resources
137 aws.describeStackResources(venv_path, env_vars, stack_name)
138
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200139 // wait for next loop
140 sleep(loop_sleep)
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200141 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200142 }
143 }
144}
145
146def getOutputs(venv_path, env_vars, stack_name, key = '') {
147 def aws = new com.mirantis.mk.Aws()
148 def common = new com.mirantis.mk.Common()
Tomáš Kukrál81aeabd2017-09-03 10:10:39 +0200149 def output = [:]
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200150
151 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
152 common.prettyPrint(stack_info)
153
154 for (int i=0; i<stack_info['Outputs'].size(); i++) {
155 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
156 }
157
158 if (key != null && key != '') {
159 return output[key]
160 } else {
161 return output
162 }
163}
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200164
165/**
166 *
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200167 * Autoscaling groups (autoscaling)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200168 *
169 */
170
171def describeAutoscalingGroup(venv_path, env_vars, group_name) {
172 def python = new com.mirantis.mk.Python()
173 def common = new com.mirantis.mk.Common()
174
175 def cmd = "aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${group_name}"
176
177 withEnv(env_vars) {
178 def out = python.runVirtualenvCommand(venv_path, cmd)
179 def out_json = common.parseJSON(out)
180 def info = out_json['AutoScalingGroups'][0]
181 common.prettyPrint(info)
182
183 return info
184 }
185}
186
187def updateAutoscalingGroup(venv_path, env_vars, group_name, parameters = []) {
188 def python = new com.mirantis.mk.Python()
189 def common = new com.mirantis.mk.Common()
190
191 if (parameters == null || parameters.size() == 0) {
192 throw new Exception("Missing parameter")
193 }
194
Tomáš Kukráleb154a32017-06-06 13:44:47 +0200195 def cmd = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${group_name} " + parameters.join(' ')
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200196
197 withEnv(env_vars) {
198 def out = python.runVirtualenvCommand(venv_path, cmd)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200199 return out
200 }
201}
202
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200203def waitForAutoscalingInstances(venv_path, env_vars, group_name, max_timeout = 600, loop_sleep = 20) {
204 def aws = new com.mirantis.mk.Aws()
Tomáš Kukrál4c5e5e92017-06-11 13:35:07 +0200205 def common = new com.mirantis.mk.Common()
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200206
207 timeout(time: max_timeout, unit: 'SECONDS') {
208 withEnv(env_vars) {
209 while (true) {
210 // get instances in autoscaling group
211 def out = aws.describeAutoscalingGroup(venv_path, env_vars, group_name)
Tomáš Kukráledca0c22017-06-13 14:12:00 +0200212 print(common.prettyPrint(out))
Tomáš Kukrálc6cb4ff2017-06-13 14:35:04 +0200213 def instances = out['Instances']
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200214
215 // check all instances are InService
Tomáš Kukrál5dd12072017-06-13 15:54:44 +0200216 if (common.countHashMapEquals(instances, 'LifecycleState', 'InService') == out['DesiredCapacity']) {
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200217 break
218 }
219
220 // wait for next loop
221 sleep(loop_sleep)
222 }
223 }
224 }
225}
226
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200227/**
228 *
229 * Load balancers (elb)
230 *
231 */
232
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200233def registerIntanceWithLb(venv_path, env_vars, lb, instances = []) {
234 def python = new com.mirantis.mk.Python()
235
236 def cmd = "aws elb register-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
237
238 withEnv(env_vars) {
239 def out = python.runVirtualenvCommand(venv_path, cmd)
240 return out
241 }
242}
243
244def deregisterIntanceWithLb(venv_path, env_vars, lb, instances = []) {
245 def python = new com.mirantis.mk.Python()
246
247 def cmd = "aws elb deregister-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
248
249 withEnv(env_vars) {
250 def out = python.runVirtualenvCommand(venv_path, cmd)
251 return out
252 }
253}