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 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [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 | */ |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [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 | |
Jakub Josef | 87a8a3c | 2018-01-26 12:11:11 +0100 | [diff] [blame] | 20 | def offlineDeployment = env.getEnvironment().containsKey("OFFLINE_DEPLOYMENT") && env["OFFLINE_DEPLOYMENT"].toBoolean() |
Mykyta Karpin | 1e4bfc9 | 2017-11-01 14:38:25 +0200 | [diff] [blame] | 21 | def virtualenv_cmd = "virtualenv ${path} --python ${python}" |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 22 | if (useSystemPackages) { |
Jakub Josef | 996f4ef | 2017-10-24 13:20:43 +0200 | [diff] [blame] | 23 | virtualenv_cmd += " --system-site-packages" |
| 24 | } |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 25 | if (clean) { |
| 26 | common.infoMsg("Cleaning venv directory " + path) |
| 27 | sh("rm -rf \"${path}\"") |
| 28 | } |
| 29 | |
Vasyl Saienko | eed4677 | 2020-01-21 12:48:59 +0000 | [diff] [blame] | 30 | if (offlineDeployment) { |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 31 | virtualenv_cmd += " --no-download" |
Vasyl Saienko | eed4677 | 2020-01-21 12:48:59 +0000 | [diff] [blame] | 32 | } |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 33 | common.infoMsg("[Python ${path}] Setup ${python} environment") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 34 | sh(returnStdout: true, script: virtualenv_cmd) |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 35 | if (!offlineDeployment) { |
| 36 | try { |
Denis Egorenko | 89171f9 | 2019-11-12 13:49:23 +0400 | [diff] [blame] | 37 | def pipPackage = 'pip' |
| 38 | if (python == 'python2') { |
| 39 | pipPackage = "\"pip<=19.3.1\"" |
| 40 | common.infoMsg("Pinning pip package due to end of life of Python2 to ${pipPackage} version.") |
| 41 | } |
Vasyl Saienko | 9a2bd37 | 2020-01-13 10:00:04 +0200 | [diff] [blame] | 42 | // NOTE(vsaienko): pin setuptools explicitly for latest version that works with python2 |
Vasyl Saienko | eed4677 | 2020-01-21 12:48:59 +0000 | [diff] [blame] | 43 | runVirtualenvCommand(path, "pip install -U \"setuptools<45.0.0\" ${pipPackage}") |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 44 | } catch (Exception e) { |
| 45 | common.warningMsg("Setuptools and pip cannot be updated, you might be offline but OFFLINE_DEPLOYMENT global property not initialized!") |
| 46 | } |
Yuriy Taraday | 67352e9 | 2017-10-12 10:54:23 +0000 | [diff] [blame] | 47 | } |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 48 | if (reqs_path == null) { |
Vladislav Naumov | 1110386 | 2017-07-19 17:02:39 +0300 | [diff] [blame] | 49 | def args = "" |
| 50 | for (req in reqs) { |
| 51 | args = args + "${req}\n" |
| 52 | } |
| 53 | writeFile file: "${path}/requirements.txt", text: args |
| 54 | reqs_path = "${path}/requirements.txt" |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 55 | } |
Jakub Josef | a2491ad | 2018-01-15 16:26:27 +0100 | [diff] [blame] | 56 | runVirtualenvCommand(path, "pip install -r ${reqs_path}", true) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Run command in specific python virtualenv |
| 61 | * |
azvyagintsev | 386e94e | 2019-06-13 13:39:04 +0300 | [diff] [blame] | 62 | * @param path Path to virtualenv |
| 63 | * @param cmd Command to be executed |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame] | 64 | * @param silent dont print any messages (optional, default false) |
azvyagintsev | 386e94e | 2019-06-13 13:39:04 +0300 | [diff] [blame] | 65 | * @param flexAnswer return answer like a dict, with format ['status' : int, 'stderr' : str, 'stdout' : str ] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 66 | */ |
azvyagintsev | 386e94e | 2019-06-13 13:39:04 +0300 | [diff] [blame] | 67 | def runVirtualenvCommand(path, cmd, silent = false, flexAnswer = false) { |
Tomáš Kukrál | 69c2545 | 2017-07-27 14:59:40 +0200 | [diff] [blame] | 68 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 386e94e | 2019-06-13 13:39:04 +0300 | [diff] [blame] | 69 | def res |
| 70 | def virtualenv_cmd = "set +x; . ${path}/bin/activate; ${cmd}" |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 71 | if (!silent) { |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame] | 72 | common.infoMsg("[Python ${path}] Run command ${cmd}") |
| 73 | } |
azvyagintsev | 386e94e | 2019-06-13 13:39:04 +0300 | [diff] [blame] | 74 | if (flexAnswer) { |
| 75 | res = common.shCmdStatus(virtualenv_cmd) |
| 76 | } else { |
| 77 | res = sh( |
| 78 | returnStdout: true, |
| 79 | script: virtualenv_cmd |
| 80 | ).trim() |
| 81 | } |
| 82 | return res |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 83 | } |
| 84 | |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 85 | /** |
Dennis Dmitriev | 8ac1fe7 | 2019-08-01 06:16:49 +0300 | [diff] [blame] | 86 | * Another command runner to control outputs and exit code |
| 87 | * |
| 88 | * - always print the executing command to control the pipeline execution |
| 89 | * - always allows to get the stdout/stderr/status in the result, even with enabled console enabled |
| 90 | * - throws an exception with stderr content, so it could be read from the job status and processed |
| 91 | * |
| 92 | * @param cmd String, command to be executed |
| 93 | * @param virtualenv String, path to Python virtualenv (optional, default: '') |
| 94 | * @param verbose Boolean, true: (default) mirror stdout to console and to the result['stdout'] at the same time, |
| 95 | * false: store stdout only to result['stdout'] |
| 96 | * @param check_status Boolean, true: (default) throw an exception which contains result['stderr'] if exit code is not 0, |
| 97 | * false: only print stderr if not empty, and return the result |
| 98 | * @return Map, ['status' : int, 'stderr' : str, 'stdout' : str ] |
| 99 | */ |
| 100 | def runCmd(String cmd, String virtualenv='', Boolean verbose=true, Boolean check_status=true) { |
| 101 | def common = new com.mirantis.mk.Common() |
| 102 | |
| 103 | def script |
| 104 | def redirect_output |
| 105 | def result = [:] |
| 106 | def stdout_path = sh(script: '#!/bin/bash +x\nmktemp', returnStdout: true).trim() |
| 107 | def stderr_path = sh(script: '#!/bin/bash +x\nmktemp', returnStdout: true).trim() |
| 108 | |
| 109 | if (verbose) { |
| 110 | // show stdout to console and store to stdout_path |
| 111 | redirect_output = " 1> >(tee -a ${stdout_path}) 2>${stderr_path}" |
| 112 | } else { |
| 113 | // only store stdout to stdout_path |
| 114 | redirect_output = " 1>${stdout_path} 2>${stderr_path}" |
| 115 | } |
| 116 | |
| 117 | if (virtualenv) { |
| 118 | common.infoMsg("Run shell command in Python virtualenv [${virtualenv}]:\n" + cmd) |
| 119 | script = """#!/bin/bash +x |
| 120 | . ${virtualenv}/bin/activate |
| 121 | ( ${cmd.stripIndent()} ) ${redirect_output} |
| 122 | """ |
| 123 | } else { |
| 124 | common.infoMsg('Run shell command:\n' + cmd) |
| 125 | script = """#!/bin/bash +x |
| 126 | ( ${cmd.stripIndent()} ) ${redirect_output} |
| 127 | """ |
| 128 | } |
| 129 | |
| 130 | result['status'] = sh(script: script, returnStatus: true) |
| 131 | result['stdout'] = readFile(stdout_path) |
| 132 | result['stderr'] = readFile(stderr_path) |
| 133 | def cleanup_script = """#!/bin/bash +x |
| 134 | rm ${stdout_path} || true |
| 135 | rm ${stderr_path} || true |
| 136 | """ |
| 137 | sh(script: cleanup_script) |
| 138 | |
| 139 | if (result['status'] != 0 && check_status) { |
| 140 | def error_message = '\nScript returned exit code: ' + result['status'] + '\n<<<<<< STDERR: >>>>>>\n' + result['stderr'] |
| 141 | common.errorMsg(error_message) |
| 142 | common.printMsg('', 'reset') |
| 143 | throw new Exception(error_message) |
| 144 | } |
| 145 | |
| 146 | if (result['stderr'] && verbose) { |
| 147 | def warning_message = '\nScript returned exit code: ' + result['status'] + '\n<<<<<< STDERR: >>>>>>\n' + result['stderr'] |
| 148 | common.warningMsg(warning_message) |
| 149 | common.printMsg('', 'reset') |
| 150 | } |
| 151 | |
| 152 | return result |
| 153 | } |
| 154 | |
| 155 | /** |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 156 | * Install docutils in isolated environment |
| 157 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 158 | * @param path Path where virtualenv is created |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 159 | */ |
Vasyl Saienko | 9a2bd37 | 2020-01-13 10:00:04 +0200 | [diff] [blame] | 160 | def setupDocutilsVirtualenv(path, python="python2") { |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 161 | requirements = [ |
Denis Egorenko | 97ef0fb | 2020-01-13 14:22:49 +0400 | [diff] [blame] | 162 | 'docutils==0.16', |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 163 | ] |
Vasyl Saienko | 9a2bd37 | 2020-01-13 10:00:04 +0200 | [diff] [blame] | 164 | setupVirtualenv(path, python, requirements) |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 168 | @NonCPS |
| 169 | def loadJson(rawData) { |
| 170 | return new groovy.json.JsonSlurperClassic().parseText(rawData) |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Parse content from markup-text tables to variables |
| 175 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 176 | * @param tableStr String representing the table |
| 177 | * @param mode Either list (1st row are keys) or item (key, value rows) |
| 178 | * @param format Format of the table |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 179 | */ |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 180 | def parseTextTable(tableStr, type = 'item', format = 'rest', path = none) { |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 181 | parserFile = "${env.WORKSPACE}/textTableParser.py" |
| 182 | parserScript = """import json |
| 183 | import argparse |
| 184 | from docutils.parsers.rst import tableparser |
| 185 | from docutils import statemachine |
| 186 | |
| 187 | def parse_item_table(raw_data): |
| 188 | i = 1 |
| 189 | pretty_raw_data = [] |
| 190 | for datum in raw_data: |
| 191 | if datum != "": |
| 192 | if datum[3] != ' ' and i > 4: |
| 193 | pretty_raw_data.append(raw_data[0]) |
| 194 | if i == 3: |
| 195 | pretty_raw_data.append(datum.replace('-', '=')) |
| 196 | else: |
| 197 | pretty_raw_data.append(datum) |
| 198 | i += 1 |
| 199 | parser = tableparser.GridTableParser() |
| 200 | block = statemachine.StringList(pretty_raw_data) |
| 201 | docutils_data = parser.parse(block) |
| 202 | final_data = {} |
| 203 | for line in docutils_data[2]: |
| 204 | key = ' '.join(line[0][3]).strip() |
| 205 | value = ' '.join(line[1][3]).strip() |
| 206 | if key != "": |
| 207 | try: |
| 208 | value = json.loads(value) |
| 209 | except: |
| 210 | pass |
| 211 | final_data[key] = value |
| 212 | i+=1 |
| 213 | return final_data |
| 214 | |
| 215 | def parse_list_table(raw_data): |
| 216 | i = 1 |
| 217 | pretty_raw_data = [] |
| 218 | for datum in raw_data: |
| 219 | if datum != "": |
| 220 | if datum[3] != ' ' and i > 4: |
| 221 | pretty_raw_data.append(raw_data[0]) |
| 222 | if i == 3: |
| 223 | pretty_raw_data.append(datum.replace('-', '=')) |
| 224 | else: |
| 225 | pretty_raw_data.append(datum) |
| 226 | i += 1 |
| 227 | parser = tableparser.GridTableParser() |
| 228 | block = statemachine.StringList(pretty_raw_data) |
| 229 | docutils_data = parser.parse(block) |
| 230 | final_data = [] |
| 231 | keys = [] |
| 232 | for line in docutils_data[1]: |
| 233 | for item in line: |
| 234 | keys.append(' '.join(item[3]).strip()) |
| 235 | for line in docutils_data[2]: |
| 236 | final_line = {} |
| 237 | key = ' '.join(line[0][3]).strip() |
| 238 | value = ' '.join(line[1][3]).strip() |
| 239 | if key != "": |
| 240 | try: |
| 241 | value = json.loads(value) |
| 242 | except: |
| 243 | pass |
| 244 | final_data[key] = value |
| 245 | i+=1 |
| 246 | return final_data |
| 247 | |
| 248 | def parse_list_table(raw_data): |
| 249 | i = 1 |
| 250 | pretty_raw_data = [] |
| 251 | for datum in raw_data: |
| 252 | if datum != "": |
| 253 | if datum[3] != ' ' and i > 4: |
| 254 | pretty_raw_data.append(raw_data[0]) |
| 255 | if i == 3: |
| 256 | pretty_raw_data.append(datum.replace('-', '=')) |
| 257 | else: |
| 258 | pretty_raw_data.append(datum) |
| 259 | i += 1 |
| 260 | parser = tableparser.GridTableParser() |
| 261 | block = statemachine.StringList(pretty_raw_data) |
| 262 | docutils_data = parser.parse(block) |
| 263 | final_data = [] |
| 264 | keys = [] |
| 265 | for line in docutils_data[1]: |
| 266 | for item in line: |
| 267 | keys.append(' '.join(item[3]).strip()) |
| 268 | for line in docutils_data[2]: |
| 269 | final_line = {} |
| 270 | i = 0 |
| 271 | for item in line: |
| 272 | value = ' '.join(item[3]).strip() |
| 273 | try: |
| 274 | value = json.loads(value) |
| 275 | except: |
| 276 | pass |
| 277 | final_line[keys[i]] = value |
| 278 | i += 1 |
| 279 | final_data.append(final_line) |
| 280 | return final_data |
| 281 | |
| 282 | def read_table_file(file): |
| 283 | table_file = open(file, 'r') |
Ales Komarek | c000c15 | 2016-12-23 15:32:54 +0100 | [diff] [blame] | 284 | raw_data = table_file.read().split('\\n') |
Ales Komarek | 0e558ee | 2016-12-23 13:02:55 +0100 | [diff] [blame] | 285 | table_file.close() |
| 286 | return raw_data |
| 287 | |
| 288 | parser = argparse.ArgumentParser() |
| 289 | parser.add_argument('-f','--file', help='File with table data', required=True) |
| 290 | parser.add_argument('-t','--type', help='Type of table (list/item)', required=True) |
| 291 | args = vars(parser.parse_args()) |
| 292 | |
| 293 | raw_data = read_table_file(args['file']) |
| 294 | |
| 295 | if args['type'] == 'list': |
| 296 | final_data = parse_list_table(raw_data) |
| 297 | else: |
| 298 | final_data = parse_item_table(raw_data) |
| 299 | |
| 300 | print json.dumps(final_data) |
| 301 | """ |
| 302 | writeFile file: parserFile, text: parserScript |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 303 | tableFile = "${env.WORKSPACE}/prettytable.txt" |
| 304 | writeFile file: tableFile, text: tableStr |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 305 | |
| 306 | cmd = "python ${parserFile} --file '${tableFile}' --type ${type}" |
| 307 | if (path) { |
Ales Komarek | c6d28dd | 2016-12-28 12:59:38 +0100 | [diff] [blame] | 308 | rawData = runVirtualenvCommand(path, cmd) |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 309 | } else { |
| 310 | rawData = sh( |
Ales Komarek | d874d48 | 2016-12-26 10:33:29 +0100 | [diff] [blame] | 311 | script: cmd, |
| 312 | returnStdout: true |
| 313 | ).trim() |
| 314 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 315 | data = loadJson(rawData) |
| 316 | echo("[Parsed table] ${data}") |
| 317 | return data |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Install cookiecutter in isolated environment |
| 322 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 323 | * @param path Path where virtualenv is created |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 324 | */ |
| 325 | def setupCookiecutterVirtualenv(path) { |
| 326 | requirements = [ |
| 327 | 'cookiecutter', |
Jakub Josef | 4df7827 | 2017-04-26 14:36:36 +0200 | [diff] [blame] | 328 | 'jinja2==2.8.1', |
Dmitry Pyzhov | b883a2d | 2018-12-14 16:42:52 +0300 | [diff] [blame] | 329 | 'PyYAML==3.12', |
| 330 | 'python-gnupg==0.4.3' |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 331 | ] |
| 332 | setupVirtualenv(path, 'python2', requirements) |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Generate the cookiecutter templates with given context |
| 337 | * |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 338 | * @param template template |
| 339 | * @param context template context |
| 340 | * @param path Path where virtualenv is created (optional) |
| 341 | * @param templatePath path to cookiecutter template repo (optional) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 342 | */ |
Jakub Josef | 4e10c37 | 2017-04-26 14:13:50 +0200 | [diff] [blame] | 343 | def buildCookiecutterTemplate(template, context, outputDir = '.', path = null, templatePath = ".") { |
azvyagintsev | 7a123aa | 2019-01-09 21:38:56 +0200 | [diff] [blame] | 344 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | dad7b46 | 2017-03-27 13:53:05 +0200 | [diff] [blame] | 345 | configFile = "default_config.yaml" |
Tomáš Kukrál | 6de8504 | 2017-04-12 17:49:05 +0200 | [diff] [blame] | 346 | writeFile file: configFile, text: context |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 347 | common.warningMsg('Old Cookiecutter env detected!') |
| 348 | 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" |
| 349 | output = sh(returnStdout: true, script: command) |
| 350 | common.infoMsg('[Cookiecutter build] Result:' + output) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
Denis Egorenko | 6c2e3ae | 2018-10-17 16:45:56 +0400 | [diff] [blame] | 354 | * |
| 355 | * @param context - context template |
| 356 | * @param contextName - context template name |
| 357 | * @param saltMasterName - hostname of Salt Master node |
| 358 | * @param virtualenv - pyvenv with CC and dep's |
| 359 | * @param templateEnvDir - root of CookieCutter |
| 360 | * @return |
| 361 | */ |
| 362 | def generateModel(context, contextName, saltMasterName, virtualenv, modelEnv, templateEnvDir, multiModels = true) { |
Denis Egorenko | fa2c675 | 2018-10-18 15:51:45 +0400 | [diff] [blame] | 363 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | 6c2e3ae | 2018-10-17 16:45:56 +0400 | [diff] [blame] | 364 | def generatedModel = multiModels ? "${modelEnv}/${contextName}" : modelEnv |
| 365 | def templateContext = readYaml text: context |
| 366 | def clusterDomain = templateContext.default_context.cluster_domain |
| 367 | def clusterName = templateContext.default_context.cluster_name |
| 368 | def outputDestination = "${generatedModel}/classes/cluster/${clusterName}" |
| 369 | def templateBaseDir = templateEnvDir |
| 370 | def templateDir = "${templateEnvDir}/dir" |
| 371 | def templateOutputDir = templateBaseDir |
| 372 | dir(templateEnvDir) { |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 373 | if (fileExists(new File(templateEnvDir, 'tox.ini').toString())) { |
Aleksey Zvyagintsev | 07b07ba | 2019-02-28 13:34:13 +0000 | [diff] [blame] | 374 | def tempContextFile = new File(templateEnvDir, 'tempContext.yaml').toString() |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 375 | writeFile file: tempContextFile, text: context |
| 376 | common.warningMsg('Generating models using context:\n') |
| 377 | print(context) |
| 378 | withEnv(["CONFIG_FILE=$tempContextFile", |
Aleksey Zvyagintsev | 07b07ba | 2019-02-28 13:34:13 +0000 | [diff] [blame] | 379 | "OUTPUT_DIR=${modelEnv}", |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 380 | ]) { |
| 381 | print('[Cookiecutter build] Result:\n' + |
| 382 | sh(returnStdout: true, script: 'tox -ve generate_auto')) |
Aleksey Zvyagintsev | 4c745e5 | 2019-02-21 10:30:02 +0000 | [diff] [blame] | 383 | } |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 384 | } else { |
| 385 | common.warningMsg("Old format: Generating model from context ${contextName}") |
| 386 | def productList = ["infra", "cicd", "kdt", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"] |
| 387 | for (product in productList) { |
| 388 | // get templateOutputDir and productDir |
| 389 | templateOutputDir = "${templateEnvDir}/output/${product}" |
| 390 | productDir = product |
| 391 | templateDir = "${templateEnvDir}/cluster_product/${productDir}" |
| 392 | // Bw for 2018.8.1 and older releases |
| 393 | if (product.startsWith("stacklight") && (!fileExists(templateDir))) { |
| 394 | common.warningMsg("Old release detected! productDir => 'stacklight2' ") |
| 395 | productDir = "stacklight2" |
| 396 | templateDir = "${templateEnvDir}/cluster_product/${productDir}" |
| 397 | } |
| 398 | // generate infra unless its explicitly disabled |
| 399 | if ((product == "infra" && templateContext.default_context.get("infra_enabled", "True").toBoolean()) |
| 400 | || (templateContext.default_context.get(product + "_enabled", "False").toBoolean())) { |
| 401 | |
| 402 | common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir) |
| 403 | |
| 404 | sh "rm -rf ${templateOutputDir} || true" |
| 405 | sh "mkdir -p ${templateOutputDir}" |
| 406 | sh "mkdir -p ${outputDestination}" |
| 407 | |
| 408 | buildCookiecutterTemplate(templateDir, context, templateOutputDir, virtualenv, templateBaseDir) |
| 409 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 410 | } else { |
| 411 | common.warningMsg("Product " + product + " is disabled") |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | def localRepositories = templateContext.default_context.local_repositories |
| 416 | localRepositories = localRepositories ? localRepositories.toBoolean() : false |
| 417 | def offlineDeployment = templateContext.default_context.offline_deployment |
| 418 | offlineDeployment = offlineDeployment ? offlineDeployment.toBoolean() : false |
| 419 | if (localRepositories && !offlineDeployment) { |
| 420 | def mcpVersion = templateContext.default_context.mcp_version |
| 421 | def aptlyModelUrl = templateContext.default_context.local_model_url |
| 422 | def ssh = new com.mirantis.mk.Ssh() |
| 423 | dir(path: modelEnv) { |
| 424 | ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\"" |
| 425 | if (!(mcpVersion in ["nightly", "testing", "stable"])) { |
| 426 | ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}" |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | def nodeFile = "${generatedModel}/nodes/${saltMasterName}.${clusterDomain}.yml" |
| 432 | def nodeString = """classes: |
| 433 | - cluster.${clusterName}.infra.config |
| 434 | parameters: |
| 435 | _param: |
| 436 | linux_system_codename: xenial |
| 437 | reclass_data_revision: master |
| 438 | linux: |
| 439 | system: |
| 440 | name: ${saltMasterName} |
| 441 | domain: ${clusterDomain} |
| 442 | """ |
| 443 | sh "mkdir -p ${generatedModel}/nodes/" |
| 444 | writeFile(file: nodeFile, text: nodeString) |
| 445 | } |
Denis Egorenko | 6c2e3ae | 2018-10-17 16:45:56 +0400 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 450 | * Install jinja rendering in isolated environment |
| 451 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 452 | * @param path Path where virtualenv is created |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 453 | */ |
| 454 | def setupJinjaVirtualenv(path) { |
| 455 | requirements = [ |
Denis Egorenko | 97ef0fb | 2020-01-13 14:22:49 +0400 | [diff] [blame] | 456 | 'jinja2-cli==0.7.0', |
| 457 | 'pyyaml==5.3', |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 458 | ] |
| 459 | setupVirtualenv(path, 'python2', requirements) |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Generate the Jinja templates with given context |
| 464 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 465 | * @param path Path where virtualenv is created |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 466 | */ |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 467 | def jinjaBuildTemplate(template, context, path = none) { |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 468 | contextFile = "jinja_context.yml" |
| 469 | contextString = "" |
| 470 | for (parameter in context) { |
| 471 | contextString = "${contextString}${parameter.key}: ${parameter.value}\n" |
| 472 | } |
| 473 | writeFile file: contextFile, text: contextString |
| 474 | cmd = "jinja2 ${template} ${contextFile} --format=yaml" |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 475 | data = sh(returnStdout: true, script: cmd) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 476 | echo(data) |
| 477 | return data |
| 478 | } |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 479 | |
| 480 | /** |
| 481 | * Install salt-pepper in isolated environment |
| 482 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 483 | * @param path Path where virtualenv is created |
| 484 | * @param url SALT_MASTER_URL |
| 485 | * @param credentialsId Credentials to salt api |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 486 | */ |
Jakub Josef | c9b6d66 | 2018-02-21 16:21:03 +0100 | [diff] [blame] | 487 | def setupPepperVirtualenv(path, url, credentialsId) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 488 | def common = new com.mirantis.mk.Common() |
| 489 | |
| 490 | // virtualenv setup |
Mykyta Karpin | 81756c9 | 2018-03-02 13:03:26 +0200 | [diff] [blame] | 491 | // pin pepper till https://mirantis.jira.com/browse/PROD-18188 is fixed |
| 492 | requirements = ['salt-pepper>=0.5.2,<0.5.4'] |
Jakub Josef | c9b6d66 | 2018-02-21 16:21:03 +0100 | [diff] [blame] | 493 | setupVirtualenv(path, 'python2', requirements, null, true, true) |
chnyda | bcfff18 | 2017-11-29 10:24:36 +0100 | [diff] [blame] | 494 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 495 | // pepperrc creation |
| 496 | rcFile = "${path}/pepperrc" |
| 497 | creds = common.getPasswordCredentials(credentialsId) |
| 498 | rc = """\ |
| 499 | [main] |
| 500 | SALTAPI_EAUTH=pam |
| 501 | SALTAPI_URL=${url} |
| 502 | SALTAPI_USER=${creds.username} |
| 503 | SALTAPI_PASS=${creds.password.toString()} |
| 504 | """ |
| 505 | writeFile file: rcFile, text: rc |
| 506 | return rcFile |
Jakub Josef | d067f61 | 2017-09-26 13:42:56 +0200 | [diff] [blame] | 507 | } |
Oleh Hryhorov | 44569fb | 2017-10-26 17:04:55 +0300 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * Install devops in isolated environment |
| 511 | * |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 512 | * @param path Path where virtualenv is created |
| 513 | * @param clean Define to true is the venv have to cleaned up before install a new one |
Oleh Hryhorov | 44569fb | 2017-10-26 17:04:55 +0300 | [diff] [blame] | 514 | */ |
Aleksey Zvyagintsev | c4f66f6 | 2019-02-21 10:39:34 +0000 | [diff] [blame] | 515 | def setupDevOpsVenv(venv, clean = false) { |
Oleh Hryhorov | 44569fb | 2017-10-26 17:04:55 +0300 | [diff] [blame] | 516 | requirements = ['git+https://github.com/openstack/fuel-devops.git'] |
| 517 | setupVirtualenv(venv, 'python2', requirements, null, false, clean) |
| 518 | } |