blob: f1476f89ae92567c027785e4f457e880632a2dea [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
Tomáš Kukrálbc4be252017-06-06 11:28:40 +020055
Ales Komarek24097402017-05-22 16:28:10 +020056timestamps {
57 node {
58 try {
59 //
60 // Prepare machines
61 //
62 stage ('Create infrastructure') {
63
64 if (STACK_TYPE == 'heat') {
65 // value defaults
66 def openstackCloud
67 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
68 def openstackEnv = "${env.WORKSPACE}/venv"
69
70 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
71 error("If you want to reuse existing stack you need to provide it's name")
72 }
73
74 if (STACK_REUSE.toBoolean() == false) {
75 // Don't allow to set custom heat stack name
76 wrap([$class: 'BuildUser']) {
77 if (env.BUILD_USER_ID) {
78 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
79 } else {
80 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
81 }
82 currentBuild.description = STACK_NAME
83 }
84 }
85
86 // set description
87 currentBuild.description = "${STACK_NAME}"
88
89 // get templates
90 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
91
92 // create openstack env
93 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
94 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
95 openstack.getKeystoneToken(openstackCloud, openstackEnv)
96 //
97 // Verify possibility of create stack for given user and stack type
98 //
99 wrap([$class: 'BuildUser']) {
100 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
101 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
102 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
103 STACK_DELETE = "false"
104 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
105 }
106 }
107 }
108 // launch stack
109 if (STACK_REUSE.toBoolean() == false) {
110 stage('Launch new Heat stack') {
111 // create stack
112 envParams = [
113 'instance_zone': HEAT_STACK_ZONE,
114 'public_net': HEAT_STACK_PUBLIC_NET
115 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200116 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200117 }
118 }
119
120 // get SALT_MASTER_URL
121 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200122 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200123
124 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200125 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200126
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200127
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
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200150 def venv_path = 'aws_venv'
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200151 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
152
153 if (STACK_REUSE.toBoolean() == false) {
154 // get templates
155 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
156
157 // setup environment
158 aws.setupVirtualEnv(venv_path)
159
160 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200161 def stack_params = [
162 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
163 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
164 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200165 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukráld4c0fed2017-06-01 17:20:11 +0200166 aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200167 }
168
169 // wait for stack to be ready
170 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
171
172 // get outputs
173 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
174 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200175 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200176
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200177 } else {
178 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
179 }
Ales Komarek24097402017-05-22 16:28:10 +0200180
Ales Komarek24097402017-05-22 16:28:10 +0200181
Tomáš Kukrál20707642017-06-09 13:35:13 +0200182 // Connect to Salt master
183 def saltMaster
Ales Komarek24097402017-05-22 16:28:10 +0200184 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Tomáš Kukrál20707642017-06-09 13:35:13 +0200185
Ales Komarek24097402017-05-22 16:28:10 +0200186 }
187
188 //
189 // Install
190 //
191
192 if (common.checkContains('STACK_INSTALL', 'core')) {
193 stage('Install core infrastructure') {
194 orchestrate.installFoundationInfra(saltMaster)
195
196 if (common.checkContains('STACK_INSTALL', 'kvm')) {
197 orchestrate.installInfraKvm(saltMaster)
198 orchestrate.installFoundationInfra(saltMaster)
199 }
200
201 orchestrate.validateFoundationInfra(saltMaster)
202 }
203 }
204
205 // install k8s
206 if (common.checkContains('STACK_INSTALL', 'k8s')) {
207 stage('Install Kubernetes infra') {
208 orchestrate.installKubernetesInfra(saltMaster)
209 }
210
211 stage('Install Kubernetes control') {
212
Ales Komarek24097402017-05-22 16:28:10 +0200213 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200214
Ales Komarek24097402017-05-22 16:28:10 +0200215 }
216
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200217 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200218 if (STACK_COMPUTE_COUNT > 0) {
219 if (STACK_TYPE == 'aws') {
220 // configure aws
221 def venv_path = 'aws_venv'
222 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200223
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200224 // get stack info
225 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200226
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200227 //update autoscaling group
228 aws.updateAutoscalingGroup(venv_path, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200229
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200230 // wait for computes to boot up
231 sleep(120)
232 }
233
234 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200235 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200236 }
Ales Komarek24097402017-05-22 16:28:10 +0200237
238 if (common.checkContains('STACK_INSTALL', 'contrail')) {
239 state('Install Contrail for Kubernetes') {
240 orchestrate.installContrailNetwork(saltMaster)
241 orchestrate.installContrailCompute(saltMaster)
242 }
243 }
244 }
245
246 // install openstack
247 if (common.checkContains('STACK_INSTALL', 'openstack')) {
248 // install Infra and control, tests, ...
249
250 stage('Install OpenStack infra') {
251 orchestrate.installOpenstackInfra(saltMaster)
252 }
253
254 stage('Install OpenStack control') {
255 orchestrate.installOpenstackControl(saltMaster)
256 }
257
258 stage('Install OpenStack network') {
259
260 if (common.checkContains('STACK_INSTALL', 'contrail')) {
261 orchestrate.installContrailNetwork(saltMaster)
262 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
263 orchestrate.installOpenstackNetwork(saltMaster)
264 }
265
266 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
267 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
268 }
269
270 stage('Install OpenStack compute') {
271 orchestrate.installOpenstackCompute(saltMaster)
272
273 if (common.checkContains('STACK_INSTALL', 'contrail')) {
274 orchestrate.installContrailCompute(saltMaster)
275 }
276 }
277
278 }
279
280
281 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
282 stage('Install StackLight') {
283 orchestrate.installStacklightControl(saltMaster)
284 orchestrate.installStacklightClient(saltMaster)
285 }
286 }
287
288 //
289 // Test
290 //
291 def artifacts_dir = '_artifacts/'
292
293 if (common.checkContains('STACK_TEST', 'k8s')) {
294 stage('Run k8s bootstrap tests') {
295 def image = 'tomkukral/k8s-scripts'
296 def output_file = image.replaceAll('/', '-') + '.output'
297
298 // run image
299 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
300
301 // collect output
302 sh "mkdir -p ${artifacts_dir}"
303 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
304 writeFile file: "${artifacts_dir}${output_file}", text: file_content
305 sh "cat ${artifacts_dir}${output_file}"
306
307 // collect artifacts
308 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
309 }
310
311 stage('Run k8s conformance e2e tests') {
312 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
313
314 def image = K8S_CONFORMANCE_IMAGE
315 def output_file = image.replaceAll('/', '-') + '.output'
316
317 // run image
318 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
319
320 // collect output
321 sh "mkdir -p ${artifacts_dir}"
322 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
323 writeFile file: "${artifacts_dir}${output_file}", text: file_content
324 sh "cat ${artifacts_dir}${output_file}"
325
326 // collect artifacts
327 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
328 }
329 }
330
331 if (common.checkContains('STACK_TEST', 'openstack')) {
332 stage('Run deployment tests') {
333 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
334 }
335
336 stage('Copy test results to config node') {
337 test.copyTempestResults(saltMaster)
338 }
339 }
340
341 stage('Finalize') {
342 if (STACK_INSTALL != '') {
343 try {
344 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
345 } catch (Exception e) {
346 common.warningMsg('State apply failed but we should continue to run')
347 }
348 }
349 }
350 } catch (Throwable e) {
351 currentBuild.result = 'FAILURE'
352 throw e
353 } finally {
354
355
356 //
357 // Clean
358 //
359
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200360 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200361 // send notification
362 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200363 }
Ales Komarek24097402017-05-22 16:28:10 +0200364
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200365 if (STACK_DELETE.toBoolean() == true) {
366 stage('Trigger cleanup job') {
367 common.errorMsg('Stack cleanup job triggered')
368 build(job: STACK_CLEANUP_JOB, parameters: [
369 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
370 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
371 ])
372 }
373 } else {
374 if (currentBuild.result == 'FAILURE') {
375 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
376 if (SALT_MASTER_URL) {
377 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200378 }
379 }
380 }
381 }
382 }
383}