blob: 95990c6ff12a2ab4d2f245fde313b255fd593b40 [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}"
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +01009def ssh = new com.mirantis.mk.Ssh()
Dennis Dmitrievf220d972018-10-10 15:19:14 +030010
11def smc = [:]
12smc['SALT_MASTER_MINION_ID'] = "cfg01.${CLUSTER_NAME}.local"
13smc['SALT_MASTER_DEPLOY_IP'] = "${SALT_MASTER_DEPLOY_IP}"
14smc['DEPLOY_NETWORK_GW'] = "${DEPLOY_NETWORK_GW}"
15smc['DEPLOY_NETWORK_NETMASK'] = "${DEPLOY_NETWORK_NETMASK}"
Dennis Dmitriev18319fa2019-03-19 22:39:02 +020016smc['DEPLOY_NETWORK_MTU'] = "${DEPLOY_NETWORK_MTU}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +030017smc['DNS_SERVERS'] = "${DNS_SERVERS}"
18smc['PIPELINES_FROM_ISO'] = '${PIPELINES_FROM_ISO}'
19smc['PIPELINE_REPO_URL'] = '${PIPELINE_REPO_URL}'
20smc['MCP_VERSION'] = "${MCP_VERSION}"
21// smc['LOCAL_REPOS'] = 'true'
22smc['MCP_SALT_REPO_KEY'] = "${MCP_SALT_REPO_KEY}"
23smc['MCP_SALT_REPO_URL'] = "${MCP_SALT_REPO_URL}"
Tatyana Leontovich4718bdf2019-05-24 12:37:06 +030024smc['MCP_SALT_REPO_UPDATES'] = "${MCP_SALT_REPO_UPDATES}"
25
Dennis Dmitrievf220d972018-10-10 15:19:14 +030026
27def entries(m) {
28 m.collect {k, v -> [k, v]}
29}
30
Dennis Dmitriev1f08b0c2019-05-27 17:03:53 +030031timeout(time: 2, unit: 'HOURS') {
Dennis Dmitrievf220d972018-10-10 15:19:14 +030032node (node_name) {
33
34 timestamps(){
35
36 stage("Clean Workspace") {
37 step([$class: 'WsCleanup'])
38 }
39
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020040 stage("Get mk-pipelines, pipeline-library and mcp-common-scripts repositories") {
41 def cloned = true
42 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
43 keyFileVariable: "GERRIT_KEY",
44 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
45 usernameVariable: "GERRIT_USERNAME",
46 passwordVariable: "GERRIT_PASSWORD"]]) {
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010047 def gerrit_user = env.GERRIT_USERNAME
48 def gerrit_password = env.GERRIT_PASSWORD
Hanna Arhipova33f15752020-12-07 13:44:40 +020049 String gerrit_host = "gerrit.mcp.mirantis.com"
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010050 String gerrit_port = "29418"
51 ssh.prepareSshAgentKey(env.GERRIT_MCP_CREDENTIALS_ID)
Dennis Dmitrievf220d972018-10-10 15:19:14 +030052
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010053 ssh.agentSh(
54 "set -ex; " +
55 "git clone ssh://${gerrit_user}@${gerrit_host}:${gerrit_port}/mcp/mcp-common-scripts mcp-common-scripts-git; " +
56 "git clone --mirror ssh://${gerrit_user}@${gerrit_host}:${gerrit_port}/mk/mk-pipelines mk-pipelines; " +
57 "git clone --mirror ssh://${gerrit_user}@${gerrit_host}:${gerrit_port}/mcp-ci/pipeline-library pipeline-library")
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020058
Dennis Dmitrieve3884652019-03-05 14:26:44 +020059 if (COMMON_SCRIPTS_COMMIT != '') {
60 sh ("""\
61 set -ex
62 cd mcp-common-scripts-git
63 git checkout ${COMMON_SCRIPTS_COMMIT}
64 git log -1
65 """)
66 }
67
68 if (MCP_COMMON_SCRIPTS_REFS != '') {
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010069 ssh.agentSh(
70 "set -ex; " +
71 "cd mcp-common-scripts-git; " +
72 "git fetch https://${gerrit_host}/mcp/mcp-common-scripts ${MCP_COMMON_SCRIPTS_REFS} && git checkout FETCH_HEAD; " +
73 "git log -1")
Dennis Dmitrieve3884652019-03-05 14:26:44 +020074 }
75
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020076 if (PIPELINE_LIBRARY_REF != '') {
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010077 ssh.agentSh(
78 "set -ex; " +
79 "cd pipeline-library; " +
80 "git fetch https://${gerrit_host}/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF}; " +
81 "git tag ${MCP_VERSION} FETCH_HEAD -f; " +
82 "git branch -f release/${MCP_VERSION} FETCH_HEAD; " +
83 "git log -1")
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020084 }
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020085 if (MK_PIPELINES_REF != '') {
Dmitriy Kruglov7844a6c2019-11-21 13:11:17 +010086 ssh.agentSh(
87 "set -ex; " +
88 "cd mk-pipelines; " +
89 "git fetch https://${gerrit_host}/mk/mk-pipelines ${MK_PIPELINES_REF}; " +
90 "git tag ${MCP_VERSION} FETCH_HEAD -f; " +
91 "git branch -f release/${MCP_VERSION} FETCH_HEAD; " +
92 "git log -1")
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020093 }
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020094 }
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020095 }
96
Dennis Dmitrievf220d972018-10-10 15:19:14 +030097 stage("Get cluster model") {
98 def model_url = "${MODEL_URL}"
99 sh "rm -rf model"
100 if (MODEL_URL_OBJECT_TYPE == 'tar.gz') {
101 sh "wget -O model.tar.gz '${model_url}'"
102 sh "mkdir model && cd model && tar zxfv ../model.tar.gz"
103 } else {
104 sh "git clone --recursive $model_url -b ${MCP_VERSION} model"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200105 // remove .git file with absolute path and re-init the file with relative path
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300106 sh "rm model/classes/system/.git"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200107 sh "cd model && git submodule update"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300108 }
109 }
110
Dmitry Tyzhnenkofcb4dab2019-03-05 20:11:59 +0200111 stage("Prepare arguments for generation config drive") {
112
113 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
114 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
115 sh "chmod +x ${config_drive_script_path}"
116
117 //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
118 args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/"
119 try {
120 sh "test -f model/encryption-key.asc"
121 args = "${args} --gpg-key model/encryption-key.asc ${iso_name}"
122
123 } catch (e) {
124 args = "${args} ${iso_name}"
125 }
126 }
127
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300128 stage("Set data"){
129 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200130 // Replace only if the variable is non-empty, leave default otherwise
131 if (i[1]) {
132 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
133 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300134 }
135 }
136
137 stage("Create user_data2"){
138 //http://jen20.com/2015/10/04/cloudconfig-merging.html
139 //TODO(ddmitriev): allow to read such file from
140 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
141 def user_data2 = """\
142#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
143
144#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
145# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
146# content: |
147# GRUB_RECORDFAIL_TIMEOUT=30
148# GRUB_TIMEOUT=10
149# GRUB_TIMEOUT_STYLE=menu
150#
151# - path: /root/interfaces
152# content: |
153# auto lo
154# iface lo inet loopback
155#
156# auto ens3
157# iface ens3 inet dhcp
158#
159# - path: /root/.ssh/config
160# owner: root:root
161# permissions: '0600'
162# content: |
163# Host *
164# ServerAliveInterval 60
165# ServerAliveCountMax 0
166# StrictHostKeyChecking no
167# UserKnownHostsFile /dev/null
168#
169# - path: /etc/cloud/master_environment_override
170# owner: root:root
171# permissions: '0600'
172# content: |
173# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
174# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
175# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
176# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
177# export DNS_SERVERS="${DNS_SERVERS}"
178# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
179# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
180# export MCP_VERSION="${MCP_VERSION}"
181# export LOCAL_REPOS="true"
182# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
183# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
Tatyana Leontovich4718bdf2019-05-24 12:37:06 +0300184# export MCP_SALT_REPO_UPDATES="${MCP_SALT_REPO_UPDATES}"
185# export ENABLE_MCP_SALT_REPO_UPDATES="true"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300186
187output:
188 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
189
190ssh_pwauth: True
191users:
192 - name: root
193 sudo: ALL=(ALL) NOPASSWD:ALL
194 shell: /bin/bash
195
196disable_root: false
197chpasswd:
198 list: |
199 root:r00tme
200 expire: False
201
202bootcmd:
203 # Block access to SSH while node is preparing
204 - cloud-init-per once sudo touch /is_cloud_init_started
205 # Enable root access
206 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
207 - service sshd restart
208
209merge_how: "dict(recurse_array)+list(append)"
210"""
211 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
212 }
213
214 stage("Create config-drive"){
215 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200216 // apt package genisoimage is required for this stage
217 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300218 }
219
220 stage("Save artifacts") {
221 archiveArtifacts allowEmptyArchive: false,
222 artifacts: "${iso_name}"
223 }
224
225 stage("Download config drive to slave") {
226 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
Dennis Dmitrieve3884652019-03-05 14:26:44 +0200227 println "Remove previous config drive ISO"
228 sh("""\
229 rm /home/jenkins/images/${iso_name} || true
230 """)
231
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300232 def b_res = build job: 'download-config-drive',
233 parameters: [
234 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
235 string(name: 'NODE_NAME', value: "${NODE_NAME}")
236 ]
237 } else {
238 echo "Drive only generated. But didn't download"
239 }
240 }
241 }
242}
Dennis Dmitriev1f08b0c2019-05-27 17:03:53 +0300243} // timeout