blob: 8e0b9ca1cde2e1ae2e2098dcefa8bb7003d97a27 [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 }
181
182 //
183 // Connect to Salt master
184 //
185
186 def saltMaster
187 stage('Connect to Salt API') {
188 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
189 }
190
191 //
192 // Install
193 //
194
195 if (common.checkContains('STACK_INSTALL', 'core')) {
196 stage('Install core infrastructure') {
197 orchestrate.installFoundationInfra(saltMaster)
198
199 if (common.checkContains('STACK_INSTALL', 'kvm')) {
200 orchestrate.installInfraKvm(saltMaster)
201 orchestrate.installFoundationInfra(saltMaster)
202 }
203
204 orchestrate.validateFoundationInfra(saltMaster)
205 }
206 }
207
208 // install k8s
209 if (common.checkContains('STACK_INSTALL', 'k8s')) {
210 stage('Install Kubernetes infra') {
211 orchestrate.installKubernetesInfra(saltMaster)
212 }
213
214 stage('Install Kubernetes control') {
215
Ales Komarek24097402017-05-22 16:28:10 +0200216 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200217
Ales Komarek24097402017-05-22 16:28:10 +0200218 }
219
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200220 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200221 if (STACK_COMPUTE_COUNT > 0) {
222 if (STACK_TYPE == 'aws') {
223 // configure aws
224 def venv_path = 'aws_venv'
225 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200226
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200227 // get stack info
228 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200229
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200230 //update autoscaling group
231 aws.updateAutoscalingGroup(venv_path, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200232
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200233 // wait for computes to boot up
234 sleep(120)
235 }
236
237 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200238 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200239 }
Ales Komarek24097402017-05-22 16:28:10 +0200240
241 if (common.checkContains('STACK_INSTALL', 'contrail')) {
242 state('Install Contrail for Kubernetes') {
243 orchestrate.installContrailNetwork(saltMaster)
244 orchestrate.installContrailCompute(saltMaster)
245 }
246 }
247 }
248
249 // install openstack
250 if (common.checkContains('STACK_INSTALL', 'openstack')) {
251 // install Infra and control, tests, ...
252
253 stage('Install OpenStack infra') {
254 orchestrate.installOpenstackInfra(saltMaster)
255 }
256
257 stage('Install OpenStack control') {
258 orchestrate.installOpenstackControl(saltMaster)
259 }
260
261 stage('Install OpenStack network') {
262
263 if (common.checkContains('STACK_INSTALL', 'contrail')) {
264 orchestrate.installContrailNetwork(saltMaster)
265 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
266 orchestrate.installOpenstackNetwork(saltMaster)
267 }
268
269 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
270 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
271 }
272
273 stage('Install OpenStack compute') {
274 orchestrate.installOpenstackCompute(saltMaster)
275
276 if (common.checkContains('STACK_INSTALL', 'contrail')) {
277 orchestrate.installContrailCompute(saltMaster)
278 }
279 }
280
281 }
282
283
284 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
285 stage('Install StackLight') {
286 orchestrate.installStacklightControl(saltMaster)
287 orchestrate.installStacklightClient(saltMaster)
288 }
289 }
290
291 //
292 // Test
293 //
294 def artifacts_dir = '_artifacts/'
295
296 if (common.checkContains('STACK_TEST', 'k8s')) {
297 stage('Run k8s bootstrap tests') {
298 def image = 'tomkukral/k8s-scripts'
299 def output_file = image.replaceAll('/', '-') + '.output'
300
301 // run image
302 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
303
304 // collect output
305 sh "mkdir -p ${artifacts_dir}"
306 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
307 writeFile file: "${artifacts_dir}${output_file}", text: file_content
308 sh "cat ${artifacts_dir}${output_file}"
309
310 // collect artifacts
311 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
312 }
313
314 stage('Run k8s conformance e2e tests') {
315 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
316
317 def image = K8S_CONFORMANCE_IMAGE
318 def output_file = image.replaceAll('/', '-') + '.output'
319
320 // run image
321 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
322
323 // collect output
324 sh "mkdir -p ${artifacts_dir}"
325 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
326 writeFile file: "${artifacts_dir}${output_file}", text: file_content
327 sh "cat ${artifacts_dir}${output_file}"
328
329 // collect artifacts
330 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
331 }
332 }
333
334 if (common.checkContains('STACK_TEST', 'openstack')) {
335 stage('Run deployment tests') {
336 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
337 }
338
339 stage('Copy test results to config node') {
340 test.copyTempestResults(saltMaster)
341 }
342 }
343
344 stage('Finalize') {
345 if (STACK_INSTALL != '') {
346 try {
347 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
348 } catch (Exception e) {
349 common.warningMsg('State apply failed but we should continue to run')
350 }
351 }
352 }
353 } catch (Throwable e) {
354 currentBuild.result = 'FAILURE'
355 throw e
356 } finally {
357
358
359 //
360 // Clean
361 //
362
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200363 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200364 // send notification
365 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200366 }
Ales Komarek24097402017-05-22 16:28:10 +0200367
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200368 if (STACK_DELETE.toBoolean() == true) {
369 stage('Trigger cleanup job') {
370 common.errorMsg('Stack cleanup job triggered')
371 build(job: STACK_CLEANUP_JOB, parameters: [
372 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
373 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
374 ])
375 }
376 } else {
377 if (currentBuild.result == 'FAILURE') {
378 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
379 if (SALT_MASTER_URL) {
380 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200381 }
382 }
383 }
384 }
385 }
386}