blob: d19a493b7d74a778179ebec8c4d498c7850085d5 [file] [log] [blame]
Tomáš Kukráldf540c42017-05-22 15:12:21 +02001package com.mirantis.mk
2
3
4
5/**
6 *
7 * AWS function functions
8 *
9 */
10
11def setupVirtualEnv(venv_path = 'aws_venv') {
12 def python = new com.mirantis.mk.Python()
13
14 def requirements = [
15 'awscli'
16 ]
17
18 python.setupVirtualenv(venv_path, 'python2', requirements)
19}
20
21def getEnvVars(credentials_id, region = 'us-west-2') {
22 def common = new com.mirantis.mk.Common()
23
24 def creds = common.getCredentials(credentials_id)
25
26 return [
27 "AWS_ACCESS_KEY_ID=${creds.username}",
28 "AWS_SECRET_ACCESS_KEY=${creds.password}",
29 "AWS_DEFAULT_REGION=${region}"
30 ]
31}
32
33def createStack(venv_path, env_vars, template_file, stack_name, parameters = []) {
34 def python = new com.mirantis.mk.Python()
35
36 def cmd = "aws cloudformation create-stack --stack-name ${stack_name} --template-body file://template/${template_file}"
37
38 if (parameters != null && parameters.size() > 0) {
39 cmd = "${cmd} --parameters"
40
41 for (int i=0; i<parameters.size(); i++) {
42 cmd = "${cmd} ${parameters[i]}"
43 }
44 }
45
46 withEnv(env_vars) {
47 def out = python.runVirtualenvCommand(venv_path, cmd)
48 }
49}
50
51def describeStack(venv_path, env_vars, stack_name) {
52 def python = new com.mirantis.mk.Python()
53 def common = new com.mirantis.mk.Common()
54
55 def cmd = "aws cloudformation describe-stacks --stack-name ${stack_name}"
56
57 withEnv(env_vars) {
58 def out = python.runVirtualenvCommand(venv_path, cmd)
59 def out_json = common.parseJSON(out)
60 def stack_info = out_json['Stacks'][0]
61 common.prettyPrint(stack_info)
62
63 return stack_info
64 }
65}
66
67def waitForStatus(venv_path, env_vars, stack_name, state, timeout = 600, loop_sleep = 30) {
68 def aws = new com.mirantis.mk.Aws()
69 def common = new com.mirantis.mk.Common()
70 def python = new com.mirantis.mk.Python()
71
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020072 timeout(time: timeout, unit: 'SECONDS') {
73 withEnv(env_vars) {
74 while (true) {
75 // get stack state
76 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
77 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
Tomáš Kukráldf540c42017-05-22 15:12:21 +020078
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020079 if (stack_info['StackStatus'] == state) {
80 common.successMsg("Stack ${stack_name} in in state ${state}")
81 common.prettyPrint(stack_info)
82 break
83 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +020084
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020085 // wait for next loop
86 sleep(loop_sleep)
Tomáš Kukráldf540c42017-05-22 15:12:21 +020087 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +020088 }
89 }
90}
91
92def getOutputs(venv_path, env_vars, stack_name, key = '') {
93 def aws = new com.mirantis.mk.Aws()
94 def common = new com.mirantis.mk.Common()
95 def output = {}
96
97 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
98 common.prettyPrint(stack_info)
99
100 for (int i=0; i<stack_info['Outputs'].size(); i++) {
101 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
102 }
103
104 if (key != null && key != '') {
105 return output[key]
106 } else {
107 return output
108 }
109}