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