blob: 113033d1fc1e0000d9c87aa0be216f43d26a6052 [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
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020051def deleteStack(venv_path, env_vars, stack_name) {
52 def python = new com.mirantis.mk.Python()
53
54 def cmd = "aws cloudformation delete-stack --stack-name ${stack_name}"
55
56 withEnv(env_vars) {
57 def out = python.runVirtualenvCommand(venv_path, cmd)
58 }
59}
60
Tomáš Kukráldf540c42017-05-22 15:12:21 +020061def describeStack(venv_path, env_vars, stack_name) {
62 def python = new com.mirantis.mk.Python()
63 def common = new com.mirantis.mk.Common()
64
65 def cmd = "aws cloudformation describe-stacks --stack-name ${stack_name}"
66
67 withEnv(env_vars) {
68 def out = python.runVirtualenvCommand(venv_path, cmd)
69 def out_json = common.parseJSON(out)
70 def stack_info = out_json['Stacks'][0]
71 common.prettyPrint(stack_info)
72
73 return stack_info
74 }
75}
76
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020077def waitForStatus(venv_path, env_vars, stack_name, state, state_failed = [], max_timeout = 600, loop_sleep = 30) {
Tomáš Kukráldf540c42017-05-22 15:12:21 +020078 def aws = new com.mirantis.mk.Aws()
79 def common = new com.mirantis.mk.Common()
80 def python = new com.mirantis.mk.Python()
81
Tomáš Kukrál735d2072017-05-25 16:17:33 +020082 timeout(time: max_timeout, unit: 'SECONDS') {
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020083 withEnv(env_vars) {
84 while (true) {
85 // get stack state
86 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
87 common.infoMsg('Stack status is ' + stack_info['StackStatus'])
Tomáš Kukráldf540c42017-05-22 15:12:21 +020088
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020089 // check for desired state
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +020090 if (stack_info['StackStatus'] == state) {
91 common.successMsg("Stack ${stack_name} in in state ${state}")
92 common.prettyPrint(stack_info)
93 break
94 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +020095
Tomáš Kukrálfa58cf62017-06-01 11:09:48 +020096 // check for failed state
97 if (stack_failed.contains(stack_info['StackStatus'])) {
98 throw new Exception("Stack ${stack_name} in in failed state")
99 }
100
Tomáš Kukrál55e1dea2017-05-25 16:12:42 +0200101 // wait for next loop
102 sleep(loop_sleep)
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200103 }
Tomáš Kukráldf540c42017-05-22 15:12:21 +0200104 }
105 }
106}
107
108def getOutputs(venv_path, env_vars, stack_name, key = '') {
109 def aws = new com.mirantis.mk.Aws()
110 def common = new com.mirantis.mk.Common()
111 def output = {}
112
113 def stack_info = aws.describeStack(venv_path, env_vars, stack_name)
114 common.prettyPrint(stack_info)
115
116 for (int i=0; i<stack_info['Outputs'].size(); i++) {
117 output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue']
118 }
119
120 if (key != null && key != '') {
121 return output[key]
122 } else {
123 return output
124 }
125}