blob: 46d50108a9cc5ce73c2f9e5a7f857eba444d6b2d [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"
148 // remove .git file with hardcoded path
149 sh "rm model/classes/system/.git"
150 }
151 }
152
153 stage("Set data"){
154 for (i in entries(smc)) {
Dennis Dmitrievd9168b52018-12-19 18:53:52 +0200155 // Replace only if the variable is non-empty, leave default otherwise
156 if (i[1]) {
157 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" ${user_data_script_path}"
158 }
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300159 }
160 }
161
162 stage("Create user_data2"){
163 //http://jen20.com/2015/10/04/cloudconfig-merging.html
164 //TODO(ddmitriev): allow to read such file from
165 // ./tcp_tests/templates/${LAB_CONFIG_NAME}/ directory for each lab
166 def user_data2 = """\
167#cloud-config, see http://cloudinit.readthedocs.io/en/latest/topics/examples.html
168
169#write_files: # write_files don't work as expected because overwrites this key from mcp-common-scripts YAML, losing data
170# - path: /etc/default/grub.d/97-enable-grub-menu.cfg
171# content: |
172# GRUB_RECORDFAIL_TIMEOUT=30
173# GRUB_TIMEOUT=10
174# GRUB_TIMEOUT_STYLE=menu
175#
176# - path: /root/interfaces
177# content: |
178# auto lo
179# iface lo inet loopback
180#
181# auto ens3
182# iface ens3 inet dhcp
183#
184# - path: /root/.ssh/config
185# owner: root:root
186# permissions: '0600'
187# content: |
188# Host *
189# ServerAliveInterval 60
190# ServerAliveCountMax 0
191# StrictHostKeyChecking no
192# UserKnownHostsFile /dev/null
193#
194# - path: /etc/cloud/master_environment_override
195# owner: root:root
196# permissions: '0600'
197# content: |
198# export SALT_MASTER_MINION_ID="cfg01.${CLUSTER_NAME}.local"
199# export SALT_MASTER_DEPLOY_IP="${SALT_MASTER_DEPLOY_IP}"
200# export DEPLOY_NETWORK_GW="${DEPLOY_NETWORK_GW}"
201# export DEPLOY_NETWORK_NETMASK="${DEPLOY_NETWORK_NETMASK}"
202# export DNS_SERVERS="${DNS_SERVERS}"
203# export PIPELINES_FROM_ISO="${PIPELINES_FROM_ISO}"
204# export PIPELINE_REPO_URL="${PIPELINE_REPO_URL}"
205# export MCP_VERSION="${MCP_VERSION}"
206# export LOCAL_REPOS="true"
207# export MCP_SALT_REPO_KEY="${MCP_SALT_REPO_KEY}"
208# export MCP_SALT_REPO_URL="${MCP_SALT_REPO_URL}"
209
210output:
211 all: '| tee -a /var/log/cloud-init-output.log /dev/tty0'
212
213ssh_pwauth: True
214users:
215 - name: root
216 sudo: ALL=(ALL) NOPASSWD:ALL
217 shell: /bin/bash
218
219disable_root: false
220chpasswd:
221 list: |
222 root:r00tme
223 expire: False
224
225bootcmd:
226 # Block access to SSH while node is preparing
227 - cloud-init-per once sudo touch /is_cloud_init_started
228 # Enable root access
229 - sed -i -e '/^PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
230 - service sshd restart
231
232merge_how: "dict(recurse_array)+list(append)"
233"""
234 writeFile(file: "user_data2", text: user_data2, encoding: "UTF-8")
235 }
236
237 stage("Create config-drive"){
238 // create cfg config-drive
Dennis Dmitriev87c22d72018-11-09 17:18:00 +0200239 // apt package genisoimage is required for this stage
240 sh "./${config_drive_script_path} ${args}"
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300241 }
242
243 stage("Save artifacts") {
244 archiveArtifacts allowEmptyArchive: false,
245 artifacts: "${iso_name}"
246 }
247
248 stage("Download config drive to slave") {
249 if (DOWNLOAD_CONFIG_DRIVE == 'true') {
250 def b_res = build job: 'download-config-drive',
251 parameters: [
252 string(name: 'IMAGE_URL', value: "${BUILD_URL}/artifact/${iso_name}"),
253 string(name: 'NODE_NAME', value: "${NODE_NAME}")
254 ]
255 } else {
256 echo "Drive only generated. But didn't download"
257 }
258 }
259 }
260}