blob: 669305d53947f0c3f188139ebdad74df95441562 [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
Tomáš Kukrál1ee80f02017-06-12 09:27:11 +0200138@NonCPS
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200139def describeAutoscalingGroup(venv_path, env_vars, group_name) {
140 def python = new com.mirantis.mk.Python()
141 def common = new com.mirantis.mk.Common()
142
143 def cmd = "aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${group_name}"
144
145 withEnv(env_vars) {
146 def out = python.runVirtualenvCommand(venv_path, cmd)
147 def out_json = common.parseJSON(out)
148 def info = out_json['AutoScalingGroups'][0]
149 common.prettyPrint(info)
150
151 return info
152 }
153}
154
155def updateAutoscalingGroup(venv_path, env_vars, group_name, parameters = []) {
156 def python = new com.mirantis.mk.Python()
157 def common = new com.mirantis.mk.Common()
158
159 if (parameters == null || parameters.size() == 0) {
160 throw new Exception("Missing parameter")
161 }
162
Tomáš Kukráleb154a32017-06-06 13:44:47 +0200163 def cmd = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${group_name} " + parameters.join(' ')
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200164
165 withEnv(env_vars) {
166 def out = python.runVirtualenvCommand(venv_path, cmd)
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200167 return out
168 }
169}
170
Tomáš Kukrál1ee80f02017-06-12 09:27:11 +0200171@NonCPS
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200172def waitForAutoscalingInstances(venv_path, env_vars, group_name, max_timeout = 600, loop_sleep = 20) {
173 def aws = new com.mirantis.mk.Aws()
Tomáš Kukrál4c5e5e92017-06-11 13:35:07 +0200174 def common = new com.mirantis.mk.Common()
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200175
176 timeout(time: max_timeout, unit: 'SECONDS') {
177 withEnv(env_vars) {
178 while (true) {
179 // get instances in autoscaling group
180 def out = aws.describeAutoscalingGroup(venv_path, env_vars, group_name)
Tomáš Kukrál79649402017-06-11 14:24:16 +0200181 common.prettyPrint(out)
Tomáš Kukrál73c18472017-06-11 14:08:26 +0200182 def instances = out['AutoScalingGroups'][0]['Instances']
Tomáš Kukrálaa8ad832017-06-09 11:52:56 +0200183
184 // check all instances are InService
185 if (instances.stream().filter{i -> !i['LifecycleState'].equals("InService")}.collect(java.util.stream.Collectors.counting()) == 0) {
186 break
187 }
188
189 // wait for next loop
190 sleep(loop_sleep)
191 }
192 }
193 }
194}
195
Tomáš Kukrál692c9772017-06-06 16:28:38 +0200196/**
197 *
198 * Load balancers (elb)
199 *
200 */
201
202
203def registerIntanceWithLb(venv_path, env_vars, lb, instances = []) {
204 def python = new com.mirantis.mk.Python()
205
206 def cmd = "aws elb register-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
207
208 withEnv(env_vars) {
209 def out = python.runVirtualenvCommand(venv_path, cmd)
210 return out
211 }
212}
213
214def deregisterIntanceWithLb(venv_path, env_vars, lb, instances = []) {
215 def python = new com.mirantis.mk.Python()
216
217 def cmd = "aws elb deregister-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ')
218
219 withEnv(env_vars) {
220 def out = python.runVirtualenvCommand(venv_path, cmd)
221 return out
222 }
223}
224
225
226
Tomáš Kukrál5c969ca2017-06-05 16:29:19 +0200227