blob: c3b2c9791639ed8d19e7099f297f0d0a0b4021cf [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Python functions
6 *
7 */
8
9/**
10 * Install python virtualenv
11 *
Vladislav Naumov11103862017-07-19 17:02:39 +030012 * @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 Kolekonovba203982016-12-21 18:32:17 +040016 */
Jakub Josef996f4ef2017-10-24 13:20:43 +020017def setupVirtualenv(path, python = 'python2', reqs=[], reqs_path=null, clean=false, useSystemPackages=false) {
Tomáš Kukrál69c25452017-07-27 14:59:40 +020018 def common = new com.mirantis.mk.Common()
19
Jakub Josef87a8a3c2018-01-26 12:11:11 +010020 def offlineDeployment = env.getEnvironment().containsKey("OFFLINE_DEPLOYMENT") && env["OFFLINE_DEPLOYMENT"].toBoolean()
Mykyta Karpin1e4bfc92017-11-01 14:38:25 +020021 def virtualenv_cmd = "virtualenv ${path} --python ${python}"
Jakub Josef996f4ef2017-10-24 13:20:43 +020022 if (useSystemPackages){
23 virtualenv_cmd += " --system-site-packages"
24 }
Tomáš Kukrál69c25452017-07-27 14:59:40 +020025 if (clean) {
26 common.infoMsg("Cleaning venv directory " + path)
27 sh("rm -rf \"${path}\"")
28 }
29
Jakub Josef87a8a3c2018-01-26 12:11:11 +010030 if(offlineDeployment){
31 virtualenv_cmd+=" --no-download"
32 }
Tomáš Kukrál69c25452017-07-27 14:59:40 +020033 common.infoMsg("[Python ${path}] Setup ${python} environment")
Sergey Kolekonovba203982016-12-21 18:32:17 +040034 sh(returnStdout: true, script: virtualenv_cmd)
Jakub Josef87a8a3c2018-01-26 12:11:11 +010035 if(!offlineDeployment){
Jakub Josef5f97d532017-11-29 18:51:41 +010036 try {
Jakub Josefa2491ad2018-01-15 16:26:27 +010037 runVirtualenvCommand(path, "pip install -U setuptools pip")
Jakub Josef5f97d532017-11-29 18:51:41 +010038 } catch(Exception e) {
Jakub Josefa2491ad2018-01-15 16:26:27 +010039 common.warningMsg("Setuptools and pip cannot be updated, you might be offline but OFFLINE_DEPLOYMENT global property not initialized!")
Jakub Josef5f97d532017-11-29 18:51:41 +010040 }
Yuriy Taraday67352e92017-10-12 10:54:23 +000041 }
Vladislav Naumov11103862017-07-19 17:02:39 +030042 if (reqs_path==null) {
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 Kolekonovba203982016-12-21 18:32:17 +040049 }
Jakub Josefa2491ad2018-01-15 16:26:27 +010050 runVirtualenvCommand(path, "pip install -r ${reqs_path}", true)
Sergey Kolekonovba203982016-12-21 18:32:17 +040051}
52
53/**
54 * Run command in specific python virtualenv
55 *
56 * @param path Path to virtualenv
57 * @param cmd Command to be executed
Jakub Josefe2f4ebb2018-01-15 16:11:51 +010058 * @param silent dont print any messages (optional, default false)
Sergey Kolekonovba203982016-12-21 18:32:17 +040059 */
Jakub Josefe2f4ebb2018-01-15 16:11:51 +010060def runVirtualenvCommand(path, cmd, silent=false) {
Tomáš Kukrál69c25452017-07-27 14:59:40 +020061 def common = new com.mirantis.mk.Common()
62
Jakub Josef5feeee42018-01-08 15:50:36 +010063 virtualenv_cmd = "set +x; . ${path}/bin/activate; ${cmd}"
Jakub Josefe2f4ebb2018-01-15 16:11:51 +010064 if(!silent){
65 common.infoMsg("[Python ${path}] Run command ${cmd}")
66 }
Sergey Kolekonovba203982016-12-21 18:32:17 +040067 output = sh(
68 returnStdout: true,
69 script: virtualenv_cmd
70 ).trim()
71 return output
72}
73
Ales Komarekd874d482016-12-26 10:33:29 +010074
75/**
76 * Install docutils in isolated environment
77 *
78 * @param path Path where virtualenv is created
79 */
80def setupDocutilsVirtualenv(path) {
81 requirements = [
82 'docutils',
83 ]
84 setupVirtualenv(path, 'python2', requirements)
85}
86
87
Sergey Kolekonovba203982016-12-21 18:32:17 +040088@NonCPS
89def loadJson(rawData) {
90 return new groovy.json.JsonSlurperClassic().parseText(rawData)
91}
92
93/**
94 * Parse content from markup-text tables to variables
95 *
96 * @param tableStr String representing the table
97 * @param mode Either list (1st row are keys) or item (key, value rows)
98 * @param format Format of the table
99 */
Ales Komarekd874d482016-12-26 10:33:29 +0100100def parseTextTable(tableStr, type = 'item', format = 'rest', path = none) {
Ales Komarek0e558ee2016-12-23 13:02:55 +0100101 parserFile = "${env.WORKSPACE}/textTableParser.py"
102 parserScript = """import json
103import argparse
104from docutils.parsers.rst import tableparser
105from docutils import statemachine
106
107def parse_item_table(raw_data):
108 i = 1
109 pretty_raw_data = []
110 for datum in raw_data:
111 if datum != "":
112 if datum[3] != ' ' and i > 4:
113 pretty_raw_data.append(raw_data[0])
114 if i == 3:
115 pretty_raw_data.append(datum.replace('-', '='))
116 else:
117 pretty_raw_data.append(datum)
118 i += 1
119 parser = tableparser.GridTableParser()
120 block = statemachine.StringList(pretty_raw_data)
121 docutils_data = parser.parse(block)
122 final_data = {}
123 for line in docutils_data[2]:
124 key = ' '.join(line[0][3]).strip()
125 value = ' '.join(line[1][3]).strip()
126 if key != "":
127 try:
128 value = json.loads(value)
129 except:
130 pass
131 final_data[key] = value
132 i+=1
133 return final_data
134
135def parse_list_table(raw_data):
136 i = 1
137 pretty_raw_data = []
138 for datum in raw_data:
139 if datum != "":
140 if datum[3] != ' ' and i > 4:
141 pretty_raw_data.append(raw_data[0])
142 if i == 3:
143 pretty_raw_data.append(datum.replace('-', '='))
144 else:
145 pretty_raw_data.append(datum)
146 i += 1
147 parser = tableparser.GridTableParser()
148 block = statemachine.StringList(pretty_raw_data)
149 docutils_data = parser.parse(block)
150 final_data = []
151 keys = []
152 for line in docutils_data[1]:
153 for item in line:
154 keys.append(' '.join(item[3]).strip())
155 for line in docutils_data[2]:
156 final_line = {}
157 key = ' '.join(line[0][3]).strip()
158 value = ' '.join(line[1][3]).strip()
159 if key != "":
160 try:
161 value = json.loads(value)
162 except:
163 pass
164 final_data[key] = value
165 i+=1
166 return final_data
167
168def parse_list_table(raw_data):
169 i = 1
170 pretty_raw_data = []
171 for datum in raw_data:
172 if datum != "":
173 if datum[3] != ' ' and i > 4:
174 pretty_raw_data.append(raw_data[0])
175 if i == 3:
176 pretty_raw_data.append(datum.replace('-', '='))
177 else:
178 pretty_raw_data.append(datum)
179 i += 1
180 parser = tableparser.GridTableParser()
181 block = statemachine.StringList(pretty_raw_data)
182 docutils_data = parser.parse(block)
183 final_data = []
184 keys = []
185 for line in docutils_data[1]:
186 for item in line:
187 keys.append(' '.join(item[3]).strip())
188 for line in docutils_data[2]:
189 final_line = {}
190 i = 0
191 for item in line:
192 value = ' '.join(item[3]).strip()
193 try:
194 value = json.loads(value)
195 except:
196 pass
197 final_line[keys[i]] = value
198 i += 1
199 final_data.append(final_line)
200 return final_data
201
202def read_table_file(file):
203 table_file = open(file, 'r')
Ales Komarekc000c152016-12-23 15:32:54 +0100204 raw_data = table_file.read().split('\\n')
Ales Komarek0e558ee2016-12-23 13:02:55 +0100205 table_file.close()
206 return raw_data
207
208parser = argparse.ArgumentParser()
209parser.add_argument('-f','--file', help='File with table data', required=True)
210parser.add_argument('-t','--type', help='Type of table (list/item)', required=True)
211args = vars(parser.parse_args())
212
213raw_data = read_table_file(args['file'])
214
215if args['type'] == 'list':
216 final_data = parse_list_table(raw_data)
217else:
218 final_data = parse_item_table(raw_data)
219
220print json.dumps(final_data)
221"""
222 writeFile file: parserFile, text: parserScript
Sergey Kolekonovba203982016-12-21 18:32:17 +0400223 tableFile = "${env.WORKSPACE}/prettytable.txt"
224 writeFile file: tableFile, text: tableStr
Ales Komarekd874d482016-12-26 10:33:29 +0100225
226 cmd = "python ${parserFile} --file '${tableFile}' --type ${type}"
227 if (path) {
Ales Komarekc6d28dd2016-12-28 12:59:38 +0100228 rawData = runVirtualenvCommand(path, cmd)
Ales Komarekd874d482016-12-26 10:33:29 +0100229 }
230 else {
231 rawData = sh (
232 script: cmd,
233 returnStdout: true
234 ).trim()
235 }
Sergey Kolekonovba203982016-12-21 18:32:17 +0400236 data = loadJson(rawData)
237 echo("[Parsed table] ${data}")
238 return data
239}
240
241/**
242 * Install cookiecutter in isolated environment
243 *
244 * @param path Path where virtualenv is created
245 */
246def setupCookiecutterVirtualenv(path) {
247 requirements = [
248 'cookiecutter',
Jakub Josef4df78272017-04-26 14:36:36 +0200249 'jinja2==2.8.1',
250 'PyYAML==3.12'
Sergey Kolekonovba203982016-12-21 18:32:17 +0400251 ]
252 setupVirtualenv(path, 'python2', requirements)
253}
254
255/**
256 * Generate the cookiecutter templates with given context
257 *
Jakub Josef4e10c372017-04-26 14:13:50 +0200258 * @param template template
259 * @param context template context
260 * @param path Path where virtualenv is created (optional)
261 * @param templatePath path to cookiecutter template repo (optional)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400262 */
Jakub Josef4e10c372017-04-26 14:13:50 +0200263def buildCookiecutterTemplate(template, context, outputDir = '.', path = null, templatePath = ".") {
Tomáš Kukráldad7b462017-03-27 13:53:05 +0200264 configFile = "default_config.yaml"
265 configString = "default_context:\n"
Tomáš Kukrál6de85042017-04-12 17:49:05 +0200266 writeFile file: configFile, text: context
Jakub Josef4e61cc02017-04-26 14:29:09 +0200267 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 Kolekonovba203982016-12-21 18:32:17 +0400268 output = sh (returnStdout: true, script: command)
269 echo("[Cookiecutter build] Output: ${output}")
270}
271
272/**
Denis Egorenko6c2e3ae2018-10-17 16:45:56 +0400273 *
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 */
281def generateModel(context, contextName, saltMasterName, virtualenv, modelEnv, templateEnvDir, multiModels = true) {
Denis Egorenkofa2c6752018-10-18 15:51:45 +0400282 def common = new com.mirantis.mk.Common()
Denis Egorenko6c2e3ae2018-10-17 16:45:56 +0400283 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) {
292 common.infoMsg("Generating model from context ${contextName}")
293 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
294 for (product in productList) {
295 // get templateOutputDir and productDir
296 templateOutputDir = "${templateEnvDir}/output/${product}"
297 productDir = product
298 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
299 // Bw for 2018.8.1 and older releases
300 if (product.startsWith("stacklight") && (!fileExists(templateDir))) {
301 common.warningMsg("Old release detected! productDir => 'stacklight2' ")
302 productDir = "stacklight2"
303 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
304 }
305 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
306 && templateContext.default_context["${product}_enabled"].toBoolean())) {
307
308 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
309
310 sh "rm -rf ${templateOutputDir} || true"
311 sh "mkdir -p ${templateOutputDir}"
312 sh "mkdir -p ${outputDestination}"
313
314 buildCookiecutterTemplate(templateDir, context, templateOutputDir, virtualenv, templateBaseDir)
315 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
316 } else {
317 common.warningMsg("Product " + product + " is disabled")
318 }
319 }
320
321 def localRepositories = templateContext.default_context.local_repositories
322 localRepositories = localRepositories ? localRepositories.toBoolean() : false
323 def offlineDeployment = templateContext.default_context.offline_deployment
324 offlineDeployment = offlineDeployment ? offlineDeployment.toBoolean() : false
325 if (localRepositories && !offlineDeployment) {
326 def mcpVersion = templateContext.default_context.mcp_version
327 def aptlyModelUrl = templateContext.default_context.local_model_url
328 def ssh = new com.mirantis.mk.Ssh()
329 dir(path: modelEnv) {
330 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
331 if (!(mcpVersion in ["nightly", "testing", "stable"])) {
332 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
333 }
334 }
335 }
336
337 def nodeFile = "${generatedModel}/nodes/${saltMasterName}.${clusterDomain}.yml"
338 def nodeString = """classes:
339- cluster.${clusterName}.infra.config
340parameters:
341 _param:
342 linux_system_codename: xenial
343 reclass_data_revision: master
344 linux:
345 system:
346 name: ${saltMasterName}
347 domain: ${clusterDomain}
348 """
349 sh "mkdir -p ${generatedModel}/nodes/"
350 writeFile(file: nodeFile, text: nodeString)
351 }
352}
353
354/**
Sergey Kolekonovba203982016-12-21 18:32:17 +0400355 * Install jinja rendering in isolated environment
356 *
357 * @param path Path where virtualenv is created
358 */
359def setupJinjaVirtualenv(path) {
360 requirements = [
361 'jinja2-cli',
362 'pyyaml',
363 ]
364 setupVirtualenv(path, 'python2', requirements)
365}
366
367/**
368 * Generate the Jinja templates with given context
369 *
370 * @param path Path where virtualenv is created
371 */
372def jinjaBuildTemplate (template, context, path = none) {
373 contextFile = "jinja_context.yml"
374 contextString = ""
375 for (parameter in context) {
376 contextString = "${contextString}${parameter.key}: ${parameter.value}\n"
377 }
378 writeFile file: contextFile, text: contextString
379 cmd = "jinja2 ${template} ${contextFile} --format=yaml"
380 data = sh (returnStdout: true, script: cmd)
381 echo(data)
382 return data
383}
Oleg Grigorovbec45582017-09-12 20:29:24 +0300384
385/**
386 * Install salt-pepper in isolated environment
387 *
388 * @param path Path where virtualenv is created
chnydaa0dbb252017-10-05 10:46:09 +0200389 * @param url SALT_MASTER_URL
390 * @param credentialsId Credentials to salt api
Oleg Grigorovbec45582017-09-12 20:29:24 +0300391 */
Jakub Josefc9b6d662018-02-21 16:21:03 +0100392def setupPepperVirtualenv(path, url, credentialsId) {
chnydaa0dbb252017-10-05 10:46:09 +0200393 def common = new com.mirantis.mk.Common()
394
395 // virtualenv setup
Mykyta Karpin81756c92018-03-02 13:03:26 +0200396 // pin pepper till https://mirantis.jira.com/browse/PROD-18188 is fixed
397 requirements = ['salt-pepper>=0.5.2,<0.5.4']
Jakub Josefc9b6d662018-02-21 16:21:03 +0100398 setupVirtualenv(path, 'python2', requirements, null, true, true)
chnydabcfff182017-11-29 10:24:36 +0100399
chnydaa0dbb252017-10-05 10:46:09 +0200400 // pepperrc creation
401 rcFile = "${path}/pepperrc"
402 creds = common.getPasswordCredentials(credentialsId)
403 rc = """\
404[main]
405SALTAPI_EAUTH=pam
406SALTAPI_URL=${url}
407SALTAPI_USER=${creds.username}
408SALTAPI_PASS=${creds.password.toString()}
409"""
410 writeFile file: rcFile, text: rc
411 return rcFile
Jakub Josefd067f612017-09-26 13:42:56 +0200412}
Oleh Hryhorov44569fb2017-10-26 17:04:55 +0300413
414/**
415 * Install devops in isolated environment
416 *
417 * @param path Path where virtualenv is created
418 * @param clean Define to true is the venv have to cleaned up before install a new one
419 */
420def setupDevOpsVenv(venv, clean=false) {
421 requirements = ['git+https://github.com/openstack/fuel-devops.git']
422 setupVirtualenv(venv, 'python2', requirements, null, false, clean)
423}