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 | |
Vasyl Saienko | a56c1ab | 2020-01-13 10:00:04 +0200 | [diff] [blame] | 25 | def setupOpenstackVirtualenv(path, version = 'latest', python="python2") { |
| 26 | def pythonLib = new com.mirantis.mk.Python() |
| 27 | pythonLib.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', |
Denis Egorenko | 71754dc | 2020-01-13 14:22:49 +0400 | [diff] [blame] | 44 | 'docutils==0.16' |
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"', |
Denis Egorenko | 71754dc | 2020-01-13 14:22:49 +0400 | [diff] [blame] | 57 | // NOTE: pin client packages to current latest to prevent |
| 58 | // downloading packages which are not support Python 2.7 |
| 59 | 'python-openstackclient==4.0.0', |
Vasyl Saienko | f14fc57 | 2020-01-13 18:25:44 +0200 | [diff] [blame] | 60 | 'python-heatclient==1.18.0', |
Mykyta Karpin | 3fe5c2e | 2020-09-09 11:24:41 +0300 | [diff] [blame] | 61 | 'docutils==0.16', |
| 62 | 'pyrsistent<0.17.1', |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 63 | ] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 64 | |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 65 | if (version == 'kilo') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 66 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 67 | } else if (version == 'liberty') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 68 | requirements = openstack_kilo_packages |
Tomáš Kukrál | c6a94c6 | 2017-06-19 14:44:24 +0200 | [diff] [blame] | 69 | } else if (version == 'mitaka') { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 70 | requirements = openstack_kilo_packages |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 71 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 72 | requirements = openstack_latest_packages |
| 73 | } |
Vasyl Saienko | a56c1ab | 2020-01-13 10:00:04 +0200 | [diff] [blame] | 74 | pythonLib.setupVirtualenv(path, python, requirements, null, true) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * create connection to OpenStack API endpoint |
| 79 | * |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 80 | * @param path Path to created venv |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 81 | * @param url OpenStack API endpoint address |
| 82 | * @param credentialsId Credentials to the OpenStack API |
| 83 | * @param project OpenStack project to connect to |
| 84 | */ |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 85 | def createOpenstackEnv(path, url, credentialsId, project, project_domain="default", |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 86 | 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] | 87 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 6c96376 | 2018-01-18 16:02:22 +0100 | [diff] [blame] | 88 | rcFile = "${path}/keystonerc" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 89 | creds = common.getPasswordCredentials(credentialsId) |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 90 | rc = """set +x |
| 91 | export OS_USERNAME=${creds.username} |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 92 | export OS_PASSWORD=${creds.password.toString()} |
| 93 | export OS_TENANT_NAME=${project} |
| 94 | export OS_AUTH_URL=${url} |
| 95 | export OS_AUTH_STRATEGY=keystone |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 96 | export OS_PROJECT_NAME=${project} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 97 | export OS_PROJECT_ID=${project_id} |
kairat_kushaev | 0a26bf7 | 2017-05-18 13:20:09 +0400 | [diff] [blame] | 98 | export OS_PROJECT_DOMAIN_ID=${project_domain} |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 99 | export OS_USER_DOMAIN_NAME=${user_domain} |
Kirill Mashchenko | 234708f | 2017-07-20 17:00:01 +0300 | [diff] [blame] | 100 | export OS_IDENTITY_API_VERSION=${api_ver} |
Tomáš Kukrál | 381a8c9 | 2017-06-21 09:01:52 +0200 | [diff] [blame] | 101 | export OS_CACERT=${cacert} |
Alexander Tivelkov | f89a188 | 2017-01-11 13:29:35 +0300 | [diff] [blame] | 102 | set -x |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 103 | """ |
| 104 | writeFile file: rcFile, text: rc |
| 105 | return rcFile |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Run command with OpenStack env params and optional python env |
| 110 | * |
| 111 | * @param cmd Command to be executed |
| 112 | * @param env Environmental parameters with endpoint credentials |
| 113 | * @param path Optional path to virtualenv with specific clients |
| 114 | */ |
| 115 | def runOpenstackCommand(cmd, venv, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 116 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 117 | openstackCmd = ". ${venv}; ${cmd}" |
| 118 | if (path) { |
| 119 | output = python.runVirtualenvCommand(path, openstackCmd) |
| 120 | } |
| 121 | else { |
| 122 | echo("[Command]: ${openstackCmd}") |
| 123 | output = sh ( |
| 124 | script: openstackCmd, |
| 125 | returnStdout: true |
| 126 | ).trim() |
| 127 | } |
| 128 | return output |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get OpenStack Keystone token for current credentials |
| 133 | * |
| 134 | * @param env Connection parameters for OpenStack API endpoint |
| 135 | * @param path Optional path to the custom virtualenv |
| 136 | */ |
| 137 | def getKeystoneToken(client, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 138 | def python = new com.mirantis.mk.Python() |
Jakub Josef | bd92732 | 2017-05-30 13:20:27 +0000 | [diff] [blame] | 139 | cmd = "openstack token issue" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 140 | outputTable = runOpenstackCommand(cmd, client, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 141 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 142 | return output |
| 143 | } |
| 144 | |
| 145 | /** |
Ales Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 146 | * Create OpenStack environment file |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 147 | * |
| 148 | * @param env Connection parameters for OpenStack API endpoint |
| 149 | * @param path Optional path to the custom virtualenv |
| 150 | */ |
| 151 | def createHeatEnv(file, environment = [], original_file = null) { |
| 152 | if (original_file) { |
| 153 | envString = readFile file: original_file |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 154 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 155 | envString = "parameters:\n" |
| 156 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 157 | |
Tomáš Kukrál | c3964e5 | 2017-02-22 14:07:37 +0100 | [diff] [blame] | 158 | p = entries(environment) |
Tomáš Kukrál | b1fe964 | 2017-02-22 11:21:17 +0100 | [diff] [blame] | 159 | for (int i = 0; i < p.size(); i++) { |
| 160 | envString = "${envString} ${p.get(i)[0]}: ${p.get(i)[1]}\n" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 161 | } |
Tomáš Kukrál | 0302944 | 2017-02-21 17:14:29 +0100 | [diff] [blame] | 162 | |
Tomáš Kukrál | e19ddea | 2017-02-21 11:09:40 +0100 | [diff] [blame] | 163 | echo("writing to env file:\n${envString}") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 164 | writeFile file: file, text: envString |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Create new OpenStack Heat stack |
| 169 | * |
| 170 | * @param env Connection parameters for OpenStack API endpoint |
| 171 | * @param template HOT template for the new Heat stack |
| 172 | * @param environment Environmentale parameters of the new Heat stack |
| 173 | * @param name Name of the new Heat stack |
| 174 | * @param path Optional path to the custom virtualenv |
| 175 | */ |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 176 | def createHeatStack(client, name, template, params = [], environment = null, path = null, action="create") { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 177 | def python = new com.mirantis.mk.Python() |
Jakub Josef | 0a89876 | 2017-08-11 16:27:44 +0200 | [diff] [blame] | 178 | def templateFile = "${env.WORKSPACE}/template/template/${template}.hot" |
| 179 | def envFile |
| 180 | def envSource |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 181 | if (environment) { |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 182 | 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 Komarek | 51b7b15 | 2017-06-27 11:14:50 +0200 | [diff] [blame] | 189 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 190 | envSource = "${env.WORKSPACE}/template/env/${environment}.env" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 191 | createHeatEnv(envFile, params, envSource) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 192 | } else { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 193 | envFile = "${env.WORKSPACE}/template/${name}.env" |
| 194 | createHeatEnv(envFile, params) |
| 195 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 196 | |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 197 | def cmd |
| 198 | def waitState |
| 199 | |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 200 | if (action == "create") { |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 201 | cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}" |
| 202 | waitState = "CREATE_COMPLETE" |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 203 | } else { |
Mykyta Karpin | cf44f81 | 2017-08-28 14:45:21 +0300 | [diff] [blame] | 204 | cmd = "heat stack-update -f ${templateFile} -e ${envFile} ${name}" |
| 205 | waitState = "UPDATE_COMPLETE" |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 208 | dir("${env.WORKSPACE}/template/template") { |
| 209 | outputTable = runOpenstackCommand(cmd, client, path) |
| 210 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 211 | |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 212 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 213 | |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 214 | def heatStatusCheckerCount = 1 |
Jakub Josef | e033727 | 2017-09-12 14:15:27 +0200 | [diff] [blame] | 215 | while (heatStatusCheckerCount <= 250) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 216 | status = getHeatStackStatus(client, name, path) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 217 | echo("[Heat Stack] Status: ${status}, Check: ${heatStatusCheckerCount}") |
Ales Komarek | dce8b47 | 2016-12-30 16:56:07 +0100 | [diff] [blame] | 218 | |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 219 | if (status.contains('CREATE_FAILED')) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 220 | info = getHeatStackInfo(client, name, path) |
| 221 | throw new Exception(info.stack_status_reason) |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 222 | |
| 223 | } else if (status.contains(waitState)) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 224 | info = getHeatStackInfo(client, name, path) |
| 225 | echo(info.stack_status_reason) |
| 226 | break |
| 227 | } |
Tomáš Kukrál | a115274 | 2017-08-22 16:21:50 +0200 | [diff] [blame] | 228 | |
Jakub Josef | e033727 | 2017-09-12 14:15:27 +0200 | [diff] [blame] | 229 | sleep(30) |
Jakub Josef | 9a59aeb | 2017-08-11 15:50:20 +0200 | [diff] [blame] | 230 | heatStatusCheckerCount++ |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 231 | } |
| 232 | echo("[Heat Stack] Status: ${status}") |
| 233 | } |
| 234 | |
| 235 | /** |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 236 | * Returns list of stacks for stack name filter |
| 237 | * |
| 238 | * @param client Connection parameters for OpenStack API endpoint |
| 239 | * @param filter Stack name filter |
| 240 | * @param path Optional path to the custom virtualenv |
| 241 | */ |
| 242 | def getStacksForNameContains(client, filter, path = null){ |
Jakub Josef | 6465fca | 2017-05-10 16:09:20 +0200 | [diff] [blame] | 243 | 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] | 244 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /** |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 249 | * Get list of stack names with given stack status |
| 250 | * |
Jakub Josef | db4baf2 | 2017-05-10 15:16:09 +0200 | [diff] [blame] | 251 | * @param client Connection parameters for OpenStack API endpoint |
Jakub Josef | 5e238a2 | 2017-04-19 16:35:15 +0200 | [diff] [blame] | 252 | * @param status Stack status |
| 253 | * @param path Optional path to the custom virtualenv |
| 254 | */ |
| 255 | def getStacksWithStatus(client, status, path = null) { |
| 256 | cmd = 'heat stack-list -f stack_status='+status+' | awk \'NR>3 {print $4}\' | sed \'$ d\'' |
| 257 | return runOpenstackCommand(cmd, client, path).trim().tokenize("\n") |
| 258 | } |
| 259 | |
| 260 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 261 | * Get life cycle status for existing OpenStack Heat stack |
| 262 | * |
| 263 | * @param env Connection parameters for OpenStack API endpoint |
| 264 | * @param name Name of the managed Heat stack instance |
| 265 | * @param path Optional path to the custom virtualenv |
| 266 | */ |
| 267 | def getHeatStackStatus(client, name, path = null) { |
| 268 | cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\'' |
| 269 | return runOpenstackCommand(cmd, client, path) |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get info about existing OpenStack Heat stack |
| 274 | * |
| 275 | * @param env Connection parameters for OpenStack API endpoint |
| 276 | * @param name Name of the managed Heat stack instance |
| 277 | * @param path Optional path to the custom virtualenv |
| 278 | */ |
| 279 | def getHeatStackInfo(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 280 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 281 | cmd = "heat stack-show ${name}" |
| 282 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 283 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 284 | return output |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get existing OpenStack Heat stack output parameter |
| 289 | * |
| 290 | * @param env Connection parameters for OpenStack API endpoint |
| 291 | * @param name Name of the managed Heat stack |
| 292 | * @param parameter Name of the output parameter |
| 293 | * @param path Optional path to the custom virtualenv |
| 294 | */ |
| 295 | def getHeatStackOutputParam(env, name, outputParam, path = null) { |
Vasyl Saienko | ea4b281 | 2017-07-10 10:36:03 +0000 | [diff] [blame] | 296 | cmd = "heat output-show ${name} ${outputParam}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 297 | output = runOpenstackCommand(cmd, env, path) |
Ales Komarek | eedc222 | 2017-01-03 10:10:03 +0100 | [diff] [blame] | 298 | echo("${cmd}: ${output}") |
Vasyl Saienko | 2a1c2de | 2017-07-11 11:41:53 +0300 | [diff] [blame] | 299 | // NOTE(vsaienko) heatclient 1.5.1 returns output in "", while later |
| 300 | // versions returns string without "". |
| 301 | // TODO Use openstack 'stack output show' when all jobs using at least Mitaka heatclient |
| 302 | return "${output}".replaceAll('"', '') |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * List all resources from existing OpenStack Heat stack |
| 307 | * |
| 308 | * @param env Connection parameters for OpenStack API endpoint |
| 309 | * @param name Name of the managed Heat stack instance |
| 310 | * @param path Optional path to the custom virtualenv |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 311 | * @param depth Optional depth of stack for listing resources, |
| 312 | * 0 - do not list nested resources |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 313 | */ |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 314 | def getHeatStackResources(env, name, path = null, depth = 0) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 315 | def python = new com.mirantis.mk.Python() |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 316 | cmd = "heat resource-list --nested-depth ${depth} ${name}" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 317 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 318 | output = python.parseTextTable(outputTable, 'list', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 319 | return output |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get info about resource from existing OpenStack Heat stack |
| 324 | * |
| 325 | * @param env Connection parameters for OpenStack API endpoint |
| 326 | * @param name Name of the managed Heat stack instance |
| 327 | * @param path Optional path to the custom virtualenv |
| 328 | */ |
| 329 | def getHeatStackResourceInfo(env, name, resource, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 330 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 331 | cmd = "heat resource-show ${name} ${resource}" |
| 332 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 333 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 334 | return output |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Update existing OpenStack Heat stack |
| 339 | * |
| 340 | * @param env Connection parameters for OpenStack API endpoint |
| 341 | * @param name Name of the managed Heat stack instance |
| 342 | * @param path Optional path to the custom virtualenv |
| 343 | */ |
| 344 | def updateHeatStack(env, name, path = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 345 | def python = new com.mirantis.mk.Python() |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 346 | cmd = "heat stack-update ${name}" |
| 347 | outputTable = runOpenstackCommand(cmd, env, path) |
Ales Komarek | e11e879 | 2016-12-28 09:42:25 +0100 | [diff] [blame] | 348 | output = python.parseTextTable(outputTable, 'item', 'prettytable', path) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 349 | return output |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Delete existing OpenStack Heat stack |
| 354 | * |
| 355 | * @param env Connection parameters for OpenStack API endpoint |
| 356 | * @param name Name of the managed Heat stack instance |
| 357 | * @param path Optional path to the custom virtualenv |
| 358 | */ |
| 359 | def deleteHeatStack(env, name, path = null) { |
| 360 | cmd = "heat stack-delete ${name}" |
| 361 | outputTable = runOpenstackCommand(cmd, env, path) |
| 362 | } |
| 363 | |
| 364 | /** |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 365 | * 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] | 366 | * |
| 367 | * @param env Connection parameters for OpenStack API endpoint |
| 368 | * @param name Name of the managed Heat stack instance |
| 369 | * @param path Optional path to the custom virtualenv |
| 370 | */ |
| 371 | def getHeatStackServers(env, name, path = null) { |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 372 | // set depth to 1000 to ensure all nested resources are shown |
| 373 | resources = getHeatStackResources(env, name, path, 1000) |
| 374 | servers = [:] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 375 | for (resource in resources) { |
| 376 | if (resource.resource_type == 'OS::Nova::Server') { |
Mykyta Karpin | 6797811 | 2018-02-22 11:16:45 +0200 | [diff] [blame] | 377 | server = getHeatStackResourceInfo(env, resource.stack_name, resource.resource_name, path) |
Mykyta Karpin | 7230636 | 2018-02-08 16:40:43 +0200 | [diff] [blame] | 378 | servers[server.attributes.id] = server.attributes.name |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | echo("[Stack ${name}] Servers: ${servers}") |
| 382 | return servers |
| 383 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 384 | |
| 385 | /** |
Mykyta Karpin | 8306a9d | 2018-07-27 11:34:10 +0300 | [diff] [blame] | 386 | * Delete nova key pair |
| 387 | * |
| 388 | * @param env Connection parameters for OpenStack API endpoint |
| 389 | * @param name Name of the key pair to delete |
| 390 | * @param path Optional path to the custom virtualenv |
| 391 | */ |
| 392 | def deleteKeyPair(env, name, path = null) { |
| 393 | def common = new com.mirantis.mk.Common() |
| 394 | common.infoMsg("Removing key pair ${name}") |
| 395 | def cmd = "openstack keypair delete ${name}" |
| 396 | runOpenstackCommand(cmd, env, path) |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Get nova key pair |
| 401 | * |
| 402 | * @param env Connection parameters for OpenStack API endpoint |
| 403 | * @param name Name of the key pair to show |
| 404 | * @param path Optional path to the custom virtualenv |
| 405 | */ |
| 406 | |
| 407 | def getKeyPair(env, name, path = null) { |
| 408 | def common = new com.mirantis.mk.Common() |
| 409 | def cmd = "openstack keypair show ${name}" |
| 410 | def outputTable |
| 411 | try { |
| 412 | outputTable = runOpenstackCommand(cmd, env, path) |
| 413 | } catch (Exception e) { |
| 414 | common.infoMsg("Key pair ${name} not found") |
| 415 | } |
| 416 | return outputTable |
| 417 | } |
| 418 | |
| 419 | /** |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 420 | * Stops all services that contain specific string (for example nova,heat, etc.) |
| 421 | * @param env Salt Connection object or pepperEnv |
| 422 | * @param probe single node on which to list service names |
| 423 | * @param target all targeted nodes |
| 424 | * @param services lists of type of services to be stopped |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 425 | * @param confirm enable/disable manual service stop confirmation |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 426 | * @return output of salt commands |
| 427 | */ |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 428 | def stopServices(env, probe, target, services=[], confirm=false) { |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 429 | def salt = new com.mirantis.mk.Salt() |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 430 | def common = new com.mirantis.mk.Common() |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 431 | for (s in services) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 432 | 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] | 433 | def servicesList = outputServicesStr.tokenize("\n").init() |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 434 | if (confirm) { |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 435 | if (servicesList) { |
| 436 | try { |
| 437 | input message: "Click PROCEED to stop ${servicesList}. Otherwise click ABORT to skip stopping them." |
| 438 | for (name in servicesList) { |
| 439 | if (!name.contains('Salt command')) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 440 | salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"]) |
Jiri Broulik | f6daac6 | 2018-03-08 13:17:53 +0100 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } catch (Exception er) { |
| 444 | common.infoMsg("skipping stopping ${servicesList} services") |
| 445 | } |
| 446 | } |
| 447 | } else { |
| 448 | if (servicesList) { |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 449 | for (name in servicesList) { |
| 450 | if (!name.contains('Salt command')) { |
Dmitry Ukov | d72cd2a | 2018-09-04 17:31:46 +0400 | [diff] [blame] | 451 | salt.runSaltProcessStep(env, target, 'service.stop', ["${name}"]) |
Jiri Broulik | 27e8305 | 2018-03-06 11:37:29 +0100 | [diff] [blame] | 452 | } |
| 453 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
Vasyl Saienko | 4129e10 | 2018-09-03 10:15:52 +0300 | [diff] [blame] | 460 | * Return intersection of globally installed services and those are |
| 461 | * defined on specific target according to theirs priorities. |
| 462 | * |
| 463 | * @param env Salt Connection object or env |
| 464 | * @param target The target node to get list of apps for. |
| 465 | **/ |
| 466 | def getOpenStackUpgradeServices(env, target){ |
| 467 | def salt = new com.mirantis.mk.Salt() |
| 468 | def common = new com.mirantis.mk.Common() |
| 469 | |
| 470 | def global_apps = salt.getConfig(env, 'I@salt:master:enabled:true', 'orchestration.upgrade.applications') |
| 471 | def node_apps = salt.getPillar(env, target, '__reclass__:applications')['return'][0].values()[0] |
| 472 | def node_sorted_apps = [] |
| 473 | if ( !global_apps['return'][0].values()[0].isEmpty() ) { |
| 474 | Map<String,Integer> _sorted_apps = [:] |
| 475 | for (k in global_apps['return'][0].values()[0].keySet()) { |
| 476 | if (k in node_apps) { |
| 477 | _sorted_apps[k] = global_apps['return'][0].values()[0][k].values()[0].toInteger() |
| 478 | } |
| 479 | } |
| 480 | node_sorted_apps = common.SortMapByValueAsc(_sorted_apps).keySet() |
| 481 | common.infoMsg("Applications are placed in following order:"+node_sorted_apps) |
| 482 | } else { |
| 483 | common.errorMsg("No applications found.") |
| 484 | } |
| 485 | |
| 486 | return node_sorted_apps |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /** |
| 491 | * Run specified upgrade phase for all services on given node. |
| 492 | * |
| 493 | * @param env Salt Connection object or env |
| 494 | * @param target The target node to run states on. |
| 495 | * @param phase The phase name to run. |
| 496 | **/ |
| 497 | def runOpenStackUpgradePhase(env, target, phase){ |
| 498 | def salt = new com.mirantis.mk.Salt() |
| 499 | def common = new com.mirantis.mk.Common() |
| 500 | |
| 501 | services = getOpenStackUpgradeServices(env, target) |
| 502 | def st |
| 503 | |
| 504 | for (service in services){ |
| 505 | st = "${service}.upgrade.${phase}".trim() |
| 506 | common.infoMsg("Running ${phase} for service ${st} on ${target}") |
| 507 | salt.enforceState(env, target, st) |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /** |
| 513 | * Run OpenStack states on specified node. |
| 514 | * |
| 515 | * @param env Salt Connection object or env |
| 516 | * @param target The target node to run states on. |
| 517 | **/ |
| 518 | def applyOpenstackAppsStates(env, target){ |
| 519 | def salt = new com.mirantis.mk.Salt() |
| 520 | def common = new com.mirantis.mk.Common() |
| 521 | |
| 522 | services = getOpenStackUpgradeServices(env, target) |
| 523 | def st |
| 524 | |
| 525 | for (service in services){ |
| 526 | st = "${service}".trim() |
| 527 | common.infoMsg("Running ${st} on ${target}") |
| 528 | salt.enforceState(env, target, st) |
| 529 | } |
| 530 | } |
| 531 | |
Martin Polreich | ddfe51c | 2019-01-21 14:31:00 +0100 | [diff] [blame] | 532 | def verifyGaleraStatus(env, slave=false, checkTimeSync=false) { |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 533 | def common = new com.mirantis.mk.Common() |
Martin Polreich | f89f9b4 | 2019-05-07 15:37:13 +0200 | [diff] [blame] | 534 | def galera = new com.mirantis.mk.Galera() |
| 535 | common.warningMsg("verifyGaleraStatus method was moved to Galera class. Please change your calls accordingly.") |
| 536 | return galera.verifyGaleraStatus(env, slave, checkTimeSync) |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Martin Polreich | 47c09d1 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 539 | def validateAndPrintGaleraStatusReport(env, out, minion) { |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 540 | def common = new com.mirantis.mk.Common() |
Martin Polreich | f89f9b4 | 2019-05-07 15:37:13 +0200 | [diff] [blame] | 541 | def galera = new com.mirantis.mk.Galera() |
| 542 | common.warningMsg("validateAndPrintGaleraStatusReport method was moved to Galera class. Please change your calls accordingly.") |
| 543 | return galera.validateAndPrintGaleraStatusReport(env, out, minion) |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Martin Polreich | 47c09d1 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 546 | def getGaleraLastShutdownNode(env) { |
Martin Polreich | 47c09d1 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 547 | def common = new com.mirantis.mk.Common() |
Martin Polreich | f89f9b4 | 2019-05-07 15:37:13 +0200 | [diff] [blame] | 548 | def galera = new com.mirantis.mk.Galera() |
| 549 | common.warningMsg("getGaleraLastShutdownNode method was moved to Galera class. Please change your calls accordingly.") |
| 550 | return galera.getGaleraLastShutdownNode(env) |
Martin Polreich | 47c09d1 | 2018-12-21 16:42:06 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Martin Polreich | db69258 | 2019-04-25 14:32:36 +0200 | [diff] [blame] | 553 | def restoreGaleraDb(env) { |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 554 | def common = new com.mirantis.mk.Common() |
Martin Polreich | f89f9b4 | 2019-05-07 15:37:13 +0200 | [diff] [blame] | 555 | def galera = new com.mirantis.mk.Galera() |
| 556 | common.warningMsg("restoreGaleraDb method was moved to Galera class. Please change your calls accordingly.") |
| 557 | return galera.restoreGaleraDb(env) |
Martin Polreich | 65864b0 | 2018-12-05 10:42:50 +0100 | [diff] [blame] | 558 | } |