blob: af697dafe9bfc4c93fdb192d29100fee2b2919b4 [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',
vnaumova64a4a62019-03-05 18:39:55 +010058 'python-octaviaclient',
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020059 'python-heatclient',
Jakub Josef60280212017-08-10 19:01:19 +020060 'docutils'
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020061 ]
Sergey Kolekonovba203982016-12-21 18:32:17 +040062
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020063 if (version == 'kilo') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040064 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020065 } else if (version == 'liberty') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040066 requirements = openstack_kilo_packages
Tomáš Kukrálc6a94c62017-06-19 14:44:24 +020067 } else if (version == 'mitaka') {
Sergey Kolekonovba203982016-12-21 18:32:17 +040068 requirements = openstack_kilo_packages
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020069 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +040070 requirements = openstack_latest_packages
71 }
Tomáš Kukrálbee0b992017-08-10 16:50:40 +020072 python.setupVirtualenv(path, 'python2', requirements, null, true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040073}
74
75/**
76 * create connection to OpenStack API endpoint
77 *
Jakub Josef6c963762018-01-18 16:02:22 +010078 * @param path Path to created venv
Sergey Kolekonovba203982016-12-21 18:32:17 +040079 * @param url OpenStack API endpoint address
80 * @param credentialsId Credentials to the OpenStack API
81 * @param project OpenStack project to connect to
82 */
Jakub Josef6c963762018-01-18 16:02:22 +010083def createOpenstackEnv(path, url, credentialsId, project, project_domain="default",
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020084 project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") {
iberezovskiyd4240b52017-02-20 17:18:28 +040085 def common = new com.mirantis.mk.Common()
Jakub Josef6c963762018-01-18 16:02:22 +010086 rcFile = "${path}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040087 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030088 rc = """set +x
89export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010090export OS_PASSWORD=${creds.password.toString()}
91export OS_TENANT_NAME=${project}
92export OS_AUTH_URL=${url}
93export OS_AUTH_STRATEGY=keystone
kairat_kushaev0a26bf72017-05-18 13:20:09 +040094export OS_PROJECT_NAME=${project}
Jakub Josefbd927322017-05-30 13:20:27 +000095export OS_PROJECT_ID=${project_id}
kairat_kushaev0a26bf72017-05-18 13:20:09 +040096export OS_PROJECT_DOMAIN_ID=${project_domain}
Jakub Josefbd927322017-05-30 13:20:27 +000097export OS_USER_DOMAIN_NAME=${user_domain}
Kirill Mashchenko234708f2017-07-20 17:00:01 +030098export OS_IDENTITY_API_VERSION=${api_ver}
Tomáš Kukrál381a8c92017-06-21 09:01:52 +020099export OS_CACERT=${cacert}
Alexander Tivelkovf89a1882017-01-11 13:29:35 +0300100set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +0100101"""
102 writeFile file: rcFile, text: rc
103 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +0400104}
105
106/**
107 * Run command with OpenStack env params and optional python env
108 *
109 * @param cmd Command to be executed
110 * @param env Environmental parameters with endpoint credentials
111 * @param path Optional path to virtualenv with specific clients
112 */
113def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400114 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400115 openstackCmd = ". ${venv}; ${cmd}"
116 if (path) {
117 output = python.runVirtualenvCommand(path, openstackCmd)
118 }
119 else {
120 echo("[Command]: ${openstackCmd}")
121 output = sh (
122 script: openstackCmd,
123 returnStdout: true
124 ).trim()
125 }
126 return output
127}
128
129/**
130 * Get OpenStack Keystone token for current credentials
131 *
132 * @param env Connection parameters for OpenStack API endpoint
133 * @param path Optional path to the custom virtualenv
134 */
135def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400136 def python = new com.mirantis.mk.Python()
Jakub Josefbd927322017-05-30 13:20:27 +0000137 cmd = "openstack token issue"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400138 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100139 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400140 return output
141}
142
143/**
Ales Komarek51b7b152017-06-27 11:14:50 +0200144 * Create OpenStack environment file
Sergey Kolekonovba203982016-12-21 18:32:17 +0400145 *
146 * @param env Connection parameters for OpenStack API endpoint
147 * @param path Optional path to the custom virtualenv
148 */
149def createHeatEnv(file, environment = [], original_file = null) {
150 if (original_file) {
151 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100152 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400153 envString = "parameters:\n"
154 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100155
Tomáš Kukrálc3964e52017-02-22 14:07:37 +0100156 p = entries(environment)
Tomáš Kukrálb1fe9642017-02-22 11:21:17 +0100157 for (int i = 0; i < p.size(); i++) {
158 envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400159 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100160
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100161 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400162 writeFile file: file, text: envString
163}
164
165/**
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200166 * Create new OpenStack Heat stack. Will wait for action to be complited in
167 * specified amount of time (by default 120min)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400168 *
169 * @param env Connection parameters for OpenStack API endpoint
170 * @param template HOT template for the new Heat stack
171 * @param environment Environmentale parameters of the new Heat stack
172 * @param name Name of the new Heat stack
173 * @param path Optional path to the custom virtualenv
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200174 * @param timeout Optional number in minutes to wait for stack action is applied.
Sergey Kolekonovba203982016-12-21 18:32:17 +0400175 */
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200176def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400177 def python = new com.mirantis.mk.Python()
Jakub Josef0a898762017-08-11 16:27:44 +0200178 def templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
179 def envFile
180 def envSource
Sergey Kolekonovba203982016-12-21 18:32:17 +0400181 if (environment) {
Tomáš Kukrála1152742017-08-22 16:21:50 +0200182 envFile = "${env.WORKSPACE}/template/env/${name}.env"
183 if (environment.contains("/")) {
184 //init() returns all elements but the last in a collection.
185 def envPath = environment.tokenize("/").init().join("/")
186 if (envPath) {
187 envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env"
188 }
Ales Komarek51b7b152017-06-27 11:14:50 +0200189 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200190 envSource = "${env.WORKSPACE}/template/env/${environment}.env"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400191 createHeatEnv(envFile, params, envSource)
Jakub Josef9a59aeb2017-08-11 15:50:20 +0200192 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400193 envFile = "${env.WORKSPACE}/template/${name}.env"
194 createHeatEnv(envFile, params)
195 }
Tomáš Kukrála1152742017-08-22 16:21:50 +0200196
Mykyta Karpincf44f812017-08-28 14:45:21 +0300197 def cmd
Vasyl Saienkob91df802019-01-23 17:22:57 +0200198 def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}"
Mykyta Karpincf44f812017-08-28 14:45:21 +0300199
Tomáš Kukrála1152742017-08-22 16:21:50 +0200200 if (action == "create") {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200201 cmd = "openstack stack create ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200202 } else {
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200203 cmd = "openstack stack update ${cmd_args}"
Tomáš Kukrála1152742017-08-22 16:21:50 +0200204 }
205
Sergey Kolekonovba203982016-12-21 18:32:17 +0400206 dir("${env.WORKSPACE}/template/template") {
Vasyl Saienkod4254192019-01-23 18:02:01 +0200207 def out = runOpenstackCommand(cmd, client, path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400208 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400209}
210
211/**
Jakub Josefdb4baf22017-05-10 15:16:09 +0200212 * Returns list of stacks for stack name filter
213 *
214 * @param client Connection parameters for OpenStack API endpoint
215 * @param filter Stack name filter
216 * @param path Optional path to the custom virtualenv
217 */
218def getStacksForNameContains(client, filter, path = null){
Jakub Josef6465fca2017-05-10 16:09:20 +0200219 cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true'
Jakub Josefdb4baf22017-05-10 15:16:09 +0200220 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
221}
222
223
224/**
Jakub Josef5e238a22017-04-19 16:35:15 +0200225 * Get list of stack names with given stack status
226 *
Jakub Josefdb4baf22017-05-10 15:16:09 +0200227 * @param client Connection parameters for OpenStack API endpoint
Jakub Josef5e238a22017-04-19 16:35:15 +0200228 * @param status Stack status
229 * @param path Optional path to the custom virtualenv
230 */
231 def getStacksWithStatus(client, status, path = null) {
232 cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\''
233 return runOpenstackCommand(cmd, client, path).trim().tokenize("\n")
234 }
235
236/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400237 * Get life cycle status for existing OpenStack Heat stack
238 *
239 * @param env Connection parameters for OpenStack API endpoint
240 * @param name Name of the managed Heat stack instance
241 * @param path Optional path to the custom virtualenv
242 */
243def getHeatStackStatus(client, name, path = null) {
244 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
245 return runOpenstackCommand(cmd, client, path)
246}
247
248/**
249 * Get info about existing OpenStack Heat stack
250 *
251 * @param env Connection parameters for OpenStack API endpoint
252 * @param name Name of the managed Heat stack instance
253 * @param path Optional path to the custom virtualenv
254 */
255def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400256 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400257 cmd = "heat stack-show ${name}"
258 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100259 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400260 return output
261}
262
263/**
264 * Get existing OpenStack Heat stack output parameter
265 *
266 * @param env Connection parameters for OpenStack API endpoint
267 * @param name Name of the managed Heat stack
268 * @param parameter Name of the output parameter
269 * @param path Optional path to the custom virtualenv
270 */
271def getHeatStackOutputParam(env, name, outputParam, path = null) {
Vasyl Saienkoea4b2812017-07-10 10:36:03 +0000272 cmd = "heat output-show ${name} ${outputParam}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400273 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100274 echo("${cmd}: ${output}")
Vasyl Saienko2a1c2de2017-07-11 11:41:53 +0300275 // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later
276 // versions returns string without "".
277 // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient
278 return "${output}".replaceAll('"', '')
Sergey Kolekonovba203982016-12-21 18:32:17 +0400279}
280
281/**
282 * List all resources from existing OpenStack Heat stack
283 *
284 * @param env Connection parameters for OpenStack API endpoint
285 * @param name Name of the managed Heat stack instance
286 * @param path Optional path to the custom virtualenv
Mykyta Karpin72306362018-02-08 16:40:43 +0200287 * @param depth Optional depth of stack for listing resources,
288 * 0 - do not list nested resources
Sergey Kolekonovba203982016-12-21 18:32:17 +0400289 */
Mykyta Karpin72306362018-02-08 16:40:43 +0200290def getHeatStackResources(env, name, path = null, depth = 0) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400291 def python = new com.mirantis.mk.Python()
Mykyta Karpin72306362018-02-08 16:40:43 +0200292 cmd = "heat resource-list --nested-depth ${depth} ${name}"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400293 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100294 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400295 return output
296}
297
298/**
299 * Get info about resource from existing OpenStack Heat stack
300 *
301 * @param env Connection parameters for OpenStack API endpoint
302 * @param name Name of the managed Heat stack instance
303 * @param path Optional path to the custom virtualenv
304 */
305def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400306 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400307 cmd = "heat resource-show ${name} ${resource}"
308 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100309 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400310 return output
311}
312
313/**
314 * Update existing OpenStack Heat stack
315 *
316 * @param env Connection parameters for OpenStack API endpoint
317 * @param name Name of the managed Heat stack instance
318 * @param path Optional path to the custom virtualenv
319 */
320def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400321 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400322 cmd = "heat stack-update ${name}"
323 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100324 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400325 return output
326}
327
328/**
329 * Delete existing OpenStack Heat stack
330 *
331 * @param env Connection parameters for OpenStack API endpoint
332 * @param name Name of the managed Heat stack instance
333 * @param path Optional path to the custom virtualenv
334 */
335def deleteHeatStack(env, name, path = null) {
336 cmd = "heat stack-delete ${name}"
337 outputTable = runOpenstackCommand(cmd, env, path)
338}
339
340/**
Mykyta Karpin72306362018-02-08 16:40:43 +0200341 * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack
Sergey Kolekonovba203982016-12-21 18:32:17 +0400342 *
343 * @param env Connection parameters for OpenStack API endpoint
344 * @param name Name of the managed Heat stack instance
345 * @param path Optional path to the custom virtualenv
346 */
347def getHeatStackServers(env, name, path = null) {
Mykyta Karpin72306362018-02-08 16:40:43 +0200348 // set depth to 1000 to ensure all nested resources are shown
349 resources = getHeatStackResources(env, name, path, 1000)
350 servers = [:]
Sergey Kolekonovba203982016-12-21 18:32:17 +0400351 for (resource in resources) {
352 if (resource.resource_type == 'OS::Nova::Server') {
Mykyta Karpin67978112018-02-22 11:16:45 +0200353 server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path)
Mykyta Karpin72306362018-02-08 16:40:43 +0200354 servers[server.attributes.id] = server.attributes.name
Sergey Kolekonovba203982016-12-21 18:32:17 +0400355 }
356 }
357 echo("[Stack ${name}] Servers: ${servers}")
358 return servers
359}
Jiri Broulikf8f96942018-02-15 10:03:42 +0100360
361/**
Mykyta Karpin8306a9d2018-07-27 11:34:10 +0300362 * Delete nova key pair
363 *
364 * @param env Connection parameters for OpenStack API endpoint
365 * @param name Name of the key pair to delete
366 * @param path Optional path to the custom virtualenv
367 */
368def deleteKeyPair(env, name, path = null) {
369 def common = new com.mirantis.mk.Common()
370 common.infoMsg("Removing key pair ${name}")
371 def cmd = "openstack keypair delete ${name}"
372 runOpenstackCommand(cmd, env, path)
373}
374
375/**
376 * Get nova key pair
377 *
378 * @param env Connection parameters for OpenStack API endpoint
379 * @param name Name of the key pair to show
380 * @param path Optional path to the custom virtualenv
381 */
382
383def getKeyPair(env, name, path = null) {
384 def common = new com.mirantis.mk.Common()
385 def cmd = "openstack keypair show ${name}"
386 def outputTable
387 try {
388 outputTable = runOpenstackCommand(cmd, env, path)
389 } catch (Exception e) {
390 common.infoMsg("Key pair ${name} not found")
391 }
392 return outputTable
393}
394
395/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100396 * Stops all services that contain specific string (for example nova,heat, etc.)
397 * @param env Salt Connection object or pepperEnv
398 * @param probe single node on which to list service names
399 * @param target all targeted nodes
400 * @param services lists of type of services to be stopped
Jiri Broulikf6daac62018-03-08 13:17:53 +0100401 * @param confirm enable/disable manual service stop confirmation
Jiri Broulikf8f96942018-02-15 10:03:42 +0100402 * @return output of salt commands
403 */
Jiri Broulik27e83052018-03-06 11:37:29 +0100404def stopServices(env, probe, target, services=[], confirm=false) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100405 def salt = new com.mirantis.mk.Salt()
Jiri Broulikf6daac62018-03-08 13:17:53 +0100406 def common = new com.mirantis.mk.Common()
Jiri Broulikf8f96942018-02-15 10:03:42 +0100407 for (s in services) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400408 def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'"))
Jiri Broulikf6daac62018-03-08 13:17:53 +0100409 def servicesList = outputServicesStr.tokenize("\n").init()
Jiri Broulik27e83052018-03-06 11:37:29 +0100410 if (confirm) {
Jiri Broulikf6daac62018-03-08 13:17:53 +0100411 if (servicesList) {
412 try {
413 input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them."
414 for (name in servicesList) {
415 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400416 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulikf6daac62018-03-08 13:17:53 +0100417 }
418 }
419 } catch (Exception er) {
420 common.infoMsg("skipping stopping ${servicesList} services")
421 }
422 }
423 } else {
424 if (servicesList) {
Jiri Broulik27e83052018-03-06 11:37:29 +0100425 for (name in servicesList) {
426 if (!name.contains('Salt command')) {
Dmitry Ukovd72cd2a2018-09-04 17:31:46 +0400427 salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"])
Jiri Broulik27e83052018-03-06 11:37:29 +0100428 }
429 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100430 }
431 }
432 }
433}
434
435/**
Vasyl Saienko4129e102018-09-03 10:15:52 +0300436 * Return intersection of globally installed services and those are
437 * defined on specific target according to theirs priorities.
438 *
439 * @param env Salt Connection object or env
440 * @param target The target node to get list of apps for.
441**/
442def getOpenStackUpgradeServices(env, target){
443 def salt = new com.mirantis.mk.Salt()
444 def common = new com.mirantis.mk.Common()
445
446 def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications')
447 def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0]
448 def node_sorted_apps = []
449 if ( !global_apps['return'][0].values()[0].isEmpty() ) {
450 Map<String,Integer> _sorted_apps = [:]
451 for (k in global_apps['return'][0].values()[0].keySet()) {
452 if (k in node_apps) {
453 _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger()
454 }
455 }
456 node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet()
457 common.infoMsg("Applications are placed in following order:"+node_sorted_apps)
458 } else {
459 common.errorMsg("No applications found.")
460 }
461
462 return node_sorted_apps
463}
464
465
466/**
467 * Run specified upgrade phase for all services on given node.
468 *
469 * @param env Salt Connection object or env
470 * @param target The target node to run states on.
471 * @param phase The phase name to run.
472**/
473def runOpenStackUpgradePhase(env, target, phase){
474 def salt = new com.mirantis.mk.Salt()
475 def common = new com.mirantis.mk.Common()
476
477 services = getOpenStackUpgradeServices(env, target)
478 def st
479
480 for (service in services){
481 st = "${service}.upgrade.${phase}".trim()
482 common.infoMsg("Running ${phase} for service ${st} on ${target}")
483 salt.enforceState(env, target, st)
484 }
485}
486
487
488/**
489 * Run OpenStack states on specified node.
490 *
491 * @param env Salt Connection object or env
492 * @param target The target node to run states on.
493**/
494def applyOpenstackAppsStates(env, target){
495 def salt = new com.mirantis.mk.Salt()
496 def common = new com.mirantis.mk.Common()
497
498 services = getOpenStackUpgradeServices(env, target)
499 def st
500
501 for (service in services){
502 st = "${service}".trim()
503 common.infoMsg("Running ${st} on ${target}")
504 salt.enforceState(env, target, st)
505 }
506}
507
508/**
Martin Polreich65864b02018-12-05 10:42:50 +0100509 * Verifies Galera database
510 *
511 * This function checks for Galera master, tests connection and if reachable, it obtains the result
512 * of Salt mysql.status function. The result is then parsed, validated and outputed to the user.
513 *
514 * @param env Salt Connection object or pepperEnv
Martin Polreich232ad902019-01-21 14:31:00 +0100515 * @param slave Boolean value to enable slave checking (if master in unreachable)
516 * @param checkTimeSync Boolean value to enable time sync check
Martin Polreich65864b02018-12-05 10:42:50 +0100517 * @return resultCode int values used to determine exit status in the calling function
518 */
Martin Polreich232ad902019-01-21 14:31:00 +0100519def verifyGaleraStatus(env, slave=false, checkTimeSync=false) {
Martin Polreich65864b02018-12-05 10:42:50 +0100520 def salt = new com.mirantis.mk.Salt()
521 def common = new com.mirantis.mk.Common()
522 def out = ""
523 def status = "unknown"
Martin Polreich9a5d6682018-12-21 16:42:06 +0100524 def testNode = ""
525 if (!slave) {
526 try {
527 galeraMaster = salt.getMinions(env, "I@galera:master")
528 common.infoMsg("Current Galera master is: ${galeraMaster}")
529 salt.minionsReachable(env, "I@salt:master", "I@galera:master")
530 testNode = "I@galera:master"
531 } catch (Exception e) {
532 common.errorMsg('Galera master is not reachable.')
533 return 128
534 }
535 } else {
536 try {
537 galeraMinions = salt.getMinions(env, "I@galera:slave")
538 common.infoMsg("Testing Galera slave minions: ${galeraMinions}")
539 } catch (Exception e) {
540 common.errorMsg("Cannot obtain Galera slave minions list.")
541 return 129
542 }
543 for (minion in galeraMinions) {
544 try {
545 salt.minionsReachable(env, "I@salt:master", minion)
546 testNode = minion
547 break
548 } catch (Exception e) {
549 common.warningMsg("Slave '${minion}' is not reachable.")
550 }
551 }
552 }
553 if (!testNode) {
554 common.errorMsg("No Galera slave was reachable.")
555 return 130
Martin Polreich65864b02018-12-05 10:42:50 +0100556 }
Martin Polreich232ad902019-01-21 14:31:00 +0100557 if (checkTimeSync && !salt.checkClusterTimeSync(env, "I@galera:master or I@galera:slave")) {
558 common.errorMsg("Time in cluster is desynchronized or it couldn't be detemined. You should fix this issue manually before proceeding.")
559 return 131
560 }
Martin Polreich65864b02018-12-05 10:42:50 +0100561 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100562 out = salt.cmdRun(env, "I@salt:master", "salt -C '${testNode}' mysql.status")
Martin Polreich65864b02018-12-05 10:42:50 +0100563 } catch (Exception e) {
564 common.errorMsg('Could not determine mysql status.')
565 return 256
566 }
567 if (out) {
568 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100569 status = validateAndPrintGaleraStatusReport(env, out, testNode)
Martin Polreich65864b02018-12-05 10:42:50 +0100570 } catch (Exception e) {
571 common.errorMsg('Could not parse the mysql status output. Check it manually.')
572 return 1
573 }
574 } else {
575 common.errorMsg("Mysql status response unrecognized or is empty. Response: ${out}")
576 return 1024
577 }
578 if (status == "OK") {
579 common.infoMsg("No errors found - MySQL status is ${status}.")
580 return 0
581 } else if (status == "unknown") {
582 common.warningMsg('MySQL status cannot be detemined')
583 return 1
584 } else {
585 common.errorMsg("Errors found.")
586 return 2
587 }
588}
589
590/** Validates and prints result of verifyGaleraStatus function
591@param env Salt Connection object or pepperEnv
592@param out Output of the mysql.status Salt function
593@return status "OK", "ERROR" or "uknown" depending on result of validation
594*/
595
Martin Polreich9a5d6682018-12-21 16:42:06 +0100596def validateAndPrintGaleraStatusReport(env, out, minion) {
Martin Polreich65864b02018-12-05 10:42:50 +0100597 def salt = new com.mirantis.mk.Salt()
598 def common = new com.mirantis.mk.Common()
Martin Polreich9a5d6682018-12-21 16:42:06 +0100599 if (minion == "I@galera:master") {
600 role = "master"
601 } else {
602 role = "slave"
603 }
Martin Polreich94321422019-01-17 16:20:24 +0100604 sizeOut = salt.getReturnValues(salt.getPillar(env, minion, "galera:${role}:members"))
Martin Polreich65864b02018-12-05 10:42:50 +0100605 expected_cluster_size = sizeOut.size()
606 outlist = out['return'][0]
607 resultString = outlist.get(outlist.keySet()[0]).replace("\n ", " ").replace(" ", "").replace("Salt command execution success", "").replace("----------", "").replace(": \n", ": no value\n")
608 resultYaml = readYaml text: resultString
609 parameters = [
610 wsrep_cluster_status: [title: 'Cluster status', expectedValues: ['Primary'], description: ''],
611 wsrep_cluster_size: [title: 'Current cluster size', expectedValues: [expected_cluster_size], description: ''],
Martin Polreich9a5d6682018-12-21 16:42:06 +0100612 wsrep_ready: [title: 'Node status', expectedValues: ['ON', true], description: ''],
613 wsrep_local_state_comment: [title: 'Node status comment', expectedValues: ['Joining', 'Waiting on SST', 'Joined', 'Synced', 'Donor'], description: ''],
614 wsrep_connected: [title: 'Node connectivity', expectedValues: ['ON', true], description: ''],
Martin Polreich65864b02018-12-05 10:42:50 +0100615 wsrep_local_recv_queue_avg: [title: 'Average size of local reveived queue', expectedThreshold: [warn: 0.5, error: 1.0], description: '(Value above 0 means that the node cannot apply write-sets as fast as it receives them, which can lead to replication throttling)'],
616 wsrep_local_send_queue_avg: [title: 'Average size of local send queue', expectedThreshold: [warn: 0.5, error: 1.0], description: '(Value above 0 indicate replication throttling or network throughput issues, such as a bottleneck on the network link.)']
617 ]
Martin Polreich65864b02018-12-05 10:42:50 +0100618 for (key in parameters.keySet()) {
619 value = resultYaml[key]
620 parameters.get(key) << [actualValue: value]
621 }
622 for (key in parameters.keySet()) {
623 param = parameters.get(key)
624 if (key == 'wsrep_local_recv_queue_avg' || key == 'wsrep_local_send_queue_avg') {
625 if (param.get('actualValue') > param.get('expectedThreshold').get('error')) {
626 param << [match: 'error']
627 } else if (param.get('actualValue') > param.get('expectedThreshold').get('warn')) {
628 param << [match: 'warn']
629 } else {
630 param << [match: 'ok']
631 }
632 } else {
633 for (expValue in param.get('expectedValues')) {
634 if (expValue == param.get('actualValue')) {
635 param << [match: 'ok']
636 break
637 } else {
638 param << [match: 'error']
639 }
640 }
641 }
642 }
643 cluster_info_report = []
644 cluster_warning_report = []
645 cluster_error_report = []
646 for (key in parameters.keySet()) {
647 param = parameters.get(key)
648 if (param.containsKey('expectedThreshold')) {
649 expValues = "below ${param.get('expectedThreshold').get('warn')}"
650 } else {
651 if (param.get('expectedValues').size() > 1) {
652 expValues = param.get('expectedValues').join(' or ')
653 } else {
654 expValues = param.get('expectedValues')[0]
655 }
656 }
657 reportString = "${param.title}: ${param.actualValue} (Expected: ${expValues}) ${param.description}"
658 if (param.get('match').equals('ok')) {
659 cluster_info_report.add("[OK ] ${reportString}")
660 } else if (param.get('match').equals('warn')) {
661 cluster_warning_report.add("[WARNING] ${reportString}")
662 } else {
663 cluster_error_report.add("[ ERROR] ${reportString})")
664 }
665 }
666 common.infoMsg("CLUSTER STATUS REPORT: ${cluster_info_report.size()} expected values, ${cluster_warning_report.size()} warnings and ${cluster_error_report.size()} error found:")
667 if (cluster_info_report.size() > 0) {
668 common.infoMsg(cluster_info_report.join('\n'))
669 }
670 if (cluster_warning_report.size() > 0) {
671 common.warningMsg(cluster_warning_report.join('\n'))
672 }
673 if (cluster_error_report.size() > 0) {
674 common.errorMsg(cluster_error_report.join('\n'))
675 return "ERROR"
676 } else {
677 return "OK"
678 }
679}
680
Martin Polreich9a5d6682018-12-21 16:42:06 +0100681def getGaleraLastShutdownNode(env) {
682 def salt = new com.mirantis.mk.Salt()
683 def common = new com.mirantis.mk.Common()
684 members = ''
685 lastNode = [ip: '', seqno: -2]
686 try {
687 members = salt.getReturnValues(salt.getPillar(env, "I@galera:master", "galera:master:members"))
688 } catch (Exception er) {
689 common.errorMsg('Could not retrieve members list')
690 return 'I@galera:master'
691 }
692 if (members) {
693 for (member in members) {
694 try {
695 salt.minionsReachable(env, 'I@salt:master', "S@${member.host}")
696 out = salt.getReturnValues(salt.cmdRun(env, "S@${member.host}", 'cat /var/lib/mysql/grastate.dat | grep "seqno" | cut -d ":" -f2', true, null, false))
697 seqno = out.tokenize('\n')[0].trim()
698 if (seqno.isNumber()) {
699 seqno = seqno.toInteger()
700 } else {
701 seqno = -2
702 }
703 highestSeqno = lastNode.get('seqno')
704 if (seqno > highestSeqno) {
705 lastNode << [ip: "${member.host}", seqno: seqno]
706 }
707 } catch (Exception er) {
708 common.warningMsg("Could not determine 'seqno' value for node ${member.host} ")
709 }
710 }
711 }
712 if (lastNode.get('ip') != '') {
713 return "S@${lastNode.ip}"
714 } else {
715 return "I@galera:master"
716 }
717}
718
Martin Polreich65864b02018-12-05 10:42:50 +0100719/**
Jiri Broulikf8f96942018-02-15 10:03:42 +0100720 * Restores Galera database
721 * @param env Salt Connection object or pepperEnv
722 * @return output of salt commands
723 */
Ivan Berezovskiy004cac22019-02-01 17:03:28 +0400724def restoreGaleraDb(env) {
Jiri Broulikf8f96942018-02-15 10:03:42 +0100725 def salt = new com.mirantis.mk.Salt()
726 def common = new com.mirantis.mk.Common()
727 try {
728 salt.runSaltProcessStep(env, 'I@galera:slave', 'service.stop', ['mysql'])
729 } catch (Exception er) {
730 common.warningMsg('Mysql service already stopped')
731 }
732 try {
733 salt.runSaltProcessStep(env, 'I@galera:master', 'service.stop', ['mysql'])
734 } catch (Exception er) {
735 common.warningMsg('Mysql service already stopped')
736 }
Martin Polreich9a5d6682018-12-21 16:42:06 +0100737 lastNodeTarget = getGaleraLastShutdownNode(env)
Jiri Broulikf8f96942018-02-15 10:03:42 +0100738 try {
739 salt.cmdRun(env, 'I@galera:slave', "rm /var/lib/mysql/ib_logfile*")
740 } catch (Exception er) {
741 common.warningMsg('Files are not present')
742 }
743 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100744 salt.cmdRun(env, 'I@galera:slave', "rm /var/lib/mysql/grastate.dat")
745 } catch (Exception er) {
746 common.warningMsg('Files are not present')
747 }
748 try {
749 salt.cmdRun(env, lastNodeTarget, "mkdir /root/mysql/mysql.bak")
Jiri Broulikf8f96942018-02-15 10:03:42 +0100750 } catch (Exception er) {
751 common.warningMsg('Directory already exists')
752 }
753 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100754 salt.cmdRun(env, lastNodeTarget, "rm -rf /root/mysql/mysql.bak/*")
Jiri Broulikf8f96942018-02-15 10:03:42 +0100755 } catch (Exception er) {
756 common.warningMsg('Directory already empty')
757 }
758 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100759 salt.cmdRun(env, lastNodeTarget, "mv /var/lib/mysql/* /root/mysql/mysql.bak")
Jiri Broulikf8f96942018-02-15 10:03:42 +0100760 } catch (Exception er) {
761 common.warningMsg('Files were already moved')
762 }
763 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100764 salt.runSaltProcessStep(env, lastNodeTarget, 'file.remove', ["/var/lib/mysql/.galera_bootstrap"])
Jiri Broulikf8f96942018-02-15 10:03:42 +0100765 } catch (Exception er) {
766 common.warningMsg('File is not present')
767 }
Martin Polreich9a5d6682018-12-21 16:42:06 +0100768 salt.cmdRun(env, lastNodeTarget, "sed -i '/gcomm/c\\wsrep_cluster_address=\"gcomm://\"' /etc/mysql/my.cnf")
769 def backup_dir = salt.getReturnValues(salt.getPillar(env, lastNodeTarget, 'xtrabackup:client:backup_dir'))
Jiri Broulikf8f96942018-02-15 10:03:42 +0100770 if(backup_dir == null || backup_dir.isEmpty()) { backup_dir='/var/backups/mysql/xtrabackup' }
Martin Polreich9a5d6682018-12-21 16:42:06 +0100771 salt.runSaltProcessStep(env, lastNodeTarget, 'file.remove', ["${backup_dir}/dbrestored"])
Jiri Broulikf8f96942018-02-15 10:03:42 +0100772 salt.cmdRun(env, 'I@xtrabackup:client', "su root -c 'salt-call state.sls xtrabackup'")
Martin Polreich9a5d6682018-12-21 16:42:06 +0100773 salt.runSaltProcessStep(env, lastNodeTarget, 'service.start', ['mysql'])
Jiri Broulikf8f96942018-02-15 10:03:42 +0100774
775 // wait until mysql service on galera master is up
Jiri Broulik22b04572018-02-16 12:02:41 +0100776 try {
Martin Polreich9a5d6682018-12-21 16:42:06 +0100777 salt.commandStatus(env, lastNodeTarget, 'service mysql status', 'running')
Jiri Broulik22b04572018-02-16 12:02:41 +0100778 } catch (Exception er) {
779 input message: "Database is not running please fix it first and only then click on PROCEED."
780 }
Jiri Broulikf8f96942018-02-15 10:03:42 +0100781
Martin Polreich9a5d6682018-12-21 16:42:06 +0100782 salt.runSaltProcessStep(env, "I@galera:master and not ${lastNodeTarget}", 'service.start', ['mysql'])
783 salt.runSaltProcessStep(env, "I@galera:slave and not ${lastNodeTarget}", 'service.start', ['mysql'])
Vasyl Saienko0adc34b2019-01-23 15:52:37 +0200784}