blob: cb34ed65de0c215ecdeb03cf251addb2da5cfa3f [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ála1d86b82017-07-10 17:24:01 +020055// Define global variables
Tomáš Kukrál31414a22017-06-09 14:17:53 +020056def saltMaster
Tomáš Kukrála1d86b82017-07-10 17:24:01 +020057def venv
Tomáš Kukrálbc4be252017-06-06 11:28:40 +020058
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020059if (STACK_TYPE == 'aws') {
Tomáš Kukrála1d86b82017-07-10 17:24:01 +020060 def aws_env_vars
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +020061}
62
Ales Komarek24097402017-05-22 16:28:10 +020063timestamps {
64 node {
65 try {
Tomáš Kukrála1d86b82017-07-10 17:24:01 +020066 // Set build-specific variables
67 venv = "${env.WORKSPACE}/venv"
68
Ales Komarek24097402017-05-22 16:28:10 +020069 //
70 // Prepare machines
71 //
72 stage ('Create infrastructure') {
73
74 if (STACK_TYPE == 'heat') {
75 // value defaults
76 def openstackCloud
77 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
Ales Komarek24097402017-05-22 16:28:10 +020078
79 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
80 error("If you want to reuse existing stack you need to provide it's name")
81 }
82
83 if (STACK_REUSE.toBoolean() == false) {
84 // Don't allow to set custom heat stack name
85 wrap([$class: 'BuildUser']) {
86 if (env.BUILD_USER_ID) {
87 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
88 } else {
89 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
90 }
91 currentBuild.description = STACK_NAME
92 }
93 }
94
95 // set description
96 currentBuild.description = "${STACK_NAME}"
97
98 // get templates
99 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
100
101 // create openstack env
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200102 openstack.setupOpenstackVirtualenv(venv, openstackVersion)
Ales Komarek24097402017-05-22 16:28:10 +0200103 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200104 openstack.getKeystoneToken(openstackCloud, venv)
Ales Komarek24097402017-05-22 16:28:10 +0200105 //
106 // Verify possibility of create stack for given user and stack type
107 //
108 wrap([$class: 'BuildUser']) {
109 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200110 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", venv)
Ales Komarek24097402017-05-22 16:28:10 +0200111 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
112 STACK_DELETE = "false"
113 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
114 }
115 }
116 }
117 // launch stack
118 if (STACK_REUSE.toBoolean() == false) {
119 stage('Launch new Heat stack') {
120 // create stack
121 envParams = [
122 'instance_zone': HEAT_STACK_ZONE,
123 'public_net': HEAT_STACK_PUBLIC_NET
124 ]
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200125 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, venv, false)
Ales Komarek24097402017-05-22 16:28:10 +0200126 }
127 }
128
129 // get SALT_MASTER_URL
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200130 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', venv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200131 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200132
133 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200134 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200135
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200136 // set aws_env_vars
137 aws_env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200138
139 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
140 error("If you want to reuse existing stack you need to provide it's name")
141 }
142
143 if (STACK_REUSE.toBoolean() == false) {
144 // Don't allow to set custom stack name
145 wrap([$class: 'BuildUser']) {
146 if (env.BUILD_USER_ID) {
147 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
148 } else {
149 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
150 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200151 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200152
153 // no underscore in STACK_NAME
154 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200155 }
156
157 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200158 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200159
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200160 if (STACK_REUSE.toBoolean() == false) {
161 // get templates
162 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
163
164 // setup environment
Tomáš Kukrál8fa9b672017-07-10 14:22:19 +0200165 aws.setupVirtualEnv(venv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200166
167 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200168 def stack_params = [
169 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
170 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
171 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200172 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200173 aws.createStack(venv, aws_env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200174 }
175
176 // wait for stack to be ready
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200177 aws.waitForStatus(venv, aws_env_vars, STACK_NAME, 'CREATE_COMPLETE')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200178
179 // get outputs
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200180 saltMasterHost = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'SaltMasterIP')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200181 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200182 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200183
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200184 } else {
185 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
186 }
Ales Komarek24097402017-05-22 16:28:10 +0200187
Tomáš Kukrál20707642017-06-09 13:35:13 +0200188 // Connect to Salt master
Ales Komarek24097402017-05-22 16:28:10 +0200189 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
190 }
191
192 //
193 // Install
194 //
195
196 if (common.checkContains('STACK_INSTALL', 'core')) {
197 stage('Install core infrastructure') {
198 orchestrate.installFoundationInfra(saltMaster)
199
200 if (common.checkContains('STACK_INSTALL', 'kvm')) {
201 orchestrate.installInfraKvm(saltMaster)
202 orchestrate.installFoundationInfra(saltMaster)
203 }
204
205 orchestrate.validateFoundationInfra(saltMaster)
206 }
207 }
208
209 // install k8s
210 if (common.checkContains('STACK_INSTALL', 'k8s')) {
211 stage('Install Kubernetes infra') {
Tomáš Kukrál39c96cd2017-07-11 10:14:13 +0200212 if (STACK_TYPE == 'aws') {
213 // configure kubernetes_control_address - save loadbalancer
214 def kubernetes_control_address = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'ControlLoadBalancer')
215 print(kubernetes_control_address)
216 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'reclass.cluster_meta_set', ['kubernetes_control_address', kubernetes_control_address], null, true)
217 }
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200218
Tomáš Kukrál4898be32017-06-19 09:54:15 +0200219 // ensure certificates are generated properly
220 salt.runSaltProcessStep(saltMaster, '*', 'saltutil.refresh_pillar', [], null, true)
221 salt.enforceState(saltMaster, '*', ['salt.minion.cert'], true)
222
Ales Komarek24097402017-05-22 16:28:10 +0200223 orchestrate.installKubernetesInfra(saltMaster)
224 }
225
226 stage('Install Kubernetes control') {
227
Ales Komarek24097402017-05-22 16:28:10 +0200228 orchestrate.installKubernetesControl(saltMaster)
229 }
230
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200231 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200232 if (STACK_COMPUTE_COUNT > 0) {
233 if (STACK_TYPE == 'aws') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200234 // get stack info
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200235 def scaling_group = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200236
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200237 //update autoscaling group
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200238 aws.updateAutoscalingGroup(venv, aws_env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200239
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200240 // wait for computes to boot up
Tomáš Kukrála1d86b82017-07-10 17:24:01 +0200241 aws.waitForAutoscalingInstances(venv, aws_env_vars, scaling_group)
Tomáš Kukrálf31a5912017-06-13 15:47:25 +0200242 sleep(60)
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200243 }
244
245 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200246 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200247 }
Ales Komarek24097402017-05-22 16:28:10 +0200248
249 if (common.checkContains('STACK_INSTALL', 'contrail')) {
Tomáš Kukrál391df0d2017-06-13 10:37:02 +0200250 stage('Install Contrail for Kubernetes') {
Ales Komarek24097402017-05-22 16:28:10 +0200251 orchestrate.installContrailNetwork(saltMaster)
252 orchestrate.installContrailCompute(saltMaster)
253 }
254 }
255 }
256
257 // install openstack
258 if (common.checkContains('STACK_INSTALL', 'openstack')) {
259 // install Infra and control, tests, ...
260
261 stage('Install OpenStack infra') {
262 orchestrate.installOpenstackInfra(saltMaster)
263 }
264
265 stage('Install OpenStack control') {
266 orchestrate.installOpenstackControl(saltMaster)
267 }
268
269 stage('Install OpenStack network') {
270
271 if (common.checkContains('STACK_INSTALL', 'contrail')) {
272 orchestrate.installContrailNetwork(saltMaster)
273 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
274 orchestrate.installOpenstackNetwork(saltMaster)
275 }
276
277 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
278 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
279 }
280
281 stage('Install OpenStack compute') {
282 orchestrate.installOpenstackCompute(saltMaster)
283
284 if (common.checkContains('STACK_INSTALL', 'contrail')) {
285 orchestrate.installContrailCompute(saltMaster)
286 }
287 }
288
289 }
290
291
292 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
293 stage('Install StackLight') {
294 orchestrate.installStacklightControl(saltMaster)
295 orchestrate.installStacklightClient(saltMaster)
296 }
297 }
298
299 //
300 // Test
301 //
302 def artifacts_dir = '_artifacts/'
303
304 if (common.checkContains('STACK_TEST', 'k8s')) {
305 stage('Run k8s bootstrap tests') {
306 def image = 'tomkukral/k8s-scripts'
307 def output_file = image.replaceAll('/', '-') + '.output'
308
309 // run image
310 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
311
312 // collect output
313 sh "mkdir -p ${artifacts_dir}"
314 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
315 writeFile file: "${artifacts_dir}${output_file}", text: file_content
316 sh "cat ${artifacts_dir}${output_file}"
317
318 // collect artifacts
319 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
320 }
321
322 stage('Run k8s conformance e2e tests') {
323 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
324
325 def image = K8S_CONFORMANCE_IMAGE
326 def output_file = image.replaceAll('/', '-') + '.output'
327
328 // run image
329 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
330
331 // collect output
332 sh "mkdir -p ${artifacts_dir}"
333 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
334 writeFile file: "${artifacts_dir}${output_file}", text: file_content
335 sh "cat ${artifacts_dir}${output_file}"
336
337 // collect artifacts
338 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
339 }
340 }
341
342 if (common.checkContains('STACK_TEST', 'openstack')) {
343 stage('Run deployment tests') {
344 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
345 }
346
347 stage('Copy test results to config node') {
348 test.copyTempestResults(saltMaster)
349 }
350 }
351
352 stage('Finalize') {
353 if (STACK_INSTALL != '') {
354 try {
355 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
356 } catch (Exception e) {
357 common.warningMsg('State apply failed but we should continue to run')
358 }
359 }
360 }
361 } catch (Throwable e) {
362 currentBuild.result = 'FAILURE'
363 throw e
364 } finally {
365
366
367 //
368 // Clean
369 //
370
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200371 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200372 // send notification
373 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200374 }
Ales Komarek24097402017-05-22 16:28:10 +0200375
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200376 if (STACK_DELETE.toBoolean() == true) {
377 stage('Trigger cleanup job') {
378 common.errorMsg('Stack cleanup job triggered')
379 build(job: STACK_CLEANUP_JOB, parameters: [
380 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
381 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
382 ])
383 }
384 } else {
385 if (currentBuild.result == 'FAILURE') {
386 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
387 if (SALT_MASTER_URL) {
388 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200389 }
390 }
391 }
392 }
393 }
394}