blob: a17e6f579abdfe812beea12bcd039a384d8b1f71 [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)
75 def stack_info = out_json['Stacks'][0]
76 common.prettyPrint(stack_info)
77
78 return stack_info
79 }
80}
81
Tomáš Kukrál024e1c02017-06-02 13:24:44 +020082def waitForStatus(venv_path, env_vars, stack_name, state, state_failed = [], max_timeout = 1200, loop_sleep = 30) {
Tomáš Kukráldf540c42017-05-22 15:12:21 +020083 def aws = new com.mirantis.mk.Aws()
84 def common = new com.mirantis.mk.Common()
85 def python = new com.mirantis.mk.Python()
86
Tomáš Kukrál735d2072017-05-25 16:17:33 +020087 timeout(time: max_timeout, unit: 'SECONDS') {
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020088 withEnv(env_vars) {
89 while (true) {
90 // get stack state
91 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
92 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
Tomáš Kukráldf540c42017-05-22 15:12:21 +020093
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020094 // check for desired state
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020095 if (stack_info['StackStatus'] == state) {
96 common.successMsg("Stack ${stack_name} in in state ${state}")
97 common.prettyPrint(stack_info)
98 break
99 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200100
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200101 // check for failed state
Tomáš Kukrál47d1f232017-06-01 15:37:41 +0200102 if (state_failed.contains(stack_info['StackStatus'])) {
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +0200103 throw new Exception("Stack ${stack_name} in in failed state")
104 }
105
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200106 // wait for next loop
107 sleep(loop_sleep)
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200108 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200109 }
110 }
111}
112
113def getOutputs(venv_path, env_vars, stack_name, key = '') {
114 def aws = new com.mirantis.mk.Aws()
115 def common = new com.mirantis.mk.Common()
116 def output = {}
117
118 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
119 common.prettyPrint(stack_info)
120
121 for (int i=0; i<stack_info['Outputs'].size(); i++) {
122 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
123 }
124
125 if (key != null && key != '') {
126 return output[key]
127 } else {
128 return output
129 }
130}
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200131
132/**
133 *
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200134 * Autoscaling groups (autoscaling)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200135 *
136 */
137
138def describeAutoscalingGroup(venv_path, env_vars, group_name) {
139 def python = new com.mirantis.mk.Python()
140 def common = new com.mirantis.mk.Common()
141
142 def cmd = "aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${group_name}"
143
144 withEnv(env_vars) {
145 def out = python.runVirtualenvCommand(venv_path, cmd)
146 def out_json = common.parseJSON(out)
147 def info = out_json['AutoScalingGroups'][0]
148 common.prettyPrint(info)
149
150 return info
151 }
152}
153
154def updateAutoscalingGroup(venv_path, env_vars, group_name, parameters = []) {
155 def python = new com.mirantis.mk.Python()
156 def common = new com.mirantis.mk.Common()
157
158 if (parameters == null || parameters.size() == 0) {
159 throw new Exception("Missing parameter")
160 }
161
Tomáš Kukráleb154a32017-06-06 13:44:47 +0200162 def cmd = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${group_name} " + parameters.join(' ')
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200163
164 withEnv(env_vars) {
165 def out = python.runVirtualenvCommand(venv_path, cmd)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200166 return out
167 }
168}
169
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200170def waitForAutoscalingInstances(venv_path, env_vars, group_name, max_timeout = 600, loop_sleep = 20) {
171 def aws = new com.mirantis.mk.Aws()
172
173 timeout(time: max_timeout, unit: 'SECONDS') {
174 withEnv(env_vars) {
175 while (true) {
176 // get instances in autoscaling group
177 def out = aws.describeAutoscalingGroup(venv_path, env_vars, group_name)
178 def out_json = common.parseJSON(out)
179 def instances = out_json['AutoScalingGroups'][0]['Instances']
180
181 // check all instances are InService
182 if (instances.stream().filter{i -> !i['LifecycleState'].equals("InService")}.collect(java.util.stream.Collectors.counting()) == 0) {
183 break
184 }
185
186 // wait for next loop
187 sleep(loop_sleep)
188 }
189 }
190 }
191}
192
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200193/**
194 *
195 * Load balancers (elb)
196 *
197 */
198
199
200def registerIntanceWithLb(venv_path, env_vars, lb, instances = []) {
201 def python = new com.mirantis.mk.Python()
202
203 def cmd = "aws elb register-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
204
205 withEnv(env_vars) {
206 def out = python.runVirtualenvCommand(venv_path, cmd)
207 return out
208 }
209}
210
211def deregisterIntanceWithLb(venv_path, env_vars, lb, instances = []) {
212 def python = new com.mirantis.mk.Python()
213
214 def cmd = "aws elb deregister-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
215
216 withEnv(env_vars) {
217 def out = python.runVirtualenvCommand(venv_path, cmd)
218 return out
219 }
220}
221
222
223
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200224