blob: bd7f194439906080862eede626c0b7f8be78492c [file] [log] [blame]
Ales Komarek24097402017-05-22 16:28:10 +02001/**
2 *
3 * Launch heat/cloudformation stack
4 *
5 * Expected parameters:
6 * STACK_NAME Infrastructure stack name
7 * STACK_TEMPLATE Stack HOT/CFN template
8 * STACK_TYPE Deploy OpenStack/AWS [heat/aws]
9 *
10 * STACK_TEMPLATE_URL URL to git repo with stack templates
11 * STACK_TEMPLATE_CREDENTIALS Credentials to the templates repo
12 * STACK_TEMPLATE_BRANCH Stack templates repo branch
13 *
14 * STACK_DELETE Delete stack when finished (bool)
15 * STACK_REUSE Reuse existing stack (don't create one)
16 * STACK_INSTALL What should be installed (k8s, openstack, ...)
17 * STACK_TEST Run tests (bool)
18 * STACK_CLEANUP_JOB Name of job for deleting stack
19 *
20 * AWS_STACK_REGION CloudFormation AWS region
21 * AWS_API_CREDENTIALS AWS Access key ID with AWS secret access key
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020022 * AWS_SSH_KEY AWS key pair name (used for SSH access)
Ales Komarek24097402017-05-22 16:28:10 +020023 *
24 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
25 * HEAT_STACK_ZONE Heat stack availability zone
26 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
27 * OPENSTACK_API_URL OpenStack API address
28 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
29 * OPENSTACK_API_PROJECT OpenStack project to connect to
30 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
31 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
32 *
33 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
34 * SALT_MASTER_URL URL of Salt master
35 *
36 * K8S_API_SERVER Kubernetes API address
37 * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests
38 *
39 * TEMPEST_IMAGE_LINK Tempest image link
40 *
41 */
42common = new com.mirantis.mk.Common()
43git = new com.mirantis.mk.Git()
44openstack = new com.mirantis.mk.Openstack()
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020045aws = new com.mirantis.mk.Aws()
Ales Komarek24097402017-05-22 16:28:10 +020046orchestrate = new com.mirantis.mk.Orchestrate()
47salt = new com.mirantis.mk.Salt()
48test = new com.mirantis.mk.Test()
49
50_MAX_PERMITTED_STACKS = 2
51overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml"
52
53timestamps {
54 node {
55 try {
56 //
57 // Prepare machines
58 //
59 stage ('Create infrastructure') {
60
61 if (STACK_TYPE == 'heat') {
62 // value defaults
63 def openstackCloud
64 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
65 def openstackEnv = "${env.WORKSPACE}/venv"
66
67 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
68 error("If you want to reuse existing stack you need to provide it's name")
69 }
70
71 if (STACK_REUSE.toBoolean() == false) {
72 // Don't allow to set custom heat stack name
73 wrap([$class: 'BuildUser']) {
74 if (env.BUILD_USER_ID) {
75 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
76 } else {
77 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
78 }
79 currentBuild.description = STACK_NAME
80 }
81 }
82
83 // set description
84 currentBuild.description = "${STACK_NAME}"
85
86 // get templates
87 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
88
89 // create openstack env
90 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
91 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
92 openstack.getKeystoneToken(openstackCloud, openstackEnv)
93 //
94 // Verify possibility of create stack for given user and stack type
95 //
96 wrap([$class: 'BuildUser']) {
97 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
98 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
99 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
100 STACK_DELETE = "false"
101 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
102 }
103 }
104 }
105 // launch stack
106 if (STACK_REUSE.toBoolean() == false) {
107 stage('Launch new Heat stack') {
108 // create stack
109 envParams = [
110 'instance_zone': HEAT_STACK_ZONE,
111 'public_net': HEAT_STACK_PUBLIC_NET
112 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200113 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200114 }
115 }
116
117 // get SALT_MASTER_URL
118 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200119 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200120
121 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200122 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200123
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200124 def venv_path = 'aws_venv'
125
126 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
127 error("If you want to reuse existing stack you need to provide it's name")
128 }
129
130 if (STACK_REUSE.toBoolean() == false) {
131 // Don't allow to set custom stack name
132 wrap([$class: 'BuildUser']) {
133 if (env.BUILD_USER_ID) {
134 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
135 } else {
136 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
137 }
138 currentBuild.description = STACK_NAME
139 }
140 }
141
142 // set description
143 currentBuild.description = "${STACK_NAME}"
144
145 // prepare configuration
146 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
147
148 if (STACK_REUSE.toBoolean() == false) {
149 // get templates
150 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
151
152 // setup environment
153 aws.setupVirtualEnv(venv_path)
154
155 // start stack
156 def stack_params = ["ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY]
157 aws.createStack(venv_path, env_vars, STACK_TEMPLATE, STACK_NAME, stack_params)
158 }
159
160 // wait for stack to be ready
161 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
162
163 // get outputs
164 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
165 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200166 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200167
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200168 } else {
169 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
170 }
Ales Komarek24097402017-05-22 16:28:10 +0200171 }
172
173 //
174 // Connect to Salt master
175 //
176
177 def saltMaster
178 stage('Connect to Salt API') {
179 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
180 }
181
182 //
183 // Install
184 //
185
186 if (common.checkContains('STACK_INSTALL', 'core')) {
187 stage('Install core infrastructure') {
188 orchestrate.installFoundationInfra(saltMaster)
189
190 if (common.checkContains('STACK_INSTALL', 'kvm')) {
191 orchestrate.installInfraKvm(saltMaster)
192 orchestrate.installFoundationInfra(saltMaster)
193 }
194
195 orchestrate.validateFoundationInfra(saltMaster)
196 }
197 }
198
199 // install k8s
200 if (common.checkContains('STACK_INSTALL', 'k8s')) {
201 stage('Install Kubernetes infra') {
202 orchestrate.installKubernetesInfra(saltMaster)
203 }
204
205 stage('Install Kubernetes control') {
206
207 // Overwrite Kubernetes vars if specified
208 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
209 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
210 }
211
212 orchestrate.installKubernetesControl(saltMaster)
213 }
214
215
216 if (common.checkContains('STACK_INSTALL', 'contrail')) {
217 state('Install Contrail for Kubernetes') {
218 orchestrate.installContrailNetwork(saltMaster)
219 orchestrate.installContrailCompute(saltMaster)
220 }
221 }
222 }
223
224 // install openstack
225 if (common.checkContains('STACK_INSTALL', 'openstack')) {
226 // install Infra and control, tests, ...
227
228 stage('Install OpenStack infra') {
229 orchestrate.installOpenstackInfra(saltMaster)
230 }
231
232 stage('Install OpenStack control') {
233 orchestrate.installOpenstackControl(saltMaster)
234 }
235
236 stage('Install OpenStack network') {
237
238 if (common.checkContains('STACK_INSTALL', 'contrail')) {
239 orchestrate.installContrailNetwork(saltMaster)
240 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
241 orchestrate.installOpenstackNetwork(saltMaster)
242 }
243
244 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
245 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
246 }
247
248 stage('Install OpenStack compute') {
249 orchestrate.installOpenstackCompute(saltMaster)
250
251 if (common.checkContains('STACK_INSTALL', 'contrail')) {
252 orchestrate.installContrailCompute(saltMaster)
253 }
254 }
255
256 }
257
258
259 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
260 stage('Install StackLight') {
261 orchestrate.installStacklightControl(saltMaster)
262 orchestrate.installStacklightClient(saltMaster)
263 }
264 }
265
266 //
267 // Test
268 //
269 def artifacts_dir = '_artifacts/'
270
271 if (common.checkContains('STACK_TEST', 'k8s')) {
272 stage('Run k8s bootstrap tests') {
273 def image = 'tomkukral/k8s-scripts'
274 def output_file = image.replaceAll('/', '-') + '.output'
275
276 // run image
277 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
278
279 // collect output
280 sh "mkdir -p ${artifacts_dir}"
281 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
282 writeFile file: "${artifacts_dir}${output_file}", text: file_content
283 sh "cat ${artifacts_dir}${output_file}"
284
285 // collect artifacts
286 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
287 }
288
289 stage('Run k8s conformance e2e tests') {
290 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
291
292 def image = K8S_CONFORMANCE_IMAGE
293 def output_file = image.replaceAll('/', '-') + '.output'
294
295 // run image
296 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
297
298 // collect output
299 sh "mkdir -p ${artifacts_dir}"
300 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
301 writeFile file: "${artifacts_dir}${output_file}", text: file_content
302 sh "cat ${artifacts_dir}${output_file}"
303
304 // collect artifacts
305 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
306 }
307 }
308
309 if (common.checkContains('STACK_TEST', 'openstack')) {
310 stage('Run deployment tests') {
311 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
312 }
313
314 stage('Copy test results to config node') {
315 test.copyTempestResults(saltMaster)
316 }
317 }
318
319 stage('Finalize') {
320 if (STACK_INSTALL != '') {
321 try {
322 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
323 } catch (Exception e) {
324 common.warningMsg('State apply failed but we should continue to run')
325 }
326 }
327 }
328 } catch (Throwable e) {
329 currentBuild.result = 'FAILURE'
330 throw e
331 } finally {
332
333
334 //
335 // Clean
336 //
337
338 if (STACK_TYPE == 'heat') {
339 // send notification
340 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
341
342 if (STACK_DELETE.toBoolean() == true) {
343 common.errorMsg('Heat job cleanup triggered')
344 stage('Trigger cleanup job') {
Ales Komarek5eb8b102017-05-24 14:00:22 +0200345 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME]]
Ales Komarek24097402017-05-22 16:28:10 +0200346 }
347 } else {
348 if (currentBuild.result == 'FAILURE') {
349 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
350 if (SALT_MASTER_URL) {
351 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
352 }
353 }
354 }
355 }
356 }
357 }
358}