blob: 10d0e5dbbf217b8c896e6d8f76f24e7363ab05bf [file] [log] [blame]
Dennis Dmitrievf220d972018-10-10 15:19:14 +03001import java.text.SimpleDateFormat
2
Dennis Dmitriev87c22d72018-11-09 17:18:00 +02003def gerrit = new com.mirantis.mk.Gerrit()
Dennis Dmitrievf220d972018-10-10 15:19:14 +03004def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
5def date = new Date()
6def common_scripts_commit = "${COMMON_SCRIPTS_COMMIT}"
7def iso_name = "${CONFIG_DRIVE_ISO_NAME}" ?: "cfg01.${CLUSTER_NAME}-config-${dateFormat.format(date)}.iso"
8def node_name = "${NODE_NAME}"
9
10def smc = [:]
11smc['SALT_MASTER_MINION_ID'] = "cfg01.${CLUSTER_NAME}.local"
12smc['SALT_MASTER_DEPLOY_IP'] = "${SALT_MASTER_DEPLOY_IP}"
13smc['DEPLOY_NETWORK_GW'] = "${DEPLOY_NETWORK_GW}"
14smc['DEPLOY_NETWORK_NETMASK'] = "${DEPLOY_NETWORK_NETMASK}"
Dennis Dmitriev18319fa2019-03-19 22:39:02 +020015smc['DEPLOY_NETWORK_MTU'] = "${DEPLOY_NETWORK_MTU}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +030016smc['DNS_SERVERS'] = "${DNS_SERVERS}"
17smc['PIPELINES_FROM_ISO'] = '${PIPELINES_FROM_ISO}'
18smc['PIPELINE_REPO_URL'] = '${PIPELINE_REPO_URL}'
19smc['MCP_VERSION'] = "${MCP_VERSION}"
20// smc['LOCAL_REPOS'] = 'true'
21smc['MCP_SALT_REPO_KEY'] = "${MCP_SALT_REPO_KEY}"
22smc['MCP_SALT_REPO_URL'] = "${MCP_SALT_REPO_URL}"
Tatyana Leontovich4718bdf2019-05-24 12:37:06 +030023smc['MCP_SALT_REPO_UPDATES'] = "${MCP_SALT_REPO_UPDATES}"
24
Dennis Dmitrievf220d972018-10-10 15:19:14 +030025
26def entries(m) {
27 m.collect {k, v -> [k, v]}
28}
29
Dennis Dmitriev1f08b0c2019-05-27 17:03:53 +030030timeout(time: 2, unit: 'HOURS') {
Dennis Dmitrievf220d972018-10-10 15:19:14 +030031node (node_name) {
32
33 timestamps(){
34
35 stage("Clean Workspace") {
36 step([$class: 'WsCleanup'])
37 }
38
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020039 stage("Get mk-pipelines, pipeline-library and mcp-common-scripts repositories") {
40 def cloned = true
41 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
42 keyFileVariable: "GERRIT_KEY",
43 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
44 usernameVariable: "GERRIT_USERNAME",
45 passwordVariable: "GERRIT_PASSWORD"]]) {
Dennis Dmitrievf220d972018-10-10 15:19:14 +030046
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020047 sh ("""\
48 set -ex
49 eval \$(ssh-agent)
50 ssh-add ${GERRIT_KEY}
Dennis Dmitrieve3884652019-03-05 14:26:44 +020051 git clone ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp/mcp-common-scripts mcp-common-scripts-git
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020052 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mk/mk-pipelines mk-pipelines
53 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp-ci/pipeline-library pipeline-library
54 """)
55
Dennis Dmitrieve3884652019-03-05 14:26:44 +020056 if (COMMON_SCRIPTS_COMMIT != '') {
57 sh ("""\
58 set -ex
59 cd mcp-common-scripts-git
60 git checkout ${COMMON_SCRIPTS_COMMIT}
61 git log -1
62 """)
63 }
64
65 if (MCP_COMMON_SCRIPTS_REFS != '') {
66 sh ("""\
67 set -ex
68 eval \$(ssh-agent)
69 ssh-add ${GERRIT_KEY}
70 cd mcp-common-scripts-git
Tatyana Leontovichbcb7bd32019-06-06 14:17:51 +030071 git fetch https://gerrit.mcp.mirantis.com/mcp/mcp-common-scripts ${MCP_COMMON_SCRIPTS_REFS} && git checkout FETCH_HEAD
Dennis Dmitrieve3884652019-03-05 14:26:44 +020072 git log -1
73 """)
74 }
75
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020076 if (PIPELINE_LIBRARY_REF != '') {
77 sh ("""\
78 set -ex
79 eval \$(ssh-agent)
80 ssh-add ${GERRIT_KEY}
81 cd pipeline-library
82 git fetch https://gerrit.mcp.mirantis.net/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF}
83 git tag ${MCP_VERSION} FETCH_HEAD -f
Dennis Dmitrieve3884652019-03-05 14:26:44 +020084 git branch -f release/${MCP_VERSION} FETCH_HEAD
85 git log -1
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020086 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020087 }
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020088 if (MK_PIPELINES_REF != '') {
89 sh ("""\
90 set -ex
91 eval \$(ssh-agent)
92 ssh-add ${GERRIT_KEY}
93 cd mk-pipelines
Tatyana Leontovich49f00ac2019-04-16 23:30:10 +030094 git fetch https://gerrit.mcp.mirantis.net/mk/mk-pipelines ${MK_PIPELINES_REF}
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020095 git tag ${MCP_VERSION} FETCH_HEAD -f
Dennis Dmitrieve3884652019-03-05 14:26:44 +020096 git branch -f release/${MCP_VERSION} FETCH_HEAD
97 git log -1
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020098 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020099 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300100
Tatyana Leontovich8ac26d72019-05-15 23:33:38 +0300101
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200102 }
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200103 }
104
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300105 stage("Get cluster model") {
106 def model_url = "${MODEL_URL}"
107 sh "rm -rf model"
108 if (MODEL_URL_OBJECT_TYPE == 'tar.gz') {
109 sh "wget -O model.tar.gz '${model_url}'"
110 sh "mkdir model && cd model && tar zxfv ../model.tar.gz"
111 } else {
112 sh "git clone --recursive $model_url -b ${MCP_VERSION} model"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200113 // remove .git file with absolute path and re-init the file with relative path
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300114 sh "rm model/classes/system/.git"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200115 sh "cd model && git submodule update"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300116 }
117 }
118
Dmitry Tyzhnenkofcb4dab2019-03-05 20:11:59 +0200119 stage("Prepare arguments for generation config drive") {
120
121 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
122 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
123 sh "chmod +x ${config_drive_script_path}"
124
125 //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
126 args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/"
127 try {
128 sh "test -f model/encryption-key.asc"
129 args = "${args} --gpg-key model/encryption-key.asc ${iso_name}"
130
131 } catch (e) {
132 args = "${args} ${iso_name}"
133 }
134 }
135
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300136 stage("Set data"){
137 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200138 // Replace only if the variable is non-empty, leave default otherwise
139 if (i[1]) {
140 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
141 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300142 }
143 }
144
145 stage("Create user_data2"){
146 //http://jen20.com/2015/10/04/cloudconfig-merging.html
147 //TODO(ddmitriev): allow to read such file from
148 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
149 def user_data2 = """\
150#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
151
152#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
153# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
154# content: |
155# GRUB_RECORDFAIL_TIMEOUT=30
156# GRUB_TIMEOUT=10
157# GRUB_TIMEOUT_STYLE=menu
158#
159# - path: /root/interfaces
160# content: |
161# auto lo
162# iface lo inet loopback
163#
164# auto ens3
165# iface ens3 inet dhcp
166#
167# - path: /root/.ssh/config
168# owner: root:root
169# permissions: '0600'
170# content: |
171# Host *
172# ServerAliveInterval 60
173# ServerAliveCountMax 0
174# StrictHostKeyChecking no
175# UserKnownHostsFile /dev/null
176#
177# - path: /etc/cloud/master_environment_override
178# owner: root:root
179# permissions: '0600'
180# content: |
181# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
182# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
183# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
184# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
185# export DNS_SERVERS="${DNS_SERVERS}"
186# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
187# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
188# export MCP_VERSION="${MCP_VERSION}"
189# export LOCAL_REPOS="true"
190# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
191# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
Tatyana Leontovich4718bdf2019-05-24 12:37:06 +0300192# export MCP_SALT_REPO_UPDATES="${MCP_SALT_REPO_UPDATES}"
193# export ENABLE_MCP_SALT_REPO_UPDATES="true"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300194
195output:
196 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
197
198ssh_pwauth: True
199users:
200 - name: root
201 sudo: ALL=(ALL) NOPASSWD:ALL
202 shell: /bin/bash
203
204disable_root: false
205chpasswd:
206 list: |
207 root:r00tme
208 expire: False
209
210bootcmd:
211 # Block access to SSH while node is preparing
212 - cloud-init-per once sudo touch /is_cloud_init_started
213 # Enable root access
214 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
215 - service sshd restart
216
217merge_how: "dict(recurse_array)+list(append)"
218"""
219 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
220 }
221
222 stage("Create config-drive"){
223 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200224 // apt package genisoimage is required for this stage
225 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300226 }
227
228 stage("Save artifacts") {
229 archiveArtifacts allowEmptyArchive: false,
230 artifacts: "${iso_name}"
231 }
232
233 stage("Download config drive to slave") {
234 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
Dennis Dmitrieve3884652019-03-05 14:26:44 +0200235 println "Remove previous config drive ISO"
236 sh("""\
237 rm /home/jenkins/images/${iso_name} || true
238 """)
239
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300240 def b_res = build job: 'download-config-drive',
241 parameters: [
242 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
243 string(name: 'NODE_NAME', value: "${NODE_NAME}")
244 ]
245 } else {
246 echo "Drive only generated. But didn't download"
247 }
248 }
249 }
250}
Dennis Dmitriev1f08b0c2019-05-27 17:03:53 +0300251} // timeout