Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Openstack functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
Tomáš Kukrál | c3964e5 | 2017-02-22 14:07:37 +0100 | [diff] [blame] | 10 | * Convert maps |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | @NonCPS def entries(m) { |
| 15 | return m.collect {k, v -> [k, v]} |
| 16 | } |
| 17 | |
| 18 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 19 | * Install OpenStack service clients in isolated environment |
| 20 | * |
| 21 | * @param path Path where virtualenv is created |
| 22 | * @param version Version of the OpenStack clients |
| 23 | */ |
| 24 | |
Tomáš Kukrál | bee0b99 | 2017-08-10 16:50:40 +0200 | [diff] [blame] | 25 | def setupOpenstackVirtualenv(path, version = 'latest') { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 26 | def python = new com.mirantis.mk.Python() |
Vasyl Saienko | 030fc18 | 2017-07-12 14:54:42 +0300 | [diff] [blame] | 27 | python.setupDocutilsVirtualenv(path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 28 | |
| 29 | def openstack_kilo_packages = [ |
| 30 | 'python-cinderclient>=1.3.1,<1.4.0', |
| 31 | 'python-glanceclient>=0.19.0,<0.20.0', |
| 32 | 'python-heatclient>=0.6.0,<0.7.0', |
| 33 | 'python-keystoneclient>=1.6.0,<1.7.0', |
| 34 | 'python-neutronclient>=2.2.6,<2.3.0', |
| 35 | 'python-novaclient>=2.19.0,<2.20.0', |
| 36 | 'python-swiftclient>=2.5.0,<2.6.0', |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 37 | 'python-openstackclient>=1.7.0,<1.8.0', |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 38 | 'oslo.config>=2.2.0,<2.3.0', |
| 39 | 'oslo.i18n>=2.3.0,<2.4.0', |
| 40 | 'oslo.serialization>=1.8.0,<1.9.0', |
| 41 | 'oslo.utils>=1.4.0,<1.5.0', |
Jakub Josef | 6028021 | 2017-08-10 19:01:19 +0200 | [diff] [blame] | 42 | 'docutils' |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 43 | ] |
| 44 | |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 45 | def openstack_latest_packages = [ |
| 46 | 'python-openstackclient', |
| 47 | 'python-heatclient', |
Jakub Josef | 6028021 | 2017-08-10 19:01:19 +0200 | [diff] [blame] | 48 | 'docutils' |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 49 | ] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 50 | |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 51 | if (version == 'kilo') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 52 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 53 | } else if (version == 'liberty') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 54 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 55 | } else if (version == 'mitaka') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 56 | requirements = openstack_kilo_packages |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 57 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 58 | requirements = openstack_latest_packages |
| 59 | } |
Tomáš Kukrál | bee0b99 | 2017-08-10 16:50:40 +0200 | [diff] [blame] | 60 | python.setupVirtualenv(path, 'python2', requirements, null, true) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * create connection to OpenStack API endpoint |
| 65 | * |
| 66 | * @param url OpenStack API endpoint address |
| 67 | * @param credentialsId Credentials to the OpenStack API |
| 68 | * @param project OpenStack project to connect to |
| 69 | */ |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 70 | def createOpenstackEnv(url, credentialsId, project, project_domain="default", |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 71 | project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 72 | def common = new com.mirantis.mk.Common() |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 73 | rcFile = "${env.WORKSPACE}/keystonerc" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 74 | creds = common.getPasswordCredentials(credentialsId) |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 75 | rc = """set +x |
| 76 | export OS_USERNAME=${creds.username} |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 77 | export OS_PASSWORD=${creds.password.toString()} |
| 78 | export OS_TENANT_NAME=${project} |
| 79 | export OS_AUTH_URL=${url} |
| 80 | export OS_AUTH_STRATEGY=keystone |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 81 | export OS_PROJECT_NAME=${project} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 82 | export OS_PROJECT_ID=${project_id} |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 83 | export OS_PROJECT_DOMAIN_ID=${project_domain} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 84 | export OS_USER_DOMAIN_NAME=${user_domain} |
Kirill Mashchenko | 234708f | 2017-07-20 17:00:01 +0300 | [diff] [blame] | 85 | export OS_IDENTITY_API_VERSION=${api_ver} |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 86 | export OS_CACERT=${cacert} |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 87 | set -x |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 88 | """ |
| 89 | writeFile file: rcFile, text: rc |
| 90 | return rcFile |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Run command with OpenStack env params and optional python env |
| 95 | * |
| 96 | * @param cmd Command to be executed |
| 97 | * @param env Environmental parameters with endpoint credentials |
| 98 | * @param path Optional path to virtualenv with specific clients |
| 99 | */ |
| 100 | def runOpenstackCommand(cmd, venv, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 101 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 102 | openstackCmd = ". ${venv}; ${cmd}" |
| 103 | if (path) { |
| 104 | output = python.runVirtualenvCommand(path, openstackCmd) |
| 105 | } |
| 106 | else { |
| 107 | echo("[Command]: ${openstackCmd}") |
| 108 | output = sh ( |
| 109 | script: openstackCmd, |
| 110 | returnStdout: true |
| 111 | ).trim() |
| 112 | } |
| 113 | return output |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get OpenStack Keystone token for current credentials |
| 118 | * |
| 119 | * @param env Connection parameters for OpenStack API endpoint |
| 120 | * @param path Optional path to the custom virtualenv |
| 121 | */ |
| 122 | def getKeystoneToken(client, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 123 | def python = new com.mirantis.mk.Python() |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 124 | cmd = "openstack token issue" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 125 | outputTable = runOpenstackCommand(cmd, client, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 126 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 127 | return output |
| 128 | } |
| 129 | |
| 130 | /** |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 131 | * Create OpenStack environment file |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 132 | * |
| 133 | * @param env Connection parameters for OpenStack API endpoint |
| 134 | * @param path Optional path to the custom virtualenv |
| 135 | */ |
| 136 | def createHeatEnv(file, environment = [], original_file = null) { |
| 137 | if (original_file) { |
| 138 | envString = readFile file: original_file |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 139 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 140 | envString = "parameters:\n" |
| 141 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 142 | |
Tomáš Kukrál | c3964e5 | 2017-02-22 14:07:37 +0100 | [diff] [blame] | 143 | p = entries(environment) |
Tomáš Kukrál | b1fe964 | 2017-02-22 11:21:17 +0100 | [diff] [blame] | 144 | for (int i = 0; i < p.size(); i++) { |
| 145 | envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 146 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 147 | |
Tomáš Kukrál | e19ddea | 2017-02-21 11:09:40 +0100 | [diff] [blame] | 148 | echo("writing to env file:\n${envString}") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 149 | writeFile file: file, text: envString |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Create new OpenStack Heat stack |
| 154 | * |
| 155 | * @param env Connection parameters for OpenStack API endpoint |
| 156 | * @param template HOT template for the new Heat stack |
| 157 | * @param environment Environmentale parameters of the new Heat stack |
| 158 | * @param name Name of the new Heat stack |
| 159 | * @param path Optional path to the custom virtualenv |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 160 | * @param legacy_env Use old path format [env_name/template_name] to |
| 161 | * target env file. |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 162 | */ |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 163 | def createHeatStack(client, name, template, params = [], environment = null, path = null, legacy_env = true) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 164 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 165 | templateFile = "${env.WORKSPACE}/template/template/${template}.hot" |
| 166 | if (environment) { |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 167 | if (legacy_env) { |
| 168 | envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env" |
| 169 | envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env" |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame^] | 170 | } else { |
| 171 | if(environment.contains("/")){ |
| 172 | //init() returns all elements but the last in a collection. |
| 173 | def envPath = env.tokenize("/").init().join("/") |
| 174 | if(envPath){ |
| 175 | name = envPath + "/" + name |
| 176 | } |
| 177 | } |
Ales Komarek | d6142a8 | 2017-06-28 15:22:04 +0200 | [diff] [blame] | 178 | envFile = "${env.WORKSPACE}/template/env/${name}.env" |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 179 | envSource = "${env.WORKSPACE}/template/env/${environment}.env" |
| 180 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 181 | createHeatEnv(envFile, params, envSource) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame^] | 182 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 183 | envFile = "${env.WORKSPACE}/template/${name}.env" |
| 184 | createHeatEnv(envFile, params) |
| 185 | } |
| 186 | cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}" |
| 187 | dir("${env.WORKSPACE}/template/template") { |
| 188 | outputTable = runOpenstackCommand(cmd, client, path) |
| 189 | } |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 190 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 191 | |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame^] | 192 | def heatStatusCheckerCount = 1 |
| 193 | while (heatStatusCheckerCount <= 500) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 194 | status = getHeatStackStatus(client, name, path) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame^] | 195 | echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}") |
Ales Komarek | dce8b47 | 2016-12-30 16:56:07 +0100 | [diff] [blame] | 196 | |
| 197 | if (status.indexOf('CREATE_FAILED') != -1) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 198 | info = getHeatStackInfo(client, name, path) |
| 199 | throw new Exception(info.stack_status_reason) |
| 200 | } |
Ales Komarek | dce8b47 | 2016-12-30 16:56:07 +0100 | [diff] [blame] | 201 | else if (status.indexOf('CREATE_COMPLETE') != -1) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 202 | info = getHeatStackInfo(client, name, path) |
| 203 | echo(info.stack_status_reason) |
| 204 | break |
| 205 | } |
| 206 | sh('sleep 5s') |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame^] | 207 | heatStatusCheckerCount++ |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 208 | } |
| 209 | echo("[Heat Stack] Status: ${status}") |
| 210 | } |
| 211 | |
| 212 | /** |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 213 | * Returns list of stacks for stack name filter |
| 214 | * |
| 215 | * @param client Connection parameters for OpenStack API endpoint |
| 216 | * @param filter Stack name filter |
| 217 | * @param path Optional path to the custom virtualenv |
| 218 | */ |
| 219 | def getStacksForNameContains(client, filter, path = null){ |
Jakub Josef | 6465fca | 2017-05-10 16:09:20 +0200 | [diff] [blame] | 220 | cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true' |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 221 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 226 | * Get list of stack names with given stack status |
| 227 | * |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 228 | * @param client Connection parameters for OpenStack API endpoint |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 229 | * @param status Stack status |
| 230 | * @param path Optional path to the custom virtualenv |
| 231 | */ |
| 232 | def getStacksWithStatus(client, status, path = null) { |
| 233 | cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\'' |
| 234 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 235 | } |
| 236 | |
| 237 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 238 | * Get life cycle status for existing OpenStack Heat stack |
| 239 | * |
| 240 | * @param env Connection parameters for OpenStack API endpoint |
| 241 | * @param name Name of the managed Heat stack instance |
| 242 | * @param path Optional path to the custom virtualenv |
| 243 | */ |
| 244 | def getHeatStackStatus(client, name, path = null) { |
| 245 | cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\'' |
| 246 | return runOpenstackCommand(cmd, client, path) |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get info about existing OpenStack Heat stack |
| 251 | * |
| 252 | * @param env Connection parameters for OpenStack API endpoint |
| 253 | * @param name Name of the managed Heat stack instance |
| 254 | * @param path Optional path to the custom virtualenv |
| 255 | */ |
| 256 | def getHeatStackInfo(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 257 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 258 | cmd = "heat stack-show ${name}" |
| 259 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 260 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 261 | return output |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get existing OpenStack Heat stack output parameter |
| 266 | * |
| 267 | * @param env Connection parameters for OpenStack API endpoint |
| 268 | * @param name Name of the managed Heat stack |
| 269 | * @param parameter Name of the output parameter |
| 270 | * @param path Optional path to the custom virtualenv |
| 271 | */ |
| 272 | def getHeatStackOutputParam(env, name, outputParam, path = null) { |
Vasyl Saienko | ea4b281 | 2017-07-10 10:36:03 +0000 | [diff] [blame] | 273 | cmd = "heat output-show ${name} ${outputParam}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 274 | output = runOpenstackCommand(cmd, env, path) |
Ales Komarek | eedc222 | 2017-01-03 10:10:03 +0100 | [diff] [blame] | 275 | echo("${cmd}: ${output}") |
Vasyl Saienko | 2a1c2de | 2017-07-11 11:41:53 +0300 | [diff] [blame] | 276 | // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later |
| 277 | // versions returns string without "". |
| 278 | // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient |
| 279 | return "${output}".replaceAll('"', '') |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * List all resources from existing OpenStack Heat stack |
| 284 | * |
| 285 | * @param env Connection parameters for OpenStack API endpoint |
| 286 | * @param name Name of the managed Heat stack instance |
| 287 | * @param path Optional path to the custom virtualenv |
| 288 | */ |
| 289 | def getHeatStackResources(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 290 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 291 | cmd = "heat resource-list ${name}" |
| 292 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 293 | output = python.parseTextTable(outputTable, 'list', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 294 | return output |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get info about resource from existing OpenStack Heat stack |
| 299 | * |
| 300 | * @param env Connection parameters for OpenStack API endpoint |
| 301 | * @param name Name of the managed Heat stack instance |
| 302 | * @param path Optional path to the custom virtualenv |
| 303 | */ |
| 304 | def getHeatStackResourceInfo(env, name, resource, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 305 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 306 | cmd = "heat resource-show ${name} ${resource}" |
| 307 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 308 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 309 | return output |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Update existing OpenStack Heat stack |
| 314 | * |
| 315 | * @param env Connection parameters for OpenStack API endpoint |
| 316 | * @param name Name of the managed Heat stack instance |
| 317 | * @param path Optional path to the custom virtualenv |
| 318 | */ |
| 319 | def updateHeatStack(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 320 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 321 | cmd = "heat stack-update ${name}" |
| 322 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 323 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 324 | return output |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Delete existing OpenStack Heat stack |
| 329 | * |
| 330 | * @param env Connection parameters for OpenStack API endpoint |
| 331 | * @param name Name of the managed Heat stack instance |
| 332 | * @param path Optional path to the custom virtualenv |
| 333 | */ |
| 334 | def deleteHeatStack(env, name, path = null) { |
| 335 | cmd = "heat stack-delete ${name}" |
| 336 | outputTable = runOpenstackCommand(cmd, env, path) |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Return list of servers from OpenStack Heat stack |
| 341 | * |
| 342 | * @param env Connection parameters for OpenStack API endpoint |
| 343 | * @param name Name of the managed Heat stack instance |
| 344 | * @param path Optional path to the custom virtualenv |
| 345 | */ |
| 346 | def getHeatStackServers(env, name, path = null) { |
| 347 | resources = heatGetStackResources(env, name, path) |
| 348 | servers = [] |
| 349 | for (resource in resources) { |
| 350 | if (resource.resource_type == 'OS::Nova::Server') { |
| 351 | resourceName = resource.resource_name |
| 352 | server = heatGetStackResourceInfo(env, name, resourceName, path) |
| 353 | servers.add(server.attributes.name) |
| 354 | } |
| 355 | } |
| 356 | echo("[Stack ${name}] Servers: ${servers}") |
| 357 | return servers |
| 358 | } |