blob: 597809c45ec1ef2b013cfceaa49ae60252210326 [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +03003@Grab(group='org.yaml', module='snakeyaml', version='1.17')
4import org.yaml.snakeyaml.Yaml
5
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03006/**
7 * https://issues.jenkins-ci.org/browse/JENKINS-26481
8 * fix groovy List.collect()
9 **/
10@NonCPS
11def constructString(ArrayList options, String keyOption, String separator = " ") {
12 return options.collect { keyOption + it }.join(separator).replaceAll("\n", "")
13}
14
15/**
16 * Generate current timestamp
17 *
18 * @param format Defaults to yyyyMMddHHmmss
19 */
20def getDatetime(format = "yyyyMMddHHmmss") {
21 def now = new Date();
22 return now.format(format, TimeZone.getTimeZone('UTC'));
Denis Egorenko8c606552016-12-07 14:22:50 +040023}
24
25/**
26 * Run tox with or without specified environment
27 * @param env String, name of environment
28 */
29def runTox(String env = null) {
30 if (env) {
31 sh "tox -v -e ${env}"
32 } else {
33 sh "tox -v"
34 }
35}
Sergey Kulanove897d8f2017-01-23 16:44:03 +020036
37/**
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +030038 * Convert YAML document to Map object
39 * @param data YAML string
40 */
41@NonCPS
42def loadYAML(String data) {
43 def yaml = new Yaml()
44 return yaml.load(data)
45}
46
47/**
48 * Convert Map object to YAML string
49 * @param map Map object
50 */
51@NonCPS
52def dumpYAML(Map map) {
53 def yaml = new Yaml()
54 return yaml.dump(map)
55}
56
57/**
Sergey Kulanove897d8f2017-01-23 16:44:03 +020058 * Run function on k8s cluster
59 *
60 * @param config LinkedHashMap
61 * config includes next parameters:
62 * - label, pod label
63 * - function, code that should be run on k8s cluster
64 * - jnlpImg, jnlp slave image
65 * - slaveImg, slave image
66 *
67 * Usage example:
68 *
69 * def runFunc = new com.mirantis.mcp.Common()
70 * runFunc.runOnKubernetes ([
71 * function : this.&buildCalicoContainers,
72 * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest',
73 * slaveImg : 'sandbox-docker-dev-local.docker.mirantis.net/skulanov/jenkins-slave-images/calico-slave:1'
74 * ])
75 * // promotion example. In case of promotion we need only jnlp container
76 * def runFunc = new com.mirantis.mcp.Common()
77 * runFunc.runOnKubernetes ([
78 * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest',
79 * function : this.&promote_artifacts
80 * ])
81 */
82def runOnKubernetes(LinkedHashMap config) {
83
84
85 def jenkinsSlaveImg = config.get('slaveImg', 'none')
86 def jnlpSlaveImg = config.get('jnlpImg', 'none')
87 def lbl = config.get('label', "buildpod.${env.JOB_NAME}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_'))
88 def toRun = config.get('function', 'none')
89
Sergey Kulanovb1aa0ff2017-01-23 17:48:44 +020090 if (jnlpSlaveImg == 'none') {
Sergey Kulanove897d8f2017-01-23 16:44:03 +020091 error('jnlp Slave image MUST be defined')
92 }
93
94 if (toRun == 'none'){
95 error('Code that should be run on k8s MUST be passed along with function parameter')
96 }
97
98 if (jenkinsSlaveImg == 'none'){
99 // we are running jnlp container only, since no jenkinsSlaveImg is specified, so
100 // we are in promotion mode
101 podTemplate(label: lbl,
102 containers: [
103 containerTemplate(
104 name: 'jnlp',
105 image: jnlpSlaveImg,
106 args: '${computer.jnlpmac} ${computer.name}'
107 )
108 ],
109 ) {
110 node(lbl){
111 container('jnlp') {
112 toRun()
113 }
114 }
115 }
116
117 } else {
118 podTemplate(label: lbl,
119 containers: [
120 containerTemplate(
121 name: 'jnlp',
122 image: jnlpSlaveImg,
123 args: '${computer.jnlpmac} ${computer.name}'
124 ),
125 containerTemplate(
126 name: 'k8s-slave',
127 image: jenkinsSlaveImg,
128 alwaysPullImage: false,
129 ttyEnabled: true,
130 privileged: true
131 )
132 ],
133 ) {
134 node(lbl){
135 container('k8s-slave') {
Sergey Kulanovb36e36e2017-01-23 18:26:40 +0200136 return toRun()
Sergey Kulanove897d8f2017-01-23 16:44:03 +0200137 }
138 }
139 }
140 } //else
141
142}