blob: f2727437e116b70f960ff0798f46636f0fd8c9bb [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 *
Tomáš Kukrál74f92862017-06-05 19:18:43 +020020 * STACK_COMPUTE_COUNT Number of compute nodes to launch
21 *
Ales Komarek24097402017-05-22 16:28:10 +020022 * AWS_STACK_REGION CloudFormation AWS region
23 * AWS_API_CREDENTIALS AWS Access key ID with AWS secret access key
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020024 * AWS_SSH_KEY AWS key pair name (used for SSH access)
Ales Komarek24097402017-05-22 16:28:10 +020025 *
26 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
27 * HEAT_STACK_ZONE Heat stack availability zone
28 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
29 * OPENSTACK_API_URL OpenStack API address
30 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
31 * OPENSTACK_API_PROJECT OpenStack project to connect to
32 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
33 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
34 *
35 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
36 * SALT_MASTER_URL URL of Salt master
37 *
38 * K8S_API_SERVER Kubernetes API address
39 * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests
40 *
41 * TEMPEST_IMAGE_LINK Tempest image link
42 *
43 */
44common = new com.mirantis.mk.Common()
45git = new com.mirantis.mk.Git()
46openstack = new com.mirantis.mk.Openstack()
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020047aws = new com.mirantis.mk.Aws()
Ales Komarek24097402017-05-22 16:28:10 +020048orchestrate = new com.mirantis.mk.Orchestrate()
49salt = new com.mirantis.mk.Salt()
50test = new com.mirantis.mk.Test()
51
52_MAX_PERMITTED_STACKS = 2
53overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml"
54
55timestamps {
56 node {
57 try {
58 //
59 // Prepare machines
60 //
61 stage ('Create infrastructure') {
62
63 if (STACK_TYPE == 'heat') {
64 // value defaults
65 def openstackCloud
66 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
67 def openstackEnv = "${env.WORKSPACE}/venv"
68
69 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
70 error("If you want to reuse existing stack you need to provide it's name")
71 }
72
73 if (STACK_REUSE.toBoolean() == false) {
74 // Don't allow to set custom heat stack name
75 wrap([$class: 'BuildUser']) {
76 if (env.BUILD_USER_ID) {
77 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
78 } else {
79 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
80 }
81 currentBuild.description = STACK_NAME
82 }
83 }
84
85 // set description
86 currentBuild.description = "${STACK_NAME}"
87
88 // get templates
89 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
90
91 // create openstack env
92 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
93 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
94 openstack.getKeystoneToken(openstackCloud, openstackEnv)
95 //
96 // Verify possibility of create stack for given user and stack type
97 //
98 wrap([$class: 'BuildUser']) {
99 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
100 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
101 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
102 STACK_DELETE = "false"
103 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
104 }
105 }
106 }
107 // launch stack
108 if (STACK_REUSE.toBoolean() == false) {
109 stage('Launch new Heat stack') {
110 // create stack
111 envParams = [
112 'instance_zone': HEAT_STACK_ZONE,
113 'public_net': HEAT_STACK_PUBLIC_NET
114 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200115 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200116 }
117 }
118
119 // get SALT_MASTER_URL
120 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200121 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200122
123 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200124 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200125
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200126 def venv_path = 'aws_venv'
127
128 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
129 error("If you want to reuse existing stack you need to provide it's name")
130 }
131
132 if (STACK_REUSE.toBoolean() == false) {
133 // Don't allow to set custom stack name
134 wrap([$class: 'BuildUser']) {
135 if (env.BUILD_USER_ID) {
136 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
137 } else {
138 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
139 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200140 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200141
142 // no underscore in STACK_NAME
143 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200144 }
145
146 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200147 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200148
149 // prepare configuration
150 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
151
152 if (STACK_REUSE.toBoolean() == false) {
153 // get templates
154 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
155
156 // setup environment
157 aws.setupVirtualEnv(venv_path)
158
159 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200160 def stack_params = [
161 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
162 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
163 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200164 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukráld4c0fed2017-06-01 17:20:11 +0200165 aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200166 }
167
168 // wait for stack to be ready
169 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
170
171 // get outputs
172 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
173 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200174 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200175
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200176 } else {
177 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
178 }
Ales Komarek24097402017-05-22 16:28:10 +0200179 }
180
181 //
182 // Connect to Salt master
183 //
184
185 def saltMaster
186 stage('Connect to Salt API') {
187 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
188 }
189
190 //
191 // Install
192 //
193
194 if (common.checkContains('STACK_INSTALL', 'core')) {
195 stage('Install core infrastructure') {
196 orchestrate.installFoundationInfra(saltMaster)
197
198 if (common.checkContains('STACK_INSTALL', 'kvm')) {
199 orchestrate.installInfraKvm(saltMaster)
200 orchestrate.installFoundationInfra(saltMaster)
201 }
202
203 orchestrate.validateFoundationInfra(saltMaster)
204 }
205 }
206
207 // install k8s
208 if (common.checkContains('STACK_INSTALL', 'k8s')) {
209 stage('Install Kubernetes infra') {
210 orchestrate.installKubernetesInfra(saltMaster)
211 }
212
213 stage('Install Kubernetes control') {
214
Ales Komarek24097402017-05-22 16:28:10 +0200215 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200216
Ales Komarek24097402017-05-22 16:28:10 +0200217 }
218
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200219 stage('Scale Kubernetes computes') {
220 if (STACK_TYPE == 'AWS') {
221 // get stack info
222 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
223
224 //update autoscaling group
225 aws.updateAutoscalingGroup(venv_path, evn_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
226 }
227
228 }
Ales Komarek24097402017-05-22 16:28:10 +0200229
230 if (common.checkContains('STACK_INSTALL', 'contrail')) {
231 state('Install Contrail for Kubernetes') {
232 orchestrate.installContrailNetwork(saltMaster)
233 orchestrate.installContrailCompute(saltMaster)
234 }
235 }
236 }
237
238 // install openstack
239 if (common.checkContains('STACK_INSTALL', 'openstack')) {
240 // install Infra and control, tests, ...
241
242 stage('Install OpenStack infra') {
243 orchestrate.installOpenstackInfra(saltMaster)
244 }
245
246 stage('Install OpenStack control') {
247 orchestrate.installOpenstackControl(saltMaster)
248 }
249
250 stage('Install OpenStack network') {
251
252 if (common.checkContains('STACK_INSTALL', 'contrail')) {
253 orchestrate.installContrailNetwork(saltMaster)
254 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
255 orchestrate.installOpenstackNetwork(saltMaster)
256 }
257
258 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
259 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
260 }
261
262 stage('Install OpenStack compute') {
263 orchestrate.installOpenstackCompute(saltMaster)
264
265 if (common.checkContains('STACK_INSTALL', 'contrail')) {
266 orchestrate.installContrailCompute(saltMaster)
267 }
268 }
269
270 }
271
272
273 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
274 stage('Install StackLight') {
275 orchestrate.installStacklightControl(saltMaster)
276 orchestrate.installStacklightClient(saltMaster)
277 }
278 }
279
280 //
281 // Test
282 //
283 def artifacts_dir = '_artifacts/'
284
285 if (common.checkContains('STACK_TEST', 'k8s')) {
286 stage('Run k8s bootstrap tests') {
287 def image = 'tomkukral/k8s-scripts'
288 def output_file = image.replaceAll('/', '-') + '.output'
289
290 // run image
291 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
292
293 // collect output
294 sh "mkdir -p ${artifacts_dir}"
295 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
296 writeFile file: "${artifacts_dir}${output_file}", text: file_content
297 sh "cat ${artifacts_dir}${output_file}"
298
299 // collect artifacts
300 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
301 }
302
303 stage('Run k8s conformance e2e tests') {
304 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
305
306 def image = K8S_CONFORMANCE_IMAGE
307 def output_file = image.replaceAll('/', '-') + '.output'
308
309 // run image
310 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
311
312 // collect output
313 sh "mkdir -p ${artifacts_dir}"
314 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
315 writeFile file: "${artifacts_dir}${output_file}", text: file_content
316 sh "cat ${artifacts_dir}${output_file}"
317
318 // collect artifacts
319 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
320 }
321 }
322
323 if (common.checkContains('STACK_TEST', 'openstack')) {
324 stage('Run deployment tests') {
325 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
326 }
327
328 stage('Copy test results to config node') {
329 test.copyTempestResults(saltMaster)
330 }
331 }
332
333 stage('Finalize') {
334 if (STACK_INSTALL != '') {
335 try {
336 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
337 } catch (Exception e) {
338 common.warningMsg('State apply failed but we should continue to run')
339 }
340 }
341 }
342 } catch (Throwable e) {
343 currentBuild.result = 'FAILURE'
344 throw e
345 } finally {
346
347
348 //
349 // Clean
350 //
351
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200352 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200353 // send notification
354 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200355 }
Ales Komarek24097402017-05-22 16:28:10 +0200356
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200357 if (STACK_DELETE.toBoolean() == true) {
358 stage('Trigger cleanup job') {
359 common.errorMsg('Stack cleanup job triggered')
360 build(job: STACK_CLEANUP_JOB, parameters: [
361 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
362 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
363 ])
364 }
365 } else {
366 if (currentBuild.result == 'FAILURE') {
367 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
368 if (SALT_MASTER_URL) {
369 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200370 }
371 }
372 }
373 }
374 }
375}