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 | |
Tomáš Kukrál | 36a2dcd | 2017-09-07 10:02:28 +0200 | [diff] [blame] | 19 | def getEnvVars(credentials, region = 'us-west-2') { |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 20 | def common = new com.mirantis.mk.Common() |
| 21 | |
Tomáš Kukrál | 36a2dcd | 2017-09-07 10:02:28 +0200 | [diff] [blame] | 22 | def creds |
| 23 | def username |
| 24 | def password |
| 25 | |
| 26 | if (credentials.contains(':')) { |
| 27 | // we have key and secret in string (delimited by :) |
Tomáš Kukrál | e364fe1 | 2017-09-07 10:16:13 +0200 | [diff] [blame^] | 28 | creds = credentials.tokenize(':') |
Tomáš Kukrál | 36a2dcd | 2017-09-07 10:02:28 +0200 | [diff] [blame] | 29 | username = creds[0] |
| 30 | password = creds[0] |
| 31 | } else { |
| 32 | // we have creadentials_id |
| 33 | creds = common.getCredentials(credentials) |
| 34 | username = creds.username |
| 35 | password = creds.password |
| 36 | } |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 37 | |
| 38 | return [ |
Tomáš Kukrál | 36a2dcd | 2017-09-07 10:02:28 +0200 | [diff] [blame] | 39 | "AWS_ACCESS_KEY_ID=${username}", |
| 40 | "AWS_SECRET_ACCESS_KEY=${password}", |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 41 | "AWS_DEFAULT_REGION=${region}" |
| 42 | ] |
| 43 | } |
| 44 | |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 48 | * CloudFormation stacks (cloudformation) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 49 | * |
| 50 | */ |
| 51 | |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 52 | def createStack(venv_path, env_vars, template_file, stack_name, parameters = []) { |
| 53 | def python = new com.mirantis.mk.Python() |
| 54 | |
Tomáš Kukrál | 2a0f88d | 2017-09-05 11:12:33 +0200 | [diff] [blame] | 55 | def cmd = "aws cloudformation create-stack --stack-name ${stack_name} --template-body file://template/${template_file} --capabilities CAPABILITY_NAMED_IAM" |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 56 | |
| 57 | if (parameters != null && parameters.size() > 0) { |
| 58 | cmd = "${cmd} --parameters" |
| 59 | |
| 60 | for (int i=0; i<parameters.size(); i++) { |
| 61 | cmd = "${cmd} ${parameters[i]}" |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | withEnv(env_vars) { |
| 66 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 67 | } |
| 68 | } |
| 69 | |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 70 | def deleteStack(venv_path, env_vars, stack_name) { |
| 71 | def python = new com.mirantis.mk.Python() |
| 72 | |
| 73 | def cmd = "aws cloudformation delete-stack --stack-name ${stack_name}" |
| 74 | |
| 75 | withEnv(env_vars) { |
| 76 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 77 | } |
| 78 | } |
| 79 | |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 80 | def describeStack(venv_path, env_vars, stack_name) { |
| 81 | def python = new com.mirantis.mk.Python() |
| 82 | def common = new com.mirantis.mk.Common() |
| 83 | |
| 84 | def cmd = "aws cloudformation describe-stacks --stack-name ${stack_name}" |
| 85 | |
| 86 | withEnv(env_vars) { |
| 87 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 88 | def out_json = common.parseJSON(out) |
Tomáš Kukrál | 3db2105 | 2017-08-29 09:55:13 +0200 | [diff] [blame] | 89 | def resources = out_json['Stacks'][0] |
| 90 | common.prettyPrint(resources) |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 91 | |
Tomáš Kukrál | 3db2105 | 2017-08-29 09:55:13 +0200 | [diff] [blame] | 92 | return resources |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | def describeStackResources(venv_path, env_vars, stack_name) { |
| 97 | def python = new com.mirantis.mk.Python() |
| 98 | def common = new com.mirantis.mk.Common() |
| 99 | |
| 100 | def cmd = "aws cloudformation describe-stack-resources --stack-name ${stack_name}" |
| 101 | |
| 102 | withEnv(env_vars) { |
| 103 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 104 | def out_json = common.parseJSON(out) |
| 105 | def resources = out_json['StackResources'] |
| 106 | common.prettyPrint(resources) |
| 107 | |
| 108 | return resources |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Tomáš Kukrál | 024e1c0 | 2017-06-02 13:24:44 +0200 | [diff] [blame] | 112 | 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] | 113 | def aws = new com.mirantis.mk.Aws() |
| 114 | def common = new com.mirantis.mk.Common() |
| 115 | def python = new com.mirantis.mk.Python() |
| 116 | |
Tomáš Kukrál | 735d207 | 2017-05-25 16:17:33 +0200 | [diff] [blame] | 117 | timeout(time: max_timeout, unit: 'SECONDS') { |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 118 | withEnv(env_vars) { |
| 119 | while (true) { |
| 120 | // get stack state |
| 121 | def stack_info = aws.describeStack(venv_path, env_vars, stack_name) |
| 122 | common.infoMsg('Stack status is ' + stack_info['StackStatus']) |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 123 | |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 124 | // check for desired state |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 125 | if (stack_info['StackStatus'] == state) { |
| 126 | common.successMsg("Stack ${stack_name} in in state ${state}") |
| 127 | common.prettyPrint(stack_info) |
| 128 | break |
| 129 | } |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 130 | |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 131 | // check for failed state |
Tomáš Kukrál | 47d1f23 | 2017-06-01 15:37:41 +0200 | [diff] [blame] | 132 | if (state_failed.contains(stack_info['StackStatus'])) { |
Tomáš Kukrál | fa58cf6 | 2017-06-01 11:09:48 +0200 | [diff] [blame] | 133 | throw new Exception("Stack ${stack_name} in in failed state") |
| 134 | } |
| 135 | |
Tomáš Kukrál | 3db2105 | 2017-08-29 09:55:13 +0200 | [diff] [blame] | 136 | // print stack resources |
| 137 | aws.describeStackResources(venv_path, env_vars, stack_name) |
| 138 | |
Tomáš Kukrál | 55e1dea | 2017-05-25 16:12:42 +0200 | [diff] [blame] | 139 | // wait for next loop |
| 140 | sleep(loop_sleep) |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 141 | } |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | def getOutputs(venv_path, env_vars, stack_name, key = '') { |
| 147 | def aws = new com.mirantis.mk.Aws() |
| 148 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | 81aeabd | 2017-09-03 10:10:39 +0200 | [diff] [blame] | 149 | def output = [:] |
Tomáš Kukrál | df540c4 | 2017-05-22 15:12:21 +0200 | [diff] [blame] | 150 | |
| 151 | def stack_info = aws.describeStack(venv_path, env_vars, stack_name) |
| 152 | common.prettyPrint(stack_info) |
| 153 | |
| 154 | for (int i=0; i<stack_info['Outputs'].size(); i++) { |
| 155 | output[stack_info['Outputs'][i]['OutputKey']] = stack_info['Outputs'][i]['OutputValue'] |
| 156 | } |
| 157 | |
| 158 | if (key != null && key != '') { |
| 159 | return output[key] |
| 160 | } else { |
| 161 | return output |
| 162 | } |
| 163 | } |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 167 | * Autoscaling groups (autoscaling) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 168 | * |
| 169 | */ |
| 170 | |
| 171 | def describeAutoscalingGroup(venv_path, env_vars, group_name) { |
| 172 | def python = new com.mirantis.mk.Python() |
| 173 | def common = new com.mirantis.mk.Common() |
| 174 | |
| 175 | def cmd = "aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ${group_name}" |
| 176 | |
| 177 | withEnv(env_vars) { |
| 178 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 179 | def out_json = common.parseJSON(out) |
| 180 | def info = out_json['AutoScalingGroups'][0] |
| 181 | common.prettyPrint(info) |
| 182 | |
| 183 | return info |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | def updateAutoscalingGroup(venv_path, env_vars, group_name, parameters = []) { |
| 188 | def python = new com.mirantis.mk.Python() |
| 189 | def common = new com.mirantis.mk.Common() |
| 190 | |
| 191 | if (parameters == null || parameters.size() == 0) { |
| 192 | throw new Exception("Missing parameter") |
| 193 | } |
| 194 | |
Tomáš Kukrál | eb154a3 | 2017-06-06 13:44:47 +0200 | [diff] [blame] | 195 | 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] | 196 | |
| 197 | withEnv(env_vars) { |
| 198 | def out = python.runVirtualenvCommand(venv_path, cmd) |
Tomáš Kukrál | 5c969ca | 2017-06-05 16:29:19 +0200 | [diff] [blame] | 199 | return out |
| 200 | } |
| 201 | } |
| 202 | |
Tomáš Kukrál | aa8ad83 | 2017-06-09 11:52:56 +0200 | [diff] [blame] | 203 | def waitForAutoscalingInstances(venv_path, env_vars, group_name, max_timeout = 600, loop_sleep = 20) { |
| 204 | def aws = new com.mirantis.mk.Aws() |
Tomáš Kukrál | 4c5e5e9 | 2017-06-11 13:35:07 +0200 | [diff] [blame] | 205 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | aa8ad83 | 2017-06-09 11:52:56 +0200 | [diff] [blame] | 206 | |
| 207 | timeout(time: max_timeout, unit: 'SECONDS') { |
| 208 | withEnv(env_vars) { |
| 209 | while (true) { |
| 210 | // get instances in autoscaling group |
| 211 | def out = aws.describeAutoscalingGroup(venv_path, env_vars, group_name) |
Tomáš Kukrál | edca0c2 | 2017-06-13 14:12:00 +0200 | [diff] [blame] | 212 | print(common.prettyPrint(out)) |
Tomáš Kukrál | c6cb4ff | 2017-06-13 14:35:04 +0200 | [diff] [blame] | 213 | def instances = out['Instances'] |
Tomáš Kukrál | aa8ad83 | 2017-06-09 11:52:56 +0200 | [diff] [blame] | 214 | |
| 215 | // check all instances are InService |
Tomáš Kukrál | 5dd1207 | 2017-06-13 15:54:44 +0200 | [diff] [blame] | 216 | if (common.countHashMapEquals(instances, 'LifecycleState', 'InService') == out['DesiredCapacity']) { |
Tomáš Kukrál | aa8ad83 | 2017-06-09 11:52:56 +0200 | [diff] [blame] | 217 | break |
| 218 | } |
| 219 | |
| 220 | // wait for next loop |
| 221 | sleep(loop_sleep) |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 227 | /** |
| 228 | * |
| 229 | * Load balancers (elb) |
| 230 | * |
| 231 | */ |
| 232 | |
Tomáš Kukrál | 692c977 | 2017-06-06 16:28:38 +0200 | [diff] [blame] | 233 | def registerIntanceWithLb(venv_path, env_vars, lb, instances = []) { |
| 234 | def python = new com.mirantis.mk.Python() |
| 235 | |
| 236 | def cmd = "aws elb register-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ') |
| 237 | |
| 238 | withEnv(env_vars) { |
| 239 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 240 | return out |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | def deregisterIntanceWithLb(venv_path, env_vars, lb, instances = []) { |
| 245 | def python = new com.mirantis.mk.Python() |
| 246 | |
| 247 | def cmd = "aws elb deregister-instances-with-load-balancer --load-balancer-name ${lb} --instances " + instances.join(' ') |
| 248 | |
| 249 | withEnv(env_vars) { |
| 250 | def out = python.runVirtualenvCommand(venv_path, cmd) |
| 251 | return out |
| 252 | } |
| 253 | } |