blob: ec8bb2e3f25288681e8658bd974679cd6170edb4 [file] [log] [blame]
Filip Pytloun0a07f702017-02-24 18:26:18 +01001/**
2 *
3 * Launch heat stack with CI/CD lab infrastructure
4 *
5 * Expected parameters:
6 * HEAT_TEMPLATE_URL URL to git repo with Heat templates
7 * HEAT_TEMPLATE_CREDENTIALS Credentials to the Heat templates repo
8 * HEAT_TEMPLATE_BRANCH Heat templates repo branch
9 * HEAT_STACK_NAME Heat stack name
10 * HEAT_STACK_TEMPLATE Heat stack HOT template
11 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
12 * HEAT_STACK_ZONE Heat stack availability zone
13 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
14 * HEAT_STACK_DELETE Delete Heat stack when finished (bool)
15 * HEAT_STACK_CLEANUP_JOB Name of job for deleting Heat stack
16 * HEAT_STACK_REUSE Reuse Heat stack (don't create one)
17 *
18 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
Filip Pytloune32fda82017-02-24 18:26:18 +010019 * SALT_MASTER_PORT Port of salt-api, defaults to 8000
Filip Pytloun0a07f702017-02-24 18:26:18 +010020 *
21 * OPENSTACK_API_URL OpenStack API address
22 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
23 * OPENSTACK_API_PROJECT OpenStack project to connect to
24 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
25 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
26 *
27 */
28
Filip Pytlounad2b36b2017-03-04 20:33:41 +010029common = new com.mirantis.mk.Common()
Filip Pytloun0a07f702017-02-24 18:26:18 +010030git = new com.mirantis.mk.Git()
31openstack = new com.mirantis.mk.Openstack()
32salt = new com.mirantis.mk.Salt()
33orchestrate = new com.mirantis.mk.Orchestrate()
34
Filip Pytlounbfce09d2017-03-01 19:00:43 +010035timestamps {
36 node {
37 try {
38 // connection objects
39 def openstackCloud
40 def saltMaster
Filip Pytloun0a07f702017-02-24 18:26:18 +010041
Filip Pytlounbfce09d2017-03-01 19:00:43 +010042 // value defaults
43 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
44 def openstackEnv = "${env.WORKSPACE}/venv"
Filip Pytloun0a07f702017-02-24 18:26:18 +010045
Filip Pytloun3eefd3d2017-03-03 14:13:41 +010046 try {
47 sshPubKey = SSH_PUBLIC_KEY
48 } catch (MissingPropertyException e) {
49 sshPubKey = false
50 }
51
Filip Pytloun794ad952017-03-03 10:39:26 +010052 if (HEAT_STACK_REUSE.toBoolean() == true && HEAT_STACK_NAME == '') {
53 error("If you want to reuse existing stack you need to provide it's name")
54 }
55
56 if (HEAT_STACK_REUSE.toBoolean() == false) {
57 // Don't allow to set custom heat stack name
58 wrap([$class: 'BuildUser']) {
Tomáš Kukrál24d7fe62017-03-03 10:57:11 +010059 if (env.BUILD_USER_ID) {
60 HEAT_STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
61 } else {
62 HEAT_STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
63 }
Filip Pytloun794ad952017-03-03 10:39:26 +010064 currentBuild.description = HEAT_STACK_NAME
65 }
Filip Pytlounfd6726a2017-02-28 19:31:16 +010066 }
Filip Pytloun5b0954b2017-03-01 10:10:18 +010067
Filip Pytloun3d045f82017-03-01 09:44:52 +010068 //
Filip Pytlounbfce09d2017-03-01 19:00:43 +010069 // Bootstrap
Filip Pytloun3d045f82017-03-01 09:44:52 +010070 //
Filip Pytlounbfce09d2017-03-01 19:00:43 +010071
72 stage ('Download Heat templates') {
73 git.checkoutGitRepository('template', HEAT_TEMPLATE_URL, HEAT_TEMPLATE_BRANCH, HEAT_TEMPLATE_CREDENTIALS)
Filip Pytloun3d045f82017-03-01 09:44:52 +010074 }
Filip Pytloun3d045f82017-03-01 09:44:52 +010075
Filip Pytlounbfce09d2017-03-01 19:00:43 +010076 stage('Install OpenStack CLI') {
77 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
78 }
Filip Pytloun64123cd2017-03-01 11:26:17 +010079
Filip Pytlounbfce09d2017-03-01 19:00:43 +010080 stage('Connect to OpenStack cloud') {
81 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
82 openstack.getKeystoneToken(openstackCloud, openstackEnv)
83 }
84
Filip Pytloun794ad952017-03-03 10:39:26 +010085 if (HEAT_STACK_REUSE.toBoolean() == false) {
Filip Pytlounbfce09d2017-03-01 19:00:43 +010086 stage('Launch new Heat stack') {
87 envParams = [
88 'instance_zone': HEAT_STACK_ZONE,
89 'public_net': HEAT_STACK_PUBLIC_NET
90 ]
91 openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
92 }
93 }
94
95 stage('Connect to Salt master') {
96 def saltMasterPort
97 try {
98 saltMasterPort = SALT_MASTER_PORT
99 } catch (MissingPropertyException e) {
Filip Pytloun2ef26132017-03-10 09:44:37 +0100100 saltMasterPort = 6969
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100101 }
102 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv)
103 saltMasterUrl = "http://${saltMasterHost}:${saltMasterPort}"
104 saltMaster = salt.connection(saltMasterUrl, SALT_MASTER_CREDENTIALS)
105 }
106
107 //
108 // Install
109 //
110
111 stage('Install core infra') {
112 // salt.master, reclass
113 // refresh_pillar
114 // sync_all
115 // linux,openssh,salt.minion.ntp
116
117 orchestrate.installFoundationInfra(saltMaster)
118 orchestrate.validateFoundationInfra(saltMaster)
119 }
120
121 stage("Deploy GlusterFS") {
122 salt.enforceState(saltMaster, 'I@glusterfs:server', 'glusterfs.server.service', true)
123 salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true)
124 sleep(5)
125 salt.enforceState(saltMaster, 'I@glusterfs:client', 'glusterfs.client', true)
Jakub Joseffafd6592017-03-27 18:53:17 +0200126 print common.prettyPrint(salt.cmdRun(saltMaster, 'I@glusterfs:client', 'mount|grep fuse.glusterfs || echo "Command failed"'))
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100127 }
128
129 stage("Deploy GlusterFS") {
130 salt.enforceState(saltMaster, 'I@haproxy:proxy', 'haproxy,keepalived')
131 }
132
133 stage("Setup Docker Swarm") {
134 salt.enforceState(saltMaster, 'I@docker:host', 'docker.host', true)
135 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.swarm', true)
136 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'salt', true)
137 salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.flush')
138 salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.update')
139 salt.enforceState(saltMaster, 'I@docker:swarm', 'docker.swarm', true)
Jakub Joseffafd6592017-03-27 18:53:17 +0200140 print common.prettyPrint(salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'docker node ls'))
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100141 }
142
143 stage("Deploy Docker services") {
144 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client')
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100145 }
146
147 stage("Configure CI/CD services") {
Filip Pytloun29d0bc12017-03-10 14:39:26 +0100148 salt.syncAll(saltMaster, '*')
149
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100150 // Aptly
151 salt.enforceState(saltMaster, 'I@aptly:server', 'aptly', true)
152
Filip Pytloun03983812017-03-28 13:07:34 +0200153 // OpenLDAP
154 timeout(10) {
155 println "Waiting for OpenLDAP to come up.."
156 salt.cmdRun(saltMaster, 'I@openldap:client', 'while true; do curl -svf ldap://172.16.10.254 >/dev/null && break; done')
157 }
158 salt.enforceState(saltMaster, 'I@openldap:client', 'openldap', true)
159
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100160 // Gerrit
161 timeout(10) {
162 println "Waiting for Gerrit to come up.."
163 salt.cmdRun(saltMaster, 'I@gerrit:client', 'while true; do curl -svf 172.16.10.254:8080 >/dev/null && break; done')
164 }
165 retry(2) {
166 // Needs to run twice to pass __virtual__ method of gerrit module
167 // after installation of dependencies
168 try {
169 salt.enforceState(saltMaster, 'I@gerrit:client', 'gerrit', true)
170 } catch (Exception e) {
Filip Pytloun85464052017-03-03 16:31:43 +0100171 common.infoMsg("Restarting Salt minion")
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100172 salt.cmdRun(saltMaster, 'I@gerrit:client', "exec 0>&-; exec 1>&-; exec 2>&-; nohup /bin/sh -c 'salt-call --local service.restart salt-minion' &")
173 sleep(5)
174 throw e
175 }
176 }
177
178 // Jenkins
179 timeout(10) {
180 println "Waiting for Jenkins to come up.."
181 salt.cmdRun(saltMaster, 'I@jenkins:client', 'while true; do curl -svf 172.16.10.254:8081 >/dev/null && break; done')
182 }
183 retry(2) {
184 // Same for jenkins
185 try {
186 salt.enforceState(saltMaster, 'I@jenkins:client', 'jenkins', true)
187 } catch (Exception e) {
Filip Pytloun85464052017-03-03 16:31:43 +0100188 common.infoMsg("Restarting Salt minion")
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100189 salt.cmdRun(saltMaster, 'I@jenkins:client', "exec 0>&-; exec 1>&-; exec 2>&-; nohup /bin/sh -c 'salt-call --local service.restart salt-minion' &")
190 sleep(5)
191 throw e
192 }
193 }
194 }
195
196 stage("Finalize") {
197 //
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100198 // Deploy user's ssh key
199 //
Filip Pytloun0da421f2017-03-03 18:50:45 +0100200 def adminUser
201 def authorizedKeysFile
Filip Pytlounbfa918a2017-03-04 10:01:30 +0100202 def adminUserCmdOut = salt.cmdRun(saltMaster, 'I@salt:master', "[ -d /home/ubuntu ] && echo 'ubuntu user exists'")
203 if (adminUserCmdOut =~ /ubuntu user exists/) {
Filip Pytloun0da421f2017-03-03 18:50:45 +0100204 adminUser = "ubuntu"
205 authorizedKeysFile = "/home/ubuntu/.ssh/authorized_keys"
206 } else {
207 adminUser = "root"
208 authorizedKeysFile = "/root/.ssh/authorized_keys"
209 }
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100210
Filip Pytloun0da421f2017-03-03 18:50:45 +0100211 if (sshPubKey) {
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100212 println "Deploying provided ssh key at ${authorizedKeysFile}"
Filip Pytloun4a847d62017-03-03 15:54:56 +0100213 salt.cmdRun(saltMaster, '*', "echo '${sshPubKey}' | tee -a ${authorizedKeysFile}")
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100214 }
215
216 //
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100217 // Generate docs
218 //
219 try {
Filip Pytloun64ff0792017-03-07 16:47:46 +0100220 try {
221 // Run sphinx state to install sphinx-build needed in
222 // upcomming orchestrate
223 salt.enforceState(saltMaster, 'I@sphinx:server', 'sphinx')
224 } catch (Throwable e) {
225 true
226 }
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100227 retry(3) {
Filip Pytloun27e8fa02017-03-01 20:02:46 +0100228 // TODO: fix salt.orchestrateSystem
229 // print salt.orchestrateSystem(saltMaster, ['expression': '*', 'type': 'compound'], 'sphinx.orch.generate_doc')
Filip Pytlounc17161d2017-03-03 09:50:54 +0100230 def out = salt.cmdRun(saltMaster, 'I@salt:master', 'salt-run state.orchestrate sphinx.orch.generate_doc || echo "Command execution failed"')
Jakub Joseffafd6592017-03-27 18:53:17 +0200231 print common.prettyPrint(out)
Filip Pytlounc17161d2017-03-03 09:50:54 +0100232 if (out =~ /Command execution failed/) {
233 throw new Exception("Command execution failed")
234 }
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100235 }
236 } catch (Throwable e) {
237 // We don't want sphinx docs to ruin whole build, so possible
238 // errors are just ignored here
239 true
240 }
241 salt.enforceState(saltMaster, 'I@nginx:server', 'nginx')
242
Filip Pytlounbd619272017-03-22 12:21:01 +0100243 def failedSvc = salt.cmdRun(saltMaster, '*', """systemctl --failed | grep -E 'loaded[ \t]+failed' && echo 'Command execution failed'""")
Jakub Joseffafd6592017-03-27 18:53:17 +0200244 print common.prettyPrint(failedSvc)
Filip Pytlounbd619272017-03-22 12:21:01 +0100245 if (failedSvc =~ /Command execution failed/) {
246 common.errorMsg("Some services are not running. Environment may not be fully functional!")
247 }
248
Filip Pytlound9427392017-03-04 13:58:08 +0100249 common.successMsg("""
Filip Pytloun794ad952017-03-03 10:39:26 +0100250 ============================================================
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100251 Your CI/CD lab has been deployed and you can enjoy it:
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100252 Use sshuttle to connect to your private subnet:
253
Filip Pytloun85464052017-03-03 16:31:43 +0100254 sshuttle -r ${adminUser}@${saltMasterHost} 172.16.10.0/24
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100255
256 And visit services running at 172.16.10.254 (vip address):
257
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100258 9600 haproxy stats
259 8080 gerrit
260 8081 jenkins
261 8091 Docker swarm visualizer
262 8090 Reclass-generated documentation
263
Filip Pytloun3eefd3d2017-03-03 14:13:41 +0100264 If you provided SSH_PUBLIC_KEY, you can use it to login,
265 otherwise you need to get private key connected to this
266 heat template.
267
268 DON'T FORGET TO TERMINATE YOUR STACK WHEN YOU DON'T NEED IT!
Filip Pytloun85464052017-03-03 16:31:43 +0100269 ============================================================""")
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100270 }
271 } catch (Throwable e) {
272 // If there was an error or exception thrown, the build failed
273 currentBuild.result = "FAILURE"
274 throw e
275 } finally {
276 // Cleanup
Filip Pytloun794ad952017-03-03 10:39:26 +0100277 if (HEAT_STACK_DELETE.toBoolean() == true) {
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100278 stage('Trigger cleanup job') {
Filip Pytloun592773c2017-03-03 14:17:00 +0100279 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]]
Filip Pytlounbfce09d2017-03-01 19:00:43 +0100280 }
Filip Pytlounfd6726a2017-02-28 19:31:16 +0100281 }
Filip Pytloun23741982017-02-27 17:43:00 +0100282 }
Filip Pytlounf6e877f2017-02-28 19:38:16 +0100283 }
Filip Pytloun0a07f702017-02-24 18:26:18 +0100284}