Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 1 | /** |
| 2 | * |
Richard Felkl | 859f4dd | 2018-01-04 23:03:27 +0100 | [diff] [blame] | 3 | * Build mirror image pipeline |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 4 | * |
| 5 | * Expected parameters: |
Richard Felkl | 859f4dd | 2018-01-04 23:03:27 +0100 | [diff] [blame] | 6 | * CLUSTER_MODEL - An URL to the Reclass model for the mirror VM. |
Richard Felkl | 0a9b5f6 | 2018-01-16 10:57:31 +0100 | [diff] [blame] | 7 | * CLUSTER_MODEL_REF - Branch or tag to use for cluster model |
Richard Felkl | 859f4dd | 2018-01-04 23:03:27 +0100 | [diff] [blame] | 8 | * CLUSTER_NAME - Cluster name used in the above model. |
| 9 | * IMAGE_NAME - Name of the result image. |
| 10 | * OS_CREDENTIALS_ID - ID of credentials for OpenStack API stored in Jenkins. |
| 11 | * OS_PROJECT - Project in OpenStack under the VM will be spawned. |
| 12 | * OS_URL - Keystone auth endpoint of the OpenStack. |
| 13 | * OS_VERSION - OpenStack version |
| 14 | * SCRIPTS_REF - ref on the github to get the scripts from. |
| 15 | * SALT_MASTER_CREDENTIALS - ID of credentials to be used to connect to the Salt API of the VM |
| 16 | * UPLOAD_URL - URL of an WebDAV used to upload the image after creating. |
| 17 | * VM_AVAILABILITY_ZONE - Availability zone in OpenStack in the VM will be spawned. |
| 18 | * VM_CONNECT_RETRIES - Number of retries for SSH connection to the VM after it’s spawned after 8 minutes. |
| 19 | * VM_CONNECT_DELAY - Delay between connect retries above. |
| 20 | * VM_FLAVOR - Flavor to be used for VM in OpenStack. |
| 21 | * VM_FLOATING_IP_POOL - Floating IP pool to be used to assign floating IP to the VM. |
| 22 | * VM_IMAGE - Name of the image to be used for VM in OpenStack. |
| 23 | * VM_IP - Static IP that is assigned to the VM which belongs to the network used. |
| 24 | * VM_IP_RETRIES - Number of retries between tries to assign the floating IP to the VM. |
| 25 | * VM_IP_DELAY - Delay between floating IP assign retries above. |
| 26 | * VM_NETWORK_ID - ID of the network that VM connects to. |
| 27 | * |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | // Load shared libs |
| 31 | def salt = new com.mirantis.mk.Salt() |
| 32 | def common = new com.mirantis.mk.Common() |
| 33 | def python = new com.mirantis.mk.Python() |
| 34 | def openstack = new com.mirantis.mk.Openstack() |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 35 | def date = new Date() |
| 36 | def dateTime = date.format("ddMMyyyy-HHmmss") |
Jakub Josef | 88aaf83 | 2018-01-18 16:18:28 +0100 | [diff] [blame] | 37 | def venvPepper = "" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 38 | def privateKey = "" |
| 39 | def floatingIP = "" |
| 40 | def openstackServer = "" |
| 41 | def rcFile = "" |
| 42 | def openstackEnv = "" |
| 43 | def serverStatus = "" |
Richard Felkl | 802e446 | 2017-12-06 10:08:05 +0100 | [diff] [blame] | 44 | def uploadImageStatus = "" |
| 45 | def uploadMd5Status = "" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 46 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 47 | timeout(time: 12, unit: 'HOURS') { |
| 48 | node("python&&disk-xl") { |
| 49 | try { |
| 50 | def workspace = common.getWorkspace() |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 51 | openstackEnv = String.format("%s/venv", workspace) |
Jakub Josef | 88aaf83 | 2018-01-18 16:18:28 +0100 | [diff] [blame] | 52 | venvPepper = String.format("%s/venvPepper", workspace) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 53 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 54 | VM_IP_DELAY = VM_IP_DELAY as Integer |
| 55 | VM_IP_RETRIES = VM_IP_RETRIES as Integer |
| 56 | VM_CONNECT_DELAY = VM_CONNECT_DELAY as Integer |
| 57 | VM_CONNECT_RETRIES = VM_CONNECT_RETRIES as Integer |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 58 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 59 | stage("Get templates"){ |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 60 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 61 | if (!fileExists("${workspace}/tmp")) { |
| 62 | sh "mkdir -p ${workspace}/tmp" |
| 63 | } |
| 64 | |
| 65 | sh "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/salt-bootstrap.sh" |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 66 | openstack.setupOpenstackVirtualenv(openstackEnv, OS_VERSION) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 69 | stage("Spawn Instance"){ |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 70 | rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "") |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 71 | privateKey = openstack.runOpenstackCommand("openstack keypair create mcp-offline-keypair-${dateTime}", rcFile, openstackEnv) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 72 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 73 | common.infoMsg(privateKey) |
| 74 | sh "echo '${privateKey}' > id_rsa;chmod 600 id_rsa" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 75 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 76 | floatingIP = openstack.runOpenstackCommand("openstack ip floating create --format value -c floating_ip_address ${VM_FLOATING_IP_POOL}", rcFile, openstackEnv) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 77 | |
Richard Felkl | 0a9b5f6 | 2018-01-16 10:57:31 +0100 | [diff] [blame] | 78 | withEnv(["CLUSTER_NAME=${CLUSTER_NAME}", "CLUSTER_MODEL=${CLUSTER_MODEL}", "CLUSTER_MODEL_REF=${CLUSTER_MODEL_REF}", "MCP_VERSION=${MCP_VERSION}"]) { |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 79 | sh "envsubst < salt-bootstrap.sh > salt-bootstrap.sh.temp;mv salt-bootstrap.sh.temp salt-bootstrap.sh; cat salt-bootstrap.sh" |
| 80 | } |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 81 | |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 82 | if(VM_IP != ""){ |
| 83 | openstackServer = openstack.runOpenstackCommand("openstack server create --key-name mcp-offline-keypair-${dateTime} --availability-zone ${VM_AVAILABILITY_ZONE} --image ${VM_IMAGE} --flavor ${VM_FLAVOR} --nic net-id=${VM_NETWORK_ID},v4-fixed-ip=${VM_IP} --user-data salt-bootstrap.sh mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 84 | }else{ |
| 85 | openstackServer = openstack.runOpenstackCommand("openstack server create --key-name mcp-offline-keypair-${dateTime} --availability-zone ${VM_AVAILABILITY_ZONE} --image ${VM_IMAGE} --flavor ${VM_FLAVOR} --nic net-id=${VM_NETWORK_ID} --user-data salt-bootstrap.sh mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 86 | } |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 87 | sleep(60) |
| 88 | |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 89 | common.retry(VM_IP_RETRIES, VM_IP_DELAY){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 90 | openstack.runOpenstackCommand("openstack ip floating add ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 91 | } |
| 92 | |
| 93 | sleep(500) |
| 94 | |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 95 | common.retry(VM_CONNECT_RETRIES, VM_CONNECT_DELAY){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 96 | sh "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa root@${floatingIP}:/srv/initComplete ./" |
| 97 | } |
| 98 | |
| 99 | python.setupPepperVirtualenv(venvPepper, "http://${floatingIP}:6969", SALT_MASTER_CREDENTIALS) |
| 100 | } |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 101 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 102 | stage("Prepare instance"){ |
| 103 | salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.refresh_pillar', [], null, true) |
| 104 | salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.sync_all', [], null, true) |
| 105 | salt.enforceState(venvPepper, '*apt*', ['salt'], true, false, null, false, -1, 2) |
| 106 | salt.enforceState(venvPepper, '*apt*', ['linux'], true, false, null, false, -1, 2) |
| 107 | salt.enforceState(venvPepper, '*apt*', ['nginx'], true, false, null, false, -1, 2) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 110 | stage("Create Docker Registry"){ |
| 111 | common.infoMsg("Creating Docker Registry") |
| 112 | salt.enforceState(venvPepper, '*apt*', ["docker.host"], true, false, null, false, -1, 2) |
| 113 | salt.cmdRun(venvPepper, '*apt*', "docker run --restart always -d -p 5000:5000 --name registry registry:2") |
| 114 | salt.enforceState(venvPepper, '*apt*', ["docker.client.registry"], true, false, null, false, -1, 2) |
| 115 | salt.cmdRun(venvPepper, '*apt*', "docker system prune --all --force") |
| 116 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/config/interfaces -O /root/interfaces") |
| 117 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/config/minion.conf -O /root/minion.conf") |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 120 | stage("Create Aptly"){ |
| 121 | common.infoMsg("Creating Aptly") |
| 122 | salt.enforceState(venvPepper, '*apt*', ['aptly'], true, false, null, false, -1, 2) |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 123 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", "runas=aptly"], null, true) |
| 124 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-acrfv", "runas=aptly"], null, true) |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 125 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/aptly/aptly-update.sh -O /srv/scripts/aptly-update.sh") |
| 126 | salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/aptly-update.sh") |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 129 | stage("Create Debmirrors"){ |
| 130 | common.infoMsg("Creating Debmirrors") |
| 131 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/debmirror.sh -O /srv/scripts/debmirror.sh") |
| 132 | salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/debmirror.sh") |
| 133 | salt.cmdRun(venvPepper, '*apt*', "export HOME='/root';export MCP_VERSION='${MCP_VERSION}';/srv/scripts/debmirror.sh") |
Richard Felkl | 7a00607 | 2017-12-13 09:47:07 +0100 | [diff] [blame] | 134 | } |
Richard Felkl | 14a4c6f | 2017-11-29 09:10:10 +0100 | [diff] [blame] | 135 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 136 | stage("Create Git mirror"){ |
| 137 | common.infoMsg("Creating Git mirror") |
| 138 | salt.enforceState(venvPepper, '*apt*', ['git.server'], true, false, null, false, -1, 2) |
| 139 | } |
| 140 | |
| 141 | stage("Create PyPi mirror"){ |
| 142 | common.infoMsg("Creating PyPi mirror") |
| 143 | salt.cmdRun(venvPepper, '*apt*', "pip install pip2pi") |
| 144 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/pypi_mirror/requirements.txt -O /srv/pypi_mirror/requirements.txt") |
| 145 | salt.cmdRun(venvPepper, '*apt*', "pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt") |
| 146 | } |
| 147 | |
| 148 | stage("Create mirror of images"){ |
| 149 | common.infoMsg("Creating mirror of images") |
| 150 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/images_mirror/images.txt -O /srv/images.txt") |
| 151 | salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/images_mirror/update-images.sh -O /srv/scripts/update-images.sh") |
| 152 | salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/update-images.sh") |
| 153 | salt.cmdRun(venvPepper, '*apt*', "/srv/scripts/update-images.sh -u http://ci.mcp.mirantis.net:8085/images") |
| 154 | } |
| 155 | |
| 156 | stage("Create instance snapshot"){ |
| 157 | salt.cmdRun(venvPepper, '*apt*', "rm -rf /var/lib/cloud/sem/* /var/lib/cloud/instance /var/lib/cloud/instances/*") |
| 158 | salt.cmdRun(venvPepper, '*apt*', "cloud-init init") |
| 159 | |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 160 | common.retry(3, 5){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 161 | openstack.runOpenstackCommand("openstack server stop mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 162 | } |
| 163 | |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 164 | common.retry(6, 30){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 165 | serverStatus = openstack.runOpenstackCommand("openstack server show --format value -c status mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 166 | if(serverStatus != "SHUTOFF"){ |
| 167 | throw new ResourceException("Instance is not ready for image create.") |
| 168 | } |
| 169 | } |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 170 | common.retry(3, 5){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 171 | openstack.runOpenstackCommand("openstack server image create --name ${IMAGE_NAME}-${dateTime} --wait mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 172 | } |
| 173 | } |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 174 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 175 | stage("Publish image"){ |
| 176 | common.infoMsg("Saving image ${IMAGE_NAME}-${dateTime}") |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 177 | common.retry(3, 5){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 178 | openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime}.qcow2 ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv) |
Richard Felkl | 802e446 | 2017-12-06 10:08:05 +0100 | [diff] [blame] | 179 | } |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 180 | sh "md5sum ${IMAGE_NAME}-${dateTime}.qcow2 > ${IMAGE_NAME}-${dateTime}.qcow2.md5" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 181 | |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 182 | common.infoMsg("Uploading image ${IMAGE_NAME}-${dateTime}") |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 183 | common.retry(3, 5){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 184 | uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2 ${UPLOAD_URL}", returnStatus: true) |
| 185 | if(uploadImageStatus!=0){ |
| 186 | throw new Exception("Image upload failed") |
| 187 | } |
| 188 | } |
Richard Felkl | 22b4161 | 2018-01-17 09:00:32 +0100 | [diff] [blame] | 189 | |
| 190 | common.retry(3, 5){ |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 191 | uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true) |
| 192 | if(uploadMd5Status != 0){ |
| 193 | throw new Exception("MD5 sum upload failed") |
| 194 | } |
| 195 | } |
| 196 | currentBuild.description = "<a href='http://ci.mcp.mirantis.net:8085/images/${IMAGE_NAME}-${dateTime}.qcow2'>${IMAGE_NAME}-${dateTime}.qcow2</a>" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 197 | } |
Richard Felkl | 994887c | 2018-01-11 17:13:59 +0100 | [diff] [blame] | 198 | |
| 199 | } catch (Throwable e) { |
| 200 | // If there was an error or exception thrown, the build failed |
| 201 | currentBuild.result = "FAILURE" |
| 202 | throw e |
| 203 | } finally { |
| 204 | stage("Cleanup"){ |
| 205 | if(openstackServer != ""){ |
| 206 | openstack.runOpenstackCommand("openstack ip floating remove ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 207 | openstack.runOpenstackCommand("openstack server delete mcp-offline-mirror-${dateTime}", rcFile, openstackEnv) |
| 208 | } |
| 209 | if(privateKey != ""){ |
| 210 | openstack.runOpenstackCommand("openstack keypair delete mcp-offline-keypair-${dateTime}", rcFile, openstackEnv) |
| 211 | } |
| 212 | sh "rm -rf ./*" |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 213 | } |
Richard Felkl | 2e9e545 | 2017-10-16 09:52:10 +0200 | [diff] [blame] | 214 | } |
| 215 | } |
Jakub Josef | 88aaf83 | 2018-01-18 16:18:28 +0100 | [diff] [blame] | 216 | } |