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