blob: 86b9eec45f385602f4272bd75bee19754876b232 [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
30node (node_name) {
31
32 timestamps(){
33
34 stage("Clean Workspace") {
35 step([$class: 'WsCleanup'])
36 }
37
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020038 stage("Get mk-pipelines, pipeline-library and mcp-common-scripts repositories") {
39 def cloned = true
40 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
41 keyFileVariable: "GERRIT_KEY",
42 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
43 usernameVariable: "GERRIT_USERNAME",
44 passwordVariable: "GERRIT_PASSWORD"]]) {
Dennis Dmitrievf220d972018-10-10 15:19:14 +030045
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020046 sh ("""\
47 set -ex
48 eval \$(ssh-agent)
49 ssh-add ${GERRIT_KEY}
Dennis Dmitrieve3884652019-03-05 14:26:44 +020050 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 +020051 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mk/mk-pipelines mk-pipelines
52 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp-ci/pipeline-library pipeline-library
53 """)
54
Dennis Dmitrieve3884652019-03-05 14:26:44 +020055 if (COMMON_SCRIPTS_COMMIT != '') {
56 sh ("""\
57 set -ex
58 cd mcp-common-scripts-git
59 git checkout ${COMMON_SCRIPTS_COMMIT}
60 git log -1
61 """)
62 }
63
64 if (MCP_COMMON_SCRIPTS_REFS != '') {
65 sh ("""\
66 set -ex
67 eval \$(ssh-agent)
68 ssh-add ${GERRIT_KEY}
69 cd mcp-common-scripts-git
70 git fetch ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts ${MCP_COMMON_SCRIPTS_REFS} && git checkout FETCH_HEAD
71 git log -1
72 """)
73 }
74
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020075 if (PIPELINE_LIBRARY_REF != '') {
76 sh ("""\
77 set -ex
78 eval \$(ssh-agent)
79 ssh-add ${GERRIT_KEY}
80 cd pipeline-library
81 git fetch https://gerrit.mcp.mirantis.net/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF}
82 git tag ${MCP_VERSION} FETCH_HEAD -f
Dennis Dmitrieve3884652019-03-05 14:26:44 +020083 git branch -f release/${MCP_VERSION} FETCH_HEAD
84 git log -1
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020085 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020086 }
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020087 if (MK_PIPELINES_REF != '') {
88 sh ("""\
89 set -ex
90 eval \$(ssh-agent)
91 ssh-add ${GERRIT_KEY}
92 cd mk-pipelines
Tatyana Leontovich49f00ac2019-04-16 23:30:10 +030093 git fetch https://gerrit.mcp.mirantis.net/mk/mk-pipelines ${MK_PIPELINES_REF}
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020094 git tag ${MCP_VERSION} FETCH_HEAD -f
Dennis Dmitrieve3884652019-03-05 14:26:44 +020095 git branch -f release/${MCP_VERSION} FETCH_HEAD
96 git log -1
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020097 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020098 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +030099
Tatyana Leontovich8ac26d72019-05-15 23:33:38 +0300100
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200101 }
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200102 }
103
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300104 stage("Get cluster model") {
105 def model_url = "${MODEL_URL}"
106 sh "rm -rf model"
107 if (MODEL_URL_OBJECT_TYPE == 'tar.gz') {
108 sh "wget -O model.tar.gz '${model_url}'"
109 sh "mkdir model && cd model && tar zxfv ../model.tar.gz"
110 } else {
111 sh "git clone --recursive $model_url -b ${MCP_VERSION} model"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200112 // remove .git file with absolute path and re-init the file with relative path
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300113 sh "rm model/classes/system/.git"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200114 sh "cd model && git submodule update"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300115 }
116 }
117
Dmitry Tyzhnenkofcb4dab2019-03-05 20:11:59 +0200118 stage("Prepare arguments for generation config drive") {
119
120 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
121 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
122 sh "chmod +x ${config_drive_script_path}"
123
124 //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
125 args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/"
126 try {
127 sh "test -f model/encryption-key.asc"
128 args = "${args} --gpg-key model/encryption-key.asc ${iso_name}"
129
130 } catch (e) {
131 args = "${args} ${iso_name}"
132 }
133 }
134
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300135 stage("Set data"){
136 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200137 // Replace only if the variable is non-empty, leave default otherwise
138 if (i[1]) {
139 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
140 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300141 }
142 }
143
144 stage("Create user_data2"){
145 //http://jen20.com/2015/10/04/cloudconfig-merging.html
146 //TODO(ddmitriev): allow to read such file from
147 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
148 def user_data2 = """\
149#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
150
151#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
152# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
153# content: |
154# GRUB_RECORDFAIL_TIMEOUT=30
155# GRUB_TIMEOUT=10
156# GRUB_TIMEOUT_STYLE=menu
157#
158# - path: /root/interfaces
159# content: |
160# auto lo
161# iface lo inet loopback
162#
163# auto ens3
164# iface ens3 inet dhcp
165#
166# - path: /root/.ssh/config
167# owner: root:root
168# permissions: '0600'
169# content: |
170# Host *
171# ServerAliveInterval 60
172# ServerAliveCountMax 0
173# StrictHostKeyChecking no
174# UserKnownHostsFile /dev/null
175#
176# - path: /etc/cloud/master_environment_override
177# owner: root:root
178# permissions: '0600'
179# content: |
180# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
181# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
182# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
183# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
184# export DNS_SERVERS="${DNS_SERVERS}"
185# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
186# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
187# export MCP_VERSION="${MCP_VERSION}"
188# export LOCAL_REPOS="true"
189# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
190# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
Tatyana Leontovich4718bdf2019-05-24 12:37:06 +0300191# export MCP_SALT_REPO_UPDATES="${MCP_SALT_REPO_UPDATES}"
192# export ENABLE_MCP_SALT_REPO_UPDATES="true"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300193
194output:
195 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
196
197ssh_pwauth: True
198users:
199 - name: root
200 sudo: ALL=(ALL) NOPASSWD:ALL
201 shell: /bin/bash
202
203disable_root: false
204chpasswd:
205 list: |
206 root:r00tme
207 expire: False
208
209bootcmd:
210 # Block access to SSH while node is preparing
211 - cloud-init-per once sudo touch /is_cloud_init_started
212 # Enable root access
213 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
214 - service sshd restart
215
216merge_how: "dict(recurse_array)+list(append)"
217"""
218 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
219 }
220
221 stage("Create config-drive"){
222 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200223 // apt package genisoimage is required for this stage
224 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300225 }
226
227 stage("Save artifacts") {
228 archiveArtifacts allowEmptyArchive: false,
229 artifacts: "${iso_name}"
230 }
231
232 stage("Download config drive to slave") {
233 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
Dennis Dmitrieve3884652019-03-05 14:26:44 +0200234 println "Remove previous config drive ISO"
235 sh("""\
236 rm /home/jenkins/images/${iso_name} || true
237 """)
238
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300239 def b_res = build job: 'download-config-drive',
240 parameters: [
241 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
242 string(name: 'NODE_NAME', value: "${NODE_NAME}")
243 ]
244 } else {
245 echo "Drive only generated. But didn't download"
246 }
247 }
248 }
249}