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