blob: b00c01876e192c0b0029212bbe2d2bac98d25df0 [file] [log] [blame]
Tomáš Kukrálb9957b32017-02-28 14:49:00 +01001/**
2 *
3 * Launch heat stack with basic k8s
4 * Flow parameters:
5 * STACK_TYPE Orchestration engine: heat, ''
6 * INSTALL What should be installed (k8s, openstack, ...)
Tomáš Kukrál55357d02017-02-28 22:56:45 +01007 * TEST What should be tested (k8s, openstack, ...)
Tomáš Kukrálb9957b32017-02-28 14:49:00 +01008 *
9 * Expected parameters:
10 *
11 * required for STACK_TYPE=heat
12 * HEAT_TEMPLATE_URL URL to git repo with Heat templates
13 * HEAT_TEMPLATE_CREDENTIALS Credentials to the Heat templates repo
14 * HEAT_TEMPLATE_BRANCH Heat templates repo branch
15 * HEAT_STACK_TEMPLATE Heat stack HOT template
16 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
17 * HEAT_STACK_ZONE Heat stack availability zone
18 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
19 * HEAT_STACK_DELETE Delete Heat stack when finished (bool)
20 * HEAT_STACK_CLEANUP_JOB Name of job for deleting Heat stack
21 * HEAT_STACK_REUSE Reuse Heat stack (don't create one)
22 * OPENSTACK_API_URL OpenStack API address
23 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
24 * OPENSTACK_API_PROJECT OpenStack project to connect to
25 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
26 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
27 *
28 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
29 *
30 * required for STACK_TYPE=NONE or empty string
31 * SALT_MASTER_URL URL of Salt-API
32
33 * K8S_API_SERVER Kubernetes API address
34 * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests
35 *
Victor Ryzhenkin5f3b7e62017-03-09 16:02:58 +040036 * TEMPEST_IMAGE_LINK Tempest image link
37 *
Matthew Mosesohna85f24e2017-04-28 13:45:19 +030038 * optional parameters for overwriting soft params
39 * KUBERNETES_HYPERKUBE_IMAGE Docker repository and tag for hyperkube image
40 *
Tomáš Kukrálb9957b32017-02-28 14:49:00 +010041 */
42
Tomáš Kukrál1f8b5012017-04-28 21:07:10 +020043common = new com.mirantis.mk.Common()
Tomáš Kukrálb9957b32017-02-28 14:49:00 +010044git = new com.mirantis.mk.Git()
45openstack = new com.mirantis.mk.Openstack()
Tomáš Kukrál1f8b5012017-04-28 21:07:10 +020046orchestrate = new com.mirantis.mk.Orchestrate()
Tomáš Kukrálb9957b32017-02-28 14:49:00 +010047salt = new com.mirantis.mk.Salt()
Ondrej Smola8f35e482017-03-30 14:04:36 +020048test = new com.mirantis.mk.Test()
Tomáš Kukrál1f8b5012017-04-28 21:07:10 +020049
Jakub Josef458913d2017-05-10 15:37:56 +020050_MAX_PERMITTED_STACKS = 2
Matthew Mosesohna85f24e2017-04-28 13:45:19 +030051overwriteFile = "/srv/salt/reclass/classes/cluster/overwrite.yml"
Tomáš Kukrále80680a2017-03-02 16:34:35 +010052
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +010053timestamps {
54 node {
Tomáš Kukrála18de112017-03-02 13:57:47 +010055 try {
56 //
57 // Prepare machines
58 //
Tomáš Kukrála18de112017-03-02 13:57:47 +010059 stage ('Create infrastructure') {
Tomáš Kukrál6d627d62017-03-23 17:39:07 +010060
Tomáš Kukrála18de112017-03-02 13:57:47 +010061 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"
Tomáš Kukrálb9957b32017-02-28 14:49:00 +010066
Filip Pytloun794ad952017-03-03 10:39:26 +010067 if (HEAT_STACK_REUSE.toBoolean() == true && HEAT_STACK_NAME == '') {
68 error("If you want to reuse existing stack you need to provide it's name")
69 }
70
71 if (HEAT_STACK_REUSE.toBoolean() == false) {
72 // Don't allow to set custom heat stack name
73 wrap([$class: 'BuildUser']) {
Tomáš Kukrál24d7fe62017-03-03 10:57:11 +010074 if (env.BUILD_USER_ID) {
75 HEAT_STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}"
76 } else {
77 HEAT_STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}"
78 }
Filip Pytloun794ad952017-03-03 10:39:26 +010079 currentBuild.description = HEAT_STACK_NAME
80 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +010081 }
Tomáš Kukrála18de112017-03-02 13:57:47 +010082
Tomáš Kukrálcbabec42017-03-02 16:24:04 +010083 // set description
Tomáš Kukrálaeb5c922017-03-02 17:00:48 +010084 currentBuild.description = "${HEAT_STACK_NAME}"
Tomáš Kukrálcbabec42017-03-02 16:24:04 +010085
Tomáš Kukrála18de112017-03-02 13:57:47 +010086 // get templates
87 git.checkoutGitRepository('template', HEAT_TEMPLATE_URL, HEAT_TEMPLATE_BRANCH, HEAT_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)
Jakub Josef458913d2017-05-10 15:37:56 +020093 //
94 // Verify possibility of create stack for given user and stack type
95 //
96 wrap([$class: 'BuildUser']) {
97 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
98 if(existingStacks.size() > _MAX_PERMITTED_STACKS){
99 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
100 }
101 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100102 // launch stack
Filip Pytloun794ad952017-03-03 10:39:26 +0100103 if (HEAT_STACK_REUSE.toBoolean() == false) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100104 stage('Launch new Heat stack') {
105 // create stack
106 envParams = [
107 'instance_zone': HEAT_STACK_ZONE,
108 'public_net': HEAT_STACK_PUBLIC_NET
109 ]
110 openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
111 }
112 }
113
114 // get SALT_MASTER_URL
115 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrálaeb5c922017-03-02 17:00:48 +0100116 currentBuild.description = "${HEAT_STACK_NAME}: ${saltMasterHost}"
Tomáš Kukrál615aa9c2017-03-04 15:29:08 +0100117
Ales Komarek47a29f12017-04-26 12:05:47 +0200118 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100119 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100120 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100121
Tomáš Kukrála18de112017-03-02 13:57:47 +0100122 //
123 // Connect to Salt master
124 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100125
Tomáš Kukrála18de112017-03-02 13:57:47 +0100126 def saltMaster
127 stage('Connect to Salt API') {
128 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
129 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100130
Tomáš Kukrála18de112017-03-02 13:57:47 +0100131 //
132 // Install
133 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100134
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100135 if (common.checkContains('INSTALL', 'core')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100136 stage('Install core infrastructure') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200137 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100138
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100139 if (common.checkContains('INSTALL', 'kvm')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200140 orchestrate.installInfraKvm(saltMaster)
141 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100142 }
143
Ales Komarek47a29f12017-04-26 12:05:47 +0200144 orchestrate.validateFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100145 }
146 }
147
148 // install k8s
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100149 if (common.checkContains('INSTALL', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100150 stage('Install Kubernetes infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200151 orchestrate.installKubernetesInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100152 }
153
Tomáš Kukrála18de112017-03-02 13:57:47 +0100154 stage('Install Kubernetes control') {
Tomáš Kukrál06c27a92017-03-01 16:21:46 +0100155
Ales Komarek47a29f12017-04-26 12:05:47 +0200156 // Overwrite Kubernetes vars if specified
157 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200158 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
Ales Komarek47a29f12017-04-26 12:05:47 +0200159 }
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100160
Ales Komarek47a29f12017-04-26 12:05:47 +0200161 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100162 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100163 }
164
Tomáš Kukrála18de112017-03-02 13:57:47 +0100165 // install openstack
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100166 if (common.checkContains('INSTALL', 'openstack')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100167 // install Infra and control, tests, ...
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100168
Tomáš Kukrála18de112017-03-02 13:57:47 +0100169 stage('Install OpenStack infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200170 orchestrate.installOpenstackInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100171 }
172
Tomáš Kukrála18de112017-03-02 13:57:47 +0100173 stage('Install OpenStack control') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200174 orchestrate.installOpenstackControl(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100175 }
176
177 stage('Install OpenStack network') {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100178
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100179 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200180 orchestrate.installContrailNetwork(saltMaster)
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100181 } else if (common.checkContains('INSTALL', 'ovs')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200182 orchestrate.installOpenstackNetwork(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100183 }
184
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200185 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
186 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
Tomáš Kukrála18de112017-03-02 13:57:47 +0100187 }
188
189 stage('Install OpenStack compute') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200190 orchestrate.installOpenstackCompute(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100191
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100192 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200193 orchestrate.installContrailCompute(saltMaster)
Tomáš Kukráld5a40af2017-03-07 15:34:58 +0100194 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100195 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100196
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100197 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100198
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100199
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100200 if (common.checkContains('INSTALL', 'stacklight')) {
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100201 stage('Install StackLight') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200202 orchestrate.installStacklightControl(saltMaster)
203 orchestrate.installStacklightClient(saltMaster)
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100204 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100205 }
206
Tomáš Kukrála18de112017-03-02 13:57:47 +0100207 //
208 // Test
209 //
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200210 def artifacts_dir = '_artifacts/'
Tomáš Kukrála18de112017-03-02 13:57:47 +0100211
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100212 if (common.checkContains('TEST', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100213 stage('Run k8s bootstrap tests') {
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200214 def image = 'tomkukral/k8s-scripts'
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200215 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200216
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200217 // run image
218 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200219
220 // collect output
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200221 sh "mkdir -p ${artifacts_dir}"
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200222 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200223 writeFile file: "${artifacts_dir}${output_file}", text: file_content
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200224 sh "cat ${artifacts_dir}${output_file}"
225
226 // collect artifacts
227 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
Tomáš Kukrála18de112017-03-02 13:57:47 +0100228 }
229
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200230 stage('Run k8s conformance e2e tests') {
231 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
232
233 def image = K8S_CONFORMANCE_IMAGE
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200234 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200235
236 // run image
237 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
238
239 // collect output
240 sh "mkdir -p ${artifacts_dir}"
241 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
242 writeFile file: "${artifacts_dir}${output_file}", text: file_content
243 sh "cat ${artifacts_dir}${output_file}"
244
245 // collect artifacts
246 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
247 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100248 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100249
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100250 if (common.checkContains('TEST', 'openstack')) {
Victor Ryzhenkin5f3b7e62017-03-09 16:02:58 +0400251 stage('Run OpenStack tests') {
252 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
253 }
254
255 stage('Copy Tempest results to config node') {
256 test.copyTempestResults(saltMaster)
257 }
258 }
259
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100260 stage('Finalize') {
Tomáš Kukrál38ab6bb2017-04-21 21:29:31 +0200261 if (INSTALL != '') {
262 try {
263 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
264 } catch (Exception e) {
265 common.warningMsg('State apply failed but we should continue to run')
266 }
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100267 }
268 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100269 } catch (Throwable e) {
270 currentBuild.result = 'FAILURE'
271 throw e
Tomáš Kukrál65fb5fd2017-03-02 14:56:49 +0100272 } finally {
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100273
Tomáš Kukrál96b12d52017-03-07 22:43:37 +0100274
Tomáš Kukrála18de112017-03-02 13:57:47 +0100275 //
276 // Clean
277 //
278
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100279 if (STACK_TYPE == 'heat') {
Tomáš Kukrál4507c6a2017-03-23 19:00:39 +0100280 // send notification
281 common.sendNotification(currentBuild.result, HEAT_STACK_NAME, ["slack"])
282
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100283 if (HEAT_STACK_DELETE.toBoolean() == true) {
284 common.errorMsg('Heat job cleanup triggered')
285 stage('Trigger cleanup job') {
286 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]]
287 }
288 } else {
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100289 if (currentBuild.result == 'FAILURE') {
290 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100291
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100292 if (SALT_MASTER_URL) {
293 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
294 }
Tomáš Kukrálb908dac2017-03-07 18:58:35 +0100295 }
296
Tomáš Kukrála18de112017-03-02 13:57:47 +0100297 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100298 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100299 }
300 }
301}