blob: f8a3c33d39040c35a3bacbd4ee9b68fbfce7e6fc [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',
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 Komarekd874d482016-12-26 10:33:29 +010040 'docutils>=0.12'
Sergey Kolekonovba203982016-12-21 18:32:17 +040041 ]
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_kushaev0a26bf72017-05-18 13:20:09 +040067def createOpenstackEnv(url, credentialsId, project, project_domain="default",
68 user_domain= "default") {
iberezovskiyd4240b52017-02-20 17:18:28 +040069 def common = new com.mirantis.mk.Common()
Ales Komarek0e558ee2016-12-23 13:02:55 +010070 rcFile = "${env.WORKSPACE}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040071 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030072 rc = """set +x
73export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010074export OS_PASSWORD=${creds.password.toString()}
75export OS_TENANT_NAME=${project}
76export OS_AUTH_URL=${url}
77export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040078export OS_PROJECT_NAME=${project}
79export OS_PROJECT_DOMAIN_ID=${project_domain}
80export OS_USER_DOMAIN_ID=${user_domain}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030081set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +010082"""
83 writeFile file: rcFile, text: rc
84 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +040085}
86
87/**
88 * Run command with OpenStack env params and optional python env
89 *
90 * @param cmd Command to be executed
91 * @param env Environmental parameters with endpoint credentials
92 * @param path Optional path to virtualenv with specific clients
93 */
94def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +040095 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +040096 openstackCmd = ". ${venv}; ${cmd}"
97 if (path) {
98 output = python.runVirtualenvCommand(path, openstackCmd)
99 }
100 else {
101 echo("[Command]: ${openstackCmd}")
102 output = sh (
103 script: openstackCmd,
104 returnStdout: true
105 ).trim()
106 }
107 return output
108}
109
110/**
111 * Get OpenStack Keystone token for current credentials
112 *
113 * @param env Connection parameters for OpenStack API endpoint
114 * @param path Optional path to the custom virtualenv
115 */
116def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400117 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400118 cmd = "keystone token-get"
119 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100120 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400121 return output
122}
123
124/**
125 * Get OpenStack Keystone token for current credentials
126 *
127 * @param env Connection parameters for OpenStack API endpoint
128 * @param path Optional path to the custom virtualenv
129 */
130def createHeatEnv(file, environment = [], original_file = null) {
131 if (original_file) {
132 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100133 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400134 envString = "parameters:\n"
135 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100136
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100137 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100138 for (int i = 0; i < p.size(); i++) {
139 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400140 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100141
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100142 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400143 writeFile file: file, text: envString
144}
145
146/**
147 * Create new OpenStack Heat stack
148 *
149 * @param env Connection parameters for OpenStack API endpoint
150 * @param template HOT template for the new Heat stack
151 * @param environment Environmentale parameters of the new Heat stack
152 * @param name Name of the new Heat stack
153 * @param path Optional path to the custom virtualenv
154 */
155def createHeatStack(client, name, template, params = [], environment = null, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400156 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400157 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
158 if (environment) {
159 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
160 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
161 createHeatEnv(envFile, params, envSource)
162 }
163 else {
164 envFile = "${env.WORKSPACE}/template/${name}.env"
165 createHeatEnv(envFile, params)
166 }
167 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
168 dir("${env.WORKSPACE}/template/template") {
169 outputTable = runOpenstackCommand(cmd, client, path)
170 }
Ales Komareke11e8792016-12-28 09:42:25 +0100171 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400172
173 i = 1
Ales Komarekdce8b472016-12-30 16:56:07 +0100174
Sergey Kolekonovba203982016-12-21 18:32:17 +0400175 while (true) {
176 status = getHeatStackStatus(client, name, path)
177 echo("[Heat Stack] Status: ${status}, Check: ${i}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100178
179 if (status.indexOf('CREATE_FAILED') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400180 info = getHeatStackInfo(client, name, path)
181 throw new Exception(info.stack_status_reason)
182 }
Ales Komarekdce8b472016-12-30 16:56:07 +0100183 else if (status.indexOf('CREATE_COMPLETE') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400184 info = getHeatStackInfo(client, name, path)
185 echo(info.stack_status_reason)
186 break
187 }
188 sh('sleep 5s')
189 i++
190 }
191 echo("[Heat Stack] Status: ${status}")
192}
193
194/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200195 * Returns list of stacks for stack name filter
196 *
197 * @param client Connection parameters for OpenStack API endpoint
198 * @param filter Stack name filter
199 * @param path Optional path to the custom virtualenv
200 */
201def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200202 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200203 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
204}
205
206
207/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200208 * Get list of stack names with given stack status
209 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200210 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200211 * @param status Stack status
212 * @param path Optional path to the custom virtualenv
213 */
214 def getStacksWithStatus(client, status, path = null) {
215 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
216 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
217 }
218
219/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400220 * Get life cycle status for existing OpenStack Heat stack
221 *
222 * @param env Connection parameters for OpenStack API endpoint
223 * @param name Name of the managed Heat stack instance
224 * @param path Optional path to the custom virtualenv
225 */
226def getHeatStackStatus(client, name, path = null) {
227 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
228 return runOpenstackCommand(cmd, client, path)
229}
230
231/**
232 * Get info about existing OpenStack Heat stack
233 *
234 * @param env Connection parameters for OpenStack API endpoint
235 * @param name Name of the managed Heat stack instance
236 * @param path Optional path to the custom virtualenv
237 */
238def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400239 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400240 cmd = "heat stack-show ${name}"
241 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100242 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400243 return output
244}
245
246/**
247 * Get existing OpenStack Heat stack output parameter
248 *
249 * @param env Connection parameters for OpenStack API endpoint
250 * @param name Name of the managed Heat stack
251 * @param parameter Name of the output parameter
252 * @param path Optional path to the custom virtualenv
253 */
254def getHeatStackOutputParam(env, name, outputParam, path = null) {
255 cmd = "heat output-show ${name} ${outputParam}"
256 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100257 echo("${cmd}: ${output}")
Ales Komarek7ebd0062017-01-03 10:59:29 +0100258 return "${output}".substring(1, output.length()-1)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400259}
260
261/**
262 * List all resources from existing OpenStack Heat stack
263 *
264 * @param env Connection parameters for OpenStack API endpoint
265 * @param name Name of the managed Heat stack instance
266 * @param path Optional path to the custom virtualenv
267 */
268def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400269 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400270 cmd = "heat resource-list ${name}"
271 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100272 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400273 return output
274}
275
276/**
277 * Get info about resource from existing OpenStack Heat stack
278 *
279 * @param env Connection parameters for OpenStack API endpoint
280 * @param name Name of the managed Heat stack instance
281 * @param path Optional path to the custom virtualenv
282 */
283def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400284 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400285 cmd = "heat resource-show ${name} ${resource}"
286 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100287 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400288 return output
289}
290
291/**
292 * Update existing OpenStack Heat stack
293 *
294 * @param env Connection parameters for OpenStack API endpoint
295 * @param name Name of the managed Heat stack instance
296 * @param path Optional path to the custom virtualenv
297 */
298def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400299 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400300 cmd = "heat stack-update ${name}"
301 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100302 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400303 return output
304}
305
306/**
307 * Delete existing OpenStack Heat stack
308 *
309 * @param env Connection parameters for OpenStack API endpoint
310 * @param name Name of the managed Heat stack instance
311 * @param path Optional path to the custom virtualenv
312 */
313def deleteHeatStack(env, name, path = null) {
314 cmd = "heat stack-delete ${name}"
315 outputTable = runOpenstackCommand(cmd, env, path)
316}
317
318/**
319 * Return list of servers from OpenStack Heat stack
320 *
321 * @param env Connection parameters for OpenStack API endpoint
322 * @param name Name of the managed Heat stack instance
323 * @param path Optional path to the custom virtualenv
324 */
325def getHeatStackServers(env, name, path = null) {
326 resources = heatGetStackResources(env, name, path)
327 servers = []
328 for (resource in resources) {
329 if (resource.resource_type == 'OS::Nova::Server') {
330 resourceName = resource.resource_name
331 server = heatGetStackResourceInfo(env, name, resourceName, path)
332 servers.add(server.attributes.name)
333 }
334 }
335 echo("[Stack ${name}] Servers: ${servers}")
336 return servers
337}