blob: 0785bbe0c919876418c4056158e8d49614f54184 [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',
Mykyta Karpine7c78612020-09-09 11:24:41 +030067 'docutils==0.16',
68 'pyrsistent<0.17.1',
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000069 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040070
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000071 if (version == 'kilo') {
72 requirements = openstack_kilo_packages
73 } else if (version == 'liberty') {
74 requirements = openstack_kilo_packages
75 } else if (version == 'mitaka') {
76 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020077 } else {
Aleksey Zvyagintsevde345a92019-07-30 11:32:45 +000078 requirements = openstack_latest_packages
Sergey Kolekonovba203982016-12-21 18:32:17 +040079 }
Vasyl Saienko9a2bd372020-01-13 10:00:04 +020080 pythonLib.setupVirtualenv(path, python, requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040081}
82
83/**
84 * create connection to OpenStack API endpoint
85 *
Jakub Josef6c963762018-01-18 16:02:22 +010086 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040087 * @param url OpenStack API endpoint address
88 * @param credentialsId Credentials to the OpenStack API
89 * @param project OpenStack project to connect to
90 */
Jakub Josef6c963762018-01-18 16:02:22 +010091def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020092 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040093 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010094 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040095 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030096 rc = """set +x
97export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010098export OS_PASSWORD=${creds.password.toString()}
99export OS_TENANT_NAME=${project}
100export OS_AUTH_URL=${url}
101export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +0400102export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +0000103export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +0400104export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +0000105export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +0300106export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +0200107export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300108set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100109"""
110 writeFile file: rcFile, text: rc
111 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400112}
113
114/**
115 * Run command with OpenStack env params and optional python env
116 *
117 * @param cmd Command to be executed
118 * @param env Environmental parameters with endpoint credentials
119 * @param path Optional path to virtualenv with specific clients
120 */
121def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400122 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400123 openstackCmd = ". ${venv}; ${cmd}"
124 if (path) {
125 output = python.runVirtualenvCommand(path, openstackCmd)
126 }
127 else {
128 echo("[Command]: ${openstackCmd}")
129 output = sh (
130 script: openstackCmd,
131 returnStdout: true
132 ).trim()
133 }
134 return output
135}
136
137/**
138 * Get OpenStack Keystone token for current credentials
139 *
140 * @param env Connection parameters for OpenStack API endpoint
141 * @param path Optional path to the custom virtualenv
142 */
143def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400144 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000145 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400146 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100147 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400148 return output
149}
150
151/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200152 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400153 *
154 * @param env Connection parameters for OpenStack API endpoint
155 * @param path Optional path to the custom virtualenv
156 */
157def createHeatEnv(file, environment = [], original_file = null) {
158 if (original_file) {
159 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100160 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400161 envString = "parameters:\n"
162 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100163
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100164 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100165 for (int i = 0; i < p.size(); i++) {
166 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400167 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100168
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100169 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400170 writeFile file: file, text: envString
171}
172
173/**
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200174 * Create new OpenStack Heat stack. Will wait for action to be complited in
175 * specified amount of time (by default 120min)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400176 *
177 * @param env Connection parameters for OpenStack API endpoint
178 * @param template HOT template for the new Heat stack
179 * @param environment Environmentale parameters of the new Heat stack
180 * @param name Name of the new Heat stack
181 * @param path Optional path to the custom virtualenv
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200182 * @param timeout Optional number in minutes to wait for stack action is applied.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400183 */
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200184def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400185 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200186 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
187 def envFile
188 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400189 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200190 envFile = "${env.WORKSPACE}/template/env/${name}.env"
191 if (environment.contains("/")) {
192 //init() returns all elements but the last in a collection.
193 def envPath = environment.tokenize("/").init().join("/")
194 if (envPath) {
195 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
196 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200197 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200198 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400199 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200200 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400201 envFile = "${env.WORKSPACE}/template/${name}.env"
202 createHeatEnv(envFile, params)
203 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200204
Mykyta Karpincf44f812017-08-28 14:45:21 +0300205 def cmd
Vasyl Saienkob91df802019-01-23 17:22:57 +0200206 def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}"
Mykyta Karpincf44f812017-08-28 14:45:21 +0300207
Tomáš Kukrála1152742017-08-22 16:21:50 +0200208 if (action == "create") {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200209 cmd = "openstack stack create ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200210 } else {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200211 cmd = "openstack stack update ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200212 }
213
Sergey Kolekonovba203982016-12-21 18:32:17 +0400214 dir("${env.WORKSPACE}/template/template") {
Vasyl Saienkod4254192019-01-23 18:02:01 +0200215 def out = runOpenstackCommand(cmd, client, path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400216 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400217}
218
219/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200220 * Returns list of stacks for stack name filter
221 *
222 * @param client Connection parameters for OpenStack API endpoint
223 * @param filter Stack name filter
224 * @param path Optional path to the custom virtualenv
225 */
226def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200227 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200228 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
229}
230
231
232/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200233 * Get list of stack names with given stack status
234 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200235 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200236 * @param status Stack status
237 * @param path Optional path to the custom virtualenv
238 */
239 def getStacksWithStatus(client, status, path = null) {
240 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
241 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
242 }
243
244/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400245 * Get life cycle status for existing OpenStack Heat stack
246 *
247 * @param env Connection parameters for OpenStack API endpoint
248 * @param name Name of the managed Heat stack instance
249 * @param path Optional path to the custom virtualenv
250 */
251def getHeatStackStatus(client, name, path = null) {
252 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
253 return runOpenstackCommand(cmd, client, path)
254}
255
256/**
257 * Get info about existing OpenStack Heat stack
258 *
259 * @param env Connection parameters for OpenStack API endpoint
260 * @param name Name of the managed Heat stack instance
261 * @param path Optional path to the custom virtualenv
262 */
263def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400264 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400265 cmd = "heat stack-show ${name}"
266 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100267 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400268 return output
269}
270
271/**
272 * Get existing OpenStack Heat stack output parameter
273 *
274 * @param env Connection parameters for OpenStack API endpoint
275 * @param name Name of the managed Heat stack
276 * @param parameter Name of the output parameter
277 * @param path Optional path to the custom virtualenv
278 */
279def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000280 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400281 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100282 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300283 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
284 // versions returns string without "".
285 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
286 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400287}
288
289/**
290 * List all resources from existing OpenStack Heat stack
291 *
292 * @param env Connection parameters for OpenStack API endpoint
293 * @param name Name of the managed Heat stack instance
294 * @param path Optional path to the custom virtualenv
Mykyta Karpin72306362018-02-08 16:40:43 +0200295 * @param depth Optional depth of stack for listing resources,
296 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400297 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200298def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400299 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200300 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400301 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100302 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400303 return output
304}
305
306/**
307 * Get info about resource from existing OpenStack Heat stack
308 *
309 * @param env Connection parameters for OpenStack API endpoint
310 * @param name Name of the managed Heat stack instance
311 * @param path Optional path to the custom virtualenv
312 */
313def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400314 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400315 cmd = "heat resource-show ${name} ${resource}"
316 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100317 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400318 return output
319}
320
321/**
322 * Update 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 updateHeatStack(env, name, 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 stack-update ${name}"
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 * Delete 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 deleteHeatStack(env, name, path = null) {
344 cmd = "heat stack-delete ${name}"
345 outputTable = runOpenstackCommand(cmd, env, path)
346}
347
348/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200349 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400350 *
351 * @param env Connection parameters for OpenStack API endpoint
352 * @param name Name of the managed Heat stack instance
353 * @param path Optional path to the custom virtualenv
354 */
355def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200356 // set depth to 1000 to ensure all nested resources are shown
357 resources = getHeatStackResources(env, name, path, 1000)
358 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400359 for (resource in resources) {
360 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200361 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200362 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400363 }
364 }
365 echo("[Stack ${name}] Servers: ${servers}")
366 return servers
367}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100368
369/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300370 * Delete nova key pair
371 *
372 * @param env Connection parameters for OpenStack API endpoint
373 * @param name Name of the key pair to delete
374 * @param path Optional path to the custom virtualenv
375 */
376def deleteKeyPair(env, name, path = null) {
377 def common = new com.mirantis.mk.Common()
378 common.infoMsg("Removing key pair ${name}")
379 def cmd = "openstack keypair delete ${name}"
380 runOpenstackCommand(cmd, env, path)
381}
382
383/**
Oleksii Grudev69382ce2020-01-03 15:31:57 +0200384 * Check if Nova keypair exists and delete it.
385 *
386 * @param env Connection parameters for OpenStack API endpoint
387 * @param name Name of the key pair to delete
388 * @param path Path to virtualenv
389**/
390def ensureKeyPairRemoved(String name, env, path) {
391 def common = new com.mirantis.mk.Common()
392 def keypairs = runOpenstackCommand("openstack keypair list -f value -c Name", env, path).tokenize('\n')
393 if (name in keypairs) {
394 deleteKeyPair(env, name, path)
395 common.infoMsg("Keypair ${name} has been deleted")
396 } else {
397 common.warningMsg("Keypair ${name} not found")
398 }
399}
400
401/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300402 * Get nova key pair
403 *
404 * @param env Connection parameters for OpenStack API endpoint
405 * @param name Name of the key pair to show
406 * @param path Optional path to the custom virtualenv
407 */
408
409def getKeyPair(env, name, path = null) {
410 def common = new com.mirantis.mk.Common()
411 def cmd = "openstack keypair show ${name}"
412 def outputTable
413 try {
414 outputTable = runOpenstackCommand(cmd, env, path)
415 } catch (Exception e) {
416 common.infoMsg("Key pair ${name} not found")
417 }
418 return outputTable
419}
420
421/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100422 * Stops all services that contain specific string (for example nova,heat, etc.)
423 * @param env Salt Connection object or pepperEnv
424 * @param probe single node on which to list service names
425 * @param target all targeted nodes
426 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100427 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100428 * @return output of salt commands
429 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100430def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100431 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100432 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100433 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400434 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100435 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100436 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100437 if (servicesList) {
438 try {
439 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
440 for (name in servicesList) {
441 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400442 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100443 }
444 }
445 } catch (Exception er) {
446 common.infoMsg("skipping stopping ${servicesList} services")
447 }
448 }
449 } else {
450 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100451 for (name in servicesList) {
452 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400453 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100454 }
455 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100456 }
457 }
458 }
459}
460
461/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300462 * Return intersection of globally installed services and those are
463 * defined on specific target according to theirs priorities.
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200464 * By default services are added to the result list only if
465 * <service>.upgrade.enabled pillar is set to "True". However if it
466 * is needed to obtain list of upgrade services regardless of
467 * <service>.upgrade.enabled pillar value it is needed to set
468 * "upgrade_condition" param to "False".
Vasyl Saienko4129e102018-09-03 10:15:52 +0300469 *
470 * @param env Salt Connection object or env
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200471 * @param target The target node to get list of apps for
472 * @param upgrade_condition Whether to take "upgrade:enabled"
473 * service pillar into consideration
474 * when obtaining list of upgrade services
Vasyl Saienko4129e102018-09-03 10:15:52 +0300475**/
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200476def getOpenStackUpgradeServices(env, target, upgrade_condition=true){
Vasyl Saienko4129e102018-09-03 10:15:52 +0300477 def salt = new com.mirantis.mk.Salt()
478 def common = new com.mirantis.mk.Common()
479
480 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
481 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200482 if (upgrade_condition) {
483 node_pillar = salt.getPillar(env, target)
484 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300485 def node_sorted_apps = []
486 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
487 Map<String,Integer> _sorted_apps = [:]
488 for (k in global_apps['return'][0].values()[0].keySet()) {
489 if (k in node_apps) {
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200490 if (upgrade_condition) {
491 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0] != null) {
492 if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0].toBoolean()) {
493 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
494 }
Oleksii Grudev3116a732019-02-14 18:16:05 +0200495 }
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200496 } else {
497 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
Oleksii Grudev3116a732019-02-14 18:16:05 +0200498 }
Vasyl Saienko4129e102018-09-03 10:15:52 +0300499 }
500 }
501 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
502 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
503 } else {
504 common.errorMsg("No applications found.")
505 }
506
507 return node_sorted_apps
508}
509
Vasyl Saienko4129e102018-09-03 10:15:52 +0300510/**
511 * Run specified upgrade phase for all services on given node.
512 *
513 * @param env Salt Connection object or env
514 * @param target The target node to run states on.
515 * @param phase The phase name to run.
516**/
517def runOpenStackUpgradePhase(env, target, phase){
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}.upgrade.${phase}".trim()
526 common.infoMsg("Running ${phase} for service ${st} on ${target}")
527 salt.enforceState(env, target, st)
528 }
529}
530
531
532/**
533 * Run OpenStack states on specified node.
534 *
535 * @param env Salt Connection object or env
536 * @param target The target node to run states on.
537**/
538def applyOpenstackAppsStates(env, target){
539 def salt = new com.mirantis.mk.Salt()
540 def common = new com.mirantis.mk.Common()
541
542 services = getOpenStackUpgradeServices(env, target)
543 def st
544
545 for (service in services){
546 st = "${service}".trim()
547 common.infoMsg("Running ${st} on ${target}")
548 salt.enforceState(env, target, st)
549 }
550}
551
Martin Polreich232ad902019-01-21 14:31:00 +0100552def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100553 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100554 def galera = new com.mirantis.mk.Galera()
555 common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.")
556 return galera.verifyGaleraStatus(env, slave, checkTimeSync)
Martin Polreich65864b02018-12-05 10:42:50 +0100557}
558
Martin Polreich9a5d6682018-12-21 16:42:06 +0100559def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100560 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100561 def galera = new com.mirantis.mk.Galera()
562 common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.")
563 return galera.validateAndPrintGaleraStatusReport(env, out, minion)
Martin Polreich65864b02018-12-05 10:42:50 +0100564}
565
Martin Polreich9a5d6682018-12-21 16:42:06 +0100566def getGaleraLastShutdownNode(env) {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100567 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100568 def galera = new com.mirantis.mk.Galera()
569 common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.")
570 return galera.getGaleraLastShutdownNode(env)
Martin Polreich9a5d6682018-12-21 16:42:06 +0100571}
572
Ivan Berezovskiy004cac22019-02-01 17:03:28 +0400573def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100574 def common = new com.mirantis.mk.Common()
Martin Polreich8f0f3ac2019-02-15 10:03:33 +0100575 def galera = new com.mirantis.mk.Galera()
576 common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.")
577 return galera.restoreGaleraDb(env)
Oleksii Grudev3aaadc22019-03-14 10:54:58 +0200578}