blob: a734235f8e27cde0fb9fdc9e3df9127fb7727a62 [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 Dmitriev87c22d72018-11-09 17:18:00 +020043 dir("mcp-common-scripts-git") {
44 cloned = gerrit.gerritPatchsetCheckout([
45 credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
46 gerritBranch: "${MCP_VERSION}",
47 gerritRefSpec: "${MCP_COMMON_SCRIPTS_REFS}",
48 gerritScheme: "ssh",
49 gerritName: "${GERRIT_USERNAME}",
50 gerritHost: "gerrit.mcp.mirantis.net",
51 gerritPort: "29418",
52 gerritProject: "mcp/mcp-common-scripts"
53 ])
54 }
55 if (!cloned) {
56 error("Failed to clone the repository mcp/mcp-common-scripts")
57 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +030058
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020059 sh ("""\
60 set -ex
61 eval \$(ssh-agent)
62 ssh-add ${GERRIT_KEY}
63 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mk/mk-pipelines mk-pipelines
64 git clone --mirror ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp-ci/pipeline-library pipeline-library
65 """)
66
67 if (PIPELINE_LIBRARY_REF != '') {
68 sh ("""\
69 set -ex
70 eval \$(ssh-agent)
71 ssh-add ${GERRIT_KEY}
72 cd pipeline-library
73 git fetch https://gerrit.mcp.mirantis.net/mcp-ci/pipeline-library ${PIPELINE_LIBRARY_REF}
74 git tag ${MCP_VERSION} FETCH_HEAD -f
75 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020076 }
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020077 if (MK_PIPELINES_REF != '') {
78 sh ("""\
79 set -ex
80 eval \$(ssh-agent)
81 ssh-add ${GERRIT_KEY}
82 cd mk-pipelines
83 git fetch https://gerrit.mcp.mirantis.net/mcp-ci/mk-pipelines ${MK_PIPELINES_REF}
84 git tag ${MCP_VERSION} FETCH_HEAD -f
85 """)
Dennis Dmitriev87c22d72018-11-09 17:18:00 +020086 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +030087
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +020088 // dir("mk-pipelines-git") {
89 // cloned = gerrit.gerritPatchsetCheckout([
90 // credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
91 // gerritRefSpec: "${MK_PIPELINES_REF}",
92 // gerritScheme: "ssh",
93 // gerritName: "${GERRIT_USERNAME}",
94 // gerritHost: "gerrit.mcp.mirantis.net",
95 // gerritPort: "29418",
96 // gerritProject: "mk/mk-pipelines"
97 // ])
98 // }
99 // if (!cloned) {
100 // error("Failed to clone the repository mk/mk-pipelines")
101 // }
102
103 // dir("pipeline-library-git") {
104 // cloned = gerrit.gerritPatchsetCheckout([
105 // credentialsId : "${GERRIT_MCP_CREDENTIALS_ID}",
106 // gerritRefSpec: "${PIPELINE_LIBRARY_REF}",
107 // gerritScheme: "ssh",
108 // gerritName: "${GERRIT_USERNAME}",
109 // gerritHost: "gerrit.mcp.mirantis.net",
110 // gerritPort: "29418",
111 // gerritProject: "mcp-ci/pipeline-library"
112 // ])
113 // }
114 // if (!cloned) {
115 // error("Failed to clone the repository mcp-ci/pipeline-library")
116 // }
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200117 }
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +0200118 //if (PIPELINE_LIBRARY_REF != '') {
119 // sh "cd pipeline-library; git tag ${MCP_VERSION} FETCH_HEAD -f ; cd .."
120 //}
121 //if (MK_PIPELINES_REF != '') {
122 // sh "cd mk-pipelines; git tag ${MCP_VERSION} FETCH_HEAD -f; cd .."
123 //}
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200124
125 // gerrit.gerritPatchsetCheckout() doesn't support clonning bare repository
Dennis Dmitrievc4a14ba2018-11-12 23:31:40 +0200126 // sh "git clone --mirror mk-pipelines-git mk-pipelines"
127 // sh "git clone --mirror pipeline-library-git pipeline-library"
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200128 }
129
130 stage("Prepare arguments for generation config drive") {
131
132 config_drive_script_path = "mcp-common-scripts-git/config-drive/create_config_drive.sh"
133 user_data_script_path = "mcp-common-scripts-git/config-drive/master_config.yaml"
134 sh "chmod +x ${config_drive_script_path}"
135
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300136 //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 +0200137 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 +0300138 }
139
140 stage("Get cluster model") {
141 def model_url = "${MODEL_URL}"
142 sh "rm -rf model"
143 if (MODEL_URL_OBJECT_TYPE == 'tar.gz') {
144 sh "wget -O model.tar.gz '${model_url}'"
145 sh "mkdir model && cd model && tar zxfv ../model.tar.gz"
146 } else {
147 sh "git clone --recursive $model_url -b ${MCP_VERSION} model"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200148 // remove .git file with absolute path and re-init the file with relative path
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300149 sh "rm model/classes/system/.git"
Dennis Dmitrieveaaafb22019-02-18 12:24:43 +0200150 sh "cd model && git submodule update"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300151 }
152 }
153
154 stage("Set data"){
155 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200156 // Replace only if the variable is non-empty, leave default otherwise
157 if (i[1]) {
158 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
159 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300160 }
161 }
162
163 stage("Create user_data2"){
164 //http://jen20.com/2015/10/04/cloudconfig-merging.html
165 //TODO(ddmitriev): allow to read such file from
166 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
167 def user_data2 = """\
168#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
169
170#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
171# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
172# content: |
173# GRUB_RECORDFAIL_TIMEOUT=30
174# GRUB_TIMEOUT=10
175# GRUB_TIMEOUT_STYLE=menu
176#
177# - path: /root/interfaces
178# content: |
179# auto lo
180# iface lo inet loopback
181#
182# auto ens3
183# iface ens3 inet dhcp
184#
185# - path: /root/.ssh/config
186# owner: root:root
187# permissions: '0600'
188# content: |
189# Host *
190# ServerAliveInterval 60
191# ServerAliveCountMax 0
192# StrictHostKeyChecking no
193# UserKnownHostsFile /dev/null
194#
195# - path: /etc/cloud/master_environment_override
196# owner: root:root
197# permissions: '0600'
198# content: |
199# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
200# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
201# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
202# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
203# export DNS_SERVERS="${DNS_SERVERS}"
204# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
205# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
206# export MCP_VERSION="${MCP_VERSION}"
207# export LOCAL_REPOS="true"
208# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
209# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
210
211output:
212 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
213
214ssh_pwauth: True
215users:
216 - name: root
217 sudo: ALL=(ALL) NOPASSWD:ALL
218 shell: /bin/bash
219
220disable_root: false
221chpasswd:
222 list: |
223 root:r00tme
224 expire: False
225
226bootcmd:
227 # Block access to SSH while node is preparing
228 - cloud-init-per once sudo touch /is_cloud_init_started
229 # Enable root access
230 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
231 - service sshd restart
232
233merge_how: "dict(recurse_array)+list(append)"
234"""
235 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
236 }
237
238 stage("Create config-drive"){
239 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200240 // apt package genisoimage is required for this stage
241 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300242 }
243
244 stage("Save artifacts") {
245 archiveArtifacts allowEmptyArchive: false,
246 artifacts: "${iso_name}"
247 }
248
249 stage("Download config drive to slave") {
250 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
251 def b_res = build job: 'download-config-drive',
252 parameters: [
253 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
254 string(name: 'NODE_NAME', value: "${NODE_NAME}")
255 ]
256 } else {
257 echo "Drive only generated. But didn't download"
258 }
259 }
260 }
261}