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