blob: 95ebc93499eaa92167f22391243c9acc2ecce465 [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
25def setupOpenstackVirtualenv(path, version = 'kilo'){
iberezovskiyd4240b52017-02-20 17:18:28 +040026 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +040027
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',
Jakub Josefbd927322017-05-30 13:20:27 +000036 'python-openstackclient>=1.7.0,<1.8.0',
Sergey Kolekonovba203982016-12-21 18:32:17 +040037 'oslo.config>=2.2.0,<2.3.0',
38 'oslo.i18n>=2.3.0,<2.4.0',
39 'oslo.serialization>=1.8.0,<1.9.0',
40 'oslo.utils>=1.4.0,<1.5.0',
Ales Komarekd874d482016-12-26 10:33:29 +010041 'docutils>=0.12'
Sergey Kolekonovba203982016-12-21 18:32:17 +040042 ]
43
44 def openstack_latest_packages = openstack_kilo_packages
45
46 if(version == 'kilo') {
47 requirements = openstack_kilo_packages
48 }
49 else if(version == 'liberty') {
50 requirements = openstack_kilo_packages
51 }
52 else if(version == 'mitaka') {
53 requirements = openstack_kilo_packages
54 }
55 else {
56 requirements = openstack_latest_packages
57 }
58 python.setupVirtualenv(path, 'python2', requirements)
59}
60
61/**
62 * create connection to OpenStack API endpoint
63 *
64 * @param url OpenStack API endpoint address
65 * @param credentialsId Credentials to the OpenStack API
66 * @param project OpenStack project to connect to
67 */
kairat_kushaev0a26bf72017-05-18 13:20:09 +040068def createOpenstackEnv(url, credentialsId, project, project_domain="default",
Jakub Josefbd927322017-05-30 13:20:27 +000069 project_id="", user_domain="default", api_ver="2") {
iberezovskiyd4240b52017-02-20 17:18:28 +040070 def common = new com.mirantis.mk.Common()
Ales Komarek0e558ee2016-12-23 13:02:55 +010071 rcFile = "${env.WORKSPACE}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040072 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030073 rc = """set +x
74export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010075export OS_PASSWORD=${creds.password.toString()}
76export OS_TENANT_NAME=${project}
77export OS_AUTH_URL=${url}
78export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040079export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000080export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040081export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000082export OS_USER_DOMAIN_NAME=${user_domain}
83export OS_API_IDENTITY_VERSION=${api_ver}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030084set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +010085"""
86 writeFile file: rcFile, text: rc
87 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +040088}
89
90/**
91 * Run command with OpenStack env params and optional python env
92 *
93 * @param cmd Command to be executed
94 * @param env Environmental parameters with endpoint credentials
95 * @param path Optional path to virtualenv with specific clients
96 */
97def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +040098 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +040099 openstackCmd = ". ${venv}; ${cmd}"
100 if (path) {
101 output = python.runVirtualenvCommand(path, openstackCmd)
102 }
103 else {
104 echo("[Command]: ${openstackCmd}")
105 output = sh (
106 script: openstackCmd,
107 returnStdout: true
108 ).trim()
109 }
110 return output
111}
112
113/**
114 * Get OpenStack Keystone token for current credentials
115 *
116 * @param env Connection parameters for OpenStack API endpoint
117 * @param path Optional path to the custom virtualenv
118 */
119def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400120 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000121 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400122 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100123 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400124 return output
125}
126
127/**
128 * Get OpenStack Keystone token for current credentials
129 *
130 * @param env Connection parameters for OpenStack API endpoint
131 * @param path Optional path to the custom virtualenv
132 */
133def createHeatEnv(file, environment = [], original_file = null) {
134 if (original_file) {
135 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100136 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400137 envString = "parameters:\n"
138 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100139
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100140 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100141 for (int i = 0; i < p.size(); i++) {
142 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400143 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100144
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100145 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400146 writeFile file: file, text: envString
147}
148
149/**
150 * Create new OpenStack Heat stack
151 *
152 * @param env Connection parameters for OpenStack API endpoint
153 * @param template HOT template for the new Heat stack
154 * @param environment Environmentale parameters of the new Heat stack
155 * @param name Name of the new Heat stack
156 * @param path Optional path to the custom virtualenv
157 */
158def createHeatStack(client, name, template, params = [], environment = null, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400159 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400160 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
161 if (environment) {
162 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
163 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
164 createHeatEnv(envFile, params, envSource)
165 }
166 else {
167 envFile = "${env.WORKSPACE}/template/${name}.env"
168 createHeatEnv(envFile, params)
169 }
170 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
171 dir("${env.WORKSPACE}/template/template") {
172 outputTable = runOpenstackCommand(cmd, client, path)
173 }
Ales Komareke11e8792016-12-28 09:42:25 +0100174 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400175
176 i = 1
Ales Komarekdce8b472016-12-30 16:56:07 +0100177
Sergey Kolekonovba203982016-12-21 18:32:17 +0400178 while (true) {
179 status = getHeatStackStatus(client, name, path)
180 echo("[Heat Stack] Status: ${status}, Check: ${i}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100181
182 if (status.indexOf('CREATE_FAILED') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400183 info = getHeatStackInfo(client, name, path)
184 throw new Exception(info.stack_status_reason)
185 }
Ales Komarekdce8b472016-12-30 16:56:07 +0100186 else if (status.indexOf('CREATE_COMPLETE') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400187 info = getHeatStackInfo(client, name, path)
188 echo(info.stack_status_reason)
189 break
190 }
191 sh('sleep 5s')
192 i++
193 }
194 echo("[Heat Stack] Status: ${status}")
195}
196
197/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200198 * Returns list of stacks for stack name filter
199 *
200 * @param client Connection parameters for OpenStack API endpoint
201 * @param filter Stack name filter
202 * @param path Optional path to the custom virtualenv
203 */
204def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200205 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200206 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
207}
208
209
210/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200211 * Get list of stack names with given stack status
212 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200213 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200214 * @param status Stack status
215 * @param path Optional path to the custom virtualenv
216 */
217 def getStacksWithStatus(client, status, path = null) {
218 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
219 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
220 }
221
222/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400223 * Get life cycle status for existing OpenStack Heat stack
224 *
225 * @param env Connection parameters for OpenStack API endpoint
226 * @param name Name of the managed Heat stack instance
227 * @param path Optional path to the custom virtualenv
228 */
229def getHeatStackStatus(client, name, path = null) {
230 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
231 return runOpenstackCommand(cmd, client, path)
232}
233
234/**
235 * Get info about existing OpenStack Heat stack
236 *
237 * @param env Connection parameters for OpenStack API endpoint
238 * @param name Name of the managed Heat stack instance
239 * @param path Optional path to the custom virtualenv
240 */
241def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400242 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400243 cmd = "heat stack-show ${name}"
244 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100245 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400246 return output
247}
248
249/**
250 * Get existing OpenStack Heat stack output parameter
251 *
252 * @param env Connection parameters for OpenStack API endpoint
253 * @param name Name of the managed Heat stack
254 * @param parameter Name of the output parameter
255 * @param path Optional path to the custom virtualenv
256 */
257def getHeatStackOutputParam(env, name, outputParam, path = null) {
258 cmd = "heat output-show ${name} ${outputParam}"
259 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100260 echo("${cmd}: ${output}")
Ales Komarek7ebd0062017-01-03 10:59:29 +0100261 return "${output}".substring(1, output.length()-1)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400262}
263
264/**
265 * List all resources from existing OpenStack Heat stack
266 *
267 * @param env Connection parameters for OpenStack API endpoint
268 * @param name Name of the managed Heat stack instance
269 * @param path Optional path to the custom virtualenv
270 */
271def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400272 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400273 cmd = "heat resource-list ${name}"
274 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100275 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400276 return output
277}
278
279/**
280 * Get info about resource from existing OpenStack Heat stack
281 *
282 * @param env Connection parameters for OpenStack API endpoint
283 * @param name Name of the managed Heat stack instance
284 * @param path Optional path to the custom virtualenv
285 */
286def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400287 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400288 cmd = "heat resource-show ${name} ${resource}"
289 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100290 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400291 return output
292}
293
294/**
295 * Update existing OpenStack Heat stack
296 *
297 * @param env Connection parameters for OpenStack API endpoint
298 * @param name Name of the managed Heat stack instance
299 * @param path Optional path to the custom virtualenv
300 */
301def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400302 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400303 cmd = "heat stack-update ${name}"
304 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100305 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400306 return output
307}
308
309/**
310 * Delete existing OpenStack Heat stack
311 *
312 * @param env Connection parameters for OpenStack API endpoint
313 * @param name Name of the managed Heat stack instance
314 * @param path Optional path to the custom virtualenv
315 */
316def deleteHeatStack(env, name, path = null) {
317 cmd = "heat stack-delete ${name}"
318 outputTable = runOpenstackCommand(cmd, env, path)
319}
320
321/**
322 * Return list of servers from OpenStack Heat stack
323 *
324 * @param env Connection parameters for OpenStack API endpoint
325 * @param name Name of the managed Heat stack instance
326 * @param path Optional path to the custom virtualenv
327 */
328def getHeatStackServers(env, name, path = null) {
329 resources = heatGetStackResources(env, name, path)
330 servers = []
331 for (resource in resources) {
332 if (resource.resource_type == 'OS::Nova::Server') {
333 resourceName = resource.resource_name
334 server = heatGetStackResourceInfo(env, name, resourceName, path)
335 servers.add(server.attributes.name)
336 }
337 }
338 echo("[Stack ${name}] Servers: ${servers}")
339 return servers
340}