blob: 03e72f1e00fee962b3e77f01bd3af8507af11003 [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
100 stage("Prepare arguments for generation config drive") {
101
102 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
103 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
104 sh "chmod +x ${config_drive_script_path}"
105
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300106 //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200107 args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300108 }
109
110 stage("Get cluster model") {
111 def model_url = "${MODEL_URL}"
112 sh "rm -rf model"
113 if (MODEL_URL_OBJECT_TYPE == 'tar.gz') {
114 sh "wget -O model.tar.gz '${model_url}'"
115 sh "mkdir model && cd model && tar zxfv ../model.tar.gz"
116 } else {
117 sh "git clone --recursive $model_url -b ${MCP_VERSION} model"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200118 // remove .git file with absolute path and re-init the file with relative path
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300119 sh "rm model/classes/system/.git"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200120 sh "cd model && git submodule update"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300121 }
122 }
123
124 stage("Set data"){
125 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200126 // Replace only if the variable is non-empty, leave default otherwise
127 if (i[1]) {
128 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
129 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300130 }
131 }
132
133 stage("Create user_data2"){
134 //http://jen20.com/2015/10/04/cloudconfig-merging.html
135 //TODO(ddmitriev): allow to read such file from
136 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
137 def user_data2 = """\
138#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
139
140#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
141# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
142# content: |
143# GRUB_RECORDFAIL_TIMEOUT=30
144# GRUB_TIMEOUT=10
145# GRUB_TIMEOUT_STYLE=menu
146#
147# - path: /root/interfaces
148# content: |
149# auto lo
150# iface lo inet loopback
151#
152# auto ens3
153# iface ens3 inet dhcp
154#
155# - path: /root/.ssh/config
156# owner: root:root
157# permissions: '0600'
158# content: |
159# Host *
160# ServerAliveInterval 60
161# ServerAliveCountMax 0
162# StrictHostKeyChecking no
163# UserKnownHostsFile /dev/null
164#
165# - path: /etc/cloud/master_environment_override
166# owner: root:root
167# permissions: '0600'
168# content: |
169# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
170# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
171# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
172# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
173# export DNS_SERVERS="${DNS_SERVERS}"
174# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
175# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
176# export MCP_VERSION="${MCP_VERSION}"
177# export LOCAL_REPOS="true"
178# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
179# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
180
181output:
182 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
183
184ssh_pwauth: True
185users:
186 - name: root
187 sudo: ALL=(ALL) NOPASSWD:ALL
188 shell: /bin/bash
189
190disable_root: false
191chpasswd:
192 list: |
193 root:r00tme
194 expire: False
195
196bootcmd:
197 # Block access to SSH while node is preparing
198 - cloud-init-per once sudo touch /is_cloud_init_started
199 # Enable root access
200 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
201 - service sshd restart
202
203merge_how: "dict(recurse_array)+list(append)"
204"""
205 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
206 }
207
208 stage("Create config-drive"){
209 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200210 // apt package genisoimage is required for this stage
211 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300212 }
213
214 stage("Save artifacts") {
215 archiveArtifacts allowEmptyArchive: false,
216 artifacts: "${iso_name}"
217 }
218
219 stage("Download config drive to slave") {
220 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
Dennis Dmitrieve3884652019-03-05 14:26:44 +0200221 println "Remove previous config drive ISO"
222 sh("""\
223 rm /home/jenkins/images/${iso_name} || true
224 """)
225
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300226 def b_res = build job: 'download-config-drive',
227 parameters: [
228 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
229 string(name: 'NODE_NAME', value: "${NODE_NAME}")
230 ]
231 } else {
232 echo "Drive only generated. But didn't download"
233 }
234 }
235 }
236}