blob: ddfe37afbb51e9fd9b623891317c0d9e8b896ad9 [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
Tomáš Kukrálab2f3702017-05-11 09:17:43 +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']) {
Tomáš Kukrálab2f3702017-05-11 09:17:43 +020097 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !HEAT_STACK_REUSE.toBoolean()) {
Jakub Josef78c3f8b2017-05-10 15:45:29 +020098 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
99 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
Jakub Josef124403a2017-05-10 15:58:06 +0200100 HEAT_STACK_DELETE = "false"
Jakub Josef78c3f8b2017-05-10 15:45:29 +0200101 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
102 }
Jakub Josef458913d2017-05-10 15:37:56 +0200103 }
104 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100105 // launch stack
Filip Pytloun794ad952017-03-03 10:39:26 +0100106 if (HEAT_STACK_REUSE.toBoolean() == false) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100107 stage('Launch new Heat stack') {
108 // create stack
109 envParams = [
110 'instance_zone': HEAT_STACK_ZONE,
111 'public_net': HEAT_STACK_PUBLIC_NET
112 ]
113 openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
114 }
115 }
116
117 // get SALT_MASTER_URL
118 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrálaeb5c922017-03-02 17:00:48 +0100119 currentBuild.description = "${HEAT_STACK_NAME}: ${saltMasterHost}"
Tomáš Kukrál615aa9c2017-03-04 15:29:08 +0100120
Ales Komarek47a29f12017-04-26 12:05:47 +0200121 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100122 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100123 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100124
Tomáš Kukrála18de112017-03-02 13:57:47 +0100125 //
126 // Connect to Salt master
127 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100128
Tomáš Kukrála18de112017-03-02 13:57:47 +0100129 def saltMaster
130 stage('Connect to Salt API') {
131 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
132 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100133
Tomáš Kukrála18de112017-03-02 13:57:47 +0100134 //
135 // Install
136 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100137
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100138 if (common.checkContains('INSTALL', 'core')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100139 stage('Install core infrastructure') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200140 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100141
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100142 if (common.checkContains('INSTALL', 'kvm')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200143 orchestrate.installInfraKvm(saltMaster)
144 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100145 }
146
Ales Komarek47a29f12017-04-26 12:05:47 +0200147 orchestrate.validateFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100148 }
149 }
150
151 // install k8s
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100152 if (common.checkContains('INSTALL', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100153 stage('Install Kubernetes infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200154 orchestrate.installKubernetesInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100155 }
156
Tomáš Kukrála18de112017-03-02 13:57:47 +0100157 stage('Install Kubernetes control') {
Tomáš Kukrál06c27a92017-03-01 16:21:46 +0100158
Ales Komarek47a29f12017-04-26 12:05:47 +0200159 // Overwrite Kubernetes vars if specified
160 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200161 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
Ales Komarek47a29f12017-04-26 12:05:47 +0200162 }
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100163
Ales Komarek47a29f12017-04-26 12:05:47 +0200164 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100165 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100166 }
167
Tomáš Kukrála18de112017-03-02 13:57:47 +0100168 // install openstack
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100169 if (common.checkContains('INSTALL', 'openstack')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100170 // install Infra and control, tests, ...
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100171
Tomáš Kukrála18de112017-03-02 13:57:47 +0100172 stage('Install OpenStack infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200173 orchestrate.installOpenstackInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100174 }
175
Tomáš Kukrála18de112017-03-02 13:57:47 +0100176 stage('Install OpenStack control') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200177 orchestrate.installOpenstackControl(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100178 }
179
180 stage('Install OpenStack network') {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100181
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100182 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200183 orchestrate.installContrailNetwork(saltMaster)
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100184 } else if (common.checkContains('INSTALL', 'ovs')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200185 orchestrate.installOpenstackNetwork(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100186 }
187
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200188 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
189 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
Tomáš Kukrála18de112017-03-02 13:57:47 +0100190 }
191
192 stage('Install OpenStack compute') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200193 orchestrate.installOpenstackCompute(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100194
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100195 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200196 orchestrate.installContrailCompute(saltMaster)
Tomáš Kukráld5a40af2017-03-07 15:34:58 +0100197 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100198 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100199
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100200 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100201
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100202
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100203 if (common.checkContains('INSTALL', 'stacklight')) {
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100204 stage('Install StackLight') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200205 orchestrate.installStacklightControl(saltMaster)
206 orchestrate.installStacklightClient(saltMaster)
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100207 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100208 }
209
Tomáš Kukrála18de112017-03-02 13:57:47 +0100210 //
211 // Test
212 //
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200213 def artifacts_dir = '_artifacts/'
Tomáš Kukrála18de112017-03-02 13:57:47 +0100214
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100215 if (common.checkContains('TEST', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100216 stage('Run k8s bootstrap tests') {
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200217 def image = 'tomkukral/k8s-scripts'
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200218 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200219
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200220 // run image
221 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200222
223 // collect output
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200224 sh "mkdir -p ${artifacts_dir}"
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200225 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200226 writeFile file: "${artifacts_dir}${output_file}", text: file_content
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200227 sh "cat ${artifacts_dir}${output_file}"
228
229 // collect artifacts
230 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
Tomáš Kukrála18de112017-03-02 13:57:47 +0100231 }
232
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200233 stage('Run k8s conformance e2e tests') {
234 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
235
236 def image = K8S_CONFORMANCE_IMAGE
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200237 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200238
239 // run image
240 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
241
242 // collect output
243 sh "mkdir -p ${artifacts_dir}"
244 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
245 writeFile file: "${artifacts_dir}${output_file}", text: file_content
246 sh "cat ${artifacts_dir}${output_file}"
247
248 // collect artifacts
249 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
250 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100251 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100252
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100253 if (common.checkContains('TEST', 'openstack')) {
Victor Ryzhenkin5f3b7e62017-03-09 16:02:58 +0400254 stage('Run OpenStack tests') {
255 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
256 }
257
258 stage('Copy Tempest results to config node') {
259 test.copyTempestResults(saltMaster)
260 }
261 }
262
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100263 stage('Finalize') {
Tomáš Kukrál38ab6bb2017-04-21 21:29:31 +0200264 if (INSTALL != '') {
265 try {
266 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
267 } catch (Exception e) {
268 common.warningMsg('State apply failed but we should continue to run')
269 }
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100270 }
271 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100272 } catch (Throwable e) {
273 currentBuild.result = 'FAILURE'
274 throw e
Tomáš Kukrál65fb5fd2017-03-02 14:56:49 +0100275 } finally {
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100276
Tomáš Kukrál96b12d52017-03-07 22:43:37 +0100277
Tomáš Kukrála18de112017-03-02 13:57:47 +0100278 //
279 // Clean
280 //
281
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100282 if (STACK_TYPE == 'heat') {
Tomáš Kukrál4507c6a2017-03-23 19:00:39 +0100283 // send notification
284 common.sendNotification(currentBuild.result, HEAT_STACK_NAME, ["slack"])
285
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100286 if (HEAT_STACK_DELETE.toBoolean() == true) {
287 common.errorMsg('Heat job cleanup triggered')
288 stage('Trigger cleanup job') {
289 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]]
290 }
291 } else {
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100292 if (currentBuild.result == 'FAILURE') {
293 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 +0100294
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100295 if (SALT_MASTER_URL) {
296 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
297 }
Tomáš Kukrálb908dac2017-03-07 18:58:35 +0100298 }
299
Tomáš Kukrála18de112017-03-02 13:57:47 +0100300 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100301 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100302 }
303 }
304}