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