blob: 4ae71ace556c06522c5e059e445e90da44011e06 [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 *
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000021 * @param path Path where virtualenv is created
22 * @param version Version of the OpenStack clients
Sergey Kolekonovba203982016-12-21 18:32:17 +040023 */
24
Vasyl Saienko9a2bd372020-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
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000029 def openstack_kilo_packages = [
30 //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463
31 'cliff==2.8',
32 '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',
39 'python-openstackclient>=1.7.0,<1.8.0',
40 '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 Egorenko97ef0fb2020-01-13 14:22:49 +040044 'docutils==0.16'
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000045 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040046
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000047 def openstack_latest_packages = [
48 //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463
49 'cliff==2.8',
50 // 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 // the same for warlock package due: https://github.com/bcwaldon/warlock/commit/4241a7a9fbccfce7eb3298c2abdf00ca2dede64a
53 // TODO(vsaienko): use upper-constraints here, as in requirements we set only lowest library
54 // versions.
55 'cmd2<0.9.0;python_version=="2.7"',
56 'cmd2>=0.9.1;python_version=="3.4"',
57 'cmd2>=0.9.1;python_version=="3.5"',
58 'warlock<=1.3.1;python_version=="2.7"',
59 'warlock>1.3.1;python_version=="3.4"',
60 'warlock>1.3.1;python_version=="3.5"',
Denis Egorenko97ef0fb2020-01-13 14:22:49 +040061 // NOTE: pin client packages to current latest to prevent
62 // downloading packages which are not support Python 2.7
63 'python-openstackclient==4.0.0',
Mykyta Karpin4ed4a812020-03-26 16:31:26 +020064 'openstacksdk<0.44.0',
Denis Egorenko97ef0fb2020-01-13 14:22:49 +040065 'python-octaviaclient==1.11.0',
Vasyl Saienkof5df2022020-01-13 18:25:44 +020066 'python-heatclient==1.18.0',
Denis Egorenko97ef0fb2020-01-13 14:22:49 +040067 'docutils==0.16'
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000068 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040069
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000070 if (version == 'kilo') {
71 requirements = openstack_kilo_packages
72 } else if (version == 'liberty') {
73 requirements = openstack_kilo_packages
74 } else if (version == 'mitaka') {
75 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020076 } else {
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000077 requirements = openstack_latest_packages
Sergey Kolekonovba203982016-12-21 18:32:17 +040078 }
Vasyl Saienko9a2bd372020-01-13 10:00:04 +020079 pythonLib.setupVirtualenv(path, python, requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040080}
81
82/**
83 * create connection to OpenStack API endpoint
84 *
Jakub Josef6c963762018-01-18 16:02:22 +010085 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040086 * @param url OpenStack API endpoint address
87 * @param credentialsId Credentials to the OpenStack API
88 * @param project OpenStack project to connect to
89 */
Jakub Josef6c963762018-01-18 16:02:22 +010090def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020091 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040092 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010093 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040094 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030095 rc = """set +x
96export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010097export OS_PASSWORD=${creds.password.toString()}
98export OS_TENANT_NAME=${project}
99export OS_AUTH_URL=${url}
100export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +0400101export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +0000102export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +0400103export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +0000104export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +0300105export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +0200106export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300107set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100108"""
109 writeFile file: rcFile, text: rc
110 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400111}
112
113/**
114 * Run command with OpenStack env params and optional python env
115 *
116 * @param cmd Command to be executed
117 * @param env Environmental parameters with endpoint credentials
118 * @param path Optional path to virtualenv with specific clients
119 */
120def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400121 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400122 openstackCmd = ". ${venv}; ${cmd}"
123 if (path) {
124 output = python.runVirtualenvCommand(path, openstackCmd)
125 }
126 else {
127 echo("[Command]: ${openstackCmd}")
128 output = sh (
129 script: openstackCmd,
130 returnStdout: true
131 ).trim()
132 }
133 return output
134}
135
136/**
137 * Get OpenStack Keystone token for current credentials
138 *
139 * @param env Connection parameters for OpenStack API endpoint
140 * @param path Optional path to the custom virtualenv
141 */
142def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400143 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000144 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400145 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100146 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400147 return output
148}
149
150/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200151 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400152 *
153 * @param env Connection parameters for OpenStack API endpoint
154 * @param path Optional path to the custom virtualenv
155 */
156def createHeatEnv(file, environment = [], original_file = null) {
157 if (original_file) {
158 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100159 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400160 envString = "parameters:\n"
161 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100162
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100163 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100164 for (int i = 0; i < p.size(); i++) {
165 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400166 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100167
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100168 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400169 writeFile file: file, text: envString
170}
171
172/**
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200173 * Create new OpenStack Heat stack. Will wait for action to be complited in
174 * specified amount of time (by default 120min)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400175 *
176 * @param env Connection parameters for OpenStack API endpoint
177 * @param template HOT template for the new Heat stack
178 * @param environment Environmentale parameters of the new Heat stack
179 * @param name Name of the new Heat stack
180 * @param path Optional path to the custom virtualenv
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200181 * @param timeout Optional number in minutes to wait for stack action is applied.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400182 */
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200183def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400184 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200185 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
186 def envFile
187 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400188 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200189 envFile = "${env.WORKSPACE}/template/env/${name}.env"
190 if (environment.contains("/")) {
191 //init() returns all elements but the last in a collection.
192 def envPath = environment.tokenize("/").init().join("/")
193 if (envPath) {
194 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
195 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200196 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200197 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400198 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200199 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400200 envFile = "${env.WORKSPACE}/template/${name}.env"
201 createHeatEnv(envFile, params)
202 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200203
Mykyta Karpincf44f812017-08-28 14:45:21 +0300204 def cmd
Vasyl Saienkob91df802019-01-23 17:22:57 +0200205 def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}"
Mykyta Karpincf44f812017-08-28 14:45:21 +0300206
Tomáš Kukrála1152742017-08-22 16:21:50 +0200207 if (action == "create") {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200208 cmd = "openstack stack create ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200209 } else {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200210 cmd = "openstack stack update ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200211 }
212
Sergey Kolekonovba203982016-12-21 18:32:17 +0400213 dir("${env.WORKSPACE}/template/template") {
Vasyl Saienkod4254192019-01-23 18:02:01 +0200214 def out = runOpenstackCommand(cmd, client, path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400215 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400216}
217
218/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200219 * Returns list of stacks for stack name filter
220 *
221 * @param client Connection parameters for OpenStack API endpoint
222 * @param filter Stack name filter
223 * @param path Optional path to the custom virtualenv
224 */
225def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200226 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200227 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
228}
229
230
231/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200232 * Get list of stack names with given stack status
233 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200234 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200235 * @param status Stack status
236 * @param path Optional path to the custom virtualenv
237 */
238 def getStacksWithStatus(client, status, path = null) {
239 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
240 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
241 }
242
243/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400244 * Get life cycle status for existing OpenStack Heat stack
245 *
246 * @param env Connection parameters for OpenStack API endpoint
247 * @param name Name of the managed Heat stack instance
248 * @param path Optional path to the custom virtualenv
249 */
250def getHeatStackStatus(client, name, path = null) {
251 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
252 return runOpenstackCommand(cmd, client, path)
253}
254
255/**
256 * Get info about existing OpenStack Heat stack
257 *
258 * @param env Connection parameters for OpenStack API endpoint
259 * @param name Name of the managed Heat stack instance
260 * @param path Optional path to the custom virtualenv
261 */
262def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400263 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400264 cmd = "heat stack-show ${name}"
265 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100266 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400267 return output
268}
269
270/**
271 * Get existing OpenStack Heat stack output parameter
272 *
273 * @param env Connection parameters for OpenStack API endpoint
274 * @param name Name of the managed Heat stack
275 * @param parameter Name of the output parameter
276 * @param path Optional path to the custom virtualenv
277 */
278def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000279 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400280 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100281 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300282 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
283 // versions returns string without "".
284 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
285 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400286}
287
288/**
289 * List all resources from existing OpenStack Heat stack
290 *
291 * @param env Connection parameters for OpenStack API endpoint
292 * @param name Name of the managed Heat stack instance
293 * @param path Optional path to the custom virtualenv
Mykyta Karpin72306362018-02-08 16:40:43 +0200294 * @param depth Optional depth of stack for listing resources,
295 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400296 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200297def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400298 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200299 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400300 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100301 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400302 return output
303}
304
305/**
306 * Get info about resource 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
311 */
312def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400313 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400314 cmd = "heat resource-show ${name} ${resource}"
315 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100316 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400317 return output
318}
319
320/**
321 * Update existing OpenStack Heat stack
322 *
323 * @param env Connection parameters for OpenStack API endpoint
324 * @param name Name of the managed Heat stack instance
325 * @param path Optional path to the custom virtualenv
326 */
327def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400328 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400329 cmd = "heat stack-update ${name}"
330 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100331 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400332 return output
333}
334
335/**
336 * Delete existing OpenStack Heat stack
337 *
338 * @param env Connection parameters for OpenStack API endpoint
339 * @param name Name of the managed Heat stack instance
340 * @param path Optional path to the custom virtualenv
341 */
342def deleteHeatStack(env, name, path = null) {
343 cmd = "heat stack-delete ${name}"
344 outputTable = runOpenstackCommand(cmd, env, path)
345}
346
347/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200348 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400349 *
350 * @param env Connection parameters for OpenStack API endpoint
351 * @param name Name of the managed Heat stack instance
352 * @param path Optional path to the custom virtualenv
353 */
354def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200355 // set depth to 1000 to ensure all nested resources are shown
356 resources = getHeatStackResources(env, name, path, 1000)
357 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400358 for (resource in resources) {
359 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200360 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200361 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400362 }
363 }
364 echo("[Stack ${name}] Servers: ${servers}")
365 return servers
366}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100367
368/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300369 * Delete nova key pair
370 *
371 * @param env Connection parameters for OpenStack API endpoint
372 * @param name Name of the key pair to delete
373 * @param path Optional path to the custom virtualenv
374 */
375def deleteKeyPair(env, name, path = null) {
376 def common = new com.mirantis.mk.Common()
377 common.infoMsg("Removing key pair ${name}")
378 def cmd = "openstack keypair delete ${name}"
379 runOpenstackCommand(cmd, env, path)
380}
381
382/**
Oleksii Grudev69382ce2020-01-03 15:31:57 +0200383 * Check if Nova keypair exists and delete it.
384 *
385 * @param env Connection parameters for OpenStack API endpoint
386 * @param name Name of the key pair to delete
387 * @param path Path to virtualenv
388**/
389def ensureKeyPairRemoved(String name, env, path) {
390 def common = new com.mirantis.mk.Common()
391 def keypairs = runOpenstackCommand("openstack keypair list -f value -c Name", env, path).tokenize('\n')
392 if (name in keypairs) {
393 deleteKeyPair(env, name, path)
394 common.infoMsg("Keypair ${name} has been deleted")
395 } else {
396 common.warningMsg("Keypair ${name} not found")
397 }
398}
399
400/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300401 * Get nova key pair
402 *
403 * @param env Connection parameters for OpenStack API endpoint
404 * @param name Name of the key pair to show
405 * @param path Optional path to the custom virtualenv
406 */
407
408def getKeyPair(env, name, path = null) {
409 def common = new com.mirantis.mk.Common()
410 def cmd = "openstack keypair show ${name}"
411 def outputTable
412 try {
413 outputTable = runOpenstackCommand(cmd, env, path)
414 } catch (Exception e) {
415 common.infoMsg("Key pair ${name} not found")
416 }
417 return outputTable
418}
419
420/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100421 * Stops all services that contain specific string (for example nova,heat, etc.)
422 * @param env Salt Connection object or pepperEnv
423 * @param probe single node on which to list service names
424 * @param target all targeted nodes
425 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100426 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100427 * @return output of salt commands
428 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100429def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100430 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100431 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100432 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400433 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100434 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100435 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100436 if (servicesList) {
437 try {
438 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
439 for (name in servicesList) {
440 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400441 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100442 }
443 }
444 } catch (Exception er) {
445 common.infoMsg("skipping stopping ${servicesList} services")
446 }
447 }
448 } else {
449 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100450 for (name in servicesList) {
451 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400452 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100453 }
454 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100455 }
456 }
457 }
458}
459
460/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300461 * Return intersection of globally installed services and those are
462 * defined on specific target according to theirs priorities.
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200463 * By default services are added to the result list only if
464 * <service>.upgrade.enabled pillar is set to "True". However if it
465 * is needed to obtain list of upgrade services regardless of
466 * <service>.upgrade.enabled pillar value it is needed to set
467 * "upgrade_condition" param to "False".
Vasyl Saienko4129e102018-09-03 10:15:52 +0300468 *
469 * @param env Salt Connection object or env
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200470 * @param target The target node to get list of apps for
471 * @param upgrade_condition Whether to take "upgrade:enabled"
472 * service pillar into consideration
473 * when obtaining list of upgrade services
Vasyl Saienko4129e102018-09-03 10:15:52 +0300474**/
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200475def getOpenStackUpgradeServices(env, target, upgrade_condition=true){
Vasyl Saienko4129e102018-09-03 10:15:52 +0300476 def salt = new com.mirantis.mk.Salt()
477 def common = new com.mirantis.mk.Common()
478
479 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
480 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200481 if (upgrade_condition) {
482 node_pillar = salt.getPillar(env, target)
483 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300484 def node_sorted_apps = []
485 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
486 Map<String,Integer> _sorted_apps = [:]
487 for (k in global_apps['return'][0].values()[0].keySet()) {
488 if (k in node_apps) {
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200489 if (upgrade_condition) {
490 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0] != null) {
491 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0].toBoolean()) {
492 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
493 }
Oleksii Grudev3116a732019-02-14 18:16:05 +0200494 }
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200495 } else {
496 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
Oleksii Grudev3116a732019-02-14 18:16:05 +0200497 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300498 }
499 }
500 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
501 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
502 } else {
503 common.errorMsg("No applications found.")
504 }
505
506 return node_sorted_apps
507}
508
Vasyl Saienko4129e102018-09-03 10:15:52 +0300509/**
510 * Run specified upgrade phase for all services on given node.
511 *
512 * @param env Salt Connection object or env
513 * @param target The target node to run states on.
514 * @param phase The phase name to run.
515**/
516def runOpenStackUpgradePhase(env, target, phase){
517 def salt = new com.mirantis.mk.Salt()
518 def common = new com.mirantis.mk.Common()
519
520 services = getOpenStackUpgradeServices(env, target)
521 def st
522
523 for (service in services){
524 st = "${service}.upgrade.${phase}".trim()
525 common.infoMsg("Running ${phase} for service ${st} on ${target}")
526 salt.enforceState(env, target, st)
527 }
528}
529
530
531/**
532 * Run OpenStack states on specified node.
533 *
534 * @param env Salt Connection object or env
535 * @param target The target node to run states on.
536**/
537def applyOpenstackAppsStates(env, target){
538 def salt = new com.mirantis.mk.Salt()
539 def common = new com.mirantis.mk.Common()
540
541 services = getOpenStackUpgradeServices(env, target)
542 def st
543
544 for (service in services){
545 st = "${service}".trim()
546 common.infoMsg("Running ${st} on ${target}")
547 salt.enforceState(env, target, st)
548 }
549}
550
Martin Polreich232ad902019-01-21 14:31:00 +0100551def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100552 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100553 def galera = new com.mirantis.mk.Galera()
554 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
555 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100556}
557
Martin Polreich9a5d6682018-12-21 16:42:06 +0100558def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100559 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100560 def galera = new com.mirantis.mk.Galera()
561 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
562 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100563}
564
Martin Polreich9a5d6682018-12-21 16:42:06 +0100565def getGaleraLastShutdownNode(env) {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100566 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100567 def galera = new com.mirantis.mk.Galera()
568 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
569 return galera.getGaleraLastShutdownNode(env)
Martin Polreich9a5d6682018-12-21 16:42:06 +0100570}
571
Ivan Berezovskiy004cac22019-02-01 17:03:28 +0400572def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100573 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100574 def galera = new com.mirantis.mk.Galera()
575 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
576 return galera.restoreGaleraDb(env)
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200577}