blob: 8348612d6ffeb42bca40070df691cc9371e0a9f0 [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
160 */
Tomáš Kukrála1152742017-08-22 16:21:50 +0200161def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") {
iberezovskiyd4240b52017-02-20 17:18:28 +0400162 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200163 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
164 def envFile
165 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400166 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200167 envFile = "${env.WORKSPACE}/template/env/${name}.env"
168 if (environment.contains("/")) {
169 //init() returns all elements but the last in a collection.
170 def envPath = environment.tokenize("/").init().join("/")
171 if (envPath) {
172 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
173 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200174 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200175 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400176 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200177 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400178 envFile = "${env.WORKSPACE}/template/${name}.env"
179 createHeatEnv(envFile, params)
180 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200181
Mykyta Karpincf44f812017-08-28 14:45:21 +0300182 def cmd
183 def waitState
184
Tomáš Kukrála1152742017-08-22 16:21:50 +0200185 if (action == "create") {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300186 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
187 waitState = "CREATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200188 } else {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300189 cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}"
190 waitState = "UPDATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200191 }
192
Sergey Kolekonovba203982016-12-21 18:32:17 +0400193 dir("${env.WORKSPACE}/template/template") {
194 outputTable = runOpenstackCommand(cmd, client, path)
195 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200196
Ales Komareke11e8792016-12-28 09:42:25 +0100197 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400198
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200199 def heatStatusCheckerCount = 1
Tomáš Kukrála1152742017-08-22 16:21:50 +0200200 while (heatStatusCheckerCount <= 125) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400201 status = getHeatStackStatus(client, name, path)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200202 echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100203
Tomáš Kukrála1152742017-08-22 16:21:50 +0200204 if (status.contains('CREATE_FAILED')) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400205 info = getHeatStackInfo(client, name, path)
206 throw new Exception(info.stack_status_reason)
Tomáš Kukrála1152742017-08-22 16:21:50 +0200207
208 } else if (status.contains(waitState)) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400209 info = getHeatStackInfo(client, name, path)
210 echo(info.stack_status_reason)
211 break
212 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200213
214 sleep(20)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200215 heatStatusCheckerCount++
Sergey Kolekonovba203982016-12-21 18:32:17 +0400216 }
217 echo("[Heat Stack] Status: ${status}")
218}
219
220/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200221 * Returns list of stacks for stack name filter
222 *
223 * @param client Connection parameters for OpenStack API endpoint
224 * @param filter Stack name filter
225 * @param path Optional path to the custom virtualenv
226 */
227def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200228 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200229 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
230}
231
232
233/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200234 * Get list of stack names with given stack status
235 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200236 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200237 * @param status Stack status
238 * @param path Optional path to the custom virtualenv
239 */
240 def getStacksWithStatus(client, status, path = null) {
241 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
242 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
243 }
244
245/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400246 * Get life cycle status for 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 getHeatStackStatus(client, name, path = null) {
253 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
254 return runOpenstackCommand(cmd, client, path)
255}
256
257/**
258 * Get info about 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 */
264def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400265 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400266 cmd = "heat stack-show ${name}"
267 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100268 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400269 return output
270}
271
272/**
273 * Get existing OpenStack Heat stack output parameter
274 *
275 * @param env Connection parameters for OpenStack API endpoint
276 * @param name Name of the managed Heat stack
277 * @param parameter Name of the output parameter
278 * @param path Optional path to the custom virtualenv
279 */
280def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000281 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400282 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100283 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300284 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
285 // versions returns string without "".
286 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
287 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400288}
289
290/**
291 * List all resources from existing OpenStack Heat stack
292 *
293 * @param env Connection parameters for OpenStack API endpoint
294 * @param name Name of the managed Heat stack instance
295 * @param path Optional path to the custom virtualenv
296 */
297def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400298 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400299 cmd = "heat resource-list ${name}"
300 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100301 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400302 return output
303}
304
305/**
306 * Get info about resource from existing OpenStack Heat stack
307 *
308 * @param env Connection parameters for OpenStack API endpoint
309 * @param name Name of the managed Heat stack instance
310 * @param path Optional path to the custom virtualenv
311 */
312def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400313 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400314 cmd = "heat resource-show ${name} ${resource}"
315 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100316 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400317 return output
318}
319
320/**
321 * Update existing OpenStack Heat stack
322 *
323 * @param env Connection parameters for OpenStack API endpoint
324 * @param name Name of the managed Heat stack instance
325 * @param path Optional path to the custom virtualenv
326 */
327def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400328 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400329 cmd = "heat stack-update ${name}"
330 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100331 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400332 return output
333}
334
335/**
336 * Delete existing 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 deleteHeatStack(env, name, path = null) {
343 cmd = "heat stack-delete ${name}"
344 outputTable = runOpenstackCommand(cmd, env, path)
345}
346
347/**
348 * Return list of servers from OpenStack Heat stack
349 *
350 * @param env Connection parameters for OpenStack API endpoint
351 * @param name Name of the managed Heat stack instance
352 * @param path Optional path to the custom virtualenv
353 */
354def getHeatStackServers(env, name, path = null) {
355 resources = heatGetStackResources(env, name, path)
356 servers = []
357 for (resource in resources) {
358 if (resource.resource_type == 'OS::Nova::Server') {
359 resourceName = resource.resource_name
360 server = heatGetStackResourceInfo(env, name, resourceName, path)
361 servers.add(server.attributes.name)
362 }
363 }
364 echo("[Stack ${name}] Servers: ${servers}")
365 return servers
366}