blob: c488210bd7c4e453bf7d861bab09f3d4a71a72e0 [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']) {
Jakub Josef78c3f8b2017-05-10 15:45:29 +020097 if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins")) {
98 def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv)
99 if(existingStacks.size() >= _MAX_PERMITTED_STACKS){
100 throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}")
101 }
Jakub Josef458913d2017-05-10 15:37:56 +0200102 }
103 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100104 // launch stack
Filip Pytloun794ad952017-03-03 10:39:26 +0100105 if (HEAT_STACK_REUSE.toBoolean() == false) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100106 stage('Launch new Heat stack') {
107 // create stack
108 envParams = [
109 'instance_zone': HEAT_STACK_ZONE,
110 'public_net': HEAT_STACK_PUBLIC_NET
111 ]
112 openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv)
113 }
114 }
115
116 // get SALT_MASTER_URL
117 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv)
Tomáš Kukrálaeb5c922017-03-02 17:00:48 +0100118 currentBuild.description = "${HEAT_STACK_NAME}: ${saltMasterHost}"
Tomáš Kukrál615aa9c2017-03-04 15:29:08 +0100119
Ales Komarek47a29f12017-04-26 12:05:47 +0200120 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100121 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100122 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100123
Tomáš Kukrála18de112017-03-02 13:57:47 +0100124 //
125 // Connect to Salt master
126 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100127
Tomáš Kukrála18de112017-03-02 13:57:47 +0100128 def saltMaster
129 stage('Connect to Salt API') {
130 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
131 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100132
Tomáš Kukrála18de112017-03-02 13:57:47 +0100133 //
134 // Install
135 //
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100136
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100137 if (common.checkContains('INSTALL', 'core')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100138 stage('Install core infrastructure') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200139 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100140
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100141 if (common.checkContains('INSTALL', 'kvm')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200142 orchestrate.installInfraKvm(saltMaster)
143 orchestrate.installFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100144 }
145
Ales Komarek47a29f12017-04-26 12:05:47 +0200146 orchestrate.validateFoundationInfra(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100147 }
148 }
149
150 // install k8s
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100151 if (common.checkContains('INSTALL', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100152 stage('Install Kubernetes infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200153 orchestrate.installKubernetesInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100154 }
155
Tomáš Kukrála18de112017-03-02 13:57:47 +0100156 stage('Install Kubernetes control') {
Tomáš Kukrál06c27a92017-03-01 16:21:46 +0100157
Ales Komarek47a29f12017-04-26 12:05:47 +0200158 // Overwrite Kubernetes vars if specified
159 if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) {
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200160 salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}")
Ales Komarek47a29f12017-04-26 12:05:47 +0200161 }
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100162
Ales Komarek47a29f12017-04-26 12:05:47 +0200163 orchestrate.installKubernetesControl(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100164 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100165 }
166
Tomáš Kukrála18de112017-03-02 13:57:47 +0100167 // install openstack
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100168 if (common.checkContains('INSTALL', 'openstack')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100169 // install Infra and control, tests, ...
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100170
Tomáš Kukrála18de112017-03-02 13:57:47 +0100171 stage('Install OpenStack infra') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200172 orchestrate.installOpenstackInfra(saltMaster)
Tomáš Kukrálc265e352017-03-02 11:45:11 +0100173 }
174
Tomáš Kukrála18de112017-03-02 13:57:47 +0100175 stage('Install OpenStack control') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200176 orchestrate.installOpenstackControl(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100177 }
178
179 stage('Install OpenStack network') {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100180
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100181 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200182 orchestrate.installContrailNetwork(saltMaster)
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100183 } else if (common.checkContains('INSTALL', 'ovs')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200184 orchestrate.installOpenstackNetwork(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100185 }
186
Tomáš Kukrálfbb98322017-05-02 10:38:05 +0200187 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list'])
188 salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list'])
Tomáš Kukrála18de112017-03-02 13:57:47 +0100189 }
190
191 stage('Install OpenStack compute') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200192 orchestrate.installOpenstackCompute(saltMaster)
Tomáš Kukrála18de112017-03-02 13:57:47 +0100193
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100194 if (common.checkContains('INSTALL', 'contrail')) {
Ales Komarek47a29f12017-04-26 12:05:47 +0200195 orchestrate.installContrailCompute(saltMaster)
Tomáš Kukráld5a40af2017-03-07 15:34:58 +0100196 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100197 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100198
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100199 }
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100200
Tomáš Kukrál00ed1302017-03-03 17:38:40 +0100201
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100202 if (common.checkContains('INSTALL', 'stacklight')) {
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100203 stage('Install StackLight') {
Ales Komarek47a29f12017-04-26 12:05:47 +0200204 orchestrate.installStacklightControl(saltMaster)
205 orchestrate.installStacklightClient(saltMaster)
Tomáš Kukrál2b4c1282017-03-03 17:25:26 +0100206 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100207 }
208
Tomáš Kukrála18de112017-03-02 13:57:47 +0100209 //
210 // Test
211 //
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200212 def artifacts_dir = '_artifacts/'
Tomáš Kukrála18de112017-03-02 13:57:47 +0100213
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100214 if (common.checkContains('TEST', 'k8s')) {
Tomáš Kukrála18de112017-03-02 13:57:47 +0100215 stage('Run k8s bootstrap tests') {
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200216 def image = 'tomkukral/k8s-scripts'
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200217 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrál695d6462017-04-21 16:31:52 +0200218
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200219 // run image
220 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200221
222 // collect output
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200223 sh "mkdir -p ${artifacts_dir}"
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200224 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200225 writeFile file: "${artifacts_dir}${output_file}", text: file_content
Tomáš Kukrálf56beed2017-04-21 16:21:06 +0200226 sh "cat ${artifacts_dir}${output_file}"
227
228 // collect artifacts
229 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
Tomáš Kukrála18de112017-03-02 13:57:47 +0100230 }
231
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200232 stage('Run k8s conformance e2e tests') {
233 //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE)
234
235 def image = K8S_CONFORMANCE_IMAGE
Tomáš Kukrál44ca7fe2017-04-21 16:44:19 +0200236 def output_file = image.replaceAll('/', '-') + '.output'
Tomáš Kukrálb22826d2017-04-21 16:38:00 +0200237
238 // run image
239 test.runConformanceTests(saltMaster, K8S_API_SERVER, image)
240
241 // collect output
242 sh "mkdir -p ${artifacts_dir}"
243 file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file)
244 writeFile file: "${artifacts_dir}${output_file}", text: file_content
245 sh "cat ${artifacts_dir}${output_file}"
246
247 // collect artifacts
248 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
249 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100250 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100251
Tomáš Kukrál6d627d62017-03-23 17:39:07 +0100252 if (common.checkContains('TEST', 'openstack')) {
Victor Ryzhenkin5f3b7e62017-03-09 16:02:58 +0400253 stage('Run OpenStack tests') {
254 test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK)
255 }
256
257 stage('Copy Tempest results to config node') {
258 test.copyTempestResults(saltMaster)
259 }
260 }
261
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100262 stage('Finalize') {
Tomáš Kukrál38ab6bb2017-04-21 21:29:31 +0200263 if (INSTALL != '') {
264 try {
265 salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true)
266 } catch (Exception e) {
267 common.warningMsg('State apply failed but we should continue to run')
268 }
Tomáš Kukrál420d7ff2017-03-21 11:38:33 +0100269 }
270 }
Tomáš Kukrála18de112017-03-02 13:57:47 +0100271 } catch (Throwable e) {
272 currentBuild.result = 'FAILURE'
273 throw e
Tomáš Kukrál65fb5fd2017-03-02 14:56:49 +0100274 } finally {
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100275
Tomáš Kukrál96b12d52017-03-07 22:43:37 +0100276
Tomáš Kukrála18de112017-03-02 13:57:47 +0100277 //
278 // Clean
279 //
280
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100281 if (STACK_TYPE == 'heat') {
Tomáš Kukrál4507c6a2017-03-23 19:00:39 +0100282 // send notification
283 common.sendNotification(currentBuild.result, HEAT_STACK_NAME, ["slack"])
284
Tomáš Kukrálc9a630a2017-03-07 17:28:53 +0100285 if (HEAT_STACK_DELETE.toBoolean() == true) {
286 common.errorMsg('Heat job cleanup triggered')
287 stage('Trigger cleanup job') {
288 build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]]
289 }
290 } else {
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100291 if (currentBuild.result == 'FAILURE') {
292 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 +0100293
Tomáš Kukrálbcc53ea2017-03-08 08:15:57 +0100294 if (SALT_MASTER_URL) {
295 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
296 }
Tomáš Kukrálb908dac2017-03-07 18:58:35 +0100297 }
298
Tomáš Kukrála18de112017-03-02 13:57:47 +0100299 }
Tomáš Kukrál9e0fb732017-03-02 10:12:05 +0100300 }
Tomáš Kukrálb9957b32017-02-28 14:49:00 +0100301 }
302 }
303}