blob: d9169b4c1cb93a88ca549df1fc4f5938e1ad411c [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.
7 * CLUSTER_NAME - Cluster name used in the above model.
8 * IMAGE_NAME - Name of the result image.
9 * OS_CREDENTIALS_ID - ID of credentials for OpenStack API stored in Jenkins.
10 * OS_PROJECT - Project in OpenStack under the VM will be spawned.
11 * OS_URL - Keystone auth endpoint of the OpenStack.
12 * OS_VERSION - OpenStack version
13 * SCRIPTS_REF - ref on the github to get the scripts from.
14 * SALT_MASTER_CREDENTIALS - ID of credentials to be used to connect to the Salt API of the VM
15 * UPLOAD_URL - URL of an WebDAV used to upload the image after creating.
16 * VM_AVAILABILITY_ZONE - Availability zone in OpenStack in the VM will be spawned.
17 * VM_CONNECT_RETRIES - Number of retries for SSH connection to the VM after it’s spawned after 8 minutes.
18 * VM_CONNECT_DELAY - Delay between connect retries above.
19 * VM_FLAVOR - Flavor to be used for VM in OpenStack.
20 * VM_FLOATING_IP_POOL - Floating IP pool to be used to assign floating IP to the VM.
21 * VM_IMAGE - Name of the image to be used for VM in OpenStack.
22 * VM_IP - Static IP that is assigned to the VM which belongs to the network used.
23 * VM_IP_RETRIES - Number of retries between tries to assign the floating IP to the VM.
24 * VM_IP_DELAY - Delay between floating IP assign retries above.
25 * VM_NETWORK_ID - ID of the network that VM connects to.
26 *
Richard Felkl2e9e5452017-10-16 09:52:10 +020027 */
28
29// Load shared libs
30def salt = new com.mirantis.mk.Salt()
31def common = new com.mirantis.mk.Common()
32def python = new com.mirantis.mk.Python()
33def openstack = new com.mirantis.mk.Openstack()
Richard Felkl2e9e5452017-10-16 09:52:10 +020034def date = new Date()
35def dateTime = date.format("ddMMyyyy-HHmmss")
36def venvPepper = "venvPepper"
Richard Felkl2e9e5452017-10-16 09:52:10 +020037def privateKey = ""
38def floatingIP = ""
39def openstackServer = ""
40def rcFile = ""
41def openstackEnv = ""
42def serverStatus = ""
Richard Felkl802e4462017-12-06 10:08:05 +010043def uploadImageStatus = ""
44def uploadMd5Status = ""
Richard Felkl2e9e5452017-10-16 09:52:10 +020045
46def retry(int times = 5, int delay = 0, Closure body) {
47 int retries = 0
48 def exceptions = []
49 while(retries++ < times) {
50 try {
51 return body.call()
52 } catch(e) {
53 sleep(delay)
54 }
55 }
56 currentBuild.result = "FAILURE"
57 throw new Exception("Failed after $times retries")
58}
59
Richard Felkl994887c2018-01-11 17:13:59 +010060timeout(time: 12, unit: 'HOURS') {
61 node("python&&disk-xl") {
62 try {
63 def workspace = common.getWorkspace()
64 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
65 openstackEnv = String.format("%s/venv", workspace)
66 def openstackVersion = OS_VERSION
Richard Felkl2e9e5452017-10-16 09:52:10 +020067
Richard Felkl994887c2018-01-11 17:13:59 +010068 VM_IP_DELAY = VM_IP_DELAY as Integer
69 VM_IP_RETRIES = VM_IP_RETRIES as Integer
70 VM_CONNECT_DELAY = VM_CONNECT_DELAY as Integer
71 VM_CONNECT_RETRIES = VM_CONNECT_RETRIES as Integer
Richard Felkl2e9e5452017-10-16 09:52:10 +020072
Richard Felkl994887c2018-01-11 17:13:59 +010073 stage("Get templates"){
Richard Felkl2e9e5452017-10-16 09:52:10 +020074
Richard Felkl994887c2018-01-11 17:13:59 +010075 if (!fileExists("${workspace}/tmp")) {
76 sh "mkdir -p ${workspace}/tmp"
77 }
78
79 sh "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/salt-bootstrap.sh"
80 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
Richard Felkl2e9e5452017-10-16 09:52:10 +020081 }
82
Richard Felkl994887c2018-01-11 17:13:59 +010083 stage("Spawn Instance"){
84 privateKey = openstack.runOpenstackCommand("openstack keypair create mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +020085
Richard Felkl994887c2018-01-11 17:13:59 +010086 common.infoMsg(privateKey)
87 sh "echo '${privateKey}' > id_rsa;chmod 600 id_rsa"
Richard Felkl2e9e5452017-10-16 09:52:10 +020088
Richard Felkl994887c2018-01-11 17:13:59 +010089 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 +020090
Richard Felkl994887c2018-01-11 17:13:59 +010091 withEnv(["CLUSTER_NAME=${CLUSTER_NAME}", "CLUSTER_MODEL=${CLUSTER_MODEL}"]) {
92 sh "envsubst < salt-bootstrap.sh > salt-bootstrap.sh.temp;mv salt-bootstrap.sh.temp salt-bootstrap.sh; cat salt-bootstrap.sh"
93 }
Richard Felkl2e9e5452017-10-16 09:52:10 +020094
Richard Felkl994887c2018-01-11 17:13:59 +010095 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)
96 sleep(60)
97
98 retry(VM_IP_RETRIES, VM_IP_DELAY){
99 openstack.runOpenstackCommand("openstack ip floating add ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
100 }
101
102 sleep(500)
103
104 retry(VM_CONNECT_RETRIES, VM_CONNECT_DELAY){
105 sh "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa root@${floatingIP}:/srv/initComplete ./"
106 }
107
108 python.setupPepperVirtualenv(venvPepper, "http://${floatingIP}:6969", SALT_MASTER_CREDENTIALS)
109 }
110 stage("Prepare instance"){
111 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.refresh_pillar', [], null, true)
112 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.sync_all', [], null, true)
113 salt.enforceState(venvPepper, '*apt*', ['salt'], true, false, null, false, -1, 2)
114 salt.enforceState(venvPepper, '*apt*', ['linux'], true, false, null, false, -1, 2)
115 salt.enforceState(venvPepper, '*apt*', ['nginx'], true, false, null, false, -1, 2)
Richard Felkl2e9e5452017-10-16 09:52:10 +0200116 }
117
Richard Felkl994887c2018-01-11 17:13:59 +0100118 stage("Create Docker Registry"){
119 common.infoMsg("Creating Docker Registry")
120 salt.enforceState(venvPepper, '*apt*', ["docker.host"], true, false, null, false, -1, 2)
121 salt.cmdRun(venvPepper, '*apt*', "docker run --restart always -d -p 5000:5000 --name registry registry:2")
122 salt.enforceState(venvPepper, '*apt*', ["docker.client.registry"], true, false, null, false, -1, 2)
123 salt.cmdRun(venvPepper, '*apt*', "docker system prune --all --force")
124 salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/config/interfaces -O /root/interfaces")
125 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 +0200126 }
127
Richard Felkl994887c2018-01-11 17:13:59 +0100128 stage("Create Aptly"){
129 common.infoMsg("Creating Aptly")
130 salt.enforceState(venvPepper, '*apt*', ['aptly'], true, false, null, false, -1, 2)
131 //TODO: Do it new way
132 salt.cmdRun(venvPepper, '*apt*', "aptly_mirror_update.sh -s -v", true, null, true, ["runas=aptly"])
133 salt.cmdRun(venvPepper, '*apt*', "nohup aptly api serve --no-lock > /dev/null 2>&1 </dev/null &", true, null, true, ["runas=aptly"])
134 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"])
135 salt.cmdRun(venvPepper, '*apt*', "aptly db cleanup", true, null, true, ["runas=aptly"])
136 //NEW way
137 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", "runas=aptly"], null, true)
138 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-acrfv", "runas=aptly"], null, true)
139 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")
140 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/aptly-update.sh")
Richard Felkl2e9e5452017-10-16 09:52:10 +0200141 }
142
Richard Felkl994887c2018-01-11 17:13:59 +0100143 stage("Create Debmirrors"){
144 common.infoMsg("Creating Debmirrors")
145 salt.cmdRun(venvPepper, '*apt*', "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${SCRIPTS_REF}/mirror-image/debmirror.sh -O /srv/scripts/debmirror.sh")
146 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/debmirror.sh")
147 salt.cmdRun(venvPepper, '*apt*', "export HOME='/root';export MCP_VERSION='${MCP_VERSION}';/srv/scripts/debmirror.sh")
Richard Felkl7a006072017-12-13 09:47:07 +0100148 }
Richard Felkl14a4c6f2017-11-29 09:10:10 +0100149
Richard Felkl994887c2018-01-11 17:13:59 +0100150 stage("Create Git mirror"){
151 common.infoMsg("Creating Git mirror")
152 salt.enforceState(venvPepper, '*apt*', ['git.server'], true, false, null, false, -1, 2)
153 }
154
155 stage("Create PyPi mirror"){
156 common.infoMsg("Creating PyPi mirror")
157 salt.cmdRun(venvPepper, '*apt*', "pip install pip2pi")
158 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")
159 salt.cmdRun(venvPepper, '*apt*', "pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt")
160 }
161
162 stage("Create mirror of images"){
163 common.infoMsg("Creating mirror of images")
164 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")
165 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")
166 salt.cmdRun(venvPepper, '*apt*', "chmod +x /srv/scripts/update-images.sh")
167 salt.cmdRun(venvPepper, '*apt*', "/srv/scripts/update-images.sh -u http://ci.mcp.mirantis.net:8085/images")
168 }
169
170 stage("Create instance snapshot"){
171 salt.cmdRun(venvPepper, '*apt*', "rm -rf /var/lib/cloud/sem/* /var/lib/cloud/instance /var/lib/cloud/instances/*")
172 salt.cmdRun(venvPepper, '*apt*', "cloud-init init")
173
174 retry(3, 5){
175 openstack.runOpenstackCommand("openstack server stop mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
176 }
177
178 retry(6, 30){
179 serverStatus = openstack.runOpenstackCommand("openstack server show --format value -c status mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
180 if(serverStatus != "SHUTOFF"){
181 throw new ResourceException("Instance is not ready for image create.")
182 }
183 }
184 retry(3, 5){
185 openstack.runOpenstackCommand("openstack server image create --name ${IMAGE_NAME}-${dateTime} --wait mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
Richard Felkl2e9e5452017-10-16 09:52:10 +0200186 }
187 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200188
Richard Felkl994887c2018-01-11 17:13:59 +0100189 stage("Publish image"){
190 common.infoMsg("Saving image ${IMAGE_NAME}-${dateTime}")
191 retry(3, 5){
192 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime}.qcow2 ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
Richard Felkl802e4462017-12-06 10:08:05 +0100193 }
Richard Felkl994887c2018-01-11 17:13:59 +0100194 sh "md5sum ${IMAGE_NAME}-${dateTime}.qcow2 > ${IMAGE_NAME}-${dateTime}.qcow2.md5"
Richard Felkl2e9e5452017-10-16 09:52:10 +0200195
Richard Felkl994887c2018-01-11 17:13:59 +0100196 common.infoMsg("Uploading image ${IMAGE_NAME}-${dateTime}")
197 retry(3, 5){
198 uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2 ${UPLOAD_URL}", returnStatus: true)
199 if(uploadImageStatus!=0){
200 throw new Exception("Image upload failed")
201 }
202 }
203 retry(3, 5){
204 uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}-${dateTime}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true)
205 if(uploadMd5Status != 0){
206 throw new Exception("MD5 sum upload failed")
207 }
208 }
209 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 +0200210 }
Richard Felkl994887c2018-01-11 17:13:59 +0100211
212 } catch (Throwable e) {
213 // If there was an error or exception thrown, the build failed
214 currentBuild.result = "FAILURE"
215 throw e
216 } finally {
217 stage("Cleanup"){
218 if(openstackServer != ""){
219 openstack.runOpenstackCommand("openstack ip floating remove ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
220 openstack.runOpenstackCommand("openstack server delete mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
221 }
222 if(privateKey != ""){
223 openstack.runOpenstackCommand("openstack keypair delete mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
224 }
225 sh "rm -rf ./*"
Richard Felkl2e9e5452017-10-16 09:52:10 +0200226 }
Richard Felkl2e9e5452017-10-16 09:52:10 +0200227 }
228 }
229}