blob: c6d73a7a440af1e7874f793db7accd345a1092b4 [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
72 timeout = timeout * 1000
73 Date date = new Date()
74 def time_start = date.getTime() // in seconds
75
76 withEnv(env_vars) {
77 while (true) {
78 // get stack state
79 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
80 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
81
82 if (stack_info['StackStatus'] == state) {
83 common.successMsg("Stack ${stack_name} in in state ${state}")
84 common.prettyPrint(stack_info)
85 break
86 }
87
88 // check for timeout
89 if (time_start + timeout < date.getTime()) {
90 throw new Exception("Timeout while waiting for state ${state} for stack ${stack}")
91 }
92
93 // wait for next loop
94 sleep(loop_sleep)
95 }
96 }
97}
98
99def getOutputs(venv_path, env_vars, stack_name, key = '') {
100 def aws = new com.mirantis.mk.Aws()
101 def common = new com.mirantis.mk.Common()
102 def output = {}
103
104 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
105 common.prettyPrint(stack_info)
106
107 for (int i=0; i<stack_info['Outputs'].size(); i++) {
108 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
109 }
110
111 if (key != null && key != '') {
112 return output[key]
113 } else {
114 return output
115 }
116}