blob: a3c31223caf56552c05f3243411646ce30361f4b [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")
37def venvPepper = "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
47def retry(int times = 5, int delay = 0, Closure body) {
48 int retries = 0
49 def exceptions = []
50 while(retries++ < times) {
51 try {
52 return body.call()
53 } catch(e) {
54 sleep(delay)
55 }
56 }
57 currentBuild.result = "FAILURE"
58 throw new Exception("Failed after $times retries")
59}
60
Richard Felkl994887c2018-01-11 17:13:59 +010061timeout(time: 12, unit: 'HOURS') {
62 node("python&&disk-xl") {
63 try {
64 def workspace = common.getWorkspace()
65 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
66 openstackEnv = String.format("%s/venv", workspace)
67 def openstackVersion = OS_VERSION
Richard Felkl2e9e5452017-10-16 09:52:10 +020068
Richard Felkl994887c2018-01-11 17:13:59 +010069 VM_IP_DELAY = VM_IP_DELAY as Integer
70 VM_IP_RETRIES = VM_IP_RETRIES as Integer
71 VM_CONNECT_DELAY = VM_CONNECT_DELAY as Integer
72 VM_CONNECT_RETRIES = VM_CONNECT_RETRIES as Integer
Richard Felkl2e9e5452017-10-16 09:52:10 +020073
Richard Felkl994887c2018-01-11 17:13:59 +010074 stage("Get templates"){
Richard Felkl2e9e5452017-10-16 09:52:10 +020075
Richard Felkl994887c2018-01-11 17:13:59 +010076 if (!fileExists("${workspace}/tmp")) {
77 sh "mkdir -p ${workspace}/tmp"
78 }
79
80 sh "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/salt-bootstrap.sh"
81 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
Richard Felkl2e9e5452017-10-16 09:52:10 +020082 }
83
Richard Felkl994887c2018-01-11 17:13:59 +010084 stage("Spawn Instance"){
85 privateKey = openstack.runOpenstackCommand("openstack keypair create mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +020086
Richard Felkl994887c2018-01-11 17:13:59 +010087 common.infoMsg(privateKey)
88 sh "echo '${privateKey}' > id_rsa;chmod 600 id_rsa"
Richard Felkl2e9e5452017-10-16 09:52:10 +020089
Richard Felkl994887c2018-01-11 17:13:59 +010090 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 +020091
Richard Felkl0a9b5f62018-01-16 10:57:31 +010092 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 +010093 sh "envsubst < salt-bootstrap.sh > salt-bootstrap.sh.temp;mv salt-bootstrap.sh.temp salt-bootstrap.sh; cat salt-bootstrap.sh"
94 }
Richard Felkl2e9e5452017-10-16 09:52:10 +020095
Richard Felkl994887c2018-01-11 17:13:59 +010096 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)
97 sleep(60)
98
99 retry(VM_IP_RETRIES, VM_IP_DELAY){
100 openstack.runOpenstackCommand("openstack ip floating add ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
101 }
102
103 sleep(500)
104
105 retry(VM_CONNECT_RETRIES, VM_CONNECT_DELAY){
106 sh "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa root@${floatingIP}:/srv/initComplete ./"
107 }
108
109 python.setupPepperVirtualenv(venvPepper, "http://${floatingIP}:6969", SALT_MASTER_CREDENTIALS)
110 }
111 stage("Prepare instance"){
112 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.refresh_pillar', [], null, true)
113 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.sync_all', [], null, true)
114 salt.enforceState(venvPepper, '*apt*', ['salt'], true, false, null, false, -1, 2)
115 salt.enforceState(venvPepper, '*apt*', ['linux'], true, false, null, false, -1, 2)
116 salt.enforceState(venvPepper, '*apt*', ['nginx'], true, false, null, false, -1, 2)
Richard Felkl2e9e5452017-10-16 09:52:10 +0200117 }
118
Richard Felkl994887c2018-01-11 17:13:59 +0100119 stage("Create Docker Registry"){
120 common.infoMsg("Creating Docker Registry")
121 salt.enforceState(venvPepper, '*apt*', ["docker.host"], true, false, null, false, -1, 2)
122 salt.cmdRun(venvPepper, '*apt*', "docker run --restart always -d -p 5000:5000 --name registry registry:2")
123 salt.enforceState(venvPepper, '*apt*', ["docker.client.registry"], true, false, null, false, -1, 2)
124 salt.cmdRun(venvPepper, '*apt*', "docker system prune --all --force")
125 salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/config/interfaces -O /root/interfaces")
126 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 +0200127 }
128
Richard Felkl994887c2018-01-11 17:13:59 +0100129 stage("Create Aptly"){
130 common.infoMsg("Creating Aptly")
131 salt.enforceState(venvPepper, '*apt*', ['aptly'], true, false, null, false, -1, 2)
132 //TODO: Do it new way
133 salt.cmdRun(venvPepper, '*apt*', "aptly_mirror_update.sh -s -v", true, null, true, ["runas=aptly"])
134 salt.cmdRun(venvPepper, '*apt*', "nohup aptly api serve --no-lock > /dev/null 2>&1 </dev/null &", true, null, true, ["runas=aptly"])
135 salt.cmdRun(venvPepper, '*apt*', "aptly-publisher --timeout=1200 publish -v -c /etc/aptly-publisher.yaml --architectures amd64 --url http://127.0.0.1:8080 --recreate --force-overwrite", true, null, true, ["runas=aptly"])
136 salt.cmdRun(venvPepper, '*apt*', "aptly db cleanup", true, null, true, ["runas=aptly"])
137 //NEW way
138 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", "runas=aptly"], null, true)
139 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-acrfv", "runas=aptly"], null, true)
140 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")
141 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/aptly-update.sh")
Richard Felkl2e9e5452017-10-16 09:52:10 +0200142 }
143
Richard Felkl994887c2018-01-11 17:13:59 +0100144 stage("Create Debmirrors"){
145 common.infoMsg("Creating Debmirrors")
146 salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/debmirror.sh -O /srv/scripts/debmirror.sh")
147 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/debmirror.sh")
148 salt.cmdRun(venvPepper, '*apt*', "export HOME='/root';export MCP_VERSION='${MCP_VERSION}';/srv/scripts/debmirror.sh")
Richard Felkl7a006072017-12-13 09:47:07 +0100149 }
Richard Felkl14a4c6f2017-11-29 09:10:10 +0100150
Richard Felkl994887c2018-01-11 17:13:59 +0100151 stage("Create Git mirror"){
152 common.infoMsg("Creating Git mirror")
153 salt.enforceState(venvPepper, '*apt*', ['git.server'], true, false, null, false, -1, 2)
154 }
155
156 stage("Create PyPi mirror"){
157 common.infoMsg("Creating PyPi mirror")
158 salt.cmdRun(venvPepper, '*apt*', "pip install pip2pi")
159 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")
160 salt.cmdRun(venvPepper, '*apt*', "pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt")
161 }
162
163 stage("Create mirror of images"){
164 common.infoMsg("Creating mirror of images")
165 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")
166 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")
167 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/update-images.sh")
168 salt.cmdRun(venvPepper, '*apt*', "/srv/scripts/update-images.sh -u http://ci.mcp.mirantis.net:8085/images")
169 }
170
171 stage("Create instance snapshot"){
172 salt.cmdRun(venvPepper, '*apt*', "rm -rf /var/lib/cloud/sem/* /var/lib/cloud/instance /var/lib/cloud/instances/*")
173 salt.cmdRun(venvPepper, '*apt*', "cloud-init init")
174
175 retry(3, 5){
176 openstack.runOpenstackCommand("openstack server stop mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
177 }
178
179 retry(6, 30){
180 serverStatus = openstack.runOpenstackCommand("openstack server show --format value -c status mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
181 if(serverStatus != "SHUTOFF"){
182 throw new ResourceException("Instance is not ready for image create.")
183 }
184 }
185 retry(3, 5){
186 openstack.runOpenstackCommand("openstack server image create --name ${IMAGE_NAME}-${dateTime} --wait mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +0200187 }
188 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200189
Richard Felkl994887c2018-01-11 17:13:59 +0100190 stage("Publish image"){
191 common.infoMsg("Saving image ${IMAGE_NAME}-${dateTime}")
192 retry(3, 5){
193 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime}.qcow2 ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
Richard Felkl802e4462017-12-06 10:08:05 +0100194 }
Richard Felkl994887c2018-01-11 17:13:59 +0100195 sh "md5sum ${IMAGE_NAME}-${dateTime}.qcow2 > ${IMAGE_NAME}-${dateTime}.qcow2.md5"
Richard Felkl2e9e5452017-10-16 09:52:10 +0200196
Richard Felkl994887c2018-01-11 17:13:59 +0100197 common.infoMsg("Uploading image ${IMAGE_NAME}-${dateTime}")
198 retry(3, 5){
199 uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2 ${UPLOAD_URL}", returnStatus: true)
200 if(uploadImageStatus!=0){
201 throw new Exception("Image upload failed")
202 }
203 }
204 retry(3, 5){
205 uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true)
206 if(uploadMd5Status != 0){
207 throw new Exception("MD5 sum upload failed")
208 }
209 }
210 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 +0200211 }
Richard Felkl994887c2018-01-11 17:13:59 +0100212
213 } catch (Throwable e) {
214 // If there was an error or exception thrown, the build failed
215 currentBuild.result = "FAILURE"
216 throw e
217 } finally {
218 stage("Cleanup"){
219 if(openstackServer != ""){
220 openstack.runOpenstackCommand("openstack ip floating remove ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
221 openstack.runOpenstackCommand("openstack server delete mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
222 }
223 if(privateKey != ""){
224 openstack.runOpenstackCommand("openstack keypair delete mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
225 }
226 sh "rm -rf ./*"
Richard Felkl2e9e5452017-10-16 09:52:10 +0200227 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200228 }
229 }
230}