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