blob: 6b7717104b8441cfc620d5154c5a565fc841511a [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
19def getEnvVars(credentials_id, region = 'us-west-2') {
20 def common = new com.mirantis.mk.Common()
21
22 def creds = common.getCredentials(credentials_id)
23
24 return [
25 "AWS_ACCESS_KEY_ID=${creds.username}",
26 "AWS_SECRET_ACCESS_KEY=${creds.password}",
27 "AWS_DEFAULT_REGION=${region}"
28 ]
29}
30
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +020031
32/**
33 *
Tomáš Kukrál692c9772017-06-06 16:28:38 +020034 * CloudFormation stacks (cloudformation)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +020035 *
36 */
37
Tomáš Kukráldf540c42017-05-22 15:12:21 +020038def createStack(venv_path, env_vars, template_file, stack_name, parameters = []) {
39 def python = new com.mirantis.mk.Python()
40
41 def cmd = "aws cloudformation create-stack --stack-name ${stack_name} --template-body file://template/${template_file}"
42
43 if (parameters != null && parameters.size() > 0) {
44 cmd = "${cmd} --parameters"
45
46 for (int i=0; i<parameters.size(); i++) {
47 cmd = "${cmd} ${parameters[i]}"
48 }
49 }
50
51 withEnv(env_vars) {
52 def out = python.runVirtualenvCommand(venv_path, cmd)
53 }
54}
55
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020056def deleteStack(venv_path, env_vars, stack_name) {
57 def python = new com.mirantis.mk.Python()
58
59 def cmd = "aws cloudformation delete-stack --stack-name ${stack_name}"
60
61 withEnv(env_vars) {
62 def out = python.runVirtualenvCommand(venv_path, cmd)
63 }
64}
65
Tomáš Kukráldf540c42017-05-22 15:12:21 +020066def describeStack(venv_path, env_vars, stack_name) {
67 def python = new com.mirantis.mk.Python()
68 def common = new com.mirantis.mk.Common()
69
70 def cmd = "aws cloudformation describe-stacks --stack-name ${stack_name}"
71
72 withEnv(env_vars) {
73 def out = python.runVirtualenvCommand(venv_path, cmd)
74 def out_json = common.parseJSON(out)
Tomáš Kukrál3db21052017-08-29 09:55:13 +020075 def resources = out_json['Stacks'][0]
76 common.prettyPrint(resources)
Tomáš Kukráldf540c42017-05-22 15:12:21 +020077
Tomáš Kukrál3db21052017-08-29 09:55:13 +020078 return resources
79 }
80}
81
82def describeStackResources(venv_path, env_vars, stack_name) {
83 def python = new com.mirantis.mk.Python()
84 def common = new com.mirantis.mk.Common()
85
86 def cmd = "aws cloudformation describe-stack-resources --stack-name ${stack_name}"
87
88 withEnv(env_vars) {
89 def out = python.runVirtualenvCommand(venv_path, cmd)
90 def out_json = common.parseJSON(out)
91 def resources = out_json['StackResources']
92 common.prettyPrint(resources)
93
94 return resources
Tomáš Kukráldf540c42017-05-22 15:12:21 +020095 }
96}
97
Tomáš Kukrál024e1c02017-06-02 13:24:44 +020098def waitForStatus(venv_path, env_vars, stack_name, state, state_failed = [], max_timeout = 1200, loop_sleep = 30) {
Tomáš Kukráldf540c42017-05-22 15:12:21 +020099 def aws = new com.mirantis.mk.Aws()
100 def common = new com.mirantis.mk.Common()
101 def python = new com.mirantis.mk.Python()
102
Tomáš Kukrál735d2072017-05-25 16:17:33 +0200103 timeout(time: max_timeout, unit: 'SECONDS') {
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200104 withEnv(env_vars) {
105 while (true) {
106 // get stack state
107 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
108 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200109
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200110 // check for desired state
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200111 if (stack_info['StackStatus'] == state) {
112 common.successMsg("Stack ${stack_name} in in state ${state}")
113 common.prettyPrint(stack_info)
114 break
115 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200116
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200117 // check for failed state
Tomáš Kukrál47d1f232017-06-01 15:37:41 +0200118 if (state_failed.contains(stack_info['StackStatus'])) {
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200119 throw new Exception("Stack ${stack_name} in in failed state")
120 }
121
Tomáš Kukrál3db21052017-08-29 09:55:13 +0200122 // print stack resources
123 aws.describeStackResources(venv_path, env_vars, stack_name)
124
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200125 // wait for next loop
126 sleep(loop_sleep)
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200127 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200128 }
129 }
130}
131
132def getOutputs(venv_path, env_vars, stack_name, key = '') {
133 def aws = new com.mirantis.mk.Aws()
134 def common = new com.mirantis.mk.Common()
135 def output = {}
136
137 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
138 common.prettyPrint(stack_info)
139
140 for (int i=0; i<stack_info['Outputs'].size(); i++) {
141 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
142 }
143
144 if (key != null && key != '') {
145 return output[key]
146 } else {
147 return output
148 }
149}
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200150
151/**
152 *
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200153 * Autoscaling groups (autoscaling)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200154 *
155 */
156
157def describeAutoscalingGroup(venv_path, env_vars, group_name) {
158 def python = new com.mirantis.mk.Python()
159 def common = new com.mirantis.mk.Common()
160
161 def cmd = "aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${group_name}"
162
163 withEnv(env_vars) {
164 def out = python.runVirtualenvCommand(venv_path, cmd)
165 def out_json = common.parseJSON(out)
166 def info = out_json['AutoScalingGroups'][0]
167 common.prettyPrint(info)
168
169 return info
170 }
171}
172
173def updateAutoscalingGroup(venv_path, env_vars, group_name, parameters = []) {
174 def python = new com.mirantis.mk.Python()
175 def common = new com.mirantis.mk.Common()
176
177 if (parameters == null || parameters.size() == 0) {
178 throw new Exception("Missing parameter")
179 }
180
Tomáš Kukráleb154a32017-06-06 13:44:47 +0200181 def cmd = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${group_name} " + parameters.join(' ')
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200182
183 withEnv(env_vars) {
184 def out = python.runVirtualenvCommand(venv_path, cmd)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200185 return out
186 }
187}
188
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200189def waitForAutoscalingInstances(venv_path, env_vars, group_name, max_timeout = 600, loop_sleep = 20) {
190 def aws = new com.mirantis.mk.Aws()
Tomáš Kukrál4c5e5e92017-06-11 13:35:07 +0200191 def common = new com.mirantis.mk.Common()
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200192
193 timeout(time: max_timeout, unit: 'SECONDS') {
194 withEnv(env_vars) {
195 while (true) {
196 // get instances in autoscaling group
197 def out = aws.describeAutoscalingGroup(venv_path, env_vars, group_name)
Tomáš Kukráledca0c22017-06-13 14:12:00 +0200198 print(common.prettyPrint(out))
Tomáš Kukrálc6cb4ff2017-06-13 14:35:04 +0200199 def instances = out['Instances']
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200200
201 // check all instances are InService
Tomáš Kukrál5dd12072017-06-13 15:54:44 +0200202 if (common.countHashMapEquals(instances, 'LifecycleState', 'InService') == out['DesiredCapacity']) {
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200203 break
204 }
205
206 // wait for next loop
207 sleep(loop_sleep)
208 }
209 }
210 }
211}
212
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200213/**
214 *
215 * Load balancers (elb)
216 *
217 */
218
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200219def registerIntanceWithLb(venv_path, env_vars, lb, instances = []) {
220 def python = new com.mirantis.mk.Python()
221
222 def cmd = "aws elb register-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
223
224 withEnv(env_vars) {
225 def out = python.runVirtualenvCommand(venv_path, cmd)
226 return out
227 }
228}
229
230def deregisterIntanceWithLb(venv_path, env_vars, lb, instances = []) {
231 def python = new com.mirantis.mk.Python()
232
233 def cmd = "aws elb deregister-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
234
235 withEnv(env_vars) {
236 def out = python.runVirtualenvCommand(venv_path, cmd)
237 return out
238 }
239}