blob: f3638699230a0e907f43756403f325a3919a5bf7 [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 Dmitrieve98328f2019-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 Dmitrieve98328f2019-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 Dmitrieve98328f2019-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 Dmitrieve98328f2019-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"
108 // remove .git file with hardcoded path
109 sh "rm model/classes/system/.git"
110 }
111 }
112
Dmitry Tyzhnenko23bb1402019-03-05 20:11:59 +0200113 stage("Prepare arguments for generation config drive") {
114
115 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
116 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
117 sh "chmod +x ${config_drive_script_path}"
118
119 //args = "--user-data user_data --vendor-data user_data2 --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/ ${iso_name}"
120 args = "--user-data user_data2 --vendor-data ${user_data_script_path} --hostname cfg01 --model model --mk-pipelines mk-pipelines/ --pipeline-library pipeline-library/"
121 try {
122 sh "test -f model/encryption-key.asc"
123 args = "${args} --gpg-key model/encryption-key.asc ${iso_name}"
124
125 } catch (e) {
126 args = "${args} ${iso_name}"
127 }
128 }
129
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300130 stage("Set data"){
131 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200132 // Replace only if the variable is non-empty, leave default otherwise
133 if (i[1]) {
134 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
135 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300136 }
137 }
138
139 stage("Create user_data2"){
140 //http://jen20.com/2015/10/04/cloudconfig-merging.html
141 //TODO(ddmitriev): allow to read such file from
142 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
143 def user_data2 = """\
144#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
145
146#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
147# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
148# content: |
149# GRUB_RECORDFAIL_TIMEOUT=30
150# GRUB_TIMEOUT=10
151# GRUB_TIMEOUT_STYLE=menu
152#
153# - path: /root/interfaces
154# content: |
155# auto lo
156# iface lo inet loopback
157#
158# auto ens3
159# iface ens3 inet dhcp
160#
161# - path: /root/.ssh/config
162# owner: root:root
163# permissions: '0600'
164# content: |
165# Host *
166# ServerAliveInterval 60
167# ServerAliveCountMax 0
168# StrictHostKeyChecking no
169# UserKnownHostsFile /dev/null
170#
171# - path: /etc/cloud/master_environment_override
172# owner: root:root
173# permissions: '0600'
174# content: |
175# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
176# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
177# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
178# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
179# export DNS_SERVERS="${DNS_SERVERS}"
180# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
181# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
182# export MCP_VERSION="${MCP_VERSION}"
183# export LOCAL_REPOS="true"
184# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
185# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
186
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 Dmitrieve98328f2019-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}