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