blob: 98df398a585f67c987a49e36761d6a19792d5ad8 [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
Vasyl Saienkoa56c1ab2020-01-13 10:00:04 +020025def setupOpenstackVirtualenv(path, version = 'latest', python="python2") {
26 def pythonLib = new com.mirantis.mk.Python()
27 pythonLib.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',
Denis Egorenko71754dc2020-01-13 14:22:49 +040044 'docutils==0.16'
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"',
Denis Egorenko71754dc2020-01-13 14:22:49 +040057 // NOTE: pin client packages to current latest to prevent
58 // downloading packages which are not support Python 2.7
59 'python-openstackclient==4.0.0',
Vasyl Saienkof14fc572020-01-13 18:25:44 +020060 'python-heatclient==1.18.0',
Denis Egorenko71754dc2020-01-13 14:22:49 +040061 'docutils==0.16'
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020062 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040063
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020064 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040065 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020066 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040067 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020068 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040069 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020070 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040071 requirements = openstack_latest_packages
72 }
Vasyl Saienkoa56c1ab2020-01-13 10:00:04 +020073 pythonLib.setupVirtualenv(path, python, requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040074}
75
76/**
77 * create connection to OpenStack API endpoint
78 *
Jakub Josef6c963762018-01-18 16:02:22 +010079 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040080 * @param url OpenStack API endpoint address
81 * @param credentialsId Credentials to the OpenStack API
82 * @param project OpenStack project to connect to
83 */
Jakub Josef6c963762018-01-18 16:02:22 +010084def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020085 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040086 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010087 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040088 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030089 rc = """set +x
90export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010091export OS_PASSWORD=${creds.password.toString()}
92export OS_TENANT_NAME=${project}
93export OS_AUTH_URL=${url}
94export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040095export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000096export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040097export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000098export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +030099export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +0200100export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300101set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100102"""
103 writeFile file: rcFile, text: rc
104 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400105}
106
107/**
108 * Run command with OpenStack env params and optional python env
109 *
110 * @param cmd Command to be executed
111 * @param env Environmental parameters with endpoint credentials
112 * @param path Optional path to virtualenv with specific clients
113 */
114def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400115 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400116 openstackCmd = ". ${venv}; ${cmd}"
117 if (path) {
118 output = python.runVirtualenvCommand(path, openstackCmd)
119 }
120 else {
121 echo("[Command]: ${openstackCmd}")
122 output = sh (
123 script: openstackCmd,
124 returnStdout: true
125 ).trim()
126 }
127 return output
128}
129
130/**
131 * Get OpenStack Keystone token for current credentials
132 *
133 * @param env Connection parameters for OpenStack API endpoint
134 * @param path Optional path to the custom virtualenv
135 */
136def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400137 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000138 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400139 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100140 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400141 return output
142}
143
144/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200145 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400146 *
147 * @param env Connection parameters for OpenStack API endpoint
148 * @param path Optional path to the custom virtualenv
149 */
150def createHeatEnv(file, environment = [], original_file = null) {
151 if (original_file) {
152 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100153 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400154 envString = "parameters:\n"
155 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100156
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100157 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100158 for (int i = 0; i < p.size(); i++) {
159 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400160 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100161
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100162 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400163 writeFile file: file, text: envString
164}
165
166/**
167 * Create new OpenStack Heat stack
168 *
169 * @param env Connection parameters for OpenStack API endpoint
170 * @param template HOT template for the new Heat stack
171 * @param environment Environmentale parameters of the new Heat stack
172 * @param name Name of the new Heat stack
173 * @param path Optional path to the custom virtualenv
174 */
Tomáš Kukrála1152742017-08-22 16:21:50 +0200175def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") {
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
197 def waitState
198
Tomáš Kukrála1152742017-08-22 16:21:50 +0200199 if (action == "create") {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300200 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
201 waitState = "CREATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200202 } else {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300203 cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}"
204 waitState = "UPDATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200205 }
206
Sergey Kolekonovba203982016-12-21 18:32:17 +0400207 dir("${env.WORKSPACE}/template/template") {
208 outputTable = runOpenstackCommand(cmd, client, path)
209 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200210
Ales Komareke11e8792016-12-28 09:42:25 +0100211 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400212
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200213 def heatStatusCheckerCount = 1
Jakub Josefe0337272017-09-12 14:15:27 +0200214 while (heatStatusCheckerCount <= 250) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400215 status = getHeatStackStatus(client, name, path)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200216 echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100217
Tomáš Kukrála1152742017-08-22 16:21:50 +0200218 if (status.contains('CREATE_FAILED')) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400219 info = getHeatStackInfo(client, name, path)
220 throw new Exception(info.stack_status_reason)
Tomáš Kukrála1152742017-08-22 16:21:50 +0200221
222 } else if (status.contains(waitState)) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400223 info = getHeatStackInfo(client, name, path)
224 echo(info.stack_status_reason)
225 break
226 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200227
Jakub Josefe0337272017-09-12 14:15:27 +0200228 sleep(30)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200229 heatStatusCheckerCount++
Sergey Kolekonovba203982016-12-21 18:32:17 +0400230 }
231 echo("[Heat Stack] Status: ${status}")
232}
233
234/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200235 * Returns list of stacks for stack name filter
236 *
237 * @param client Connection parameters for OpenStack API endpoint
238 * @param filter Stack name filter
239 * @param path Optional path to the custom virtualenv
240 */
241def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200242 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200243 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
244}
245
246
247/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200248 * Get list of stack names with given stack status
249 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200250 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200251 * @param status Stack status
252 * @param path Optional path to the custom virtualenv
253 */
254 def getStacksWithStatus(client, status, path = null) {
255 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
256 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
257 }
258
259/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400260 * Get life cycle status for existing OpenStack Heat stack
261 *
262 * @param env Connection parameters for OpenStack API endpoint
263 * @param name Name of the managed Heat stack instance
264 * @param path Optional path to the custom virtualenv
265 */
266def getHeatStackStatus(client, name, path = null) {
267 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
268 return runOpenstackCommand(cmd, client, path)
269}
270
271/**
272 * Get info about existing OpenStack Heat stack
273 *
274 * @param env Connection parameters for OpenStack API endpoint
275 * @param name Name of the managed Heat stack instance
276 * @param path Optional path to the custom virtualenv
277 */
278def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400279 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400280 cmd = "heat stack-show ${name}"
281 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100282 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400283 return output
284}
285
286/**
287 * Get existing OpenStack Heat stack output parameter
288 *
289 * @param env Connection parameters for OpenStack API endpoint
290 * @param name Name of the managed Heat stack
291 * @param parameter Name of the output parameter
292 * @param path Optional path to the custom virtualenv
293 */
294def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000295 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400296 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100297 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300298 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
299 // versions returns string without "".
300 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
301 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400302}
303
304/**
305 * List all resources from existing OpenStack Heat stack
306 *
307 * @param env Connection parameters for OpenStack API endpoint
308 * @param name Name of the managed Heat stack instance
309 * @param path Optional path to the custom virtualenv
Mykyta Karpin72306362018-02-08 16:40:43 +0200310 * @param depth Optional depth of stack for listing resources,
311 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400312 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200313def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400314 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200315 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400316 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100317 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400318 return output
319}
320
321/**
322 * Get info about resource from existing 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 getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400329 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400330 cmd = "heat resource-show ${name} ${resource}"
331 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100332 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400333 return output
334}
335
336/**
337 * Update existing OpenStack Heat stack
338 *
339 * @param env Connection parameters for OpenStack API endpoint
340 * @param name Name of the managed Heat stack instance
341 * @param path Optional path to the custom virtualenv
342 */
343def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400344 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400345 cmd = "heat stack-update ${name}"
346 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100347 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400348 return output
349}
350
351/**
352 * Delete existing OpenStack Heat stack
353 *
354 * @param env Connection parameters for OpenStack API endpoint
355 * @param name Name of the managed Heat stack instance
356 * @param path Optional path to the custom virtualenv
357 */
358def deleteHeatStack(env, name, path = null) {
359 cmd = "heat stack-delete ${name}"
360 outputTable = runOpenstackCommand(cmd, env, path)
361}
362
363/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200364 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400365 *
366 * @param env Connection parameters for OpenStack API endpoint
367 * @param name Name of the managed Heat stack instance
368 * @param path Optional path to the custom virtualenv
369 */
370def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200371 // set depth to 1000 to ensure all nested resources are shown
372 resources = getHeatStackResources(env, name, path, 1000)
373 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400374 for (resource in resources) {
375 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200376 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200377 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400378 }
379 }
380 echo("[Stack ${name}] Servers: ${servers}")
381 return servers
382}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100383
384/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300385 * Delete nova key pair
386 *
387 * @param env Connection parameters for OpenStack API endpoint
388 * @param name Name of the key pair to delete
389 * @param path Optional path to the custom virtualenv
390 */
391def deleteKeyPair(env, name, path = null) {
392 def common = new com.mirantis.mk.Common()
393 common.infoMsg("Removing key pair ${name}")
394 def cmd = "openstack keypair delete ${name}"
395 runOpenstackCommand(cmd, env, path)
396}
397
398/**
399 * Get nova key pair
400 *
401 * @param env Connection parameters for OpenStack API endpoint
402 * @param name Name of the key pair to show
403 * @param path Optional path to the custom virtualenv
404 */
405
406def getKeyPair(env, name, path = null) {
407 def common = new com.mirantis.mk.Common()
408 def cmd = "openstack keypair show ${name}"
409 def outputTable
410 try {
411 outputTable = runOpenstackCommand(cmd, env, path)
412 } catch (Exception e) {
413 common.infoMsg("Key pair ${name} not found")
414 }
415 return outputTable
416}
417
418/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100419 * Stops all services that contain specific string (for example nova,heat, etc.)
420 * @param env Salt Connection object or pepperEnv
421 * @param probe single node on which to list service names
422 * @param target all targeted nodes
423 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100424 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100425 * @return output of salt commands
426 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100427def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100428 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100429 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100430 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400431 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100432 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100433 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100434 if (servicesList) {
435 try {
436 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
437 for (name in servicesList) {
438 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400439 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100440 }
441 }
442 } catch (Exception er) {
443 common.infoMsg("skipping stopping ${servicesList} services")
444 }
445 }
446 } else {
447 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100448 for (name in servicesList) {
449 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400450 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100451 }
452 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100453 }
454 }
455 }
456}
457
458/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300459 * Return intersection of globally installed services and those are
460 * defined on specific target according to theirs priorities.
461 *
462 * @param env Salt Connection object or env
463 * @param target The target node to get list of apps for.
464**/
465def getOpenStackUpgradeServices(env, target){
466 def salt = new com.mirantis.mk.Salt()
467 def common = new com.mirantis.mk.Common()
468
469 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
470 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
471 def node_sorted_apps = []
472 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
473 Map<String,Integer> _sorted_apps = [:]
474 for (k in global_apps['return'][0].values()[0].keySet()) {
475 if (k in node_apps) {
476 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
477 }
478 }
479 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
480 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
481 } else {
482 common.errorMsg("No applications found.")
483 }
484
485 return node_sorted_apps
486}
487
488
489/**
490 * Run specified upgrade phase for all services on given node.
491 *
492 * @param env Salt Connection object or env
493 * @param target The target node to run states on.
494 * @param phase The phase name to run.
495**/
496def runOpenStackUpgradePhase(env, target, phase){
497 def salt = new com.mirantis.mk.Salt()
498 def common = new com.mirantis.mk.Common()
499
500 services = getOpenStackUpgradeServices(env, target)
501 def st
502
503 for (service in services){
504 st = "${service}.upgrade.${phase}".trim()
505 common.infoMsg("Running ${phase} for service ${st} on ${target}")
506 salt.enforceState(env, target, st)
507 }
508}
509
510
511/**
512 * Run OpenStack states on specified node.
513 *
514 * @param env Salt Connection object or env
515 * @param target The target node to run states on.
516**/
517def applyOpenstackAppsStates(env, target){
518 def salt = new com.mirantis.mk.Salt()
519 def common = new com.mirantis.mk.Common()
520
521 services = getOpenStackUpgradeServices(env, target)
522 def st
523
524 for (service in services){
525 st = "${service}".trim()
526 common.infoMsg("Running ${st} on ${target}")
527 salt.enforceState(env, target, st)
528 }
529}
530
Martin Polreichddfe51c2019-01-21 14:31:00 +0100531def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100532 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200533 def galera = new com.mirantis.mk.Galera()
534 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
535 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100536}
537
Martin Polreich47c09d12018-12-21 16:42:06 +0100538def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100539 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200540 def galera = new com.mirantis.mk.Galera()
541 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
542 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100543}
544
Martin Polreich47c09d12018-12-21 16:42:06 +0100545def getGaleraLastShutdownNode(env) {
Martin Polreich47c09d12018-12-21 16:42:06 +0100546 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200547 def galera = new com.mirantis.mk.Galera()
548 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
549 return galera.getGaleraLastShutdownNode(env)
Martin Polreich47c09d12018-12-21 16:42:06 +0100550}
551
Martin Polreichdb692582019-04-25 14:32:36 +0200552def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100553 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200554 def galera = new com.mirantis.mk.Galera()
555 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
556 return galera.restoreGaleraDb(env)
Martin Polreich65864b02018-12-05 10:42:50 +0100557}