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