blob: da2495d55c43e01d74049d5138772e4c38e6f489 [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 = [
Jakub Josef268bc842017-10-10 14:36:17 +020030 //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463
31 'cliff==2.8',
Sergey Kolekonovba203982016-12-21 18:32:17 +040032 'python-cinderclient>=1.3.1,<1.4.0',
33 'python-glanceclient>=0.19.0,<0.20.0',
34 'python-heatclient>=0.6.0,<0.7.0',
35 'python-keystoneclient>=1.6.0,<1.7.0',
36 'python-neutronclient>=2.2.6,<2.3.0',
37 'python-novaclient>=2.19.0,<2.20.0',
38 'python-swiftclient>=2.5.0,<2.6.0',
Jakub Josefbd927322017-05-30 13:20:27 +000039 'python-openstackclient>=1.7.0,<1.8.0',
Sergey Kolekonovba203982016-12-21 18:32:17 +040040 'oslo.config>=2.2.0,<2.3.0',
41 'oslo.i18n>=2.3.0,<2.4.0',
42 'oslo.serialization>=1.8.0,<1.9.0',
43 'oslo.utils>=1.4.0,<1.5.0',
Jakub Josef60280212017-08-10 19:01:19 +020044 'docutils'
Sergey Kolekonovba203982016-12-21 18:32:17 +040045 ]
46
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020047 def openstack_latest_packages = [
Jakub Josef268bc842017-10-10 14:36:17 +020048 //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463
49 'cliff==2.8',
Vasyl Saienko36a019d2018-05-30 09:51:18 +030050 // NOTE(vsaienko): cmd2 is dependency for cliff, since we don't using upper-contstraints
51 // we have to pin cmd2 < 0.9.0 as later versions are not compatible with python2.
52 // TODO(vsaienko): use upper-constraints here, as in requirements we set only lowest library
53 // versions.
54 'cmd2<0.9.0;python_version=="2.7"',
55 'cmd2>=0.9.1;python_version=="3.4"',
56 'cmd2>=0.9.1;python_version=="3.5"',
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020057 'python-openstackclient',
58 'python-heatclient',
Jakub Josef60280212017-08-10 19:01:19 +020059 'docutils'
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020060 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040061
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020062 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040063 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020064 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040065 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020066 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040067 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020068 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040069 requirements = openstack_latest_packages
70 }
Tomáš Kukrálbee0b992017-08-10 16:50:40 +020071 python.setupVirtualenv(path, 'python2', requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040072}
73
74/**
75 * create connection to OpenStack API endpoint
76 *
Jakub Josef6c963762018-01-18 16:02:22 +010077 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040078 * @param url OpenStack API endpoint address
79 * @param credentialsId Credentials to the OpenStack API
80 * @param project OpenStack project to connect to
81 */
Jakub Josef6c963762018-01-18 16:02:22 +010082def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020083 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040084 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010085 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040086 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030087 rc = """set +x
88export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010089export OS_PASSWORD=${creds.password.toString()}
90export OS_TENANT_NAME=${project}
91export OS_AUTH_URL=${url}
92export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040093export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000094export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040095export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000096export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +030097export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020098export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030099set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100100"""
101 writeFile file: rcFile, text: rc
102 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400103}
104
105/**
106 * Run command with OpenStack env params and optional python env
107 *
108 * @param cmd Command to be executed
109 * @param env Environmental parameters with endpoint credentials
110 * @param path Optional path to virtualenv with specific clients
111 */
112def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400113 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400114 openstackCmd = ". ${venv}; ${cmd}"
115 if (path) {
116 output = python.runVirtualenvCommand(path, openstackCmd)
117 }
118 else {
119 echo("[Command]: ${openstackCmd}")
120 output = sh (
121 script: openstackCmd,
122 returnStdout: true
123 ).trim()
124 }
125 return output
126}
127
128/**
129 * Get OpenStack Keystone token for current credentials
130 *
131 * @param env Connection parameters for OpenStack API endpoint
132 * @param path Optional path to the custom virtualenv
133 */
134def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400135 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000136 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400137 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100138 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400139 return output
140}
141
142/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200143 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400144 *
145 * @param env Connection parameters for OpenStack API endpoint
146 * @param path Optional path to the custom virtualenv
147 */
148def createHeatEnv(file, environment = [], original_file = null) {
149 if (original_file) {
150 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100151 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400152 envString = "parameters:\n"
153 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100154
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100155 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100156 for (int i = 0; i < p.size(); i++) {
157 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400158 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100159
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100160 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400161 writeFile file: file, text: envString
162}
163
164/**
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200165 * Create new OpenStack Heat stack. Will wait for action to be complited in
166 * specified amount of time (by default 120min)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400167 *
168 * @param env Connection parameters for OpenStack API endpoint
169 * @param template HOT template for the new Heat stack
170 * @param environment Environmentale parameters of the new Heat stack
171 * @param name Name of the new Heat stack
172 * @param path Optional path to the custom virtualenv
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200173 * @param timeout Optional number in minutes to wait for stack action is applied.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400174 */
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200175def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400176 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200177 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
178 def envFile
179 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400180 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200181 envFile = "${env.WORKSPACE}/template/env/${name}.env"
182 if (environment.contains("/")) {
183 //init() returns all elements but the last in a collection.
184 def envPath = environment.tokenize("/").init().join("/")
185 if (envPath) {
186 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
187 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200188 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200189 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400190 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200191 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400192 envFile = "${env.WORKSPACE}/template/${name}.env"
193 createHeatEnv(envFile, params)
194 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200195
Mykyta Karpincf44f812017-08-28 14:45:21 +0300196 def cmd
Vasyl Saienkob91df802019-01-23 17:22:57 +0200197 def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}"
Mykyta Karpincf44f812017-08-28 14:45:21 +0300198
Tomáš Kukrála1152742017-08-22 16:21:50 +0200199 if (action == "create") {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200200 cmd = "openstack stack create ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200201 } else {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200202 cmd = "openstack stack update ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200203 }
204
Sergey Kolekonovba203982016-12-21 18:32:17 +0400205 dir("${env.WORKSPACE}/template/template") {
Vasyl Saienkod4254192019-01-23 18:02:01 +0200206 def out = runOpenstackCommand(cmd, client, path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400207 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400208}
209
210/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200211 * Returns list of stacks for stack name filter
212 *
213 * @param client Connection parameters for OpenStack API endpoint
214 * @param filter Stack name filter
215 * @param path Optional path to the custom virtualenv
216 */
217def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200218 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200219 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
220}
221
222
223/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200224 * Get list of stack names with given stack status
225 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200226 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200227 * @param status Stack status
228 * @param path Optional path to the custom virtualenv
229 */
230 def getStacksWithStatus(client, status, path = null) {
231 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
232 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
233 }
234
235/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400236 * Get life cycle status for existing OpenStack Heat stack
237 *
238 * @param env Connection parameters for OpenStack API endpoint
239 * @param name Name of the managed Heat stack instance
240 * @param path Optional path to the custom virtualenv
241 */
242def getHeatStackStatus(client, name, path = null) {
243 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
244 return runOpenstackCommand(cmd, client, path)
245}
246
247/**
248 * Get info about existing OpenStack Heat stack
249 *
250 * @param env Connection parameters for OpenStack API endpoint
251 * @param name Name of the managed Heat stack instance
252 * @param path Optional path to the custom virtualenv
253 */
254def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400255 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400256 cmd = "heat stack-show ${name}"
257 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100258 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400259 return output
260}
261
262/**
263 * Get existing OpenStack Heat stack output parameter
264 *
265 * @param env Connection parameters for OpenStack API endpoint
266 * @param name Name of the managed Heat stack
267 * @param parameter Name of the output parameter
268 * @param path Optional path to the custom virtualenv
269 */
270def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000271 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400272 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100273 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300274 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
275 // versions returns string without "".
276 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
277 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400278}
279
280/**
281 * List all resources from existing 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
Mykyta Karpin72306362018-02-08 16:40:43 +0200286 * @param depth Optional depth of stack for listing resources,
287 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400288 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200289def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400290 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200291 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400292 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100293 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400294 return output
295}
296
297/**
298 * Get info about resource from existing OpenStack Heat stack
299 *
300 * @param env Connection parameters for OpenStack API endpoint
301 * @param name Name of the managed Heat stack instance
302 * @param path Optional path to the custom virtualenv
303 */
304def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400305 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400306 cmd = "heat resource-show ${name} ${resource}"
307 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100308 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400309 return output
310}
311
312/**
313 * Update existing OpenStack Heat stack
314 *
315 * @param env Connection parameters for OpenStack API endpoint
316 * @param name Name of the managed Heat stack instance
317 * @param path Optional path to the custom virtualenv
318 */
319def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400320 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400321 cmd = "heat stack-update ${name}"
322 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100323 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400324 return output
325}
326
327/**
328 * Delete existing OpenStack Heat stack
329 *
330 * @param env Connection parameters for OpenStack API endpoint
331 * @param name Name of the managed Heat stack instance
332 * @param path Optional path to the custom virtualenv
333 */
334def deleteHeatStack(env, name, path = null) {
335 cmd = "heat stack-delete ${name}"
336 outputTable = runOpenstackCommand(cmd, env, path)
337}
338
339/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200340 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400341 *
342 * @param env Connection parameters for OpenStack API endpoint
343 * @param name Name of the managed Heat stack instance
344 * @param path Optional path to the custom virtualenv
345 */
346def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200347 // set depth to 1000 to ensure all nested resources are shown
348 resources = getHeatStackResources(env, name, path, 1000)
349 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400350 for (resource in resources) {
351 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200352 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200353 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400354 }
355 }
356 echo("[Stack ${name}] Servers: ${servers}")
357 return servers
358}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100359
360/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300361 * Delete nova key pair
362 *
363 * @param env Connection parameters for OpenStack API endpoint
364 * @param name Name of the key pair to delete
365 * @param path Optional path to the custom virtualenv
366 */
367def deleteKeyPair(env, name, path = null) {
368 def common = new com.mirantis.mk.Common()
369 common.infoMsg("Removing key pair ${name}")
370 def cmd = "openstack keypair delete ${name}"
371 runOpenstackCommand(cmd, env, path)
372}
373
374/**
375 * Get nova key pair
376 *
377 * @param env Connection parameters for OpenStack API endpoint
378 * @param name Name of the key pair to show
379 * @param path Optional path to the custom virtualenv
380 */
381
382def getKeyPair(env, name, path = null) {
383 def common = new com.mirantis.mk.Common()
384 def cmd = "openstack keypair show ${name}"
385 def outputTable
386 try {
387 outputTable = runOpenstackCommand(cmd, env, path)
388 } catch (Exception e) {
389 common.infoMsg("Key pair ${name} not found")
390 }
391 return outputTable
392}
393
394/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100395 * Stops all services that contain specific string (for example nova,heat, etc.)
396 * @param env Salt Connection object or pepperEnv
397 * @param probe single node on which to list service names
398 * @param target all targeted nodes
399 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100400 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100401 * @return output of salt commands
402 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100403def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100404 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100405 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100406 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400407 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100408 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100409 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100410 if (servicesList) {
411 try {
412 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
413 for (name in servicesList) {
414 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400415 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100416 }
417 }
418 } catch (Exception er) {
419 common.infoMsg("skipping stopping ${servicesList} services")
420 }
421 }
422 } else {
423 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100424 for (name in servicesList) {
425 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400426 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100427 }
428 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100429 }
430 }
431 }
432}
433
434/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300435 * Return intersection of globally installed services and those are
436 * defined on specific target according to theirs priorities.
437 *
438 * @param env Salt Connection object or env
439 * @param target The target node to get list of apps for.
440**/
441def getOpenStackUpgradeServices(env, target){
442 def salt = new com.mirantis.mk.Salt()
443 def common = new com.mirantis.mk.Common()
444
445 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
446 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
Oleksii Grudev3116a732019-02-14 18:16:05 +0200447 def node_pillar = salt.getPillar(env, target)
Vasyl Saienko4129e102018-09-03 10:15:52 +0300448 def node_sorted_apps = []
449 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
450 Map<String,Integer> _sorted_apps = [:]
451 for (k in global_apps['return'][0].values()[0].keySet()) {
452 if (k in node_apps) {
Oleksii Grudev3116a732019-02-14 18:16:05 +0200453 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0] != null) {
454 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0].toBoolean()) {
455 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
456 }
457 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300458 }
459 }
460 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
461 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
462 } else {
463 common.errorMsg("No applications found.")
464 }
465
466 return node_sorted_apps
467}
468
469
470/**
471 * Run specified upgrade phase for all services on given node.
472 *
473 * @param env Salt Connection object or env
474 * @param target The target node to run states on.
475 * @param phase The phase name to run.
476**/
477def runOpenStackUpgradePhase(env, target, phase){
478 def salt = new com.mirantis.mk.Salt()
479 def common = new com.mirantis.mk.Common()
480
481 services = getOpenStackUpgradeServices(env, target)
482 def st
483
484 for (service in services){
485 st = "${service}.upgrade.${phase}".trim()
486 common.infoMsg("Running ${phase} for service ${st} on ${target}")
487 salt.enforceState(env, target, st)
488 }
489}
490
491
492/**
493 * Run OpenStack states on specified node.
494 *
495 * @param env Salt Connection object or env
496 * @param target The target node to run states on.
497**/
498def applyOpenstackAppsStates(env, target){
499 def salt = new com.mirantis.mk.Salt()
500 def common = new com.mirantis.mk.Common()
501
502 services = getOpenStackUpgradeServices(env, target)
503 def st
504
505 for (service in services){
506 st = "${service}".trim()
507 common.infoMsg("Running ${st} on ${target}")
508 salt.enforceState(env, target, st)
509 }
510}
511
Martin Polreich232ad902019-01-21 14:31:00 +0100512def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100513 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100514 def galera = new com.mirantis.mk.Galera()
515 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
516 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100517}
518
Martin Polreich9a5d6682018-12-21 16:42:06 +0100519def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100520 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100521 def galera = new com.mirantis.mk.Galera()
522 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
523 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100524}
525
Martin Polreich9a5d6682018-12-21 16:42:06 +0100526def getGaleraLastShutdownNode(env) {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100527 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100528 def galera = new com.mirantis.mk.Galera()
529 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
530 return galera.getGaleraLastShutdownNode(env)
Martin Polreich9a5d6682018-12-21 16:42:06 +0100531}
532
Ivan Berezovskiy004cac22019-02-01 17:03:28 +0400533def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100534 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100535 def galera = new com.mirantis.mk.Galera()
536 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
537 return galera.restoreGaleraDb(env)
538}