blob: 5812b43e813ed84cf9749fa89719049a2ba7fc7f [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ál31414a22017-06-09 14:17:53 +020055def saltMaster
Tomáš Kukrálbc4be252017-06-06 11:28:40 +020056
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +020057venv = "${env.WORKSPACE}/venv"
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020058if (STACK_TYPE == 'aws') {
Tomáš Kukrál0ecd76a2017-06-13 17:28:09 +020059 env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020060}
61
Ales Komarek24097402017-05-22 16:28:10 +020062timestamps {
63 node {
64 try {
65 //
66 // Prepare machines
67 //
68 stage ('Create infrastructure') {
69
70 if (STACK_TYPE == 'heat') {
71 // value defaults
72 def openstackCloud
73 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
Ales Komarek24097402017-05-22 16:28:10 +020074
75 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
76 error("If you want to reuse existing stack you need to provide it's name")
77 }
78
79 if (STACK_REUSE.toBoolean() == false) {
80 // Don't allow to set custom heat stack name
81 wrap([$class: 'BuildUser']) {
82 if (env.BUILD_USER_ID) {
83 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
84 } else {
85 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
86 }
87 currentBuild.description = STACK_NAME
88 }
89 }
90
91 // set description
92 currentBuild.description = "${STACK_NAME}"
93
94 // get templates
95 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
96
97 // create openstack env
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +020098 openstack.setupOpenstackVirtualenv(venv, openstackVersion)
Ales Komarek24097402017-05-22 16:28:10 +020099 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200100 openstack.getKeystoneToken(openstackCloud, venv)
Ales Komarek24097402017-05-22 16:28:10 +0200101 //
102 // Verify possibility of create stack for given user and stack type
103 //
104 wrap([$class: 'BuildUser']) {
105 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200106 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", venv)
Ales Komarek24097402017-05-22 16:28:10 +0200107 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
108 STACK_DELETE = "false"
109 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
110 }
111 }
112 }
113 // launch stack
114 if (STACK_REUSE.toBoolean() == false) {
115 stage('Launch new Heat stack') {
116 // create stack
117 envParams = [
118 'instance_zone': HEAT_STACK_ZONE,
119 'public_net': HEAT_STACK_PUBLIC_NET
120 ]
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200121 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, venv, false)
Ales Komarek24097402017-05-22 16:28:10 +0200122 }
123 }
124
125 // get SALT_MASTER_URL
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200126 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', venv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200127 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200128
129 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200130 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200131
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200132
133 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
134 error("If you want to reuse existing stack you need to provide it's name")
135 }
136
137 if (STACK_REUSE.toBoolean() == false) {
138 // Don't allow to set custom stack name
139 wrap([$class: 'BuildUser']) {
140 if (env.BUILD_USER_ID) {
141 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
142 } else {
143 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
144 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200145 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200146
147 // no underscore in STACK_NAME
148 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200149 }
150
151 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200152 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200153
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200154 if (STACK_REUSE.toBoolean() == false) {
155 // get templates
156 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
157
158 // setup environment
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200159 aws.setupVirtualEnv(venv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200160
161 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200162 def stack_params = [
163 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
164 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
165 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200166 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200167 aws.createStack(venv, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200168 }
169
170 // wait for stack to be ready
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200171 aws.waitForStatus(venv, env_vars, STACK_NAME, 'CREATE_COMPLETE')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200172
173 // get outputs
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200174 saltMasterHost = aws.getOutputs(venv, env_vars, STACK_NAME, 'SaltMasterIP')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200175 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200176 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200177
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200178 } else {
179 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
180 }
Ales Komarek24097402017-05-22 16:28:10 +0200181
Tomáš Kukrál20707642017-06-09 13:35:13 +0200182 // Connect to Salt master
Ales Komarek24097402017-05-22 16:28:10 +0200183 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
184 }
185
186 //
187 // Install
188 //
189
190 if (common.checkContains('STACK_INSTALL', 'core')) {
191 stage('Install core infrastructure') {
192 orchestrate.installFoundationInfra(saltMaster)
193
194 if (common.checkContains('STACK_INSTALL', 'kvm')) {
195 orchestrate.installInfraKvm(saltMaster)
196 orchestrate.installFoundationInfra(saltMaster)
197 }
198
199 orchestrate.validateFoundationInfra(saltMaster)
200 }
201 }
202
203 // install k8s
204 if (common.checkContains('STACK_INSTALL', 'k8s')) {
205 stage('Install Kubernetes infra') {
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200206 // configure kubernetes_control_address - save loadbalancer
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200207 def kubernetes_control_address = aws.getOutputs(venv, env_vars, STACK_NAME, 'ControlLoadBalancer')
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200208 print(kubernetes_control_address)
Tomáš Kukrálc4662612017-06-14 13:46:29 +0200209 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 +0200210
Tomáš Kukrál4898be32017-06-19 09:54:15 +0200211 // ensure certificates are generated properly
212 salt.runSaltProcessStep(saltMaster, '*', 'saltutil.refresh_pillar', [], null, true)
213 salt.enforceState(saltMaster, '*', ['salt.minion.cert'], true)
214
Ales Komarek24097402017-05-22 16:28:10 +0200215 orchestrate.installKubernetesInfra(saltMaster)
216 }
217
218 stage('Install Kubernetes control') {
219
Ales Komarek24097402017-05-22 16:28:10 +0200220 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200221
Ales Komarek24097402017-05-22 16:28:10 +0200222 }
223
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200224 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200225 if (STACK_COMPUTE_COUNT > 0) {
226 if (STACK_TYPE == 'aws') {
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200227
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200228 // get stack info
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200229 def scaling_group = aws.getOutputs(venv, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200230
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200231 //update autoscaling group
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200232 aws.updateAutoscalingGroup(venv, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200233
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200234 // wait for computes to boot up
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200235 aws.waitForAutoscalingInstances(venv, env_vars, scaling_group)
Tomáš Kukrálf31a5912017-06-13 15:47:25 +0200236 sleep(60)
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200237 }
238
239 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200240 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200241 }
Ales Komarek24097402017-05-22 16:28:10 +0200242
243 if (common.checkContains('STACK_INSTALL', 'contrail')) {
Tomáš Kukrál391df0d2017-06-13 10:37:02 +0200244 stage('Install Contrail for Kubernetes') {
Ales Komarek24097402017-05-22 16:28:10 +0200245 orchestrate.installContrailNetwork(saltMaster)
246 orchestrate.installContrailCompute(saltMaster)
247 }
248 }
249 }
250
251 // install openstack
252 if (common.checkContains('STACK_INSTALL', 'openstack')) {
253 // install Infra and control, tests, ...
254
255 stage('Install OpenStack infra') {
256 orchestrate.installOpenstackInfra(saltMaster)
257 }
258
259 stage('Install OpenStack control') {
260 orchestrate.installOpenstackControl(saltMaster)
261 }
262
263 stage('Install OpenStack network') {
264
265 if (common.checkContains('STACK_INSTALL', 'contrail')) {
266 orchestrate.installContrailNetwork(saltMaster)
267 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
268 orchestrate.installOpenstackNetwork(saltMaster)
269 }
270
271 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
272 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
273 }
274
275 stage('Install OpenStack compute') {
276 orchestrate.installOpenstackCompute(saltMaster)
277
278 if (common.checkContains('STACK_INSTALL', 'contrail')) {
279 orchestrate.installContrailCompute(saltMaster)
280 }
281 }
282
283 }
284
285
286 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
287 stage('Install StackLight') {
288 orchestrate.installStacklightControl(saltMaster)
289 orchestrate.installStacklightClient(saltMaster)
290 }
291 }
292
293 //
294 // Test
295 //
296 def artifacts_dir = '_artifacts/'
297
298 if (common.checkContains('STACK_TEST', 'k8s')) {
299 stage('Run k8s bootstrap tests') {
300 def image = 'tomkukral/k8s-scripts'
301 def output_file = image.replaceAll('/', '-') + '.output'
302
303 // run image
304 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
305
306 // collect output
307 sh "mkdir -p ${artifacts_dir}"
308 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
309 writeFile file: "${artifacts_dir}${output_file}", text: file_content
310 sh "cat ${artifacts_dir}${output_file}"
311
312 // collect artifacts
313 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
314 }
315
316 stage('Run k8s conformance e2e tests') {
317 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
318
319 def image = K8S_CONFORMANCE_IMAGE
320 def output_file = image.replaceAll('/', '-') + '.output'
321
322 // run image
323 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
324
325 // collect output
326 sh "mkdir -p ${artifacts_dir}"
327 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
328 writeFile file: "${artifacts_dir}${output_file}", text: file_content
329 sh "cat ${artifacts_dir}${output_file}"
330
331 // collect artifacts
332 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
333 }
334 }
335
336 if (common.checkContains('STACK_TEST', 'openstack')) {
337 stage('Run deployment tests') {
338 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
339 }
340
341 stage('Copy test results to config node') {
342 test.copyTempestResults(saltMaster)
343 }
344 }
345
346 stage('Finalize') {
347 if (STACK_INSTALL != '') {
348 try {
349 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
350 } catch (Exception e) {
351 common.warningMsg('State apply failed but we should continue to run')
352 }
353 }
354 }
355 } catch (Throwable e) {
356 currentBuild.result = 'FAILURE'
357 throw e
358 } finally {
359
360
361 //
362 // Clean
363 //
364
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200365 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200366 // send notification
367 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200368 }
Ales Komarek24097402017-05-22 16:28:10 +0200369
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200370 if (STACK_DELETE.toBoolean() == true) {
371 stage('Trigger cleanup job') {
372 common.errorMsg('Stack cleanup job triggered')
373 build(job: STACK_CLEANUP_JOB, parameters: [
374 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
375 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
376 ])
377 }
378 } else {
379 if (currentBuild.result == 'FAILURE') {
380 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
381 if (SALT_MASTER_URL) {
382 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200383 }
384 }
385 }
386 }
387 }
388}