Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 1 | import java.text.SimpleDateFormat |
| 2 | |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 3 | def gerrit = new com.mirantis.mk.Gerrit() |
Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 4 | def dateFormat = new SimpleDateFormat("yyyyMMddHHmm") |
| 5 | def date = new Date() |
| 6 | def common_scripts_commit = "${COMMON_SCRIPTS_COMMIT}" |
| 7 | def iso_name = "${CONFIG_DRIVE_ISO_NAME}" ?: "cfg01.${CLUSTER_NAME}-config-${dateFormat.format(date)}.iso" |
| 8 | def node_name = "${NODE_NAME}" |
| 9 | |
| 10 | def smc = [:] |
| 11 | smc['SALT_MASTER_MINION_ID'] = "cfg01.${CLUSTER_NAME}.local" |
| 12 | smc['SALT_MASTER_DEPLOY_IP'] = "${SALT_MASTER_DEPLOY_IP}" |
| 13 | smc['DEPLOY_NETWORK_GW'] = "${DEPLOY_NETWORK_GW}" |
| 14 | smc['DEPLOY_NETWORK_NETMASK'] = "${DEPLOY_NETWORK_NETMASK}" |
| 15 | smc['DNS_SERVERS'] = "${DNS_SERVERS}" |
| 16 | smc['PIPELINES_FROM_ISO'] = '${PIPELINES_FROM_ISO}' |
| 17 | smc['PIPELINE_REPO_URL'] = '${PIPELINE_REPO_URL}' |
| 18 | smc['MCP_VERSION'] = "${MCP_VERSION}" |
| 19 | // smc['LOCAL_REPOS'] = 'true' |
| 20 | smc['MCP_SALT_REPO_KEY'] = "${MCP_SALT_REPO_KEY}" |
| 21 | smc['MCP_SALT_REPO_URL'] = "${MCP_SALT_REPO_URL}" |
| 22 | |
| 23 | def entries(m) { |
| 24 | m.collect {k, v -> [k, v]} |
| 25 | } |
| 26 | |
| 27 | node (node_name) { |
| 28 | |
| 29 | timestamps(){ |
| 30 | |
| 31 | stage("Clean Workspace") { |
| 32 | step([$class: 'WsCleanup']) |
| 33 | } |
| 34 | |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 35 | 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 Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 42 | |
Dennis Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 43 | sh ("""\ |
| 44 | set -ex |
| 45 | eval \$(ssh-agent) |
| 46 | ssh-add ${GERRIT_KEY} |
Dennis Dmitriev | e98328f | 2019-03-05 14:26:44 +0200 | [diff] [blame] | 47 | git clone ssh://${GERRIT_USERNAME}@gerrit.mcp.mirantis.net:29418/mcp/mcp-common-scripts mcp-common-scripts-git |
Dennis Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 48 | 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 Dmitriev | e98328f | 2019-03-05 14:26:44 +0200 | [diff] [blame] | 52 | 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 Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 72 | 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 Dmitriev | e98328f | 2019-03-05 14:26:44 +0200 | [diff] [blame] | 80 | git branch -f release/${MCP_VERSION} FETCH_HEAD |
| 81 | git log -1 |
Dennis Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 82 | """) |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 83 | } |
Dennis Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 84 | 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 Dmitriev | e98328f | 2019-03-05 14:26:44 +0200 | [diff] [blame] | 92 | git branch -f release/${MCP_VERSION} FETCH_HEAD |
| 93 | git log -1 |
Dennis Dmitriev | c4a14ba | 2018-11-12 23:31:40 +0200 | [diff] [blame] | 94 | """) |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 95 | } |
Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 96 | |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 97 | } |
Dennis Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 100 | 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 Tyzhnenko | 23bb140 | 2019-03-05 20:11:59 +0200 | [diff] [blame] | 113 | 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 Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 130 | stage("Set data"){ |
| 131 | for (i in entries(smc)) { |
Dennis Dmitriev | d9168b5 | 2018-12-19 18:53:52 +0200 | [diff] [blame] | 132 | // 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 Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 136 | } |
| 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 | |
| 187 | output: |
| 188 | all: '| tee -a /var/log/cloud-init-output.log /dev/tty0' |
| 189 | |
| 190 | ssh_pwauth: True |
| 191 | users: |
| 192 | - name: root |
| 193 | sudo: ALL=(ALL) NOPASSWD:ALL |
| 194 | shell: /bin/bash |
| 195 | |
| 196 | disable_root: false |
| 197 | chpasswd: |
| 198 | list: | |
| 199 | root:r00tme |
| 200 | expire: False |
| 201 | |
| 202 | bootcmd: |
| 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 | |
| 209 | merge_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 Dmitriev | 87c22d7 | 2018-11-09 17:18:00 +0200 | [diff] [blame] | 216 | // apt package genisoimage is required for this stage |
| 217 | sh "./${config_drive_script_path} ${args}" |
Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 218 | } |
| 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 Dmitriev | e98328f | 2019-03-05 14:26:44 +0200 | [diff] [blame] | 227 | println "Remove previous config drive ISO" |
| 228 | sh("""\ |
| 229 | rm /home/jenkins/images/${iso_name} || true |
| 230 | """) |
| 231 | |
Dennis Dmitriev | f220d97 | 2018-10-10 15:19:14 +0300 | [diff] [blame] | 232 | 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 | } |