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