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