blob: b34db68319d68ba9590d5cac2ffc95f4b0d168cd [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
158 def stack_params = ["ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY]
159 aws.createStack(venv_path, env_vars, STACK_TEMPLATE, STACK_NAME, stack_params)
160 }
161
162 // wait for stack to be ready
163 aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE')
164
165 // get outputs
166 saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP')
167 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
Ales Komarek24097402017-05-22 16:28:10 +0200168 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Ales Komarek24097402017-05-22 16:28:10 +0200169
Tomáš Kukrál7a8a4fa2017-05-25 14:54:24 +0200170 } else {
171 throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported")
172 }
Ales Komarek24097402017-05-22 16:28:10 +0200173 }
174
175 //
176 // Connect to Salt master
177 //
178
179 def saltMaster
180 stage('Connect to Salt API') {
181 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
182 }
183
184 //
185 // Install
186 //
187
188 if (common.checkContains('STACK_INSTALL', 'core')) {
189 stage('Install core infrastructure') {
190 orchestrate.installFoundationInfra(saltMaster)
191
192 if (common.checkContains('STACK_INSTALL', 'kvm')) {
193 orchestrate.installInfraKvm(saltMaster)
194 orchestrate.installFoundationInfra(saltMaster)
195 }
196
197 orchestrate.validateFoundationInfra(saltMaster)
198 }
199 }
200
201 // install k8s
202 if (common.checkContains('STACK_INSTALL', 'k8s')) {
203 stage('Install Kubernetes infra') {
204 orchestrate.installKubernetesInfra(saltMaster)
205 }
206
207 stage('Install Kubernetes control') {
208
209 // Overwrite Kubernetes vars if specified
210 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
211 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
212 }
213
214 orchestrate.installKubernetesControl(saltMaster)
215 }
216
217
218 if (common.checkContains('STACK_INSTALL', 'contrail')) {
219 state('Install Contrail for Kubernetes') {
220 orchestrate.installContrailNetwork(saltMaster)
221 orchestrate.installContrailCompute(saltMaster)
222 }
223 }
224 }
225
226 // install openstack
227 if (common.checkContains('STACK_INSTALL', 'openstack')) {
228 // install Infra and control, tests, ...
229
230 stage('Install OpenStack infra') {
231 orchestrate.installOpenstackInfra(saltMaster)
232 }
233
234 stage('Install OpenStack control') {
235 orchestrate.installOpenstackControl(saltMaster)
236 }
237
238 stage('Install OpenStack network') {
239
240 if (common.checkContains('STACK_INSTALL', 'contrail')) {
241 orchestrate.installContrailNetwork(saltMaster)
242 } else if (common.checkContains('STACK_INSTALL', 'ovs')) {
243 orchestrate.installOpenstackNetwork(saltMaster)
244 }
245
246 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
247 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
248 }
249
250 stage('Install OpenStack compute') {
251 orchestrate.installOpenstackCompute(saltMaster)
252
253 if (common.checkContains('STACK_INSTALL', 'contrail')) {
254 orchestrate.installContrailCompute(saltMaster)
255 }
256 }
257
258 }
259
260
261 if (common.checkContains('STACK_INSTALL', 'stacklight')) {
262 stage('Install StackLight') {
263 orchestrate.installStacklightControl(saltMaster)
264 orchestrate.installStacklightClient(saltMaster)
265 }
266 }
267
268 //
269 // Test
270 //
271 def artifacts_dir = '_artifacts/'
272
273 if (common.checkContains('STACK_TEST', 'k8s')) {
274 stage('Run k8s bootstrap tests') {
275 def image = 'tomkukral/k8s-scripts'
276 def output_file = image.replaceAll('/', '-') + '.output'
277
278 // run image
279 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
280
281 // collect output
282 sh "mkdir -p ${artifacts_dir}"
283 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
284 writeFile file: "${artifacts_dir}${output_file}", text: file_content
285 sh "cat ${artifacts_dir}${output_file}"
286
287 // collect artifacts
288 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
289 }
290
291 stage('Run k8s conformance e2e tests') {
292 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
293
294 def image = K8S_CONFORMANCE_IMAGE
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
311 if (common.checkContains('STACK_TEST', 'openstack')) {
312 stage('Run deployment tests') {
313 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
314 }
315
316 stage('Copy test results to config node') {
317 test.copyTempestResults(saltMaster)
318 }
319 }
320
321 stage('Finalize') {
322 if (STACK_INSTALL != '') {
323 try {
324 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
325 } catch (Exception e) {
326 common.warningMsg('State apply failed but we should continue to run')
327 }
328 }
329 }
330 } catch (Throwable e) {
331 currentBuild.result = 'FAILURE'
332 throw e
333 } finally {
334
335
336 //
337 // Clean
338 //
339
340 if (STACK_TYPE == 'heat') {
341 // send notification
342 common.sendNotification(currentBuild.result, STACK_NAME, ["slack"])
343
344 if (STACK_DELETE.toBoolean() == true) {
345 common.errorMsg('Heat job cleanup triggered')
346 stage('Trigger cleanup job') {
Ales Komarek5eb8b102017-05-24 14:00:22 +0200347 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME]]
Ales Komarek24097402017-05-22 16:28:10 +0200348 }
349 } else {
350 if (currentBuild.result == 'FAILURE') {
351 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
352 if (SALT_MASTER_URL) {
353 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
354 }
355 }
356 }
357 }
358 }
359 }
360}