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