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 | /** |
| 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ál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 173 | def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 174 | def python = new com.mirantis.mk.Python() |
Jakub Josef | 0a89876 | 2017-08-11 16:27:44 +0200 | [diff] [blame] | 175 | def templateFile = "${env.WORKSPACE}/template/template/${template}.hot" |
| 176 | def envFile |
| 177 | def envSource |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 178 | if (environment) { |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 179 | 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 Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 186 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 187 | envSource = "${env.WORKSPACE}/template/env/${environment}.env" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 188 | createHeatEnv(envFile, params, envSource) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 189 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 190 | envFile = "${env.WORKSPACE}/template/${name}.env" |
| 191 | createHeatEnv(envFile, params) |
| 192 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 193 | |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 194 | def cmd |
| 195 | def waitState |
| 196 | |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 197 | if (action == "create") { |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 198 | cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}" |
| 199 | waitState = "CREATE_COMPLETE" |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 200 | } else { |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 201 | cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}" |
| 202 | waitState = "UPDATE_COMPLETE" |
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") { |
| 206 | outputTable = runOpenstackCommand(cmd, client, path) |
| 207 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 208 | |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 209 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 210 | |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 211 | def heatStatusCheckerCount = 1 |
Jakub Josef | e033727 | 2017-09-12 14:15:27 +0200 | [diff] [blame] | 212 | while (heatStatusCheckerCount <= 250) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 213 | status = getHeatStackStatus(client, name, path) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 214 | echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}") |
Ales Komarek | dce8b47 | 2016-12-30 16:56:07 +0100 | [diff] [blame] | 215 | |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 216 | if (status.contains('CREATE_FAILED')) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 217 | info = getHeatStackInfo(client, name, path) |
| 218 | throw new Exception(info.stack_status_reason) |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 219 | |
| 220 | } else if (status.contains(waitState)) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 221 | info = getHeatStackInfo(client, name, path) |
| 222 | echo(info.stack_status_reason) |
| 223 | break |
| 224 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 225 | |
Jakub Josef | e033727 | 2017-09-12 14:15:27 +0200 | [diff] [blame] | 226 | sleep(30) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 227 | heatStatusCheckerCount++ |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 228 | } |
| 229 | echo("[Heat Stack] Status: ${status}") |
| 230 | } |
| 231 | |
| 232 | /** |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 233 | * 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 | */ |
| 239 | def getStacksForNameContains(client, filter, path = null){ |
Jakub Josef | 6465fca | 2017-05-10 16:09:20 +0200 | [diff] [blame] | 240 | 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] | 241 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 246 | * Get list of stack names with given stack status |
| 247 | * |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 248 | * @param client Connection parameters for OpenStack API endpoint |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 249 | * @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 Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 258 | * 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 | */ |
| 264 | def 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 | */ |
| 276 | def getHeatStackInfo(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 277 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 278 | cmd = "heat stack-show ${name}" |
| 279 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 280 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 281 | 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 | */ |
| 292 | def getHeatStackOutputParam(env, name, outputParam, path = null) { |
Vasyl Saienko | ea4b281 | 2017-07-10 10:36:03 +0000 | [diff] [blame] | 293 | cmd = "heat output-show ${name} ${outputParam}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 294 | output = runOpenstackCommand(cmd, env, path) |
Ales Komarek | eedc222 | 2017-01-03 10:10:03 +0100 | [diff] [blame] | 295 | echo("${cmd}: ${output}") |
Vasyl Saienko | 2a1c2de | 2017-07-11 11:41:53 +0300 | [diff] [blame] | 296 | // 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 Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 300 | } |
| 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 Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 308 | * @param depth Optional depth of stack for listing resources, |
| 309 | * 0 - do not list nested resources |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 310 | */ |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 311 | def getHeatStackResources(env, name, path = null, depth = 0) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 312 | def python = new com.mirantis.mk.Python() |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 313 | cmd = "heat resource-list --nested-depth ${depth} ${name}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 314 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 315 | output = python.parseTextTable(outputTable, 'list', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 316 | 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 | */ |
| 326 | def getHeatStackResourceInfo(env, name, resource, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 327 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 328 | cmd = "heat resource-show ${name} ${resource}" |
| 329 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 330 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 331 | 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 | */ |
| 341 | def updateHeatStack(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 342 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 343 | cmd = "heat stack-update ${name}" |
| 344 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 345 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 346 | 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 | */ |
| 356 | def deleteHeatStack(env, name, path = null) { |
| 357 | cmd = "heat stack-delete ${name}" |
| 358 | outputTable = runOpenstackCommand(cmd, env, path) |
| 359 | } |
| 360 | |
| 361 | /** |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 362 | * 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] | 363 | * |
| 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 | */ |
| 368 | def getHeatStackServers(env, name, path = null) { |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 369 | // set depth to 1000 to ensure all nested resources are shown |
| 370 | resources = getHeatStackResources(env, name, path, 1000) |
| 371 | servers = [:] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 372 | for (resource in resources) { |
| 373 | if (resource.resource_type == 'OS::Nova::Server') { |
Mykyta Karpin | 6797811 | 2018-02-22 11:16:45 +0200 | [diff] [blame] | 374 | server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path) |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 375 | servers[server.attributes.id] = server.attributes.name |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | echo("[Stack ${name}] Servers: ${servers}") |
| 379 | return servers |
| 380 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 381 | |
| 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 Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 388 | * @param confirm enable/disable manual service stop confirmation |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 389 | * @return output of salt commands |
| 390 | */ |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 391 | def stopServices(env, probe, target, services=[], confirm=false) { |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 392 | def salt = new com.mirantis.mk.Salt() |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 393 | def common = new com.mirantis.mk.Common() |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 394 | for (s in services) { |
| 395 | 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] | 396 | def servicesList = outputServicesStr.tokenize("\n").init() |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 397 | if (confirm) { |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 398 | 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 Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 412 | for (name in servicesList) { |
| 413 | if (!name.contains('Salt command')) { |
| 414 | salt.runSaltProcessStep(env, "${target}*", 'service.stop', ["${name}"]) |
| 415 | } |
| 416 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Restores Galera database |
| 424 | * @param env Salt Connection object or pepperEnv |
| 425 | * @return output of salt commands |
| 426 | */ |
| 427 | def 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 Broulik | 22b0457 | 2018-02-16 12:02:41 +0100 | [diff] [blame] | 473 | 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 Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 478 | |
| 479 | salt.runSaltProcessStep(env, 'I@galera:slave', 'service.start', ['mysql']) |
| 480 | } |