Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Python functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Install python virtualenv |
| 11 | * |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 12 | * @param path Path to virtualenv |
| 13 | * @param python Version of Python (python/python3) |
| 14 | * @param reqs Environment requirements in list format |
| 15 | * @param reqs_path Environment requirements path in str format |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 16 | */ |
Jakub Josef | 996f4ef | 2017-10-24 13:20:43 +0200 | [diff] [blame] | 17 | def setupVirtualenv(path, python = 'python2', reqs=[], reqs_path=null, clean=false, useSystemPackages=false) { |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 18 | def common = new com.mirantis.mk.Common() |
| 19 | |
Mykyta Karpin | 1e4bfc9 | 2017-11-01 14:38:25 +0200 | [diff] [blame] | 20 | def virtualenv_cmd = "virtualenv ${path} --python ${python}" |
Jakub Josef | 996f4ef | 2017-10-24 13:20:43 +0200 | [diff] [blame] | 21 | if (useSystemPackages){ |
| 22 | virtualenv_cmd += " --system-site-packages" |
| 23 | } |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 24 | if (clean) { |
| 25 | common.infoMsg("Cleaning venv directory " + path) |
| 26 | sh("rm -rf \"${path}\"") |
| 27 | } |
| 28 | |
| 29 | common.infoMsg("[Python ${path}] Setup ${python} environment") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 30 | sh(returnStdout: true, script: virtualenv_cmd) |
Jakub Josef | 5f97d53 | 2017-11-29 18:51:41 +0100 | [diff] [blame] | 31 | if(!env.getEnvironment().containsKey("OFFLINE_DEPLOYMENT") || !env["OFFLINE_DEPLOYMENT"].toBoolean()){ |
| 32 | try { |
| 33 | //TODO: remove wget after global env prop enforcments |
Alexander Noskov | a5f1196 | 2017-12-05 11:25:16 +0400 | [diff] [blame] | 34 | runVirtualenvCommand(path, "wget -q -T 10 --spider http://google.com && pip install -U setuptools pip") |
Jakub Josef | 5f97d53 | 2017-11-29 18:51:41 +0100 | [diff] [blame] | 35 | } catch(Exception e) { |
| 36 | common.warningMsg("Setuptools and pip cannot be updated, you might be offline") |
| 37 | } |
Yuriy Taraday | 67352e9 | 2017-10-12 10:54:23 +0000 | [diff] [blame] | 38 | } |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 39 | if (reqs_path==null) { |
| 40 | def args = "" |
| 41 | for (req in reqs) { |
| 42 | args = args + "${req}\n" |
| 43 | } |
| 44 | writeFile file: "${path}/requirements.txt", text: args |
| 45 | reqs_path = "${path}/requirements.txt" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 46 | } |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 47 | runVirtualenvCommand(path, "pip install -r ${reqs_path}") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Run command in specific python virtualenv |
| 52 | * |
| 53 | * @param path Path to virtualenv |
| 54 | * @param cmd Command to be executed |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame^] | 55 | * @param silent dont print any messages (optional, default false) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 56 | */ |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame^] | 57 | def runVirtualenvCommand(path, cmd, silent=false) { |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 58 | def common = new com.mirantis.mk.Common() |
| 59 | |
Jakub Josef | 5feeee4 | 2018-01-08 15:50:36 +0100 | [diff] [blame] | 60 | virtualenv_cmd = "set +x; . ${path}/bin/activate; ${cmd}" |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame^] | 61 | if(!silent){ |
| 62 | common.infoMsg("[Python ${path}] Run command ${cmd}") |
| 63 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 64 | output = sh( |
| 65 | returnStdout: true, |
| 66 | script: virtualenv_cmd |
| 67 | ).trim() |
| 68 | return output |
| 69 | } |
| 70 | |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Install docutils in isolated environment |
| 74 | * |
| 75 | * @param path Path where virtualenv is created |
| 76 | */ |
| 77 | def setupDocutilsVirtualenv(path) { |
| 78 | requirements = [ |
| 79 | 'docutils', |
| 80 | ] |
| 81 | setupVirtualenv(path, 'python2', requirements) |
| 82 | } |
| 83 | |
| 84 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 85 | @NonCPS |
| 86 | def loadJson(rawData) { |
| 87 | return new groovy.json.JsonSlurperClassic().parseText(rawData) |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Parse content from markup-text tables to variables |
| 92 | * |
| 93 | * @param tableStr String representing the table |
| 94 | * @param mode Either list (1st row are keys) or item (key, value rows) |
| 95 | * @param format Format of the table |
| 96 | */ |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 97 | def parseTextTable(tableStr, type = 'item', format = 'rest', path = none) { |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 98 | parserFile = "${env.WORKSPACE}/textTableParser.py" |
| 99 | parserScript = """import json |
| 100 | import argparse |
| 101 | from docutils.parsers.rst import tableparser |
| 102 | from docutils import statemachine |
| 103 | |
| 104 | def parse_item_table(raw_data): |
| 105 | i = 1 |
| 106 | pretty_raw_data = [] |
| 107 | for datum in raw_data: |
| 108 | if datum != "": |
| 109 | if datum[3] != ' ' and i > 4: |
| 110 | pretty_raw_data.append(raw_data[0]) |
| 111 | if i == 3: |
| 112 | pretty_raw_data.append(datum.replace('-', '=')) |
| 113 | else: |
| 114 | pretty_raw_data.append(datum) |
| 115 | i += 1 |
| 116 | parser = tableparser.GridTableParser() |
| 117 | block = statemachine.StringList(pretty_raw_data) |
| 118 | docutils_data = parser.parse(block) |
| 119 | final_data = {} |
| 120 | for line in docutils_data[2]: |
| 121 | key = ' '.join(line[0][3]).strip() |
| 122 | value = ' '.join(line[1][3]).strip() |
| 123 | if key != "": |
| 124 | try: |
| 125 | value = json.loads(value) |
| 126 | except: |
| 127 | pass |
| 128 | final_data[key] = value |
| 129 | i+=1 |
| 130 | return final_data |
| 131 | |
| 132 | def parse_list_table(raw_data): |
| 133 | i = 1 |
| 134 | pretty_raw_data = [] |
| 135 | for datum in raw_data: |
| 136 | if datum != "": |
| 137 | if datum[3] != ' ' and i > 4: |
| 138 | pretty_raw_data.append(raw_data[0]) |
| 139 | if i == 3: |
| 140 | pretty_raw_data.append(datum.replace('-', '=')) |
| 141 | else: |
| 142 | pretty_raw_data.append(datum) |
| 143 | i += 1 |
| 144 | parser = tableparser.GridTableParser() |
| 145 | block = statemachine.StringList(pretty_raw_data) |
| 146 | docutils_data = parser.parse(block) |
| 147 | final_data = [] |
| 148 | keys = [] |
| 149 | for line in docutils_data[1]: |
| 150 | for item in line: |
| 151 | keys.append(' '.join(item[3]).strip()) |
| 152 | for line in docutils_data[2]: |
| 153 | final_line = {} |
| 154 | key = ' '.join(line[0][3]).strip() |
| 155 | value = ' '.join(line[1][3]).strip() |
| 156 | if key != "": |
| 157 | try: |
| 158 | value = json.loads(value) |
| 159 | except: |
| 160 | pass |
| 161 | final_data[key] = value |
| 162 | i+=1 |
| 163 | return final_data |
| 164 | |
| 165 | def parse_list_table(raw_data): |
| 166 | i = 1 |
| 167 | pretty_raw_data = [] |
| 168 | for datum in raw_data: |
| 169 | if datum != "": |
| 170 | if datum[3] != ' ' and i > 4: |
| 171 | pretty_raw_data.append(raw_data[0]) |
| 172 | if i == 3: |
| 173 | pretty_raw_data.append(datum.replace('-', '=')) |
| 174 | else: |
| 175 | pretty_raw_data.append(datum) |
| 176 | i += 1 |
| 177 | parser = tableparser.GridTableParser() |
| 178 | block = statemachine.StringList(pretty_raw_data) |
| 179 | docutils_data = parser.parse(block) |
| 180 | final_data = [] |
| 181 | keys = [] |
| 182 | for line in docutils_data[1]: |
| 183 | for item in line: |
| 184 | keys.append(' '.join(item[3]).strip()) |
| 185 | for line in docutils_data[2]: |
| 186 | final_line = {} |
| 187 | i = 0 |
| 188 | for item in line: |
| 189 | value = ' '.join(item[3]).strip() |
| 190 | try: |
| 191 | value = json.loads(value) |
| 192 | except: |
| 193 | pass |
| 194 | final_line[keys[i]] = value |
| 195 | i += 1 |
| 196 | final_data.append(final_line) |
| 197 | return final_data |
| 198 | |
| 199 | def read_table_file(file): |
| 200 | table_file = open(file, 'r') |
Ales Komarek | c000c15 | 2016-12-23 15:32:54 +0100 | [diff] [blame] | 201 | raw_data = table_file.read().split('\\n') |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 202 | table_file.close() |
| 203 | return raw_data |
| 204 | |
| 205 | parser = argparse.ArgumentParser() |
| 206 | parser.add_argument('-f','--file', help='File with table data', required=True) |
| 207 | parser.add_argument('-t','--type', help='Type of table (list/item)', required=True) |
| 208 | args = vars(parser.parse_args()) |
| 209 | |
| 210 | raw_data = read_table_file(args['file']) |
| 211 | |
| 212 | if args['type'] == 'list': |
| 213 | final_data = parse_list_table(raw_data) |
| 214 | else: |
| 215 | final_data = parse_item_table(raw_data) |
| 216 | |
| 217 | print json.dumps(final_data) |
| 218 | """ |
| 219 | writeFile file: parserFile, text: parserScript |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 220 | tableFile = "${env.WORKSPACE}/prettytable.txt" |
| 221 | writeFile file: tableFile, text: tableStr |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 222 | |
| 223 | cmd = "python ${parserFile} --file '${tableFile}' --type ${type}" |
| 224 | if (path) { |
Ales Komarek | c6d28dd | 2016-12-28 12:59:38 +0100 | [diff] [blame] | 225 | rawData = runVirtualenvCommand(path, cmd) |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 226 | } |
| 227 | else { |
| 228 | rawData = sh ( |
| 229 | script: cmd, |
| 230 | returnStdout: true |
| 231 | ).trim() |
| 232 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 233 | data = loadJson(rawData) |
| 234 | echo("[Parsed table] ${data}") |
| 235 | return data |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Install cookiecutter in isolated environment |
| 240 | * |
| 241 | * @param path Path where virtualenv is created |
| 242 | */ |
| 243 | def setupCookiecutterVirtualenv(path) { |
| 244 | requirements = [ |
| 245 | 'cookiecutter', |
Jakub Josef | 4df7827 | 2017-04-26 14:36:36 +0200 | [diff] [blame] | 246 | 'jinja2==2.8.1', |
| 247 | 'PyYAML==3.12' |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 248 | ] |
| 249 | setupVirtualenv(path, 'python2', requirements) |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Generate the cookiecutter templates with given context |
| 254 | * |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 255 | * @param template template |
| 256 | * @param context template context |
| 257 | * @param path Path where virtualenv is created (optional) |
| 258 | * @param templatePath path to cookiecutter template repo (optional) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 259 | */ |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 260 | def buildCookiecutterTemplate(template, context, outputDir = '.', path = null, templatePath = ".") { |
Tomáš Kukrál | dad7b46 | 2017-03-27 13:53:05 +0200 | [diff] [blame] | 261 | configFile = "default_config.yaml" |
| 262 | configString = "default_context:\n" |
Tomáš Kukrál | 6de8504 | 2017-04-12 17:49:05 +0200 | [diff] [blame] | 263 | writeFile file: configFile, text: context |
Jakub Josef | 4e61cc0 | 2017-04-26 14:29:09 +0200 | [diff] [blame] | 264 | command = ". ${path}/bin/activate; if [ -f ${templatePath}/generate.py ]; then python ${templatePath}/generate.py --config-file ${configFile} --template ${template} --output-dir ${outputDir}; else cookiecutter --config-file ${configFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}; fi" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 265 | output = sh (returnStdout: true, script: command) |
| 266 | echo("[Cookiecutter build] Output: ${output}") |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Install jinja rendering in isolated environment |
| 271 | * |
| 272 | * @param path Path where virtualenv is created |
| 273 | */ |
| 274 | def setupJinjaVirtualenv(path) { |
| 275 | requirements = [ |
| 276 | 'jinja2-cli', |
| 277 | 'pyyaml', |
| 278 | ] |
| 279 | setupVirtualenv(path, 'python2', requirements) |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Generate the Jinja templates with given context |
| 284 | * |
| 285 | * @param path Path where virtualenv is created |
| 286 | */ |
| 287 | def jinjaBuildTemplate (template, context, path = none) { |
| 288 | contextFile = "jinja_context.yml" |
| 289 | contextString = "" |
| 290 | for (parameter in context) { |
| 291 | contextString = "${contextString}${parameter.key}: ${parameter.value}\n" |
| 292 | } |
| 293 | writeFile file: contextFile, text: contextString |
| 294 | cmd = "jinja2 ${template} ${contextFile} --format=yaml" |
| 295 | data = sh (returnStdout: true, script: cmd) |
| 296 | echo(data) |
| 297 | return data |
| 298 | } |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * Install salt-pepper in isolated environment |
| 302 | * |
| 303 | * @param path Path where virtualenv is created |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 304 | * @param url SALT_MASTER_URL |
| 305 | * @param credentialsId Credentials to salt api |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 306 | */ |
Mykyta Karpin | 9491623 | 2017-11-15 10:20:59 +0200 | [diff] [blame] | 307 | def setupPepperVirtualenv(path, url, credentialsId, clean = false) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 308 | def common = new com.mirantis.mk.Common() |
| 309 | |
| 310 | // virtualenv setup |
Jakub Josef | 96cb63a | 2017-12-06 18:20:59 +0100 | [diff] [blame] | 311 | requirements = ['salt-pepper==0.5.2'] |
Mykyta Karpin | 9491623 | 2017-11-15 10:20:59 +0200 | [diff] [blame] | 312 | setupVirtualenv(path, 'python2', requirements, null, clean, true) |
chnyda | bcfff18 | 2017-11-29 10:24:36 +0100 | [diff] [blame] | 313 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 314 | // pepperrc creation |
| 315 | rcFile = "${path}/pepperrc" |
| 316 | creds = common.getPasswordCredentials(credentialsId) |
| 317 | rc = """\ |
| 318 | [main] |
| 319 | SALTAPI_EAUTH=pam |
| 320 | SALTAPI_URL=${url} |
| 321 | SALTAPI_USER=${creds.username} |
| 322 | SALTAPI_PASS=${creds.password.toString()} |
| 323 | """ |
| 324 | writeFile file: rcFile, text: rc |
| 325 | return rcFile |
Jakub Josef | d067f61 | 2017-09-26 13:42:56 +0200 | [diff] [blame] | 326 | } |
Oleh Hryhorov | 44569fb | 2017-10-26 17:04:55 +0300 | [diff] [blame] | 327 | |
| 328 | /** |
| 329 | * Install devops in isolated environment |
| 330 | * |
| 331 | * @param path Path where virtualenv is created |
| 332 | * @param clean Define to true is the venv have to cleaned up before install a new one |
| 333 | */ |
| 334 | def setupDevOpsVenv(venv, clean=false) { |
| 335 | requirements = ['git+https://github.com/openstack/fuel-devops.git'] |
| 336 | setupVirtualenv(venv, 'python2', requirements, null, false, clean) |
| 337 | } |