blob: 0e02f2a4e76574b4f438b7642c411e3badb25ac1 [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',
44 'docutils'
45 ]
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"',
61 'python-openstackclient',
62 'python-octaviaclient',
63 'python-heatclient',
64 'docutils'
65 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040066
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000067 if (version == 'kilo') {
68 requirements = openstack_kilo_packages
69 } else if (version == 'liberty') {
70 requirements = openstack_kilo_packages
71 } else if (version == 'mitaka') {
72 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020073 } else {
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000074 requirements = openstack_latest_packages
Sergey Kolekonovba203982016-12-21 18:32:17 +040075 }
Vasyl Saienko9a2bd372020-01-13 10:00:04 +020076 pythonLib.setupVirtualenv(path, python, requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040077}
78
79/**
80 * create connection to OpenStack API endpoint
81 *
Jakub Josef6c963762018-01-18 16:02:22 +010082 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040083 * @param url OpenStack API endpoint address
84 * @param credentialsId Credentials to the OpenStack API
85 * @param project OpenStack project to connect to
86 */
Jakub Josef6c963762018-01-18 16:02:22 +010087def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020088 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040089 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010090 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040091 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030092 rc = """set +x
93export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010094export OS_PASSWORD=${creds.password.toString()}
95export OS_TENANT_NAME=${project}
96export OS_AUTH_URL=${url}
97export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040098export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000099export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +0400100export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +0000101export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +0300102export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +0200103export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300104set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100105"""
106 writeFile file: rcFile, text: rc
107 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400108}
109
110/**
111 * Run command with OpenStack env params and optional python env
112 *
113 * @param cmd Command to be executed
114 * @param env Environmental parameters with endpoint credentials
115 * @param path Optional path to virtualenv with specific clients
116 */
117def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400118 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400119 openstackCmd = ". ${venv}; ${cmd}"
120 if (path) {
121 output = python.runVirtualenvCommand(path, openstackCmd)
122 }
123 else {
124 echo("[Command]: ${openstackCmd}")
125 output = sh (
126 script: openstackCmd,
127 returnStdout: true
128 ).trim()
129 }
130 return output
131}
132
133/**
134 * Get OpenStack Keystone token for current credentials
135 *
136 * @param env Connection parameters for OpenStack API endpoint
137 * @param path Optional path to the custom virtualenv
138 */
139def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400140 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000141 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400142 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100143 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400144 return output
145}
146
147/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200148 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400149 *
150 * @param env Connection parameters for OpenStack API endpoint
151 * @param path Optional path to the custom virtualenv
152 */
153def createHeatEnv(file, environment = [], original_file = null) {
154 if (original_file) {
155 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100156 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400157 envString = "parameters:\n"
158 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100159
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100160 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100161 for (int i = 0; i < p.size(); i++) {
162 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400163 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100164
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100165 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400166 writeFile file: file, text: envString
167}
168
169/**
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200170 * Create new OpenStack Heat stack. Will wait for action to be complited in
171 * specified amount of time (by default 120min)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400172 *
173 * @param env Connection parameters for OpenStack API endpoint
174 * @param template HOT template for the new Heat stack
175 * @param environment Environmentale parameters of the new Heat stack
176 * @param name Name of the new Heat stack
177 * @param path Optional path to the custom virtualenv
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200178 * @param timeout Optional number in minutes to wait for stack action is applied.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400179 */
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200180def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400181 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200182 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
183 def envFile
184 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400185 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200186 envFile = "${env.WORKSPACE}/template/env/${name}.env"
187 if (environment.contains("/")) {
188 //init() returns all elements but the last in a collection.
189 def envPath = environment.tokenize("/").init().join("/")
190 if (envPath) {
191 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
192 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200193 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200194 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400195 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200196 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400197 envFile = "${env.WORKSPACE}/template/${name}.env"
198 createHeatEnv(envFile, params)
199 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200200
Mykyta Karpincf44f812017-08-28 14:45:21 +0300201 def cmd
Vasyl Saienkob91df802019-01-23 17:22:57 +0200202 def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}"
Mykyta Karpincf44f812017-08-28 14:45:21 +0300203
Tomáš Kukrála1152742017-08-22 16:21:50 +0200204 if (action == "create") {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200205 cmd = "openstack stack create ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200206 } else {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200207 cmd = "openstack stack update ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200208 }
209
Sergey Kolekonovba203982016-12-21 18:32:17 +0400210 dir("${env.WORKSPACE}/template/template") {
Vasyl Saienkod4254192019-01-23 18:02:01 +0200211 def out = runOpenstackCommand(cmd, client, path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400212 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400213}
214
215/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200216 * Returns list of stacks for stack name filter
217 *
218 * @param client Connection parameters for OpenStack API endpoint
219 * @param filter Stack name filter
220 * @param path Optional path to the custom virtualenv
221 */
222def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200223 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200224 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
225}
226
227
228/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200229 * Get list of stack names with given stack status
230 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200231 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200232 * @param status Stack status
233 * @param path Optional path to the custom virtualenv
234 */
235 def getStacksWithStatus(client, status, path = null) {
236 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
237 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
238 }
239
240/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400241 * Get life cycle status for existing OpenStack Heat stack
242 *
243 * @param env Connection parameters for OpenStack API endpoint
244 * @param name Name of the managed Heat stack instance
245 * @param path Optional path to the custom virtualenv
246 */
247def getHeatStackStatus(client, name, path = null) {
248 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
249 return runOpenstackCommand(cmd, client, path)
250}
251
252/**
253 * Get info about existing OpenStack Heat stack
254 *
255 * @param env Connection parameters for OpenStack API endpoint
256 * @param name Name of the managed Heat stack instance
257 * @param path Optional path to the custom virtualenv
258 */
259def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400260 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400261 cmd = "heat stack-show ${name}"
262 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100263 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400264 return output
265}
266
267/**
268 * Get existing OpenStack Heat stack output parameter
269 *
270 * @param env Connection parameters for OpenStack API endpoint
271 * @param name Name of the managed Heat stack
272 * @param parameter Name of the output parameter
273 * @param path Optional path to the custom virtualenv
274 */
275def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000276 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400277 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100278 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300279 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
280 // versions returns string without "".
281 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
282 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400283}
284
285/**
286 * List all resources from existing OpenStack Heat stack
287 *
288 * @param env Connection parameters for OpenStack API endpoint
289 * @param name Name of the managed Heat stack instance
290 * @param path Optional path to the custom virtualenv
Mykyta Karpin72306362018-02-08 16:40:43 +0200291 * @param depth Optional depth of stack for listing resources,
292 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400293 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200294def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400295 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200296 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400297 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100298 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400299 return output
300}
301
302/**
303 * Get info about resource from existing OpenStack Heat stack
304 *
305 * @param env Connection parameters for OpenStack API endpoint
306 * @param name Name of the managed Heat stack instance
307 * @param path Optional path to the custom virtualenv
308 */
309def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400310 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400311 cmd = "heat resource-show ${name} ${resource}"
312 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100313 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400314 return output
315}
316
317/**
318 * Update existing OpenStack Heat stack
319 *
320 * @param env Connection parameters for OpenStack API endpoint
321 * @param name Name of the managed Heat stack instance
322 * @param path Optional path to the custom virtualenv
323 */
324def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400325 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400326 cmd = "heat stack-update ${name}"
327 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100328 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400329 return output
330}
331
332/**
333 * Delete existing OpenStack Heat stack
334 *
335 * @param env Connection parameters for OpenStack API endpoint
336 * @param name Name of the managed Heat stack instance
337 * @param path Optional path to the custom virtualenv
338 */
339def deleteHeatStack(env, name, path = null) {
340 cmd = "heat stack-delete ${name}"
341 outputTable = runOpenstackCommand(cmd, env, path)
342}
343
344/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200345 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400346 *
347 * @param env Connection parameters for OpenStack API endpoint
348 * @param name Name of the managed Heat stack instance
349 * @param path Optional path to the custom virtualenv
350 */
351def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200352 // set depth to 1000 to ensure all nested resources are shown
353 resources = getHeatStackResources(env, name, path, 1000)
354 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400355 for (resource in resources) {
356 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200357 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200358 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400359 }
360 }
361 echo("[Stack ${name}] Servers: ${servers}")
362 return servers
363}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100364
365/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300366 * Delete nova key pair
367 *
368 * @param env Connection parameters for OpenStack API endpoint
369 * @param name Name of the key pair to delete
370 * @param path Optional path to the custom virtualenv
371 */
372def deleteKeyPair(env, name, path = null) {
373 def common = new com.mirantis.mk.Common()
374 common.infoMsg("Removing key pair ${name}")
375 def cmd = "openstack keypair delete ${name}"
376 runOpenstackCommand(cmd, env, path)
377}
378
379/**
Oleksii Grudev69382ce2020-01-03 15:31:57 +0200380 * Check if Nova keypair exists and delete it.
381 *
382 * @param env Connection parameters for OpenStack API endpoint
383 * @param name Name of the key pair to delete
384 * @param path Path to virtualenv
385**/
386def ensureKeyPairRemoved(String name, env, path) {
387 def common = new com.mirantis.mk.Common()
388 def keypairs = runOpenstackCommand("openstack keypair list -f value -c Name", env, path).tokenize('\n')
389 if (name in keypairs) {
390 deleteKeyPair(env, name, path)
391 common.infoMsg("Keypair ${name} has been deleted")
392 } else {
393 common.warningMsg("Keypair ${name} not found")
394 }
395}
396
397/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300398 * Get nova key pair
399 *
400 * @param env Connection parameters for OpenStack API endpoint
401 * @param name Name of the key pair to show
402 * @param path Optional path to the custom virtualenv
403 */
404
405def getKeyPair(env, name, path = null) {
406 def common = new com.mirantis.mk.Common()
407 def cmd = "openstack keypair show ${name}"
408 def outputTable
409 try {
410 outputTable = runOpenstackCommand(cmd, env, path)
411 } catch (Exception e) {
412 common.infoMsg("Key pair ${name} not found")
413 }
414 return outputTable
415}
416
417/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100418 * Stops all services that contain specific string (for example nova,heat, etc.)
419 * @param env Salt Connection object or pepperEnv
420 * @param probe single node on which to list service names
421 * @param target all targeted nodes
422 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100423 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100424 * @return output of salt commands
425 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100426def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100427 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100428 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100429 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400430 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100431 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100432 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100433 if (servicesList) {
434 try {
435 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
436 for (name in servicesList) {
437 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400438 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100439 }
440 }
441 } catch (Exception er) {
442 common.infoMsg("skipping stopping ${servicesList} services")
443 }
444 }
445 } else {
446 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100447 for (name in servicesList) {
448 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400449 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100450 }
451 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100452 }
453 }
454 }
455}
456
457/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300458 * Return intersection of globally installed services and those are
459 * defined on specific target according to theirs priorities.
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200460 * By default services are added to the result list only if
461 * <service>.upgrade.enabled pillar is set to "True". However if it
462 * is needed to obtain list of upgrade services regardless of
463 * <service>.upgrade.enabled pillar value it is needed to set
464 * "upgrade_condition" param to "False".
Vasyl Saienko4129e102018-09-03 10:15:52 +0300465 *
466 * @param env Salt Connection object or env
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200467 * @param target The target node to get list of apps for
468 * @param upgrade_condition Whether to take "upgrade:enabled"
469 * service pillar into consideration
470 * when obtaining list of upgrade services
Vasyl Saienko4129e102018-09-03 10:15:52 +0300471**/
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200472def getOpenStackUpgradeServices(env, target, upgrade_condition=true){
Vasyl Saienko4129e102018-09-03 10:15:52 +0300473 def salt = new com.mirantis.mk.Salt()
474 def common = new com.mirantis.mk.Common()
475
476 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
477 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200478 if (upgrade_condition) {
479 node_pillar = salt.getPillar(env, target)
480 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300481 def node_sorted_apps = []
482 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
483 Map<String,Integer> _sorted_apps = [:]
484 for (k in global_apps['return'][0].values()[0].keySet()) {
485 if (k in node_apps) {
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200486 if (upgrade_condition) {
487 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0] != null) {
488 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0].toBoolean()) {
489 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
490 }
Oleksii Grudev3116a732019-02-14 18:16:05 +0200491 }
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200492 } else {
493 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
Oleksii Grudev3116a732019-02-14 18:16:05 +0200494 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300495 }
496 }
497 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
498 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
499 } else {
500 common.errorMsg("No applications found.")
501 }
502
503 return node_sorted_apps
504}
505
Vasyl Saienko4129e102018-09-03 10:15:52 +0300506/**
507 * Run specified upgrade phase for all services on given node.
508 *
509 * @param env Salt Connection object or env
510 * @param target The target node to run states on.
511 * @param phase The phase name to run.
512**/
513def runOpenStackUpgradePhase(env, target, phase){
514 def salt = new com.mirantis.mk.Salt()
515 def common = new com.mirantis.mk.Common()
516
517 services = getOpenStackUpgradeServices(env, target)
518 def st
519
520 for (service in services){
521 st = "${service}.upgrade.${phase}".trim()
522 common.infoMsg("Running ${phase} for service ${st} on ${target}")
523 salt.enforceState(env, target, st)
524 }
525}
526
527
528/**
529 * Run OpenStack states on specified node.
530 *
531 * @param env Salt Connection object or env
532 * @param target The target node to run states on.
533**/
534def applyOpenstackAppsStates(env, target){
535 def salt = new com.mirantis.mk.Salt()
536 def common = new com.mirantis.mk.Common()
537
538 services = getOpenStackUpgradeServices(env, target)
539 def st
540
541 for (service in services){
542 st = "${service}".trim()
543 common.infoMsg("Running ${st} on ${target}")
544 salt.enforceState(env, target, st)
545 }
546}
547
Martin Polreich232ad902019-01-21 14:31:00 +0100548def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100549 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100550 def galera = new com.mirantis.mk.Galera()
551 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
552 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100553}
554
Martin Polreich9a5d6682018-12-21 16:42:06 +0100555def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100556 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100557 def galera = new com.mirantis.mk.Galera()
558 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
559 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100560}
561
Martin Polreich9a5d6682018-12-21 16:42:06 +0100562def getGaleraLastShutdownNode(env) {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100563 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100564 def galera = new com.mirantis.mk.Galera()
565 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
566 return galera.getGaleraLastShutdownNode(env)
Martin Polreich9a5d6682018-12-21 16:42:06 +0100567}
568
Ivan Berezovskiy004cac22019-02-01 17:03:28 +0400569def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100570 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100571 def galera = new com.mirantis.mk.Galera()
572 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
573 return galera.restoreGaleraDb(env)
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200574}