blob: eedfbd8eccf2e61bd115684b66a41acf481a6d66 [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
Tomáš Kukrálbee0b992017-08-10 16:50:40 +020025def setupOpenstackVirtualenv(path, version = 'latest') {
iberezovskiyd4240b52017-02-20 17:18:28 +040026 def python = new com.mirantis.mk.Python()
Vasyl Saienko030fc182017-07-12 14:54:42 +030027 python.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',
Jakub Josef60280212017-08-10 19:01:19 +020044 'docutils'
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"',
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020057 'python-openstackclient',
58 'python-heatclient',
Jakub Josef60280212017-08-10 19:01:19 +020059 'docutils'
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020060 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040061
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020062 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040063 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020064 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040065 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020066 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040067 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020068 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040069 requirements = openstack_latest_packages
70 }
Tomáš Kukrálbee0b992017-08-10 16:50:40 +020071 python.setupVirtualenv(path, 'python2', requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040072}
73
74/**
75 * create connection to OpenStack API endpoint
76 *
Jakub Josef6c963762018-01-18 16:02:22 +010077 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040078 * @param url OpenStack API endpoint address
79 * @param credentialsId Credentials to the OpenStack API
80 * @param project OpenStack project to connect to
81 */
Jakub Josef6c963762018-01-18 16:02:22 +010082def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020083 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040084 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010085 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040086 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030087 rc = """set +x
88export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010089export OS_PASSWORD=${creds.password.toString()}
90export OS_TENANT_NAME=${project}
91export OS_AUTH_URL=${url}
92export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040093export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000094export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040095export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000096export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +030097export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020098export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030099set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100100"""
101 writeFile file: rcFile, text: rc
102 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400103}
104
105/**
106 * Run command with OpenStack env params and optional python env
107 *
108 * @param cmd Command to be executed
109 * @param env Environmental parameters with endpoint credentials
110 * @param path Optional path to virtualenv with specific clients
111 */
112def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400113 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400114 openstackCmd = ". ${venv}; ${cmd}"
115 if (path) {
116 output = python.runVirtualenvCommand(path, openstackCmd)
117 }
118 else {
119 echo("[Command]: ${openstackCmd}")
120 output = sh (
121 script: openstackCmd,
122 returnStdout: true
123 ).trim()
124 }
125 return output
126}
127
128/**
129 * Get OpenStack Keystone token for current credentials
130 *
131 * @param env Connection parameters for OpenStack API endpoint
132 * @param path Optional path to the custom virtualenv
133 */
134def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400135 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000136 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400137 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100138 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400139 return output
140}
141
142/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200143 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400144 *
145 * @param env Connection parameters for OpenStack API endpoint
146 * @param path Optional path to the custom virtualenv
147 */
148def createHeatEnv(file, environment = [], original_file = null) {
149 if (original_file) {
150 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100151 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400152 envString = "parameters:\n"
153 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100154
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100155 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100156 for (int i = 0; i < p.size(); i++) {
157 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400158 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100159
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100160 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400161 writeFile file: file, text: envString
162}
163
164/**
165 * Create new OpenStack Heat stack
166 *
167 * @param env Connection parameters for OpenStack API endpoint
168 * @param template HOT template for the new Heat stack
169 * @param environment Environmentale parameters of the new Heat stack
170 * @param name Name of the new Heat stack
171 * @param path Optional path to the custom virtualenv
172 */
Tomáš Kukrála1152742017-08-22 16:21:50 +0200173def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") {
iberezovskiyd4240b52017-02-20 17:18:28 +0400174 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200175 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
176 def envFile
177 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400178 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200179 envFile = "${env.WORKSPACE}/template/env/${name}.env"
180 if (environment.contains("/")) {
181 //init() returns all elements but the last in a collection.
182 def envPath = environment.tokenize("/").init().join("/")
183 if (envPath) {
184 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
185 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200186 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200187 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400188 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200189 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400190 envFile = "${env.WORKSPACE}/template/${name}.env"
191 createHeatEnv(envFile, params)
192 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200193
Mykyta Karpincf44f812017-08-28 14:45:21 +0300194 def cmd
195 def waitState
196
Tomáš Kukrála1152742017-08-22 16:21:50 +0200197 if (action == "create") {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300198 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
199 waitState = "CREATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200200 } else {
Mykyta Karpincf44f812017-08-28 14:45:21 +0300201 cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}"
202 waitState = "UPDATE_COMPLETE"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200203 }
204
Sergey Kolekonovba203982016-12-21 18:32:17 +0400205 dir("${env.WORKSPACE}/template/template") {
206 outputTable = runOpenstackCommand(cmd, client, path)
207 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200208
Ales Komareke11e8792016-12-28 09:42:25 +0100209 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400210
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200211 def heatStatusCheckerCount = 1
Jakub Josefe0337272017-09-12 14:15:27 +0200212 while (heatStatusCheckerCount <= 250) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400213 status = getHeatStackStatus(client, name, path)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200214 echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100215
Tomáš Kukrála1152742017-08-22 16:21:50 +0200216 if (status.contains('CREATE_FAILED')) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400217 info = getHeatStackInfo(client, name, path)
218 throw new Exception(info.stack_status_reason)
Tomáš Kukrála1152742017-08-22 16:21:50 +0200219
220 } else if (status.contains(waitState)) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400221 info = getHeatStackInfo(client, name, path)
222 echo(info.stack_status_reason)
223 break
224 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200225
Jakub Josefe0337272017-09-12 14:15:27 +0200226 sleep(30)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200227 heatStatusCheckerCount++
Sergey Kolekonovba203982016-12-21 18:32:17 +0400228 }
229 echo("[Heat Stack] Status: ${status}")
230}
231
232/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200233 * Returns list of stacks for stack name filter
234 *
235 * @param client Connection parameters for OpenStack API endpoint
236 * @param filter Stack name filter
237 * @param path Optional path to the custom virtualenv
238 */
239def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200240 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200241 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
242}
243
244
245/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200246 * Get list of stack names with given stack status
247 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200248 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200249 * @param status Stack status
250 * @param path Optional path to the custom virtualenv
251 */
252 def getStacksWithStatus(client, status, path = null) {
253 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
254 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
255 }
256
257/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400258 * Get life cycle status for existing OpenStack Heat stack
259 *
260 * @param env Connection parameters for OpenStack API endpoint
261 * @param name Name of the managed Heat stack instance
262 * @param path Optional path to the custom virtualenv
263 */
264def getHeatStackStatus(client, name, path = null) {
265 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
266 return runOpenstackCommand(cmd, client, path)
267}
268
269/**
270 * Get info about existing OpenStack Heat stack
271 *
272 * @param env Connection parameters for OpenStack API endpoint
273 * @param name Name of the managed Heat stack instance
274 * @param path Optional path to the custom virtualenv
275 */
276def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400277 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400278 cmd = "heat stack-show ${name}"
279 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100280 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400281 return output
282}
283
284/**
285 * Get existing OpenStack Heat stack output parameter
286 *
287 * @param env Connection parameters for OpenStack API endpoint
288 * @param name Name of the managed Heat stack
289 * @param parameter Name of the output parameter
290 * @param path Optional path to the custom virtualenv
291 */
292def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000293 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400294 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100295 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300296 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
297 // versions returns string without "".
298 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
299 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400300}
301
302/**
303 * List all resources 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
Mykyta Karpin72306362018-02-08 16:40:43 +0200308 * @param depth Optional depth of stack for listing resources,
309 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400310 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200311def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400312 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200313 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400314 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100315 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400316 return output
317}
318
319/**
320 * Get info about resource from existing OpenStack Heat stack
321 *
322 * @param env Connection parameters for OpenStack API endpoint
323 * @param name Name of the managed Heat stack instance
324 * @param path Optional path to the custom virtualenv
325 */
326def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400327 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400328 cmd = "heat resource-show ${name} ${resource}"
329 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100330 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400331 return output
332}
333
334/**
335 * Update existing OpenStack Heat stack
336 *
337 * @param env Connection parameters for OpenStack API endpoint
338 * @param name Name of the managed Heat stack instance
339 * @param path Optional path to the custom virtualenv
340 */
341def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400342 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400343 cmd = "heat stack-update ${name}"
344 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100345 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400346 return output
347}
348
349/**
350 * Delete existing OpenStack Heat stack
351 *
352 * @param env Connection parameters for OpenStack API endpoint
353 * @param name Name of the managed Heat stack instance
354 * @param path Optional path to the custom virtualenv
355 */
356def deleteHeatStack(env, name, path = null) {
357 cmd = "heat stack-delete ${name}"
358 outputTable = runOpenstackCommand(cmd, env, path)
359}
360
361/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200362 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400363 *
364 * @param env Connection parameters for OpenStack API endpoint
365 * @param name Name of the managed Heat stack instance
366 * @param path Optional path to the custom virtualenv
367 */
368def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200369 // set depth to 1000 to ensure all nested resources are shown
370 resources = getHeatStackResources(env, name, path, 1000)
371 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400372 for (resource in resources) {
373 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200374 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200375 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400376 }
377 }
378 echo("[Stack ${name}] Servers: ${servers}")
379 return servers
380}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100381
382/**
383 * Stops all services that contain specific string (for example nova,heat, etc.)
384 * @param env Salt Connection object or pepperEnv
385 * @param probe single node on which to list service names
386 * @param target all targeted nodes
387 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100388 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100389 * @return output of salt commands
390 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100391def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100392 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100393 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100394 for (s in services) {
395 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, "${probe}*", "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100396 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100397 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100398 if (servicesList) {
399 try {
400 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
401 for (name in servicesList) {
402 if (!name.contains('Salt command')) {
403 salt.runSaltProcessStep(env, "${target}*", 'service.stop', ["${name}"])
404 }
405 }
406 } catch (Exception er) {
407 common.infoMsg("skipping stopping ${servicesList} services")
408 }
409 }
410 } else {
411 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100412 for (name in servicesList) {
413 if (!name.contains('Salt command')) {
414 salt.runSaltProcessStep(env, "${target}*", 'service.stop', ["${name}"])
415 }
416 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100417 }
418 }
419 }
420}
421
422/**
423 * Restores Galera database
424 * @param env Salt Connection object or pepperEnv
425 * @return output of salt commands
426 */
427def restoreGaleraDb(env) {
428 def salt = new com.mirantis.mk.Salt()
429 def common = new com.mirantis.mk.Common()
430 try {
431 salt.runSaltProcessStep(env, 'I@galera:slave', 'service.stop', ['mysql'])
432 } catch (Exception er) {
433 common.warningMsg('Mysql service already stopped')
434 }
435 try {
436 salt.runSaltProcessStep(env, 'I@galera:master', 'service.stop', ['mysql'])
437 } catch (Exception er) {
438 common.warningMsg('Mysql service already stopped')
439 }
440 try {
441 salt.cmdRun(env, 'I@galera:slave', "rm /var/lib/mysql/ib_logfile*")
442 } catch (Exception er) {
443 common.warningMsg('Files are not present')
444 }
445 try {
446 salt.cmdRun(env, 'I@galera:master', "mkdir /root/mysql/mysql.bak")
447 } catch (Exception er) {
448 common.warningMsg('Directory already exists')
449 }
450 try {
451 salt.cmdRun(env, 'I@galera:master', "rm -rf /root/mysql/mysql.bak/*")
452 } catch (Exception er) {
453 common.warningMsg('Directory already empty')
454 }
455 try {
456 salt.cmdRun(env, 'I@galera:master', "mv /var/lib/mysql/* /root/mysql/mysql.bak")
457 } catch (Exception er) {
458 common.warningMsg('Files were already moved')
459 }
460 try {
461 salt.runSaltProcessStep(env, 'I@galera:master', 'file.remove', ["/var/lib/mysql/.galera_bootstrap"])
462 } catch (Exception er) {
463 common.warningMsg('File is not present')
464 }
465 salt.cmdRun(env, 'I@galera:master', "sed -i '/gcomm/c\\wsrep_cluster_address=\"gcomm://\"' /etc/mysql/my.cnf")
466 def backup_dir = salt.getReturnValues(salt.getPillar(env, "I@galera:master", 'xtrabackup:client:backup_dir'))
467 if(backup_dir == null || backup_dir.isEmpty()) { backup_dir='/var/backups/mysql/xtrabackup' }
468 salt.runSaltProcessStep(env, 'I@galera:master', 'file.remove', ["${backup_dir}/dbrestored"])
469 salt.cmdRun(env, 'I@xtrabackup:client', "su root -c 'salt-call state.sls xtrabackup'")
470 salt.runSaltProcessStep(env, 'I@galera:master', 'service.start', ['mysql'])
471
472 // wait until mysql service on galera master is up
Jiri Broulik22b04572018-02-16 12:02:41 +0100473 try {
474 salt.commandStatus(env, 'I@galera:master', 'service mysql status', 'running')
475 } catch (Exception er) {
476 input message: "Database is not running please fix it first and only then click on PROCEED."
477 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100478
479 salt.runSaltProcessStep(env, 'I@galera:slave', 'service.start', ['mysql'])
480}