blob: 0b181f91c431d04f6798632097b01e921cd9b790 [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álb19bdda2017-06-13 17:09:43 +020057if (STACK_TYPE == 'aws') {
58 def venv_path = 'aws_venv'
59 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
60}
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'
74 def openstackEnv = "${env.WORKSPACE}/venv"
75
76 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
77 error("If you want to reuse existing stack you need to provide it's name")
78 }
79
80 if (STACK_REUSE.toBoolean() == false) {
81 // Don't allow to set custom heat stack name
82 wrap([$class: 'BuildUser']) {
83 if (env.BUILD_USER_ID) {
84 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
85 } else {
86 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
87 }
88 currentBuild.description = STACK_NAME
89 }
90 }
91
92 // set description
93 currentBuild.description = "${STACK_NAME}"
94
95 // get templates
96 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
97
98 // create openstack env
99 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
100 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
101 openstack.getKeystoneToken(openstackCloud, openstackEnv)
102 //
103 // Verify possibility of create stack for given user and stack type
104 //
105 wrap([$class: 'BuildUser']) {
106 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
107 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
108 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
109 STACK_DELETE = "false"
110 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
111 }
112 }
113 }
114 // launch stack
115 if (STACK_REUSE.toBoolean() == false) {
116 stage('Launch new Heat stack') {
117 // create stack
118 envParams = [
119 'instance_zone': HEAT_STACK_ZONE,
120 'public_net': HEAT_STACK_PUBLIC_NET
121 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200122 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200123 }
124 }
125
126 // get SALT_MASTER_URL
127 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200128 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200129
130 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200131 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200132
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200133
134 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
135 error("If you want to reuse existing stack you need to provide it's name")
136 }
137
138 if (STACK_REUSE.toBoolean() == false) {
139 // Don't allow to set custom stack name
140 wrap([$class: 'BuildUser']) {
141 if (env.BUILD_USER_ID) {
142 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
143 } else {
144 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
145 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200146 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200147
148 // no underscore in STACK_NAME
149 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200150 }
151
152 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200153 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200154
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200155 if (STACK_REUSE.toBoolean() == false) {
156 // get templates
157 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
158
159 // setup environment
160 aws.setupVirtualEnv(venv_path)
161
162 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200163 def stack_params = [
164 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
165 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
166 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200167 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukráld4c0fed2017-06-01 17:20:11 +0200168 aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200169 }
170
171 // wait for stack to be ready
172 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
173
174 // get outputs
175 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
176 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200177 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200178
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200179 } else {
180 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
181 }
Ales Komarek24097402017-05-22 16:28:10 +0200182
Tomáš Kukrál20707642017-06-09 13:35:13 +0200183 // Connect to Salt master
Ales Komarek24097402017-05-22 16:28:10 +0200184 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
185 }
186
187 //
188 // Install
189 //
190
191 if (common.checkContains('STACK_INSTALL', 'core')) {
192 stage('Install core infrastructure') {
193 orchestrate.installFoundationInfra(saltMaster)
194
195 if (common.checkContains('STACK_INSTALL', 'kvm')) {
196 orchestrate.installInfraKvm(saltMaster)
197 orchestrate.installFoundationInfra(saltMaster)
198 }
199
200 orchestrate.validateFoundationInfra(saltMaster)
201 }
202 }
203
204 // install k8s
205 if (common.checkContains('STACK_INSTALL', 'k8s')) {
206 stage('Install Kubernetes infra') {
Tomáš Kukrálb19bdda2017-06-13 17:09:43 +0200207 // configure kubernetes_control_address - save loadbalancer
208 def kubernetes_control_address = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ControlLoadBalancer')
209 print(kubernetes_control_address)
210 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'reclass.cluster_meta_set', 'kubernetes_control_address', kubernetes_control_address)
211
Ales Komarek24097402017-05-22 16:28:10 +0200212 orchestrate.installKubernetesInfra(saltMaster)
213 }
214
215 stage('Install Kubernetes control') {
216
Ales Komarek24097402017-05-22 16:28:10 +0200217 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200218
Ales Komarek24097402017-05-22 16:28:10 +0200219 }
220
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200221 stage('Scale Kubernetes computes') {
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200222 if (STACK_COMPUTE_COUNT > 0) {
223 if (STACK_TYPE == 'aws') {
Tomáš Kukrálfa0c7452017-06-06 11:44:11 +0200224
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200225 // get stack info
226 def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup')
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200227
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200228 //update autoscaling group
229 aws.updateAutoscalingGroup(venv_path, env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
Tomáš Kukrál8130a562017-06-06 11:09:22 +0200230
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200231 // wait for computes to boot up
Tomáš Kukráledc1d8d2017-06-09 13:44:13 +0200232 aws.waitForAutoscalingInstances(venv_path, env_vars, scaling_group)
Tomáš Kukrálf31a5912017-06-13 15:47:25 +0200233 sleep(60)
Tomáš Kukrálb9a7d6f2017-06-07 14:05:31 +0200234 }
235
236 orchestrate.installKubernetesCompute(saltMaster)
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200237 }
Tomáš Kukrál74f92862017-06-05 19:18:43 +0200238 }
Ales Komarek24097402017-05-22 16:28:10 +0200239
240 if (common.checkContains('STACK_INSTALL', 'contrail')) {
Tomáš Kukrál391df0d2017-06-13 10:37:02 +0200241 stage('Install Contrail for Kubernetes') {
Ales Komarek24097402017-05-22 16:28:10 +0200242 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}