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