blob: eacfd7dbf59106287c60a7d04a699cd3af63cf1c [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
Ales Komarek24097402017-05-22 16:28:10 +020057timestamps {
58 node {
59 try {
60 //
61 // Prepare machines
62 //
63 stage ('Create infrastructure') {
64
65 if (STACK_TYPE == 'heat') {
66 // value defaults
67 def openstackCloud
68 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
69 def openstackEnv = "${env.WORKSPACE}/venv"
70
71 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
72 error("If you want to reuse existing stack you need to provide it's name")
73 }
74
75 if (STACK_REUSE.toBoolean() == false) {
76 // Don't allow to set custom heat stack name
77 wrap([$class: 'BuildUser']) {
78 if (env.BUILD_USER_ID) {
79 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
80 } else {
81 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
82 }
83 currentBuild.description = STACK_NAME
84 }
85 }
86
87 // set description
88 currentBuild.description = "${STACK_NAME}"
89
90 // get templates
91 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
92
93 // create openstack env
94 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
95 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
96 openstack.getKeystoneToken(openstackCloud, openstackEnv)
97 //
98 // Verify possibility of create stack for given user and stack type
99 //
100 wrap([$class: 'BuildUser']) {
101 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
102 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
103 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
104 STACK_DELETE = "false"
105 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
106 }
107 }
108 }
109 // launch stack
110 if (STACK_REUSE.toBoolean() == false) {
111 stage('Launch new Heat stack') {
112 // create stack
113 envParams = [
114 'instance_zone': HEAT_STACK_ZONE,
115 'public_net': HEAT_STACK_PUBLIC_NET
116 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200117 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200118 }
119 }
120
121 // get SALT_MASTER_URL
122 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200123 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200124
125 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200126 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200127
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200128
129 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
130 error("If you want to reuse existing stack you need to provide it's name")
131 }
132
133 if (STACK_REUSE.toBoolean() == false) {
134 // Don't allow to set custom stack name
135 wrap([$class: 'BuildUser']) {
136 if (env.BUILD_USER_ID) {
137 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
138 } else {
139 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
140 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200141 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200142
143 // no underscore in STACK_NAME
144 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200145 }
146
147 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200148 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200149
150 // prepare configuration
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200151 def venv_path = 'aws_venv'
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200152 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
153
154 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
159 aws.setupVirtualEnv(venv_path)
160
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áld4c0fed2017-06-01 17:20:11 +0200167 aws.createStack(venv_path, 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
171 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
172
173 // get outputs
174 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
175 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') {
206 orchestrate.installKubernetesInfra(saltMaster)
207 }
208
209 stage('Install Kubernetes control') {
210
Ales Komarek24097402017-05-22 16:28:10 +0200211 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200212
Ales Komarek24097402017-05-22 16:28:10 +0200213 }
214
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200215 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200216 if (STACK_COMPUTE_COUNT > 0) {
217 if (STACK_TYPE == 'aws') {
218 // configure aws
219 def venv_path = 'aws_venv'
220 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200221
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200222 // get stack info
223 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200224
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200225 //update autoscaling group
226 aws.updateAutoscalingGroup(venv_path, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200227
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200228 // wait for computes to boot up
Tomáš Kukráledc1d8d2017-06-09 13:44:13 +0200229 aws.waitForAutoscalingInstances(venv_path, env_vars, scaling_group)
Tomáš Kukrálf31a5912017-06-13 15:47:25 +0200230 sleep(60)
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200231 }
232
233 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200234 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200235 }
Ales Komarek24097402017-05-22 16:28:10 +0200236
237 if (common.checkContains('STACK_INSTALL', 'contrail')) {
Tomáš Kukrál391df0d2017-06-13 10:37:02 +0200238 stage('Install Contrail for Kubernetes') {
Ales Komarek24097402017-05-22 16:28:10 +0200239 orchestrate.installContrailNetwork(saltMaster)
240 orchestrate.installContrailCompute(saltMaster)
241 }
242 }
243 }
244
245 // install openstack
246 if (common.checkContains('STACK_INSTALL', 'openstack')) {
247 // install Infra and control, tests, ...
248
249 stage('Install OpenStack infra') {
250 orchestrate.installOpenstackInfra(saltMaster)
251 }
252
253 stage('Install OpenStack control') {
254 orchestrate.installOpenstackControl(saltMaster)
255 }
256
257 stage('Install OpenStack network') {
258
259 if (common.checkContains('STACK_INSTALL', 'contrail')) {
260 orchestrate.installContrailNetwork(saltMaster)
261 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
262 orchestrate.installOpenstackNetwork(saltMaster)
263 }
264
265 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
266 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
267 }
268
269 stage('Install OpenStack compute') {
270 orchestrate.installOpenstackCompute(saltMaster)
271
272 if (common.checkContains('STACK_INSTALL', 'contrail')) {
273 orchestrate.installContrailCompute(saltMaster)
274 }
275 }
276
277 }
278
279
280 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
281 stage('Install StackLight') {
282 orchestrate.installStacklightControl(saltMaster)
283 orchestrate.installStacklightClient(saltMaster)
284 }
285 }
286
287 //
288 // Test
289 //
290 def artifacts_dir = '_artifacts/'
291
292 if (common.checkContains('STACK_TEST', 'k8s')) {
293 stage('Run k8s bootstrap tests') {
294 def image = 'tomkukral/k8s-scripts'
295 def output_file = image.replaceAll('/', '-') + '.output'
296
297 // run image
298 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
299
300 // collect output
301 sh "mkdir -p ${artifacts_dir}"
302 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
303 writeFile file: "${artifacts_dir}${output_file}", text: file_content
304 sh "cat ${artifacts_dir}${output_file}"
305
306 // collect artifacts
307 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
308 }
309
310 stage('Run k8s conformance e2e tests') {
311 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
312
313 def image = K8S_CONFORMANCE_IMAGE
314 def output_file = image.replaceAll('/', '-') + '.output'
315
316 // run image
317 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
318
319 // collect output
320 sh "mkdir -p ${artifacts_dir}"
321 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
322 writeFile file: "${artifacts_dir}${output_file}", text: file_content
323 sh "cat ${artifacts_dir}${output_file}"
324
325 // collect artifacts
326 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
327 }
328 }
329
330 if (common.checkContains('STACK_TEST', 'openstack')) {
331 stage('Run deployment tests') {
332 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
333 }
334
335 stage('Copy test results to config node') {
336 test.copyTempestResults(saltMaster)
337 }
338 }
339
340 stage('Finalize') {
341 if (STACK_INSTALL != '') {
342 try {
343 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
344 } catch (Exception e) {
345 common.warningMsg('State apply failed but we should continue to run')
346 }
347 }
348 }
349 } catch (Throwable e) {
350 currentBuild.result = 'FAILURE'
351 throw e
352 } finally {
353
354
355 //
356 // Clean
357 //
358
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200359 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200360 // send notification
361 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200362 }
Ales Komarek24097402017-05-22 16:28:10 +0200363
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200364 if (STACK_DELETE.toBoolean() == true) {
365 stage('Trigger cleanup job') {
366 common.errorMsg('Stack cleanup job triggered')
367 build(job: STACK_CLEANUP_JOB, parameters: [
368 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
369 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
370 ])
371 }
372 } else {
373 if (currentBuild.result == 'FAILURE') {
374 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
375 if (SALT_MASTER_URL) {
376 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200377 }
378 }
379 }
380 }
381 }
382}