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