Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Openstack functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
Tomáš Kukrál | c3964e5 | 2017-02-22 14:07:37 +0100 | [diff] [blame] | 10 | * Convert maps |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | @NonCPS def entries(m) { |
| 15 | return m.collect {k, v -> [k, v]} |
| 16 | } |
| 17 | |
| 18 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 19 | * 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ál | bee0b99 | 2017-08-10 16:50:40 +0200 | [diff] [blame] | 25 | def setupOpenstackVirtualenv(path, version = 'latest') { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 26 | def python = new com.mirantis.mk.Python() |
Vasyl Saienko | 030fc18 | 2017-07-12 14:54:42 +0300 | [diff] [blame] | 27 | python.setupDocutilsVirtualenv(path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 28 | |
| 29 | def openstack_kilo_packages = [ |
Jakub Josef | 268bc84 | 2017-10-10 14:36:17 +0200 | [diff] [blame] | 30 | //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463 |
| 31 | 'cliff==2.8', |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 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', |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 39 | 'python-openstackclient>=1.7.0,<1.8.0', |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 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', |
Jakub Josef | 6028021 | 2017-08-10 19:01:19 +0200 | [diff] [blame] | 44 | 'docutils' |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 45 | ] |
| 46 | |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 47 | def openstack_latest_packages = [ |
Jakub Josef | 268bc84 | 2017-10-10 14:36:17 +0200 | [diff] [blame] | 48 | //XXX: hack to fix https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463 |
| 49 | 'cliff==2.8', |
Vasyl Saienko | 36a019d | 2018-05-30 09:51:18 +0300 | [diff] [blame] | 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 | // 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ál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 57 | 'python-openstackclient', |
| 58 | 'python-heatclient', |
Jakub Josef | 6028021 | 2017-08-10 19:01:19 +0200 | [diff] [blame] | 59 | 'docutils' |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 60 | ] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 61 | |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 62 | if (version == 'kilo') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 63 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 64 | } else if (version == 'liberty') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 65 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 66 | } else if (version == 'mitaka') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 67 | requirements = openstack_kilo_packages |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 68 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 69 | requirements = openstack_latest_packages |
| 70 | } |
Tomáš Kukrál | bee0b99 | 2017-08-10 16:50:40 +0200 | [diff] [blame] | 71 | python.setupVirtualenv(path, 'python2', requirements, null, true) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * create connection to OpenStack API endpoint |
| 76 | * |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 77 | * @param path Path to created venv |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 78 | * @param url OpenStack API endpoint address |
| 79 | * @param credentialsId Credentials to the OpenStack API |
| 80 | * @param project OpenStack project to connect to |
| 81 | */ |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 82 | def createOpenstackEnv(path, url, credentialsId, project, project_domain="default", |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 83 | project_id="", user_domain="default", api_ver="2", cacert="/etc/ssl/certs/ca-certificates.crt") { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 84 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 85 | rcFile = "${path}/keystonerc" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 86 | creds = common.getPasswordCredentials(credentialsId) |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 87 | rc = """set +x |
| 88 | export OS_USERNAME=${creds.username} |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 89 | export OS_PASSWORD=${creds.password.toString()} |
| 90 | export OS_TENANT_NAME=${project} |
| 91 | export OS_AUTH_URL=${url} |
| 92 | export OS_AUTH_STRATEGY=keystone |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 93 | export OS_PROJECT_NAME=${project} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 94 | export OS_PROJECT_ID=${project_id} |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 95 | export OS_PROJECT_DOMAIN_ID=${project_domain} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 96 | export OS_USER_DOMAIN_NAME=${user_domain} |
Kirill Mashchenko | 234708f | 2017-07-20 17:00:01 +0300 | [diff] [blame] | 97 | export OS_IDENTITY_API_VERSION=${api_ver} |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 98 | export OS_CACERT=${cacert} |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 99 | set -x |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 100 | """ |
| 101 | writeFile file: rcFile, text: rc |
| 102 | return rcFile |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 103 | } |
| 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 | */ |
| 112 | def runOpenstackCommand(cmd, venv, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 113 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 114 | 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 | */ |
| 134 | def getKeystoneToken(client, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 135 | def python = new com.mirantis.mk.Python() |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 136 | cmd = "openstack token issue" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 137 | outputTable = runOpenstackCommand(cmd, client, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 138 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 139 | return output |
| 140 | } |
| 141 | |
| 142 | /** |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 143 | * Create OpenStack environment file |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 144 | * |
| 145 | * @param env Connection parameters for OpenStack API endpoint |
| 146 | * @param path Optional path to the custom virtualenv |
| 147 | */ |
| 148 | def createHeatEnv(file, environment = [], original_file = null) { |
| 149 | if (original_file) { |
| 150 | envString = readFile file: original_file |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 151 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 152 | envString = "parameters:\n" |
| 153 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 154 | |
Tomáš Kukrál | c3964e5 | 2017-02-22 14:07:37 +0100 | [diff] [blame] | 155 | p = entries(environment) |
Tomáš Kukrál | b1fe964 | 2017-02-22 11:21:17 +0100 | [diff] [blame] | 156 | for (int i = 0; i < p.size(); i++) { |
| 157 | envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 158 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 159 | |
Tomáš Kukrál | e19ddea | 2017-02-21 11:09:40 +0100 | [diff] [blame] | 160 | echo("writing to env file:\n${envString}") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 161 | writeFile file: file, text: envString |
| 162 | } |
| 163 | |
| 164 | /** |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 165 | * Create new OpenStack Heat stack. Will wait for action to be complited in |
| 166 | * specified amount of time (by default 120min) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 167 | * |
| 168 | * @param env Connection parameters for OpenStack API endpoint |
| 169 | * @param template HOT template for the new Heat stack |
| 170 | * @param environment Environmentale parameters of the new Heat stack |
| 171 | * @param name Name of the new Heat stack |
| 172 | * @param path Optional path to the custom virtualenv |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 173 | * @param timeout Optional number in minutes to wait for stack action is applied. |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 174 | */ |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 175 | def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create", timeout=120) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 176 | def python = new com.mirantis.mk.Python() |
Jakub Josef | 0a89876 | 2017-08-11 16:27:44 +0200 | [diff] [blame] | 177 | def templateFile = "${env.WORKSPACE}/template/template/${template}.hot" |
| 178 | def envFile |
| 179 | def envSource |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 180 | if (environment) { |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 181 | envFile = "${env.WORKSPACE}/template/env/${name}.env" |
| 182 | if (environment.contains("/")) { |
| 183 | //init() returns all elements but the last in a collection. |
| 184 | def envPath = environment.tokenize("/").init().join("/") |
| 185 | if (envPath) { |
| 186 | envFile = "${env.WORKSPACE}/template/env/${envPath}/${name}.env" |
| 187 | } |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 188 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 189 | envSource = "${env.WORKSPACE}/template/env/${environment}.env" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 190 | createHeatEnv(envFile, params, envSource) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 191 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 192 | envFile = "${env.WORKSPACE}/template/${name}.env" |
| 193 | createHeatEnv(envFile, params) |
| 194 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 195 | |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 196 | def cmd |
Vasyl Saienko | b91df80 | 2019-01-23 17:22:57 +0200 | [diff] [blame] | 197 | def cmd_args = "-t ${templateFile} -e ${envFile} --timeout ${timeout} --wait ${name}" |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 198 | |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 199 | if (action == "create") { |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 200 | cmd = "openstack stack create ${cmd_args}" |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 201 | } else { |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 202 | cmd = "openstack stack update ${cmd_args}" |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 205 | dir("${env.WORKSPACE}/template/template") { |
Vasyl Saienko | d425419 | 2019-01-23 18:02:01 +0200 | [diff] [blame] | 206 | def out = runOpenstackCommand(cmd, client, path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 207 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 211 | * Returns list of stacks for stack name filter |
| 212 | * |
| 213 | * @param client Connection parameters for OpenStack API endpoint |
| 214 | * @param filter Stack name filter |
| 215 | * @param path Optional path to the custom virtualenv |
| 216 | */ |
| 217 | def getStacksForNameContains(client, filter, path = null){ |
Jakub Josef | 6465fca | 2017-05-10 16:09:20 +0200 | [diff] [blame] | 218 | cmd = 'heat stack-list | awk \'NR>3 {print $4}\' | sed \'$ d\' | grep ' + filter + '|| true' |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 219 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 224 | * Get list of stack names with given stack status |
| 225 | * |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 226 | * @param client Connection parameters for OpenStack API endpoint |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 227 | * @param status Stack status |
| 228 | * @param path Optional path to the custom virtualenv |
| 229 | */ |
| 230 | def getStacksWithStatus(client, status, path = null) { |
| 231 | cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\'' |
| 232 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 233 | } |
| 234 | |
| 235 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 236 | * Get life cycle status for existing OpenStack Heat stack |
| 237 | * |
| 238 | * @param env Connection parameters for OpenStack API endpoint |
| 239 | * @param name Name of the managed Heat stack instance |
| 240 | * @param path Optional path to the custom virtualenv |
| 241 | */ |
| 242 | def getHeatStackStatus(client, name, path = null) { |
| 243 | cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\'' |
| 244 | return runOpenstackCommand(cmd, client, path) |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get info about existing OpenStack Heat stack |
| 249 | * |
| 250 | * @param env Connection parameters for OpenStack API endpoint |
| 251 | * @param name Name of the managed Heat stack instance |
| 252 | * @param path Optional path to the custom virtualenv |
| 253 | */ |
| 254 | def getHeatStackInfo(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 255 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 256 | cmd = "heat stack-show ${name}" |
| 257 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 258 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 259 | return output |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get existing OpenStack Heat stack output parameter |
| 264 | * |
| 265 | * @param env Connection parameters for OpenStack API endpoint |
| 266 | * @param name Name of the managed Heat stack |
| 267 | * @param parameter Name of the output parameter |
| 268 | * @param path Optional path to the custom virtualenv |
| 269 | */ |
| 270 | def getHeatStackOutputParam(env, name, outputParam, path = null) { |
Vasyl Saienko | ea4b281 | 2017-07-10 10:36:03 +0000 | [diff] [blame] | 271 | cmd = "heat output-show ${name} ${outputParam}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 272 | output = runOpenstackCommand(cmd, env, path) |
Ales Komarek | eedc222 | 2017-01-03 10:10:03 +0100 | [diff] [blame] | 273 | echo("${cmd}: ${output}") |
Vasyl Saienko | 2a1c2de | 2017-07-11 11:41:53 +0300 | [diff] [blame] | 274 | // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later |
| 275 | // versions returns string without "". |
| 276 | // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient |
| 277 | return "${output}".replaceAll('"', '') |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * List all resources from existing OpenStack Heat stack |
| 282 | * |
| 283 | * @param env Connection parameters for OpenStack API endpoint |
| 284 | * @param name Name of the managed Heat stack instance |
| 285 | * @param path Optional path to the custom virtualenv |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 286 | * @param depth Optional depth of stack for listing resources, |
| 287 | * 0 - do not list nested resources |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 288 | */ |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 289 | def getHeatStackResources(env, name, path = null, depth = 0) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 290 | def python = new com.mirantis.mk.Python() |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 291 | cmd = "heat resource-list --nested-depth ${depth} ${name}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 292 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 293 | output = python.parseTextTable(outputTable, 'list', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 294 | return output |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get info about resource from existing OpenStack Heat stack |
| 299 | * |
| 300 | * @param env Connection parameters for OpenStack API endpoint |
| 301 | * @param name Name of the managed Heat stack instance |
| 302 | * @param path Optional path to the custom virtualenv |
| 303 | */ |
| 304 | def getHeatStackResourceInfo(env, name, resource, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 305 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 306 | cmd = "heat resource-show ${name} ${resource}" |
| 307 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 308 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 309 | return output |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Update existing OpenStack Heat stack |
| 314 | * |
| 315 | * @param env Connection parameters for OpenStack API endpoint |
| 316 | * @param name Name of the managed Heat stack instance |
| 317 | * @param path Optional path to the custom virtualenv |
| 318 | */ |
| 319 | def updateHeatStack(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 320 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 321 | cmd = "heat stack-update ${name}" |
| 322 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 323 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 324 | return output |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Delete existing OpenStack Heat stack |
| 329 | * |
| 330 | * @param env Connection parameters for OpenStack API endpoint |
| 331 | * @param name Name of the managed Heat stack instance |
| 332 | * @param path Optional path to the custom virtualenv |
| 333 | */ |
| 334 | def deleteHeatStack(env, name, path = null) { |
| 335 | cmd = "heat stack-delete ${name}" |
| 336 | outputTable = runOpenstackCommand(cmd, env, path) |
| 337 | } |
| 338 | |
| 339 | /** |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 340 | * Return hashmap of hashes server_id:server_name of servers from OpenStack Heat stack |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 341 | * |
| 342 | * @param env Connection parameters for OpenStack API endpoint |
| 343 | * @param name Name of the managed Heat stack instance |
| 344 | * @param path Optional path to the custom virtualenv |
| 345 | */ |
| 346 | def getHeatStackServers(env, name, path = null) { |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 347 | // set depth to 1000 to ensure all nested resources are shown |
| 348 | resources = getHeatStackResources(env, name, path, 1000) |
| 349 | servers = [:] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 350 | for (resource in resources) { |
| 351 | if (resource.resource_type == 'OS::Nova::Server') { |
Mykyta Karpin | 6797811 | 2018-02-22 11:16:45 +0200 | [diff] [blame] | 352 | server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path) |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 353 | servers[server.attributes.id] = server.attributes.name |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | echo("[Stack ${name}] Servers: ${servers}") |
| 357 | return servers |
| 358 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 359 | |
| 360 | /** |
Mykyta Karpin | 8306a9d | 2018-07-27 11:34:10 +0300 | [diff] [blame] | 361 | * Delete nova key pair |
| 362 | * |
| 363 | * @param env Connection parameters for OpenStack API endpoint |
| 364 | * @param name Name of the key pair to delete |
| 365 | * @param path Optional path to the custom virtualenv |
| 366 | */ |
| 367 | def deleteKeyPair(env, name, path = null) { |
| 368 | def common = new com.mirantis.mk.Common() |
| 369 | common.infoMsg("Removing key pair ${name}") |
| 370 | def cmd = "openstack keypair delete ${name}" |
| 371 | runOpenstackCommand(cmd, env, path) |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get nova key pair |
| 376 | * |
| 377 | * @param env Connection parameters for OpenStack API endpoint |
| 378 | * @param name Name of the key pair to show |
| 379 | * @param path Optional path to the custom virtualenv |
| 380 | */ |
| 381 | |
| 382 | def getKeyPair(env, name, path = null) { |
| 383 | def common = new com.mirantis.mk.Common() |
| 384 | def cmd = "openstack keypair show ${name}" |
| 385 | def outputTable |
| 386 | try { |
| 387 | outputTable = runOpenstackCommand(cmd, env, path) |
| 388 | } catch (Exception e) { |
| 389 | common.infoMsg("Key pair ${name} not found") |
| 390 | } |
| 391 | return outputTable |
| 392 | } |
| 393 | |
| 394 | /** |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 395 | * Stops all services that contain specific string (for example nova,heat, etc.) |
| 396 | * @param env Salt Connection object or pepperEnv |
| 397 | * @param probe single node on which to list service names |
| 398 | * @param target all targeted nodes |
| 399 | * @param services lists of type of services to be stopped |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 400 | * @param confirm enable/disable manual service stop confirmation |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 401 | * @return output of salt commands |
| 402 | */ |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 403 | def stopServices(env, probe, target, services=[], confirm=false) { |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 404 | def salt = new com.mirantis.mk.Salt() |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 405 | def common = new com.mirantis.mk.Common() |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 406 | for (s in services) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 407 | def outputServicesStr = salt.getReturnValues(salt.cmdRun(env, probe, "service --status-all | grep ${s} | awk \'{print \$4}\'")) |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 408 | def servicesList = outputServicesStr.tokenize("\n").init() |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 409 | if (confirm) { |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 410 | if (servicesList) { |
| 411 | try { |
| 412 | input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them." |
| 413 | for (name in servicesList) { |
| 414 | if (!name.contains('Salt command')) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 415 | salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"]) |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } catch (Exception er) { |
| 419 | common.infoMsg("skipping stopping ${servicesList} services") |
| 420 | } |
| 421 | } |
| 422 | } else { |
| 423 | if (servicesList) { |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 424 | for (name in servicesList) { |
| 425 | if (!name.contains('Salt command')) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 426 | salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"]) |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
Vasyl Saienko | 4129e10 | 2018-09-03 10:15:52 +0300 | [diff] [blame] | 435 | * Return intersection of globally installed services and those are |
| 436 | * defined on specific target according to theirs priorities. |
| 437 | * |
| 438 | * @param env Salt Connection object or env |
| 439 | * @param target The target node to get list of apps for. |
| 440 | **/ |
| 441 | def getOpenStackUpgradeServices(env, target){ |
| 442 | def salt = new com.mirantis.mk.Salt() |
| 443 | def common = new com.mirantis.mk.Common() |
| 444 | |
| 445 | def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications') |
| 446 | def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0] |
Oleksii Grudev | 3116a73 | 2019-02-14 18:16:05 +0200 | [diff] [blame^] | 447 | def node_pillar = salt.getPillar(env, target) |
Vasyl Saienko | 4129e10 | 2018-09-03 10:15:52 +0300 | [diff] [blame] | 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) { |
Oleksii Grudev | 3116a73 | 2019-02-14 18:16:05 +0200 | [diff] [blame^] | 453 | if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0] != null) { |
| 454 | if (node_pillar['return'][0].values()[k]['upgrade']['enabled'][0].toBoolean()) { |
| 455 | _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger() |
| 456 | } |
| 457 | } |
Vasyl Saienko | 4129e10 | 2018-09-03 10:15:52 +0300 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet() |
| 461 | common.infoMsg("Applications are placed in following order:"+node_sorted_apps) |
| 462 | } else { |
| 463 | common.errorMsg("No applications found.") |
| 464 | } |
| 465 | |
| 466 | return node_sorted_apps |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /** |
| 471 | * Run specified upgrade phase for all services on given node. |
| 472 | * |
| 473 | * @param env Salt Connection object or env |
| 474 | * @param target The target node to run states on. |
| 475 | * @param phase The phase name to run. |
| 476 | **/ |
| 477 | def runOpenStackUpgradePhase(env, target, phase){ |
| 478 | def salt = new com.mirantis.mk.Salt() |
| 479 | def common = new com.mirantis.mk.Common() |
| 480 | |
| 481 | services = getOpenStackUpgradeServices(env, target) |
| 482 | def st |
| 483 | |
| 484 | for (service in services){ |
| 485 | st = "${service}.upgrade.${phase}".trim() |
| 486 | common.infoMsg("Running ${phase} for service ${st} on ${target}") |
| 487 | salt.enforceState(env, target, st) |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | /** |
| 493 | * Run OpenStack states on specified node. |
| 494 | * |
| 495 | * @param env Salt Connection object or env |
| 496 | * @param target The target node to run states on. |
| 497 | **/ |
| 498 | def applyOpenstackAppsStates(env, target){ |
| 499 | def salt = new com.mirantis.mk.Salt() |
| 500 | def common = new com.mirantis.mk.Common() |
| 501 | |
| 502 | services = getOpenStackUpgradeServices(env, target) |
| 503 | def st |
| 504 | |
| 505 | for (service in services){ |
| 506 | st = "${service}".trim() |
| 507 | common.infoMsg("Running ${st} on ${target}") |
| 508 | salt.enforceState(env, target, st) |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 513 | * Verifies Galera database |
| 514 | * |
| 515 | * This function checks for Galera master, tests connection and if reachable, it obtains the result |
| 516 | * of Salt mysql.status function. The result is then parsed, validated and outputed to the user. |
| 517 | * |
| 518 | * @param env Salt Connection object or pepperEnv |
Martin Polreich | 232ad90 | 2019-01-21 14:31:00 +0100 | [diff] [blame] | 519 | * @param slave Boolean value to enable slave checking (if master in unreachable) |
| 520 | * @param checkTimeSync Boolean value to enable time sync check |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 521 | * @return resultCode int values used to determine exit status in the calling function |
| 522 | */ |
Martin Polreich | 232ad90 | 2019-01-21 14:31:00 +0100 | [diff] [blame] | 523 | def verifyGaleraStatus(env, slave=false, checkTimeSync=false) { |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 524 | def salt = new com.mirantis.mk.Salt() |
| 525 | def common = new com.mirantis.mk.Common() |
| 526 | def out = "" |
| 527 | def status = "unknown" |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 528 | def testNode = "" |
| 529 | if (!slave) { |
| 530 | try { |
| 531 | galeraMaster = salt.getMinions(env, "I@galera:master") |
| 532 | common.infoMsg("Current Galera master is: ${galeraMaster}") |
| 533 | salt.minionsReachable(env, "I@salt:master", "I@galera:master") |
| 534 | testNode = "I@galera:master" |
| 535 | } catch (Exception e) { |
| 536 | common.errorMsg('Galera master is not reachable.') |
| 537 | return 128 |
| 538 | } |
| 539 | } else { |
| 540 | try { |
| 541 | galeraMinions = salt.getMinions(env, "I@galera:slave") |
| 542 | common.infoMsg("Testing Galera slave minions: ${galeraMinions}") |
| 543 | } catch (Exception e) { |
| 544 | common.errorMsg("Cannot obtain Galera slave minions list.") |
| 545 | return 129 |
| 546 | } |
| 547 | for (minion in galeraMinions) { |
| 548 | try { |
| 549 | salt.minionsReachable(env, "I@salt:master", minion) |
| 550 | testNode = minion |
| 551 | break |
| 552 | } catch (Exception e) { |
| 553 | common.warningMsg("Slave '${minion}' is not reachable.") |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | if (!testNode) { |
| 558 | common.errorMsg("No Galera slave was reachable.") |
| 559 | return 130 |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 560 | } |
Martin Polreich | 232ad90 | 2019-01-21 14:31:00 +0100 | [diff] [blame] | 561 | if (checkTimeSync && !salt.checkClusterTimeSync(env, "I@galera:master or I@galera:slave")) { |
| 562 | common.errorMsg("Time in cluster is desynchronized or it couldn't be detemined. You should fix this issue manually before proceeding.") |
| 563 | return 131 |
| 564 | } |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 565 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 566 | out = salt.cmdRun(env, "I@salt:master", "salt -C '${testNode}' mysql.status") |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 567 | } catch (Exception e) { |
| 568 | common.errorMsg('Could not determine mysql status.') |
| 569 | return 256 |
| 570 | } |
| 571 | if (out) { |
| 572 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 573 | status = validateAndPrintGaleraStatusReport(env, out, testNode) |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 574 | } catch (Exception e) { |
| 575 | common.errorMsg('Could not parse the mysql status output. Check it manually.') |
| 576 | return 1 |
| 577 | } |
| 578 | } else { |
| 579 | common.errorMsg("Mysql status response unrecognized or is empty. Response: ${out}") |
| 580 | return 1024 |
| 581 | } |
| 582 | if (status == "OK") { |
| 583 | common.infoMsg("No errors found - MySQL status is ${status}.") |
| 584 | return 0 |
| 585 | } else if (status == "unknown") { |
| 586 | common.warningMsg('MySQL status cannot be detemined') |
| 587 | return 1 |
| 588 | } else { |
| 589 | common.errorMsg("Errors found.") |
| 590 | return 2 |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /** Validates and prints result of verifyGaleraStatus function |
| 595 | @param env Salt Connection object or pepperEnv |
| 596 | @param out Output of the mysql.status Salt function |
| 597 | @return status "OK", "ERROR" or "uknown" depending on result of validation |
| 598 | */ |
| 599 | |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 600 | def validateAndPrintGaleraStatusReport(env, out, minion) { |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 601 | def salt = new com.mirantis.mk.Salt() |
| 602 | def common = new com.mirantis.mk.Common() |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 603 | if (minion == "I@galera:master") { |
| 604 | role = "master" |
| 605 | } else { |
| 606 | role = "slave" |
| 607 | } |
Martin Polreich | 9432142 | 2019-01-17 16:20:24 +0100 | [diff] [blame] | 608 | sizeOut = salt.getReturnValues(salt.getPillar(env, minion, "galera:${role}:members")) |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 609 | expected_cluster_size = sizeOut.size() |
| 610 | outlist = out['return'][0] |
| 611 | resultString = outlist.get(outlist.keySet()[0]).replace("\n ", " ").replace(" ", "").replace("Salt command execution success", "").replace("----------", "").replace(": \n", ": no value\n") |
| 612 | resultYaml = readYaml text: resultString |
| 613 | parameters = [ |
| 614 | wsrep_cluster_status: [title: 'Cluster status', expectedValues: ['Primary'], description: ''], |
| 615 | wsrep_cluster_size: [title: 'Current cluster size', expectedValues: [expected_cluster_size], description: ''], |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 616 | wsrep_ready: [title: 'Node status', expectedValues: ['ON', true], description: ''], |
| 617 | wsrep_local_state_comment: [title: 'Node status comment', expectedValues: ['Joining', 'Waiting on SST', 'Joined', 'Synced', 'Donor'], description: ''], |
| 618 | wsrep_connected: [title: 'Node connectivity', expectedValues: ['ON', true], description: ''], |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 619 | 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)'], |
| 620 | 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.)'] |
| 621 | ] |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 622 | for (key in parameters.keySet()) { |
| 623 | value = resultYaml[key] |
| 624 | parameters.get(key) << [actualValue: value] |
| 625 | } |
| 626 | for (key in parameters.keySet()) { |
| 627 | param = parameters.get(key) |
| 628 | if (key == 'wsrep_local_recv_queue_avg' || key == 'wsrep_local_send_queue_avg') { |
| 629 | if (param.get('actualValue') > param.get('expectedThreshold').get('error')) { |
| 630 | param << [match: 'error'] |
| 631 | } else if (param.get('actualValue') > param.get('expectedThreshold').get('warn')) { |
| 632 | param << [match: 'warn'] |
| 633 | } else { |
| 634 | param << [match: 'ok'] |
| 635 | } |
| 636 | } else { |
| 637 | for (expValue in param.get('expectedValues')) { |
| 638 | if (expValue == param.get('actualValue')) { |
| 639 | param << [match: 'ok'] |
| 640 | break |
| 641 | } else { |
| 642 | param << [match: 'error'] |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | cluster_info_report = [] |
| 648 | cluster_warning_report = [] |
| 649 | cluster_error_report = [] |
| 650 | for (key in parameters.keySet()) { |
| 651 | param = parameters.get(key) |
| 652 | if (param.containsKey('expectedThreshold')) { |
| 653 | expValues = "below ${param.get('expectedThreshold').get('warn')}" |
| 654 | } else { |
| 655 | if (param.get('expectedValues').size() > 1) { |
| 656 | expValues = param.get('expectedValues').join(' or ') |
| 657 | } else { |
| 658 | expValues = param.get('expectedValues')[0] |
| 659 | } |
| 660 | } |
| 661 | reportString = "${param.title}: ${param.actualValue} (Expected: ${expValues}) ${param.description}" |
| 662 | if (param.get('match').equals('ok')) { |
| 663 | cluster_info_report.add("[OK ] ${reportString}") |
| 664 | } else if (param.get('match').equals('warn')) { |
| 665 | cluster_warning_report.add("[WARNING] ${reportString}") |
| 666 | } else { |
| 667 | cluster_error_report.add("[ ERROR] ${reportString})") |
| 668 | } |
| 669 | } |
| 670 | common.infoMsg("CLUSTER STATUS REPORT: ${cluster_info_report.size()} expected values, ${cluster_warning_report.size()} warnings and ${cluster_error_report.size()} error found:") |
| 671 | if (cluster_info_report.size() > 0) { |
| 672 | common.infoMsg(cluster_info_report.join('\n')) |
| 673 | } |
| 674 | if (cluster_warning_report.size() > 0) { |
| 675 | common.warningMsg(cluster_warning_report.join('\n')) |
| 676 | } |
| 677 | if (cluster_error_report.size() > 0) { |
| 678 | common.errorMsg(cluster_error_report.join('\n')) |
| 679 | return "ERROR" |
| 680 | } else { |
| 681 | return "OK" |
| 682 | } |
| 683 | } |
| 684 | |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 685 | def getGaleraLastShutdownNode(env) { |
| 686 | def salt = new com.mirantis.mk.Salt() |
| 687 | def common = new com.mirantis.mk.Common() |
| 688 | members = '' |
| 689 | lastNode = [ip: '', seqno: -2] |
| 690 | try { |
| 691 | members = salt.getReturnValues(salt.getPillar(env, "I@galera:master", "galera:master:members")) |
| 692 | } catch (Exception er) { |
| 693 | common.errorMsg('Could not retrieve members list') |
| 694 | return 'I@galera:master' |
| 695 | } |
| 696 | if (members) { |
| 697 | for (member in members) { |
| 698 | try { |
| 699 | salt.minionsReachable(env, 'I@salt:master', "S@${member.host}") |
| 700 | out = salt.getReturnValues(salt.cmdRun(env, "S@${member.host}", 'cat /var/lib/mysql/grastate.dat | grep "seqno" | cut -d ":" -f2', true, null, false)) |
| 701 | seqno = out.tokenize('\n')[0].trim() |
| 702 | if (seqno.isNumber()) { |
| 703 | seqno = seqno.toInteger() |
| 704 | } else { |
| 705 | seqno = -2 |
| 706 | } |
| 707 | highestSeqno = lastNode.get('seqno') |
| 708 | if (seqno > highestSeqno) { |
| 709 | lastNode << [ip: "${member.host}", seqno: seqno] |
| 710 | } |
| 711 | } catch (Exception er) { |
| 712 | common.warningMsg("Could not determine 'seqno' value for node ${member.host} ") |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | if (lastNode.get('ip') != '') { |
| 717 | return "S@${lastNode.ip}" |
| 718 | } else { |
| 719 | return "I@galera:master" |
| 720 | } |
| 721 | } |
| 722 | |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 723 | /** |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 724 | * Restores Galera database |
| 725 | * @param env Salt Connection object or pepperEnv |
| 726 | * @return output of salt commands |
| 727 | */ |
Ivan Berezovskiy | 004cac2 | 2019-02-01 17:03:28 +0400 | [diff] [blame] | 728 | def restoreGaleraDb(env) { |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 729 | def salt = new com.mirantis.mk.Salt() |
| 730 | def common = new com.mirantis.mk.Common() |
| 731 | try { |
| 732 | salt.runSaltProcessStep(env, 'I@galera:slave', 'service.stop', ['mysql']) |
| 733 | } catch (Exception er) { |
| 734 | common.warningMsg('Mysql service already stopped') |
| 735 | } |
| 736 | try { |
| 737 | salt.runSaltProcessStep(env, 'I@galera:master', 'service.stop', ['mysql']) |
| 738 | } catch (Exception er) { |
| 739 | common.warningMsg('Mysql service already stopped') |
| 740 | } |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 741 | lastNodeTarget = getGaleraLastShutdownNode(env) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 742 | try { |
| 743 | salt.cmdRun(env, 'I@galera:slave', "rm /var/lib/mysql/ib_logfile*") |
| 744 | } catch (Exception er) { |
| 745 | common.warningMsg('Files are not present') |
| 746 | } |
| 747 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 748 | salt.cmdRun(env, 'I@galera:slave', "rm /var/lib/mysql/grastate.dat") |
| 749 | } catch (Exception er) { |
| 750 | common.warningMsg('Files are not present') |
| 751 | } |
| 752 | try { |
| 753 | salt.cmdRun(env, lastNodeTarget, "mkdir /root/mysql/mysql.bak") |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 754 | } catch (Exception er) { |
| 755 | common.warningMsg('Directory already exists') |
| 756 | } |
| 757 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 758 | salt.cmdRun(env, lastNodeTarget, "rm -rf /root/mysql/mysql.bak/*") |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 759 | } catch (Exception er) { |
| 760 | common.warningMsg('Directory already empty') |
| 761 | } |
| 762 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 763 | salt.cmdRun(env, lastNodeTarget, "mv /var/lib/mysql/* /root/mysql/mysql.bak") |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 764 | } catch (Exception er) { |
| 765 | common.warningMsg('Files were already moved') |
| 766 | } |
| 767 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 768 | salt.runSaltProcessStep(env, lastNodeTarget, 'file.remove', ["/var/lib/mysql/.galera_bootstrap"]) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 769 | } catch (Exception er) { |
| 770 | common.warningMsg('File is not present') |
| 771 | } |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 772 | salt.cmdRun(env, lastNodeTarget, "sed -i '/gcomm/c\\wsrep_cluster_address=\"gcomm://\"' /etc/mysql/my.cnf") |
| 773 | def backup_dir = salt.getReturnValues(salt.getPillar(env, lastNodeTarget, 'xtrabackup:client:backup_dir')) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 774 | if(backup_dir == null || backup_dir.isEmpty()) { backup_dir='/var/backups/mysql/xtrabackup' } |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 775 | salt.runSaltProcessStep(env, lastNodeTarget, 'file.remove', ["${backup_dir}/dbrestored"]) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 776 | salt.cmdRun(env, 'I@xtrabackup:client', "su root -c 'salt-call state.sls xtrabackup'") |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 777 | salt.runSaltProcessStep(env, lastNodeTarget, 'service.start', ['mysql']) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 778 | |
| 779 | // wait until mysql service on galera master is up |
Jiri Broulik | 22b0457 | 2018-02-16 12:02:41 +0100 | [diff] [blame] | 780 | try { |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 781 | salt.commandStatus(env, lastNodeTarget, 'service mysql status', 'running') |
Jiri Broulik | 22b0457 | 2018-02-16 12:02:41 +0100 | [diff] [blame] | 782 | } catch (Exception er) { |
| 783 | input message: "Database is not running please fix it first and only then click on PROCEED." |
| 784 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 785 | |
Martin Polreich | 9a5d668 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 786 | salt.runSaltProcessStep(env, "I@galera:master and not ${lastNodeTarget}", 'service.start', ['mysql']) |
| 787 | salt.runSaltProcessStep(env, "I@galera:slave and not ${lastNodeTarget}", 'service.start', ['mysql']) |
Vasyl Saienko | 0adc34b | 2019-01-23 15:52:37 +0200 | [diff] [blame] | 788 | } |