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