blob: 6f4dfae131fcdf625dc66aa537b38ca96ae07ec6 [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 *
20 * AWS_STACK_REGION CloudFormation AWS region
21 * AWS_API_CREDENTIALS AWS Access key ID with AWS secret access key
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020022 * AWS_SSH_KEY AWS key pair name (used for SSH access)
Ales Komarek24097402017-05-22 16:28:10 +020023 *
24 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
25 * HEAT_STACK_ZONE Heat stack availability zone
26 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
27 * OPENSTACK_API_URL OpenStack API address
28 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
29 * OPENSTACK_API_PROJECT OpenStack project to connect to
30 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
31 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
32 *
33 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
34 * SALT_MASTER_URL URL of Salt master
35 *
36 * K8S_API_SERVER Kubernetes API address
37 * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests
38 *
39 * TEMPEST_IMAGE_LINK Tempest image link
40 *
41 */
42common = new com.mirantis.mk.Common()
43git = new com.mirantis.mk.Git()
44openstack = new com.mirantis.mk.Openstack()
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +020045aws = new com.mirantis.mk.Aws()
Ales Komarek24097402017-05-22 16:28:10 +020046orchestrate = new com.mirantis.mk.Orchestrate()
47salt = new com.mirantis.mk.Salt()
48test = new com.mirantis.mk.Test()
49
50_MAX_PERMITTED_STACKS = 2
51overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml"
52
53timestamps {
54 node {
55 try {
56 //
57 // Prepare machines
58 //
59 stage ('Create infrastructure') {
60
61 if (STACK_TYPE == 'heat') {
62 // value defaults
63 def openstackCloud
64 def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
65 def openstackEnv = "${env.WORKSPACE}/venv"
66
67 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
68 error("If you want to reuse existing stack you need to provide it's name")
69 }
70
71 if (STACK_REUSE.toBoolean() == false) {
72 // Don't allow to set custom heat stack name
73 wrap([$class: 'BuildUser']) {
74 if (env.BUILD_USER_ID) {
75 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
76 } else {
77 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
78 }
79 currentBuild.description = STACK_NAME
80 }
81 }
82
83 // set description
84 currentBuild.description = "${STACK_NAME}"
85
86 // get templates
87 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
88
89 // create openstack env
90 openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
91 openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT)
92 openstack.getKeystoneToken(openstackCloud, openstackEnv)
93 //
94 // Verify possibility of create stack for given user and stack type
95 //
96 wrap([$class: 'BuildUser']) {
97 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) {
98 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
99 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
100 STACK_DELETE = "false"
101 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
102 }
103 }
104 }
105 // launch stack
106 if (STACK_REUSE.toBoolean() == false) {
107 stage('Launch new Heat stack') {
108 // create stack
109 envParams = [
110 'instance_zone': HEAT_STACK_ZONE,
111 'public_net': HEAT_STACK_PUBLIC_NET
112 ]
Ales Komarek5eb8b102017-05-24 14:00:22 +0200113 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
Ales Komarek24097402017-05-22 16:28:10 +0200114 }
115 }
116
117 // get SALT_MASTER_URL
118 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200119 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200120
121 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200122 } else if (STACK_TYPE == 'aws') {
Ales Komarek24097402017-05-22 16:28:10 +0200123
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200124 def venv_path = 'aws_venv'
125
126 if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') {
127 error("If you want to reuse existing stack you need to provide it's name")
128 }
129
130 if (STACK_REUSE.toBoolean() == false) {
131 // Don't allow to set custom stack name
132 wrap([$class: 'BuildUser']) {
133 if (env.BUILD_USER_ID) {
134 STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
135 } else {
136 STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
137 }
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200138 }
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200139
140 // no underscore in STACK_NAME
141 STACK_NAME = STACK_NAME.replaceAll('_', '-')
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200142 }
143
144 // set description
Tomáš Kukráldbf8ca82017-05-25 15:13:20 +0200145 currentBuild.description = STACK_NAME
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200146
147 // prepare configuration
148 def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION)
149
150 if (STACK_REUSE.toBoolean() == false) {
151 // get templates
152 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
153
154 // setup environment
155 aws.setupVirtualEnv(venv_path)
156
157 // start stack
Tomáš Kukrálad79d322017-06-05 15:40:14 +0200158 def stack_params = [
159 "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY,
160 "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT
161 ]
Tomáš Kukráldbb58402017-06-02 07:08:53 +0200162 def template_file = 'cfn/' + STACK_TEMPLATE + '.yml'
Tomáš Kukráld4c0fed2017-06-01 17:20:11 +0200163 aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params)
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200164 }
165
166 // wait for stack to be ready
167 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
168
169 // get outputs
170 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
171 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200172 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200173
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200174 } else {
175 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
176 }
Ales Komarek24097402017-05-22 16:28:10 +0200177 }
178
179 //
180 // Connect to Salt master
181 //
182
183 def saltMaster
184 stage('Connect to Salt API') {
185 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
186 }
187
188 //
189 // Install
190 //
191
192 if (common.checkContains('STACK_INSTALL', 'core')) {
193 stage('Install core infrastructure') {
194 orchestrate.installFoundationInfra(saltMaster)
195
196 if (common.checkContains('STACK_INSTALL', 'kvm')) {
197 orchestrate.installInfraKvm(saltMaster)
198 orchestrate.installFoundationInfra(saltMaster)
199 }
200
201 orchestrate.validateFoundationInfra(saltMaster)
202 }
203 }
204
205 // install k8s
206 if (common.checkContains('STACK_INSTALL', 'k8s')) {
207 stage('Install Kubernetes infra') {
208 orchestrate.installKubernetesInfra(saltMaster)
209 }
210
211 stage('Install Kubernetes control') {
212
213 // Overwrite Kubernetes vars if specified
214 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
215 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
216 }
217
218 orchestrate.installKubernetesControl(saltMaster)
219 }
220
221
222 if (common.checkContains('STACK_INSTALL', 'contrail')) {
223 state('Install Contrail for Kubernetes') {
224 orchestrate.installContrailNetwork(saltMaster)
225 orchestrate.installContrailCompute(saltMaster)
226 }
227 }
228 }
229
230 // install openstack
231 if (common.checkContains('STACK_INSTALL', 'openstack')) {
232 // install Infra and control, tests, ...
233
234 stage('Install OpenStack infra') {
235 orchestrate.installOpenstackInfra(saltMaster)
236 }
237
238 stage('Install OpenStack control') {
239 orchestrate.installOpenstackControl(saltMaster)
240 }
241
242 stage('Install OpenStack network') {
243
244 if (common.checkContains('STACK_INSTALL', 'contrail')) {
245 orchestrate.installContrailNetwork(saltMaster)
246 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
247 orchestrate.installOpenstackNetwork(saltMaster)
248 }
249
250 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
251 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
252 }
253
254 stage('Install OpenStack compute') {
255 orchestrate.installOpenstackCompute(saltMaster)
256
257 if (common.checkContains('STACK_INSTALL', 'contrail')) {
258 orchestrate.installContrailCompute(saltMaster)
259 }
260 }
261
262 }
263
264
265 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
266 stage('Install StackLight') {
267 orchestrate.installStacklightControl(saltMaster)
268 orchestrate.installStacklightClient(saltMaster)
269 }
270 }
271
272 //
273 // Test
274 //
275 def artifacts_dir = '_artifacts/'
276
277 if (common.checkContains('STACK_TEST', 'k8s')) {
278 stage('Run k8s bootstrap tests') {
279 def image = 'tomkukral/k8s-scripts'
280 def output_file = image.replaceAll('/', '-') + '.output'
281
282 // run image
283 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
284
285 // collect output
286 sh "mkdir -p ${artifacts_dir}"
287 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
288 writeFile file: "${artifacts_dir}${output_file}", text: file_content
289 sh "cat ${artifacts_dir}${output_file}"
290
291 // collect artifacts
292 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
293 }
294
295 stage('Run k8s conformance e2e tests') {
296 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
297
298 def image = K8S_CONFORMANCE_IMAGE
299 def output_file = image.replaceAll('/', '-') + '.output'
300
301 // run image
302 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
303
304 // collect output
305 sh "mkdir -p ${artifacts_dir}"
306 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
307 writeFile file: "${artifacts_dir}${output_file}", text: file_content
308 sh "cat ${artifacts_dir}${output_file}"
309
310 // collect artifacts
311 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
312 }
313 }
314
315 if (common.checkContains('STACK_TEST', 'openstack')) {
316 stage('Run deployment tests') {
317 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
318 }
319
320 stage('Copy test results to config node') {
321 test.copyTempestResults(saltMaster)
322 }
323 }
324
325 stage('Finalize') {
326 if (STACK_INSTALL != '') {
327 try {
328 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
329 } catch (Exception e) {
330 common.warningMsg('State apply failed but we should continue to run')
331 }
332 }
333 }
334 } catch (Throwable e) {
335 currentBuild.result = 'FAILURE'
336 throw e
337 } finally {
338
339
340 //
341 // Clean
342 //
343
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200344 if (STACK_NAME && STACK_NAME != '') {
Ales Komarek24097402017-05-22 16:28:10 +0200345 // send notification
346 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200347 }
Ales Komarek24097402017-05-22 16:28:10 +0200348
Tomáš Kukrál439c62e2017-06-02 11:42:36 +0200349 if (STACK_DELETE.toBoolean() == true) {
350 stage('Trigger cleanup job') {
351 common.errorMsg('Stack cleanup job triggered')
352 build(job: STACK_CLEANUP_JOB, parameters: [
353 [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME],
354 [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE]
355 ])
356 }
357 } else {
358 if (currentBuild.result == 'FAILURE') {
359 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
360 if (SALT_MASTER_URL) {
361 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
Ales Komarek24097402017-05-22 16:28:10 +0200362 }
363 }
364 }
365 }
366 }
367}