blob: 0aff603286a52a5b894d82e377e1c402128312e3 [file] [log] [blame]
Richard Felkl2e9e5452017-10-16 09:52:10 +02001/**
2 *
Richard Felkl859f4dd2018-01-04 23:03:27 +01003 * Build mirror image pipeline
Richard Felkl2e9e5452017-10-16 09:52:10 +02004 *
5 * Expected parameters:
Richard Felkl859f4dd2018-01-04 23:03:27 +01006 * CLUSTER_MODEL - An URL to the Reclass model for the mirror VM.
Richard Felkl0a9b5f62018-01-16 10:57:31 +01007 * CLUSTER_MODEL_REF - Branch or tag to use for cluster model
Richard Felkl859f4dd2018-01-04 23:03:27 +01008 * 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 Felkl2e9e5452017-10-16 09:52:10 +020028 */
29
30// Load shared libs
31def salt = new com.mirantis.mk.Salt()
32def common = new com.mirantis.mk.Common()
33def python = new com.mirantis.mk.Python()
34def openstack = new com.mirantis.mk.Openstack()
Richard Felkl2e9e5452017-10-16 09:52:10 +020035def date = new Date()
36def dateTime = date.format("ddMMyyyy-HHmmss")
Jakub Josef88aaf832018-01-18 16:18:28 +010037def venvPepper = ""
Richard Felkl2e9e5452017-10-16 09:52:10 +020038def privateKey = ""
39def floatingIP = ""
40def openstackServer = ""
41def rcFile = ""
42def openstackEnv = ""
43def serverStatus = ""
Richard Felkl802e4462017-12-06 10:08:05 +010044def uploadImageStatus = ""
45def uploadMd5Status = ""
Richard Felkl2e9e5452017-10-16 09:52:10 +020046
Richard Felkl994887c2018-01-11 17:13:59 +010047timeout(time: 12, unit: 'HOURS') {
48 node("python&&disk-xl") {
49 try {
50 def workspace = common.getWorkspace()
Richard Felkl994887c2018-01-11 17:13:59 +010051 openstackEnv = String.format("%s/venv", workspace)
Jakub Josef88aaf832018-01-18 16:18:28 +010052 venvPepper = String.format("%s/venvPepper", workspace)
Richard Felkl2e9e5452017-10-16 09:52:10 +020053
Richard Felkl994887c2018-01-11 17:13:59 +010054 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 Felkl2e9e5452017-10-16 09:52:10 +020058
Richard Felkl994887c2018-01-11 17:13:59 +010059 stage("Get templates"){
Richard Felkl2e9e5452017-10-16 09:52:10 +020060
Richard Felkl994887c2018-01-11 17:13:59 +010061 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 Felkl22b41612018-01-17 09:00:32 +010066 openstack.setupOpenstackVirtualenv(openstackEnv, OS_VERSION)
Richard Felkl2e9e5452017-10-16 09:52:10 +020067 }
68
Richard Felkl994887c2018-01-11 17:13:59 +010069 stage("Spawn Instance"){
Richard Felkl22b41612018-01-17 09:00:32 +010070 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
Richard Felkl994887c2018-01-11 17:13:59 +010071 privateKey = openstack.runOpenstackCommand("openstack keypair create mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +020072
Richard Felkl994887c2018-01-11 17:13:59 +010073 common.infoMsg(privateKey)
74 sh "echo '${privateKey}' > id_rsa;chmod 600 id_rsa"
Richard Felkl2e9e5452017-10-16 09:52:10 +020075
Richard Felkl994887c2018-01-11 17:13:59 +010076 floatingIP = openstack.runOpenstackCommand("openstack ip floating create --format value -c floating_ip_address ${VM_FLOATING_IP_POOL}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +020077
Richard Felkl0a9b5f62018-01-16 10:57:31 +010078 withEnv(["CLUSTER_NAME=${CLUSTER_NAME}", "CLUSTER_MODEL=${CLUSTER_MODEL}", "CLUSTER_MODEL_REF=${CLUSTER_MODEL_REF}", "MCP_VERSION=${MCP_VERSION}"]) {
Richard Felkl994887c2018-01-11 17:13:59 +010079 sh "envsubst < salt-bootstrap.sh > salt-bootstrap.sh.temp;mv salt-bootstrap.sh.temp salt-bootstrap.sh; cat salt-bootstrap.sh"
80 }
Richard Felkl2e9e5452017-10-16 09:52:10 +020081
Richard Felkl22b41612018-01-17 09:00:32 +010082 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 Felkl994887c2018-01-11 17:13:59 +010087 sleep(60)
88
Richard Felkl22b41612018-01-17 09:00:32 +010089 common.retry(VM_IP_RETRIES, VM_IP_DELAY){
Richard Felkl994887c2018-01-11 17:13:59 +010090 openstack.runOpenstackCommand("openstack ip floating add ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
91 }
92
93 sleep(500)
94
Richard Felkl22b41612018-01-17 09:00:32 +010095 common.retry(VM_CONNECT_RETRIES, VM_CONNECT_DELAY){
Richard Felkl994887c2018-01-11 17:13:59 +010096 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 Felkl22b41612018-01-17 09:00:32 +0100101
Richard Felkl994887c2018-01-11 17:13:59 +0100102 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 Felkl2e9e5452017-10-16 09:52:10 +0200108 }
109
Richard Felkl994887c2018-01-11 17:13:59 +0100110 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 Felkl2e9e5452017-10-16 09:52:10 +0200118 }
119
Richard Felkl994887c2018-01-11 17:13:59 +0100120 stage("Create Aptly"){
121 common.infoMsg("Creating Aptly")
122 salt.enforceState(venvPepper, '*apt*', ['aptly'], true, false, null, false, -1, 2)
Richard Felkl22b41612018-01-17 09:00:32 +0100123 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 Felkl994887c2018-01-11 17:13:59 +0100125 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 Felkl2e9e5452017-10-16 09:52:10 +0200127 }
128
Richard Felkl994887c2018-01-11 17:13:59 +0100129 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 Felkl7a006072017-12-13 09:47:07 +0100134 }
Richard Felkl14a4c6f2017-11-29 09:10:10 +0100135
Richard Felkl994887c2018-01-11 17:13:59 +0100136 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 Felkl22b41612018-01-17 09:00:32 +0100160 common.retry(3, 5){
Richard Felkl994887c2018-01-11 17:13:59 +0100161 openstack.runOpenstackCommand("openstack server stop mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
162 }
163
Richard Felkl22b41612018-01-17 09:00:32 +0100164 common.retry(6, 30){
Richard Felkl994887c2018-01-11 17:13:59 +0100165 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 Felkl22b41612018-01-17 09:00:32 +0100170 common.retry(3, 5){
Richard Felkl994887c2018-01-11 17:13:59 +0100171 openstack.runOpenstackCommand("openstack server image create --name ${IMAGE_NAME}-${dateTime} --wait mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +0200172 }
173 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200174
Richard Felkl994887c2018-01-11 17:13:59 +0100175 stage("Publish image"){
176 common.infoMsg("Saving image ${IMAGE_NAME}-${dateTime}")
Richard Felkl22b41612018-01-17 09:00:32 +0100177 common.retry(3, 5){
Richard Felkl994887c2018-01-11 17:13:59 +0100178 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime}.qcow2 ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
Richard Felkl802e4462017-12-06 10:08:05 +0100179 }
Richard Felkl994887c2018-01-11 17:13:59 +0100180 sh "md5sum ${IMAGE_NAME}-${dateTime}.qcow2 > ${IMAGE_NAME}-${dateTime}.qcow2.md5"
Richard Felkl2e9e5452017-10-16 09:52:10 +0200181
Richard Felkl994887c2018-01-11 17:13:59 +0100182 common.infoMsg("Uploading image ${IMAGE_NAME}-${dateTime}")
Richard Felkl22b41612018-01-17 09:00:32 +0100183 common.retry(3, 5){
Richard Felkl994887c2018-01-11 17:13:59 +0100184 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 Felkl22b41612018-01-17 09:00:32 +0100189
190 common.retry(3, 5){
Richard Felkl994887c2018-01-11 17:13:59 +0100191 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 Felkl2e9e5452017-10-16 09:52:10 +0200197 }
Richard Felkl994887c2018-01-11 17:13:59 +0100198
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 Felkl2e9e5452017-10-16 09:52:10 +0200213 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200214 }
215 }
Jakub Josef88aaf832018-01-18 16:18:28 +0100216}