blob: 74c882d493de7ea28aa4cc3eedc855387bee562d [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
29git = new com.mirantis.mk.Git()
30openstack = new com.mirantis.mk.Openstack()
31salt = new com.mirantis.mk.Salt()
32orchestrate = new com.mirantis.mk.Orchestrate()
33
Filip Pytloun8fe1f7e2017-03-01 18:45:09 +010034def waitForServices(saltMaster) {
35 retry(30) {
36 out = salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', """/bin/bash -c 'docker service ls | grep -E "0/[0-9]+"' && echo 'Some services are not running'""")
37 for (int a = 0; a < out['return'].size(); a++) {
38 def entry = out['return'].get(a)
39 for (int i = 0; i < entry.size(); i++) {
40 def node = entry.get(i)
41 if (node) {
42 if (node.value =~ /Some services are not running/) {
43 sleep(10)
44 throw new Exception("$node.key: $node.value")
45 } else {
46 print out
47 }
48 }
49 }
50 }
51 }
52}
53
Filip Pytlounbfce09d2017-03-01 19:00:43 +010054timestamps {
55 node {
56 try {
57 // connection objects
58 def openstackCloud
59 def saltMaster
Filip Pytloun0a07f702017-02-24 18:26:18 +010060
Filip Pytlounbfce09d2017-03-01 19:00:43 +010061 // value defaults
62 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
63 def openstackEnv = "${env.WORKSPACE}/venv"
Filip Pytloun0a07f702017-02-24 18:26:18 +010064
Filip Pytlounbfce09d2017-03-01 19:00:43 +010065 if (HEAT_STACK_NAME == '') {
66 HEAT_STACK_NAME = BUILD_TAG
Filip Pytlounfd6726a2017-02-28 19:31:16 +010067 }
Filip Pytloun5b0954b2017-03-01 10:10:18 +010068
Filip Pytloun3d045f82017-03-01 09:44:52 +010069 //
Filip Pytlounbfce09d2017-03-01 19:00:43 +010070 // Bootstrap
Filip Pytloun3d045f82017-03-01 09:44:52 +010071 //
Filip Pytlounbfce09d2017-03-01 19:00:43 +010072
73 stage ('Download Heat templates') {
74 git.checkoutGitRepository('template', HEAT_TEMPLATE_URL, HEAT_TEMPLATE_BRANCH, HEAT_TEMPLATE_CREDENTIALS)
Filip Pytloun3d045f82017-03-01 09:44:52 +010075 }
Filip Pytloun3d045f82017-03-01 09:44:52 +010076
Filip Pytlounbfce09d2017-03-01 19:00:43 +010077 stage('Install OpenStack CLI') {
78 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
79 }
Filip Pytloun64123cd2017-03-01 11:26:17 +010080
Filip Pytlounbfce09d2017-03-01 19:00:43 +010081 stage('Connect to OpenStack cloud') {
82 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
83 openstack.getKeystoneToken(openstackCloud, openstackEnv)
84 }
85
86 if (HEAT_STACK_REUSE == 'false') {
87 stage('Launch new Heat stack') {
88 envParams = [
89 'instance_zone': HEAT_STACK_ZONE,
90 'public_net': HEAT_STACK_PUBLIC_NET
91 ]
92 openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
93 }
94 }
95
96 stage('Connect to Salt master') {
97 def saltMasterPort
98 try {
99 saltMasterPort = SALT_MASTER_PORT
100 } catch (MissingPropertyException e) {
101 saltMasterPort = 8000
102 }
103 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv)
104 saltMasterUrl = "http://${saltMasterHost}:${saltMasterPort}"
105 saltMaster = salt.connection(saltMasterUrl, SALT_MASTER_CREDENTIALS)
106 }
107
108 //
109 // Install
110 //
111
112 stage('Install core infra') {
113 // salt.master, reclass
114 // refresh_pillar
115 // sync_all
116 // linux,openssh,salt.minion.ntp
117
118 orchestrate.installFoundationInfra(saltMaster)
119 orchestrate.validateFoundationInfra(saltMaster)
120 }
121
122 stage("Deploy GlusterFS") {
123 salt.enforceState(saltMaster, 'I@glusterfs:server', 'glusterfs.server.service', true)
124 salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true)
125 sleep(5)
126 salt.enforceState(saltMaster, 'I@glusterfs:client', 'glusterfs.client', true)
127 print salt.cmdRun(saltMaster, 'I@glusterfs:client', 'mount|grep fuse.glusterfs || echo "Command failed"')
128 }
129
130 stage("Deploy GlusterFS") {
131 salt.enforceState(saltMaster, 'I@haproxy:proxy', 'haproxy,keepalived')
132 }
133
134 stage("Setup Docker Swarm") {
135 salt.enforceState(saltMaster, 'I@docker:host', 'docker.host', true)
136 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.swarm', true)
137 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'salt', true)
138 salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.flush')
139 salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.update')
140 salt.enforceState(saltMaster, 'I@docker:swarm', 'docker.swarm', true)
141 print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'docker node ls')
142 }
143
144 stage("Deploy Docker services") {
145 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client')
146
147 // XXX: Hack to fix dependency of gerrit on mysql
148 print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', "docker service rm gerrit; sleep 5; rm -rf /srv/volumes/gerrit/*")
149 waitForServices(saltMaster)
150
151 timeout(10) {
152 salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'apt-get install -y mysql-client')
153 println "Waiting for MySQL to come up.."
154 salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'while true; do mysql -h172.16.10.254 -ppassword -e"show status;" >/dev/null && break; done')
155 }
156 salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client')
157 // ---- cut here (end of hack) ----
158
159 waitForServices(saltMaster)
160 }
161
162 stage("Configure CI/CD services") {
163 // Aptly
164 salt.enforceState(saltMaster, 'I@aptly:server', 'aptly', true)
165
166 // Gerrit
167 timeout(10) {
168 println "Waiting for Gerrit to come up.."
169 salt.cmdRun(saltMaster, 'I@gerrit:client', 'while true; do curl -svf 172.16.10.254:8080 >/dev/null && break; done')
170 }
171 retry(2) {
172 // Needs to run twice to pass __virtual__ method of gerrit module
173 // after installation of dependencies
174 try {
175 salt.enforceState(saltMaster, 'I@gerrit:client', 'gerrit', true)
176 } catch (Exception e) {
177 print "Restarting Salt minion"
178 salt.cmdRun(saltMaster, 'I@gerrit:client', "exec 0>&-; exec 1>&-; exec 2>&-; nohup /bin/sh -c 'salt-call --local service.restart salt-minion' &")
179 sleep(5)
180 throw e
181 }
182 }
183
184 // Jenkins
185 timeout(10) {
186 println "Waiting for Jenkins to come up.."
187 salt.cmdRun(saltMaster, 'I@jenkins:client', 'while true; do curl -svf 172.16.10.254:8081 >/dev/null && break; done')
188 }
189 retry(2) {
190 // Same for jenkins
191 try {
192 salt.enforceState(saltMaster, 'I@jenkins:client', 'jenkins', true)
193 } catch (Exception e) {
194 print "Restarting Salt minion"
195 salt.cmdRun(saltMaster, 'I@jenkins:client', "exec 0>&-; exec 1>&-; exec 2>&-; nohup /bin/sh -c 'salt-call --local service.restart salt-minion' &")
196 sleep(5)
197 throw e
198 }
199 }
200 }
201
202 stage("Finalize") {
203 //
204 // Generate docs
205 //
206 try {
207 retry(3) {
208 print salt.orchestrateSystem(saltMaster, ['expression': '*', 'type': 'compound'], 'sphinx.orch.generate_doc')
209 }
210 } catch (Throwable e) {
211 // We don't want sphinx docs to ruin whole build, so possible
212 // errors are just ignored here
213 true
214 }
215 salt.enforceState(saltMaster, 'I@nginx:server', 'nginx')
216
217 print """============================================================
218 Your CI/CD lab has been deployed and you can enjoy it:
219 Use sshuttle -r ubuntu@${saltMasterHost} 172.16.10.0/24
220 to connect to your private subnet and visit services
221 running at 172.16.10.254 (vip address):
222 9600 haproxy stats
223 8080 gerrit
224 8081 jenkins
225 8091 Docker swarm visualizer
226 8090 Reclass-generated documentation
227
228 Don't forget to terminate your stack when you don't needed!
229 ============================================================"""
230 }
231 } catch (Throwable e) {
232 // If there was an error or exception thrown, the build failed
233 currentBuild.result = "FAILURE"
234 throw e
235 } finally {
236 // Cleanup
237 if (HEAT_STACK_DELETE == 'true') {
238 stage('Trigger cleanup job') {
239 build job: 'deploy_heat_cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]]
240 }
Filip Pytlounfd6726a2017-02-28 19:31:16 +0100241 }
Filip Pytloun23741982017-02-27 17:43:00 +0100242 }
Filip Pytlounf6e877f2017-02-28 19:38:16 +0100243 }
Filip Pytloun0a07f702017-02-24 18:26:18 +0100244}