blob: 87c11eb1600fcf7efd97fbf5122a5bcbf149a335 [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Openstack functions
6 *
7 */
8
9/**
Tomáš Kukrálc3964e52017-02-22 14:07:37 +010010 * Convert maps
11 *
12 */
13
14@NonCPS def entries(m) {
15 return m.collect {k, v -> [k, v]}
16}
17
18/**
Sergey Kolekonovba203982016-12-21 18:32:17 +040019 * 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álbee0b992017-08-10 16:50:40 +020025def setupOpenstackVirtualenv(path, version = 'latest') {
iberezovskiyd4240b52017-02-20 17:18:28 +040026 def python = new com.mirantis.mk.Python()
Vasyl Saienko030fc182017-07-12 14:54:42 +030027 python.setupDocutilsVirtualenv(path)
Sergey Kolekonovba203982016-12-21 18:32:17 +040028
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 Josefbd927322017-05-30 13:20:27 +000037 'python-openstackclient>=1.7.0,<1.8.0',
Sergey Kolekonovba203982016-12-21 18:32:17 +040038 '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 Josef60280212017-08-10 19:01:19 +020042 'docutils'
Sergey Kolekonovba203982016-12-21 18:32:17 +040043 ]
44
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020045 def openstack_latest_packages = [
46 'python-openstackclient',
47 'python-heatclient',
Jakub Josef60280212017-08-10 19:01:19 +020048 'docutils'
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020049 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040050
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020051 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040052 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020053 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040054 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020055 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040056 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020057 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040058 requirements = openstack_latest_packages
59 }
Tomáš Kukrálbee0b992017-08-10 16:50:40 +020060 python.setupVirtualenv(path, 'python2', requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040061}
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_kushaev0a26bf72017-05-18 13:20:09 +040070def createOpenstackEnv(url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020071 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040072 def common = new com.mirantis.mk.Common()
Ales Komarek0e558ee2016-12-23 13:02:55 +010073 rcFile = "${env.WORKSPACE}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040074 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030075 rc = """set +x
76export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010077export OS_PASSWORD=${creds.password.toString()}
78export OS_TENANT_NAME=${project}
79export OS_AUTH_URL=${url}
80export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040081export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000082export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040083export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000084export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +030085export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020086export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030087set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +010088"""
89 writeFile file: rcFile, text: rc
90 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +040091}
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 */
100def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400101 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400102 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 */
122def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400123 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000124 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400125 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100126 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400127 return output
128}
129
130/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200131 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400132 *
133 * @param env Connection parameters for OpenStack API endpoint
134 * @param path Optional path to the custom virtualenv
135 */
136def createHeatEnv(file, environment = [], original_file = null) {
137 if (original_file) {
138 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100139 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400140 envString = "parameters:\n"
141 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100142
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100143 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100144 for (int i = 0; i < p.size(); i++) {
145 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400146 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100147
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100148 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400149 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 Komarek51b7b152017-06-27 11:14:50 +0200160 * @param legacy_env Use old path format [env_name/template_name] to
161 * target env file.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400162 */
Ales Komarek51b7b152017-06-27 11:14:50 +0200163def createHeatStack(client, name, template, params = [], environment = null, path = null, legacy_env = true) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400164 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400165 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
166 if (environment) {
Ales Komarek51b7b152017-06-27 11:14:50 +0200167 if (legacy_env) {
168 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
169 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
170 }
171 else {
Ales Komarekd6142a82017-06-28 15:22:04 +0200172 envFile = "${env.WORKSPACE}/template/env/${name}.env"
Ales Komarek51b7b152017-06-27 11:14:50 +0200173 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
174 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400175 createHeatEnv(envFile, params, envSource)
176 }
177 else {
178 envFile = "${env.WORKSPACE}/template/${name}.env"
179 createHeatEnv(envFile, params)
180 }
181 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
182 dir("${env.WORKSPACE}/template/template") {
183 outputTable = runOpenstackCommand(cmd, client, path)
184 }
Ales Komareke11e8792016-12-28 09:42:25 +0100185 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400186
187 i = 1
Ales Komarekdce8b472016-12-30 16:56:07 +0100188
Sergey Kolekonovba203982016-12-21 18:32:17 +0400189 while (true) {
190 status = getHeatStackStatus(client, name, path)
191 echo("[Heat Stack] Status: ${status}, Check: ${i}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100192
193 if (status.indexOf('CREATE_FAILED') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400194 info = getHeatStackInfo(client, name, path)
195 throw new Exception(info.stack_status_reason)
196 }
Ales Komarekdce8b472016-12-30 16:56:07 +0100197 else if (status.indexOf('CREATE_COMPLETE') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400198 info = getHeatStackInfo(client, name, path)
199 echo(info.stack_status_reason)
200 break
201 }
202 sh('sleep 5s')
203 i++
204 }
205 echo("[Heat Stack] Status: ${status}")
206}
207
208/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200209 * Returns list of stacks for stack name filter
210 *
211 * @param client Connection parameters for OpenStack API endpoint
212 * @param filter Stack name filter
213 * @param path Optional path to the custom virtualenv
214 */
215def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200216 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200217 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
218}
219
220
221/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200222 * Get list of stack names with given stack status
223 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200224 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200225 * @param status Stack status
226 * @param path Optional path to the custom virtualenv
227 */
228 def getStacksWithStatus(client, status, path = null) {
229 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
230 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
231 }
232
233/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400234 * Get life cycle status for existing OpenStack Heat stack
235 *
236 * @param env Connection parameters for OpenStack API endpoint
237 * @param name Name of the managed Heat stack instance
238 * @param path Optional path to the custom virtualenv
239 */
240def getHeatStackStatus(client, name, path = null) {
241 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
242 return runOpenstackCommand(cmd, client, path)
243}
244
245/**
246 * Get info about existing OpenStack Heat stack
247 *
248 * @param env Connection parameters for OpenStack API endpoint
249 * @param name Name of the managed Heat stack instance
250 * @param path Optional path to the custom virtualenv
251 */
252def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400253 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400254 cmd = "heat stack-show ${name}"
255 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100256 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400257 return output
258}
259
260/**
261 * Get existing OpenStack Heat stack output parameter
262 *
263 * @param env Connection parameters for OpenStack API endpoint
264 * @param name Name of the managed Heat stack
265 * @param parameter Name of the output parameter
266 * @param path Optional path to the custom virtualenv
267 */
268def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000269 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400270 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100271 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300272 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
273 // versions returns string without "".
274 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
275 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400276}
277
278/**
279 * List all resources from existing OpenStack Heat stack
280 *
281 * @param env Connection parameters for OpenStack API endpoint
282 * @param name Name of the managed Heat stack instance
283 * @param path Optional path to the custom virtualenv
284 */
285def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400286 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400287 cmd = "heat resource-list ${name}"
288 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100289 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400290 return output
291}
292
293/**
294 * Get info about resource from existing OpenStack Heat stack
295 *
296 * @param env Connection parameters for OpenStack API endpoint
297 * @param name Name of the managed Heat stack instance
298 * @param path Optional path to the custom virtualenv
299 */
300def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400301 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400302 cmd = "heat resource-show ${name} ${resource}"
303 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100304 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400305 return output
306}
307
308/**
309 * Update existing OpenStack Heat stack
310 *
311 * @param env Connection parameters for OpenStack API endpoint
312 * @param name Name of the managed Heat stack instance
313 * @param path Optional path to the custom virtualenv
314 */
315def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400316 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400317 cmd = "heat stack-update ${name}"
318 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100319 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400320 return output
321}
322
323/**
324 * Delete existing OpenStack Heat stack
325 *
326 * @param env Connection parameters for OpenStack API endpoint
327 * @param name Name of the managed Heat stack instance
328 * @param path Optional path to the custom virtualenv
329 */
330def deleteHeatStack(env, name, path = null) {
331 cmd = "heat stack-delete ${name}"
332 outputTable = runOpenstackCommand(cmd, env, path)
333}
334
335/**
336 * Return list of servers from OpenStack Heat stack
337 *
338 * @param env Connection parameters for OpenStack API endpoint
339 * @param name Name of the managed Heat stack instance
340 * @param path Optional path to the custom virtualenv
341 */
342def getHeatStackServers(env, name, path = null) {
343 resources = heatGetStackResources(env, name, path)
344 servers = []
345 for (resource in resources) {
346 if (resource.resource_type == 'OS::Nova::Server') {
347 resourceName = resource.resource_name
348 server = heatGetStackResourceInfo(env, name, resourceName, path)
349 servers.add(server.attributes.name)
350 }
351 }
352 echo("[Stack ${name}] Servers: ${servers}")
353 return servers
354}