blob: 2b3f312fbb5e33f00fc8e83f77ceeaf8c9baee5b [file] [log] [blame]
Richard Felkl2e9e5452017-10-16 09:52:10 +02001/**
2 *
3 * Update Salt environment pipeline
4 *
5 * Expected parameters:
6 * SALT_MASTER_URL Salt API server location
7 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
8 * UPDATE_FORMULAS Boolean switch for enforcing updating formulas
9 */
10
11// Load shared libs
12def salt = new com.mirantis.mk.Salt()
13def common = new com.mirantis.mk.Common()
14def python = new com.mirantis.mk.Python()
15def openstack = new com.mirantis.mk.Openstack()
16def git = new com.mirantis.mk.Git()
17def date = new Date()
18def dateTime = date.format("ddMMyyyy-HHmmss")
19def venvPepper = "venvPepper"
20def venvS4cmd = "venvS4cmd"
21def privateKey = ""
22def floatingIP = ""
23def openstackServer = ""
24def rcFile = ""
25def openstackEnv = ""
26def serverStatus = ""
27
28def retry(int times = 5, int delay = 0, Closure body) {
29 int retries = 0
30 def exceptions = []
31 while(retries++ < times) {
32 try {
33 return body.call()
34 } catch(e) {
35 sleep(delay)
36 }
37 }
38 currentBuild.result = "FAILURE"
39 throw new Exception("Failed after $times retries")
40}
41
42node("python&&disk-xl") {
43 try {
44 def workspace = common.getWorkspace()
45 rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "")
46 openstackEnv = String.format("%s/venv", workspace)
47 def openstackVersion = "ocata"
48
49 VM_IP_DELAY = VM_IP_DELAY as Integer
50 VM_IP_RETRIES = VM_IP_RETRIES as Integer
51 VM_CONNECT_DELAY = VM_CONNECT_DELAY as Integer
52 VM_CONNECT_RETRIES = VM_CONNECT_RETRIES as Integer
53
54 stage("Get templates"){
55
56 if (!fileExists("${workspace}/tmp")) {
57 sh "mkdir -p ${workspace}/tmp"
58 }
59
60 sh "wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/mirror-image/salt-bootstrap.sh"
61 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
62 }
63
64 stage("Spawn Instance"){
65 privateKey = openstack.runOpenstackCommand("openstack keypair create mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
66
67 common.infoMsg(privateKey)
68 sh "echo '${privateKey}' > id_rsa;chmod 600 id_rsa"
69
70 floatingIP = openstack.runOpenstackCommand("openstack ip floating create --format value -c floating_ip_address ${VM_FLOATING_IP_POOL}", rcFile, openstackEnv)
71
72 withEnv(["CLUSTER_NAME=${CLUSTER_NAME}", "CLUSTER_MODEL=${CLUSTER_MODEL}"]) {
73 sh "envsubst < salt-bootstrap.sh > salt-bootstrap.sh.temp;mv salt-bootstrap.sh.temp salt-bootstrap.sh; cat salt-bootstrap.sh"
74 }
75
Richard Felkl54cc3d52017-11-28 17:19:22 +010076 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)
Richard Felkl2e9e5452017-10-16 09:52:10 +020077 sleep(60)
78
79 retry(VM_IP_RETRIES, VM_IP_DELAY){
80 openstack.runOpenstackCommand("openstack ip floating add ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
81 }
82
83 sleep(500)
84
85 retry(VM_CONNECT_RETRIES, VM_CONNECT_DELAY){
86 sh "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa root@${floatingIP}:/srv/initComplete ./"
87 }
88
89 python.setupPepperVirtualenv(venvPepper, "http://${floatingIP}:6969", SALT_MASTER_CREDENTIALS)
90 }
91 stage("Prepare instance"){
92 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.refresh_pillar', [], null, true)
93 salt.runSaltProcessStep(venvPepper, '*apt*', 'saltutil.sync_all', [], null, true)
94 salt.enforceState(venvPepper, '*apt*', ['salt'], true, false, null, false, -1, 2)
95 salt.enforceState(venvPepper, '*apt*', ['linux'], true, false, null, false, -1, 2)
96 salt.enforceState(venvPepper, '*apt*', ['nginx'], true, false, null, false, -1, 2)
97 }
98
99 stage("Create Docker Registry"){
100 common.infoMsg("Creating Docker Registry")
101 salt.enforceState(venvPepper, '*apt*', ['docker.host'], true, false, null, false, -1, 2)
102 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['docker run --restart always -d -p 5000:5000 --name registry registry:2'], null, true)
103 salt.enforceState(venvPepper, '*apt*', ['docker.client.registry'], true, false, null, false, -1, 2)
104 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['docker system prune --all --force'], null, true)
105 }
106
107 stage("Create Aptly"){
108 common.infoMsg("Creating Aptly")
109 salt.enforceState(venvPepper, '*apt*', ['aptly'], true, false, null, false, -1, 2)
110 //TODO: Do it new way
111 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['aptly_mirror_update.sh -s -v', 'runas=aptly'], null, true)
112 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['nohup aptly api serve --no-lock > /dev/null 2>&1 </dev/null &', 'runas=aptly'], null, true)
113 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['aptly-publisher --timeout=1200 publish -v -c /etc/aptly-publisher.yaml --architectures amd64 --url http://127.0.0.1:8080 --recreate --force-overwrite', 'runas=aptly'], null, true)
114 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/mirror-image/aptly/aptly-update.sh -O /srv/scripts/aptly-update.sh'], null, true)
115 //NEW way
116 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
117 //salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-arf", 'runas=aptly'], null, true)
118 }
119
120 stage("Create Git mirror"){
121 common.infoMsg("Creating Git mirror")
122 salt.enforceState(venvPepper, '*apt*', ['git.server'], true, false, null, false, -1, 2)
123 }
124
125 stage("Create PyPi mirror"){
126 common.infoMsg("Creating PyPi mirror")
127 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['pip install pip2pi'], null, true)
128 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/mirror-image/pypi_mirror/requirements.txt -O /srv/pypi_mirror/requirements.txt'], null, true)
129 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt'], null, true)
130 }
131
132 stage("Create mirror of images"){
133 common.infoMsg("Creating mirror of images")
134 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/mirror-image/images_mirror/images.txt -O /srv/images.txt'], null, true)
135 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['wget https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/mirror-image/images_mirror/update-images.sh -O /srv/scripts/update-images.sh'], null, true)
136 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['chmod +x /srv/scripts/update-images.sh'], null, true)
137 salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['/srv/scripts/update-images.sh -u http://ci.mcp.mirantis.net:8085/images'], null, true)
138 }
139
140 stage("Create instance snapshot"){
141 openstack.runOpenstackCommand("openstack server stop mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
142 retry(6, 30){
143 serverStatus = openstack.runOpenstackCommand("openstack server show --format value -c status mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
144 if(serverStatus != "SHUTOFF"){
145 throw new ResourceException("Instance is not ready for image create.")
146 }
147 }
148 openstack.runOpenstackCommand("openstack server image create --name ${IMAGE_NAME}-${dateTime} --wait mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
149 }
150
151 stage("Publish image"){
152 openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}-${dateTime} ${IMAGE_NAME}-${dateTime}", rcFile, openstackEnv)
153 python.setupVirtualenv(venvS4cmd)
154 python.runVirtualenvCommand(venvS4cmd, "pip install s4cmd")
155 creds = common.getPasswordCredentials(AWS_CREDENTIALS_ID)
156 python.runVirtualenvCommand(venvS4cmd, "python ./${venvS4cmd}/bin/s4cmd.py --access-key ${creds.username} --secret-key ${creds.password.toString()} --multipart-split-size=5368709120 put ${IMAGE_NAME}-${dateTime} s3://${AWS_S3_BUCKET_NAME}/${IMAGE_NAME}-${dateTime}")
157 }
158
159 } catch (Throwable e) {
160 // If there was an error or exception thrown, the build failed
161 currentBuild.result = "FAILURE"
162 throw e
163 } finally {
164 stage("Cleanup"){
165 if(openstackServer != ""){
166 openstack.runOpenstackCommand("openstack ip floating remove ${floatingIP} mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
167 openstack.runOpenstackCommand("openstack server delete mcp-offline-mirror-${dateTime}", rcFile, openstackEnv)
168 }
169 if(privateKey != ""){
170 openstack.runOpenstackCommand("openstack keypair delete mcp-offline-keypair-${dateTime}", rcFile, openstackEnv)
171 }
172 sh "rm -rf ./*"
173 }
174 }
175}