blob: 69f75d05d0defcd509bebbb2ebb9e6c36ea3875c [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
Matthew Mosesohn13933442017-07-04 21:20:08 +030040 * SALT_OVERRIDES YAML with overrides for Salt deployment
Ales Komarek24097402017-05-22 16:28:10 +020041 *
42 * TEMPEST_IMAGE_LINK Tempest image link
43 *
44 */
45common = new com.mirantis.mk.Common()
46git = new com.mirantis.mk.Git()
47openstack = new com.mirantis.mk.Openstack()
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020048aws = new com.mirantis.mk.Aws()
Ales Komarek24097402017-05-22 16:28:10 +020049orchestrate = new com.mirantis.mk.Orchestrate()
50salt = new com.mirantis.mk.Salt()
51test = new com.mirantis.mk.Test()
52
53_MAX_PERMITTED_STACKS = 2
54overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml"
55
Tomáš Kukrál31414a22017-06-09 14:17:53 +020056def saltMaster
Tomáš Kukrálbc4be252017-06-06 11:28:40 +020057
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020058if (STACK_TYPE == 'aws') {
Tomáš Kukrál0ecd76a2017-06-13 17:28:09 +020059 venv_path = 'aws_venv'
60 env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020061}
62
Ales Komarek24097402017-05-22 16:28:10 +020063timestamps {
64 node {
65 try {
66 //
67 // Prepare machines
68 //
69 stage ('Create infrastructure') {
70
71 if (STACK_TYPE == 'heat') {
72 // value defaults
73 def openstackCloud
74 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
75 def openstackEnv = "${env.WORKSPACE}/venv"
76
77 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
78 error("If you want to reuse existing stack you need to provide it's name")
79 }
80
81 if (STACK_REUSE.toBoolean() == false) {
82 // Don't allow to set custom heat stack name
83 wrap([$class: 'BuildUser']) {
84 if (env.BUILD_USER_ID) {
85 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
86 } else {
87 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
88 }
89 currentBuild.description = STACK_NAME
90 }
91 }
92
93 // set description
94 currentBuild.description = "${STACK_NAME}"
95
96 // get templates
97 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
98
99 // create openstack env
100 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
101 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
102 openstack.getKeystoneToken(openstackCloud, openstackEnv)
103 //
104 // Verify possibility of create stack for given user and stack type
105 //
106 wrap([$class: 'BuildUser']) {
107 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
108 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
109 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
110 STACK_DELETE = "false"
111 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
112 }
113 }
114 }
115 // launch stack
116 if (STACK_REUSE.toBoolean() == false) {
117 stage('Launch new Heat stack') {
118 // create stack
119 envParams = [
120 'instance_zone': HEAT_STACK_ZONE,
121 'public_net': HEAT_STACK_PUBLIC_NET
122 ]
Ales Komareke0999ae2017-06-27 11:26:10 +0200123 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv, false)
Ales Komarek24097402017-05-22 16:28:10 +0200124 }
125 }
126
127 // get SALT_MASTER_URL
128 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200129 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200130
131 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200132 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200133
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200134
135 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
136 error("If you want to reuse existing stack you need to provide it's name")
137 }
138
139 if (STACK_REUSE.toBoolean() == false) {
140 // Don't allow to set custom stack name
141 wrap([$class: 'BuildUser']) {
142 if (env.BUILD_USER_ID) {
143 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
144 } else {
145 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
146 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200147 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200148
149 // no underscore in STACK_NAME
150 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200151 }
152
153 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200154 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200155
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200156 if (STACK_REUSE.toBoolean() == false) {
157 // get templates
158 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
159
160 // setup environment
161 aws.setupVirtualEnv(venv_path)
162
163 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200164 def stack_params = [
165 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
166 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
167 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200168 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukráld4c0fed2017-06-01 17:20:11 +0200169 aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200170 }
171
172 // wait for stack to be ready
173 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
174
175 // get outputs
176 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
177 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200178 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200179
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200180 } else {
181 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
182 }
Ales Komarek24097402017-05-22 16:28:10 +0200183
Tomáš Kukrál20707642017-06-09 13:35:13 +0200184 // Connect to Salt master
Ales Komarek24097402017-05-22 16:28:10 +0200185 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
186 }
187
Matthew Mosesohn13933442017-07-04 21:20:08 +0300188 // Set up override params
189 if (env.getEnvironment().containsKey('SALT_OVERRIDES')) {
190 stage('Set Salt overrides') {
191 salt.setSaltOverrides(saltMaster, SALT_OVERRIDES)
192 }
193 }
194
Ales Komarek24097402017-05-22 16:28:10 +0200195 //
196 // Install
197 //
198
199 if (common.checkContains('STACK_INSTALL', 'core')) {
200 stage('Install core infrastructure') {
201 orchestrate.installFoundationInfra(saltMaster)
202
203 if (common.checkContains('STACK_INSTALL', 'kvm')) {
204 orchestrate.installInfraKvm(saltMaster)
205 orchestrate.installFoundationInfra(saltMaster)
206 }
207
208 orchestrate.validateFoundationInfra(saltMaster)
209 }
210 }
211
212 // install k8s
213 if (common.checkContains('STACK_INSTALL', 'k8s')) {
214 stage('Install Kubernetes infra') {
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200215 // configure kubernetes_control_address - save loadbalancer
216 def kubernetes_control_address = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ControlLoadBalancer')
217 print(kubernetes_control_address)
Tomáš Kukrálc4662612017-06-14 13:46:29 +0200218 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'reclass.cluster_meta_set', ['kubernetes_control_address', kubernetes_control_address], null, true)
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200219
Tomáš Kukrál4898be32017-06-19 09:54:15 +0200220 // ensure certificates are generated properly
221 salt.runSaltProcessStep(saltMaster, '*', 'saltutil.refresh_pillar', [], null, true)
222 salt.enforceState(saltMaster, '*', ['salt.minion.cert'], true)
223
Ales Komarek24097402017-05-22 16:28:10 +0200224 orchestrate.installKubernetesInfra(saltMaster)
225 }
226
227 stage('Install Kubernetes control') {
228
Ales Komarek24097402017-05-22 16:28:10 +0200229 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200230
Ales Komarek24097402017-05-22 16:28:10 +0200231 }
232
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200233 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200234 if (STACK_COMPUTE_COUNT > 0) {
235 if (STACK_TYPE == 'aws') {
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200236
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200237 // get stack info
238 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200239
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200240 //update autoscaling group
241 aws.updateAutoscalingGroup(venv_path, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200242
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200243 // wait for computes to boot up
Tomáš Kukráledc1d8d2017-06-09 13:44:13 +0200244 aws.waitForAutoscalingInstances(venv_path, env_vars, scaling_group)
Tomáš Kukrálf31a5912017-06-13 15:47:25 +0200245 sleep(60)
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200246 }
247
248 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200249 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200250 }
Ales Komarek24097402017-05-22 16:28:10 +0200251
252 if (common.checkContains('STACK_INSTALL', 'contrail')) {
Tomáš Kukrál391df0d2017-06-13 10:37:02 +0200253 stage('Install Contrail for Kubernetes') {
Ales Komarek24097402017-05-22 16:28:10 +0200254 orchestrate.installContrailNetwork(saltMaster)
255 orchestrate.installContrailCompute(saltMaster)
256 }
257 }
258 }
259
260 // install openstack
261 if (common.checkContains('STACK_INSTALL', 'openstack')) {
262 // install Infra and control, tests, ...
263
264 stage('Install OpenStack infra') {
265 orchestrate.installOpenstackInfra(saltMaster)
266 }
267
268 stage('Install OpenStack control') {
269 orchestrate.installOpenstackControl(saltMaster)
270 }
271
272 stage('Install OpenStack network') {
273
274 if (common.checkContains('STACK_INSTALL', 'contrail')) {
275 orchestrate.installContrailNetwork(saltMaster)
276 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
277 orchestrate.installOpenstackNetwork(saltMaster)
278 }
279
280 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
281 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
282 }
283
284 stage('Install OpenStack compute') {
285 orchestrate.installOpenstackCompute(saltMaster)
286
287 if (common.checkContains('STACK_INSTALL', 'contrail')) {
288 orchestrate.installContrailCompute(saltMaster)
289 }
290 }
291
292 }
293
294
295 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
296 stage('Install StackLight') {
297 orchestrate.installStacklightControl(saltMaster)
298 orchestrate.installStacklightClient(saltMaster)
299 }
300 }
301
302 //
303 // Test
304 //
305 def artifacts_dir = '_artifacts/'
306
307 if (common.checkContains('STACK_TEST', 'k8s')) {
308 stage('Run k8s bootstrap tests') {
309 def image = 'tomkukral/k8s-scripts'
310 def output_file = image.replaceAll('/', '-') + '.output'
311
312 // run image
313 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
314
315 // collect output
316 sh "mkdir -p ${artifacts_dir}"
317 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
318 writeFile file: "${artifacts_dir}${output_file}", text: file_content
319 sh "cat ${artifacts_dir}${output_file}"
320
321 // collect artifacts
322 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
323 }
324
325 stage('Run k8s conformance e2e tests') {
326 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
327
328 def image = K8S_CONFORMANCE_IMAGE
329 def output_file = image.replaceAll('/', '-') + '.output'
330
331 // run image
332 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
333
334 // collect output
335 sh "mkdir -p ${artifacts_dir}"
336 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
337 writeFile file: "${artifacts_dir}${output_file}", text: file_content
338 sh "cat ${artifacts_dir}${output_file}"
339
340 // collect artifacts
341 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
342 }
343 }
344
345 if (common.checkContains('STACK_TEST', 'openstack')) {
346 stage('Run deployment tests') {
347 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
348 }
349
350 stage('Copy test results to config node') {
351 test.copyTempestResults(saltMaster)
352 }
353 }
354
355 stage('Finalize') {
356 if (STACK_INSTALL != '') {
357 try {
358 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
359 } catch (Exception e) {
360 common.warningMsg('State apply failed but we should continue to run')
361 }
362 }
363 }
364 } catch (Throwable e) {
365 currentBuild.result = 'FAILURE'
366 throw e
367 } finally {
368
369
370 //
371 // Clean
372 //
373
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200374 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200375 // send notification
376 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200377 }
Ales Komarek24097402017-05-22 16:28:10 +0200378
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200379 if (STACK_DELETE.toBoolean() == true) {
380 stage('Trigger cleanup job') {
381 common.errorMsg('Stack cleanup job triggered')
382 build(job: STACK_CLEANUP_JOB, parameters: [
383 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
384 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
385 ])
386 }
387 } else {
388 if (currentBuild.result == 'FAILURE') {
389 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
390 if (SALT_MASTER_URL) {
391 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200392 }
393 }
394 }
395 }
396 }
397}