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 | * |
| 12 | * @param path Path to virtualenv |
| 13 | * @param python Version of Python (python/python3) |
| 14 | * @param reqs Environment requirements in list format |
| 15 | */ |
| 16 | def setupVirtualenv(path, python = 'python2', reqs = []) { |
| 17 | virtualenv_cmd = "virtualenv ${path} --python ${python}" |
| 18 | |
| 19 | echo("[Python ${path}] Setup ${python} environment") |
| 20 | sh(returnStdout: true, script: virtualenv_cmd) |
| 21 | args = "" |
| 22 | for (req in reqs) { |
| 23 | args = args + "${req}\n" |
| 24 | } |
| 25 | writeFile file: "${path}/requirements.txt", text: args |
| 26 | runVirtualenvCommand(path, "pip install -r ${path}/requirements.txt") |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Run command in specific python virtualenv |
| 31 | * |
| 32 | * @param path Path to virtualenv |
| 33 | * @param cmd Command to be executed |
| 34 | */ |
| 35 | def runVirtualenvCommand(path, cmd) { |
| 36 | virtualenv_cmd = ". ${path}/bin/activate; ${cmd}" |
| 37 | echo("[Python ${path}] Run command ${cmd}") |
| 38 | output = sh( |
| 39 | returnStdout: true, |
| 40 | script: virtualenv_cmd |
| 41 | ).trim() |
| 42 | return output |
| 43 | } |
| 44 | |
| 45 | @NonCPS |
| 46 | def loadJson(rawData) { |
| 47 | return new groovy.json.JsonSlurperClassic().parseText(rawData) |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Parse content from markup-text tables to variables |
| 52 | * |
| 53 | * @param tableStr String representing the table |
| 54 | * @param mode Either list (1st row are keys) or item (key, value rows) |
| 55 | * @param format Format of the table |
| 56 | */ |
| 57 | def parseTextTable(tableStr, type = 'item', format = 'rest') { |
| 58 | parserScript = "${env.WORKSPACE}/scripts/parse_text_table.py" |
| 59 | tableFile = "${env.WORKSPACE}/prettytable.txt" |
| 60 | writeFile file: tableFile, text: tableStr |
| 61 | rawData = sh ( |
| 62 | script: "python ${parserScript} --file '${tableFile}' --type ${type}", |
| 63 | returnStdout: true |
| 64 | ).trim() |
| 65 | data = loadJson(rawData) |
| 66 | echo("[Parsed table] ${data}") |
| 67 | return data |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Install cookiecutter in isolated environment |
| 72 | * |
| 73 | * @param path Path where virtualenv is created |
| 74 | */ |
| 75 | def setupCookiecutterVirtualenv(path) { |
| 76 | requirements = [ |
| 77 | 'cookiecutter', |
| 78 | ] |
| 79 | setupVirtualenv(path, 'python2', requirements) |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Generate the cookiecutter templates with given context |
| 84 | * |
| 85 | * @param path Path where virtualenv is created |
| 86 | */ |
| 87 | def buildCookiecutterTemplate (template, context, path = none) { |
| 88 | contextFile = "default_context.json" |
| 89 | contextString = "parameters:\n" |
| 90 | for (parameter in context) { |
| 91 | contextString = "${contextString} ${parameter.key}: ${parameter.value}\n" |
| 92 | } |
| 93 | writeFile file: contextFile, text: contextString |
| 94 | command = ". ./${work_dir}/bin/activate; cookiecutter --config-file ${cookiecutter_context_file} --overwrite-if-exists --verbose --no-input ${template_dir}" |
| 95 | output = sh (returnStdout: true, script: command) |
| 96 | echo("[Cookiecutter build] Output: ${output}") |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Install jinja rendering in isolated environment |
| 101 | * |
| 102 | * @param path Path where virtualenv is created |
| 103 | */ |
| 104 | def setupJinjaVirtualenv(path) { |
| 105 | requirements = [ |
| 106 | 'jinja2-cli', |
| 107 | 'pyyaml', |
| 108 | ] |
| 109 | setupVirtualenv(path, 'python2', requirements) |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Generate the Jinja templates with given context |
| 114 | * |
| 115 | * @param path Path where virtualenv is created |
| 116 | */ |
| 117 | def jinjaBuildTemplate (template, context, path = none) { |
| 118 | contextFile = "jinja_context.yml" |
| 119 | contextString = "" |
| 120 | for (parameter in context) { |
| 121 | contextString = "${contextString}${parameter.key}: ${parameter.value}\n" |
| 122 | } |
| 123 | writeFile file: contextFile, text: contextString |
| 124 | cmd = "jinja2 ${template} ${contextFile} --format=yaml" |
| 125 | data = sh (returnStdout: true, script: cmd) |
| 126 | echo(data) |
| 127 | return data |
| 128 | } |