Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 3 | /** |
| 4 | * |
| 5 | * AWS function functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | def 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 | |
| 19 | def 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ál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 34 | * CloudFormation stacks (cloudformation) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 35 | * |
| 36 | */ |
| 37 | |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 38 | def 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ál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 56 | def 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ál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 66 | def 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ál | 024e1c0 | 2017-06-02 13:24:44 +0200 | [diff] [blame] | 82 | def waitForStatus(venv_path, env_vars, stack_name, state, state_failed = [], max_timeout = 1200, loop_sleep = 30) { |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 83 | 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ál | 735d207 | 2017-05-25 16:17:33 +0200 | [diff] [blame] | 87 | timeout(time: max_timeout, unit: 'SECONDS') { |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 88 | 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ál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 93 | |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 94 | // check for desired state |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 95 | 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ál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 100 | |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 101 | // check for failed state |
Tomáš Kukrál | 47d1f23 | 2017-06-01 15:37:41 +0200 | [diff] [blame] | 102 | if (state_failed.contains(stack_info['StackStatus'])) { |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 103 | throw new Exception("Stack ${stack_name} in in failed state") |
| 104 | } |
| 105 | |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 106 | // wait for next loop |
| 107 | sleep(loop_sleep) |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 108 | } |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | def 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ál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 134 | * Autoscaling groups (autoscaling) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 135 | * |
| 136 | */ |
| 137 | |
| 138 | def 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 | |
| 154 | def 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ál | eb154a3 | 2017-06-06 13:44:47 +0200 | [diff] [blame] | 162 | def cmd = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${group_name} " + parameters.join(' ') |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 163 | |
| 164 | withEnv(env_vars) { |
| 165 | def out = python.runVirtualenvCommand(venv_path, cmd) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 166 | return out |
| 167 | } |
| 168 | } |
| 169 | |
Tomáš Kukrál | aa8ad83 | 2017-06-09 11:52:56 +0200 | [diff] [blame^] | 170 | def 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ál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 193 | /** |
| 194 | * |
| 195 | * Load balancers (elb) |
| 196 | * |
| 197 | */ |
| 198 | |
| 199 | |
| 200 | def 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 | |
| 211 | def 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ál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 224 | |