blob: 0a55867ad0eb91615310b49384e38f513546f40f [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',
Mykyta Karpin3fe5c2e2020-09-09 11:24:41 +030061 'docutils==0.16',
62 'pyrsistent<0.17.1',
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020063 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040064
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020065 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040066 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020067 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040068 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020069 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040070 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020071 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040072 requirements = openstack_latest_packages
73 }
Vasyl Saienkoa56c1ab2020-01-13 10:00:04 +020074 pythonLib.setupVirtualenv(path, python, requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040075}
76
77/**
78 * create connection to OpenStack API endpoint
79 *
Jakub Josef6c963762018-01-18 16:02:22 +010080 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040081 * @param url OpenStack API endpoint address
82 * @param credentialsId Credentials to the OpenStack API
83 * @param project OpenStack project to connect to
84 */
Jakub Josef6c963762018-01-18 16:02:22 +010085def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020086 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040087 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010088 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040089 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030090 rc = """set +x
91export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010092export OS_PASSWORD=${creds.password.toString()}
93export OS_TENANT_NAME=${project}
94export OS_AUTH_URL=${url}
95export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040096export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000097export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040098export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000099export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +0300100export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +0200101export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300102set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100103"""
104 writeFile file: rcFile, text: rc
105 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400106}
107
108/**
109 * Run command with OpenStack env params and optional python env
110 *
111 * @param cmd Command to be executed
112 * @param env Environmental parameters with endpoint credentials
113 * @param path Optional path to virtualenv with specific clients
114 */
115def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400116 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400117 openstackCmd = ". ${venv}; ${cmd}"
118 if (path) {
119 output = python.runVirtualenvCommand(path, openstackCmd)
120 }
121 else {
122 echo("[Command]: ${openstackCmd}")
123 output = sh (
124 script: openstackCmd,
125 returnStdout: true
126 ).trim()
127 }
128 return output
129}
130
131/**
132 * Get OpenStack Keystone token for current credentials
133 *
134 * @param env Connection parameters for OpenStack API endpoint
135 * @param path Optional path to the custom virtualenv
136 */
137def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400138 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000139 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400140 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100141 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400142 return output
143}
144
145/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200146 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400147 *
148 * @param env Connection parameters for OpenStack API endpoint
149 * @param path Optional path to the custom virtualenv
150 */
151def createHeatEnv(file, environment = [], original_file = null) {
152 if (original_file) {
153 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100154 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400155 envString = "parameters:\n"
156 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100157
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100158 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100159 for (int i = 0; i < p.size(); i++) {
160 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400161 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100162
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100163 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400164 writeFile file: file, text: envString
165}
166
167/**
168 * Create new OpenStack Heat stack
169 *
170 * @param env Connection parameters for OpenStack API endpoint
171 * @param template HOT template for the new Heat stack
172 * @param environment Environmentale parameters of the new Heat stack
173 * @param name Name of the new Heat stack
174 * @param path Optional path to the custom virtualenv
175 */
Tomáš Kukrála1152742017-08-22 16:21:50 +0200176def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") {
iberezovskiyd4240b52017-02-20 17:18:28 +0400177 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200178 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
179 def envFile
180 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400181 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200182 envFile = "${env.WORKSPACE}/template/env/${name}.env"
183 if (environment.contains("/")) {
184 //init() returns all elements but the last in a collection.
185 def envPath = environment.tokenize("/").init().join("/")
186 if (envPath) {
187 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
188 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200189 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200190 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400191 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200192 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400193 envFile = "${env.WORKSPACE}/template/${name}.env"
194 createHeatEnv(envFile, params)
195 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200196
Mykyta Karpincf44f812017-08-28 14:45:21 +0300197 def cmd
198 def waitState
199
Tomáš Kukrála1152742017-08-22 16:21:50 +0200200 if (action == "create") {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300201 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
202 waitState = "CREATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200203 } else {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300204 cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}"
205 waitState = "UPDATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200206 }
207
Sergey Kolekonovba203982016-12-21 18:32:17 +0400208 dir("${env.WORKSPACE}/template/template") {
209 outputTable = runOpenstackCommand(cmd, client, path)
210 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200211
Ales Komareke11e8792016-12-28 09:42:25 +0100212 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400213
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200214 def heatStatusCheckerCount = 1
Jakub Josefe0337272017-09-12 14:15:27 +0200215 while (heatStatusCheckerCount <= 250) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400216 status = getHeatStackStatus(client, name, path)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200217 echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100218
Tomáš Kukrála1152742017-08-22 16:21:50 +0200219 if (status.contains('CREATE_FAILED')) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400220 info = getHeatStackInfo(client, name, path)
221 throw new Exception(info.stack_status_reason)
Tomáš Kukrála1152742017-08-22 16:21:50 +0200222
223 } else if (status.contains(waitState)) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400224 info = getHeatStackInfo(client, name, path)
225 echo(info.stack_status_reason)
226 break
227 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200228
Jakub Josefe0337272017-09-12 14:15:27 +0200229 sleep(30)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200230 heatStatusCheckerCount++
Sergey Kolekonovba203982016-12-21 18:32:17 +0400231 }
232 echo("[Heat Stack] Status: ${status}")
233}
234
235/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200236 * Returns list of stacks for stack name filter
237 *
238 * @param client Connection parameters for OpenStack API endpoint
239 * @param filter Stack name filter
240 * @param path Optional path to the custom virtualenv
241 */
242def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200243 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200244 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
245}
246
247
248/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200249 * Get list of stack names with given stack status
250 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200251 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200252 * @param status Stack status
253 * @param path Optional path to the custom virtualenv
254 */
255 def getStacksWithStatus(client, status, path = null) {
256 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
257 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
258 }
259
260/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400261 * Get life cycle status for existing OpenStack Heat stack
262 *
263 * @param env Connection parameters for OpenStack API endpoint
264 * @param name Name of the managed Heat stack instance
265 * @param path Optional path to the custom virtualenv
266 */
267def getHeatStackStatus(client, name, path = null) {
268 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
269 return runOpenstackCommand(cmd, client, path)
270}
271
272/**
273 * Get info about existing OpenStack Heat stack
274 *
275 * @param env Connection parameters for OpenStack API endpoint
276 * @param name Name of the managed Heat stack instance
277 * @param path Optional path to the custom virtualenv
278 */
279def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400280 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400281 cmd = "heat stack-show ${name}"
282 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100283 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400284 return output
285}
286
287/**
288 * Get existing OpenStack Heat stack output parameter
289 *
290 * @param env Connection parameters for OpenStack API endpoint
291 * @param name Name of the managed Heat stack
292 * @param parameter Name of the output parameter
293 * @param path Optional path to the custom virtualenv
294 */
295def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000296 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400297 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100298 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300299 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
300 // versions returns string without "".
301 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
302 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400303}
304
305/**
306 * List all resources 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
Mykyta Karpin72306362018-02-08 16:40:43 +0200311 * @param depth Optional depth of stack for listing resources,
312 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400313 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200314def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400315 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200316 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400317 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100318 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400319 return output
320}
321
322/**
323 * Get info about resource from existing OpenStack Heat stack
324 *
325 * @param env Connection parameters for OpenStack API endpoint
326 * @param name Name of the managed Heat stack instance
327 * @param path Optional path to the custom virtualenv
328 */
329def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400330 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400331 cmd = "heat resource-show ${name} ${resource}"
332 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100333 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400334 return output
335}
336
337/**
338 * Update existing OpenStack Heat stack
339 *
340 * @param env Connection parameters for OpenStack API endpoint
341 * @param name Name of the managed Heat stack instance
342 * @param path Optional path to the custom virtualenv
343 */
344def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400345 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400346 cmd = "heat stack-update ${name}"
347 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100348 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400349 return output
350}
351
352/**
353 * Delete existing OpenStack Heat stack
354 *
355 * @param env Connection parameters for OpenStack API endpoint
356 * @param name Name of the managed Heat stack instance
357 * @param path Optional path to the custom virtualenv
358 */
359def deleteHeatStack(env, name, path = null) {
360 cmd = "heat stack-delete ${name}"
361 outputTable = runOpenstackCommand(cmd, env, path)
362}
363
364/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200365 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400366 *
367 * @param env Connection parameters for OpenStack API endpoint
368 * @param name Name of the managed Heat stack instance
369 * @param path Optional path to the custom virtualenv
370 */
371def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200372 // set depth to 1000 to ensure all nested resources are shown
373 resources = getHeatStackResources(env, name, path, 1000)
374 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400375 for (resource in resources) {
376 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200377 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200378 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400379 }
380 }
381 echo("[Stack ${name}] Servers: ${servers}")
382 return servers
383}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100384
385/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300386 * Delete nova key pair
387 *
388 * @param env Connection parameters for OpenStack API endpoint
389 * @param name Name of the key pair to delete
390 * @param path Optional path to the custom virtualenv
391 */
392def deleteKeyPair(env, name, path = null) {
393 def common = new com.mirantis.mk.Common()
394 common.infoMsg("Removing key pair ${name}")
395 def cmd = "openstack keypair delete ${name}"
396 runOpenstackCommand(cmd, env, path)
397}
398
399/**
400 * Get nova key pair
401 *
402 * @param env Connection parameters for OpenStack API endpoint
403 * @param name Name of the key pair to show
404 * @param path Optional path to the custom virtualenv
405 */
406
407def getKeyPair(env, name, path = null) {
408 def common = new com.mirantis.mk.Common()
409 def cmd = "openstack keypair show ${name}"
410 def outputTable
411 try {
412 outputTable = runOpenstackCommand(cmd, env, path)
413 } catch (Exception e) {
414 common.infoMsg("Key pair ${name} not found")
415 }
416 return outputTable
417}
418
419/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100420 * Stops all services that contain specific string (for example nova,heat, etc.)
421 * @param env Salt Connection object or pepperEnv
422 * @param probe single node on which to list service names
423 * @param target all targeted nodes
424 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100425 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100426 * @return output of salt commands
427 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100428def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100429 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100430 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100431 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400432 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100433 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100434 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100435 if (servicesList) {
436 try {
437 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
438 for (name in servicesList) {
439 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400440 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100441 }
442 }
443 } catch (Exception er) {
444 common.infoMsg("skipping stopping ${servicesList} services")
445 }
446 }
447 } else {
448 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100449 for (name in servicesList) {
450 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400451 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100452 }
453 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100454 }
455 }
456 }
457}
458
459/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300460 * Return intersection of globally installed services and those are
461 * defined on specific target according to theirs priorities.
462 *
463 * @param env Salt Connection object or env
464 * @param target The target node to get list of apps for.
465**/
466def getOpenStackUpgradeServices(env, target){
467 def salt = new com.mirantis.mk.Salt()
468 def common = new com.mirantis.mk.Common()
469
470 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
471 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
472 def node_sorted_apps = []
473 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
474 Map<String,Integer> _sorted_apps = [:]
475 for (k in global_apps['return'][0].values()[0].keySet()) {
476 if (k in node_apps) {
477 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
478 }
479 }
480 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
481 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
482 } else {
483 common.errorMsg("No applications found.")
484 }
485
486 return node_sorted_apps
487}
488
489
490/**
491 * Run specified upgrade phase for all services on given node.
492 *
493 * @param env Salt Connection object or env
494 * @param target The target node to run states on.
495 * @param phase The phase name to run.
496**/
497def runOpenStackUpgradePhase(env, target, phase){
498 def salt = new com.mirantis.mk.Salt()
499 def common = new com.mirantis.mk.Common()
500
501 services = getOpenStackUpgradeServices(env, target)
502 def st
503
504 for (service in services){
505 st = "${service}.upgrade.${phase}".trim()
506 common.infoMsg("Running ${phase} for service ${st} on ${target}")
507 salt.enforceState(env, target, st)
508 }
509}
510
511
512/**
513 * Run OpenStack states on specified node.
514 *
515 * @param env Salt Connection object or env
516 * @param target The target node to run states on.
517**/
518def applyOpenstackAppsStates(env, target){
519 def salt = new com.mirantis.mk.Salt()
520 def common = new com.mirantis.mk.Common()
521
522 services = getOpenStackUpgradeServices(env, target)
523 def st
524
525 for (service in services){
526 st = "${service}".trim()
527 common.infoMsg("Running ${st} on ${target}")
528 salt.enforceState(env, target, st)
529 }
530}
531
Martin Polreichddfe51c2019-01-21 14:31:00 +0100532def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100533 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200534 def galera = new com.mirantis.mk.Galera()
535 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
536 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100537}
538
Martin Polreich47c09d12018-12-21 16:42:06 +0100539def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100540 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200541 def galera = new com.mirantis.mk.Galera()
542 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
543 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100544}
545
Martin Polreich47c09d12018-12-21 16:42:06 +0100546def getGaleraLastShutdownNode(env) {
Martin Polreich47c09d12018-12-21 16:42:06 +0100547 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200548 def galera = new com.mirantis.mk.Galera()
549 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
550 return galera.getGaleraLastShutdownNode(env)
Martin Polreich47c09d12018-12-21 16:42:06 +0100551}
552
Martin Polreichdb692582019-04-25 14:32:36 +0200553def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100554 def common = new com.mirantis.mk.Common()
Martin Polreichf89f9b42019-05-07 15:37:13 +0200555 def galera = new com.mirantis.mk.Galera()
556 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
557 return galera.restoreGaleraDb(env)
Martin Polreich65864b02018-12-05 10:42:50 +0100558}