Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 1 | /** |
| 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 |
| 22 | * |
| 23 | * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters |
| 24 | * HEAT_STACK_ZONE Heat stack availability zone |
| 25 | * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool |
| 26 | * OPENSTACK_API_URL OpenStack API address |
| 27 | * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API |
| 28 | * OPENSTACK_API_PROJECT OpenStack project to connect to |
| 29 | * OPENSTACK_API_CLIENT Versions of OpenStack python clients |
| 30 | * OPENSTACK_API_VERSION Version of the OpenStack API (2/3) |
| 31 | * |
| 32 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| 33 | * SALT_MASTER_URL URL of Salt master |
| 34 | * |
| 35 | * K8S_API_SERVER Kubernetes API address |
| 36 | * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests |
| 37 | * |
| 38 | * TEMPEST_IMAGE_LINK Tempest image link |
| 39 | * |
| 40 | */ |
| 41 | common = new com.mirantis.mk.Common() |
| 42 | git = new com.mirantis.mk.Git() |
| 43 | openstack = new com.mirantis.mk.Openstack() |
| 44 | orchestrate = new com.mirantis.mk.Orchestrate() |
| 45 | salt = new com.mirantis.mk.Salt() |
| 46 | test = new com.mirantis.mk.Test() |
| 47 | |
| 48 | _MAX_PERMITTED_STACKS = 2 |
| 49 | overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml" |
| 50 | |
| 51 | timestamps { |
| 52 | node { |
| 53 | try { |
| 54 | // |
| 55 | // Prepare machines |
| 56 | // |
| 57 | stage ('Create infrastructure') { |
| 58 | |
| 59 | if (STACK_TYPE == 'heat') { |
| 60 | // value defaults |
| 61 | def openstackCloud |
| 62 | def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty' |
| 63 | def openstackEnv = "${env.WORKSPACE}/venv" |
| 64 | |
| 65 | if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') { |
| 66 | error("If you want to reuse existing stack you need to provide it's name") |
| 67 | } |
| 68 | |
| 69 | if (STACK_REUSE.toBoolean() == false) { |
| 70 | // Don't allow to set custom heat stack name |
| 71 | wrap([$class: 'BuildUser']) { |
| 72 | if (env.BUILD_USER_ID) { |
| 73 | STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}" |
| 74 | } else { |
| 75 | STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}" |
| 76 | } |
| 77 | currentBuild.description = STACK_NAME |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // set description |
| 82 | currentBuild.description = "${STACK_NAME}" |
| 83 | |
| 84 | // get templates |
| 85 | git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS) |
| 86 | |
| 87 | // create openstack env |
| 88 | openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion) |
| 89 | openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT) |
| 90 | openstack.getKeystoneToken(openstackCloud, openstackEnv) |
| 91 | // |
| 92 | // Verify possibility of create stack for given user and stack type |
| 93 | // |
| 94 | wrap([$class: 'BuildUser']) { |
| 95 | if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) { |
| 96 | def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv) |
| 97 | if(existingStacks.size() >= _MAX_PERMITTED_STACKS){ |
| 98 | STACK_DELETE = "false" |
| 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 | } |
| 102 | } |
| 103 | // launch stack |
| 104 | if (STACK_REUSE.toBoolean() == false) { |
| 105 | stage('Launch new Heat stack') { |
| 106 | // create stack |
| 107 | envParams = [ |
| 108 | 'instance_zone': HEAT_STACK_ZONE, |
| 109 | 'public_net': HEAT_STACK_PUBLIC_NET |
| 110 | ] |
Ales Komarek | 5eb8b10 | 2017-05-24 14:00:22 +0200 | [diff] [blame^] | 111 | openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv) |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | // get SALT_MASTER_URL |
| 116 | saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv) |
| 117 | currentBuild.description = "${STACK_NAME}: ${saltMasterHost}" |
| 118 | |
| 119 | SALT_MASTER_URL = "http://${saltMasterHost}:6969" |
| 120 | } |
| 121 | |
| 122 | if (STACK_TYPE == 'aws') { |
| 123 | saltMasterHost = '' |
| 124 | currentBuild.description = "${STACK_NAME}: ${saltMasterHost}" |
| 125 | SALT_MASTER_URL = "http://${saltMasterHost}:6969" |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // |
| 131 | // Connect to Salt master |
| 132 | // |
| 133 | |
| 134 | def saltMaster |
| 135 | stage('Connect to Salt API') { |
| 136 | saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 137 | } |
| 138 | |
| 139 | // |
| 140 | // Install |
| 141 | // |
| 142 | |
| 143 | if (common.checkContains('STACK_INSTALL', 'core')) { |
| 144 | stage('Install core infrastructure') { |
| 145 | orchestrate.installFoundationInfra(saltMaster) |
| 146 | |
| 147 | if (common.checkContains('STACK_INSTALL', 'kvm')) { |
| 148 | orchestrate.installInfraKvm(saltMaster) |
| 149 | orchestrate.installFoundationInfra(saltMaster) |
| 150 | } |
| 151 | |
| 152 | orchestrate.validateFoundationInfra(saltMaster) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // install k8s |
| 157 | if (common.checkContains('STACK_INSTALL', 'k8s')) { |
| 158 | stage('Install Kubernetes infra') { |
| 159 | orchestrate.installKubernetesInfra(saltMaster) |
| 160 | } |
| 161 | |
| 162 | stage('Install Kubernetes control') { |
| 163 | |
| 164 | // Overwrite Kubernetes vars if specified |
| 165 | if (env.getEnvironment().containsKey("KUBERNETES_HYPERKUBE_IMAGE")) { |
| 166 | salt.runSaltProcessStep(saltMaster, 'I@salt:master', 'file.append', overwriteFile, " kubernetes_hyperkube_image: ${KUBERNETES_HYPERKUBE_IMAGE}") |
| 167 | } |
| 168 | |
| 169 | orchestrate.installKubernetesControl(saltMaster) |
| 170 | } |
| 171 | |
| 172 | |
| 173 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 174 | state('Install Contrail for Kubernetes') { |
| 175 | orchestrate.installContrailNetwork(saltMaster) |
| 176 | orchestrate.installContrailCompute(saltMaster) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // install openstack |
| 182 | if (common.checkContains('STACK_INSTALL', 'openstack')) { |
| 183 | // install Infra and control, tests, ... |
| 184 | |
| 185 | stage('Install OpenStack infra') { |
| 186 | orchestrate.installOpenstackInfra(saltMaster) |
| 187 | } |
| 188 | |
| 189 | stage('Install OpenStack control') { |
| 190 | orchestrate.installOpenstackControl(saltMaster) |
| 191 | } |
| 192 | |
| 193 | stage('Install OpenStack network') { |
| 194 | |
| 195 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 196 | orchestrate.installContrailNetwork(saltMaster) |
| 197 | } else if (common.checkContains('STACK_INSTALL', 'ovs')) { |
| 198 | orchestrate.installOpenstackNetwork(saltMaster) |
| 199 | } |
| 200 | |
| 201 | salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list']) |
| 202 | salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list']) |
| 203 | } |
| 204 | |
| 205 | stage('Install OpenStack compute') { |
| 206 | orchestrate.installOpenstackCompute(saltMaster) |
| 207 | |
| 208 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 209 | orchestrate.installContrailCompute(saltMaster) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | if (common.checkContains('STACK_INSTALL', 'stacklight')) { |
| 217 | stage('Install StackLight') { |
| 218 | orchestrate.installStacklightControl(saltMaster) |
| 219 | orchestrate.installStacklightClient(saltMaster) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // |
| 224 | // Test |
| 225 | // |
| 226 | def artifacts_dir = '_artifacts/' |
| 227 | |
| 228 | if (common.checkContains('STACK_TEST', 'k8s')) { |
| 229 | stage('Run k8s bootstrap tests') { |
| 230 | def image = 'tomkukral/k8s-scripts' |
| 231 | def output_file = image.replaceAll('/', '-') + '.output' |
| 232 | |
| 233 | // run image |
| 234 | test.runConformanceTests(saltMaster, K8S_API_SERVER, image) |
| 235 | |
| 236 | // collect output |
| 237 | sh "mkdir -p ${artifacts_dir}" |
| 238 | file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file) |
| 239 | writeFile file: "${artifacts_dir}${output_file}", text: file_content |
| 240 | sh "cat ${artifacts_dir}${output_file}" |
| 241 | |
| 242 | // collect artifacts |
| 243 | archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| 244 | } |
| 245 | |
| 246 | stage('Run k8s conformance e2e tests') { |
| 247 | //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE) |
| 248 | |
| 249 | def image = K8S_CONFORMANCE_IMAGE |
| 250 | def output_file = image.replaceAll('/', '-') + '.output' |
| 251 | |
| 252 | // run image |
| 253 | test.runConformanceTests(saltMaster, K8S_API_SERVER, image) |
| 254 | |
| 255 | // collect output |
| 256 | sh "mkdir -p ${artifacts_dir}" |
| 257 | file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file) |
| 258 | writeFile file: "${artifacts_dir}${output_file}", text: file_content |
| 259 | sh "cat ${artifacts_dir}${output_file}" |
| 260 | |
| 261 | // collect artifacts |
| 262 | archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (common.checkContains('STACK_TEST', 'openstack')) { |
| 267 | stage('Run deployment tests') { |
| 268 | test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK) |
| 269 | } |
| 270 | |
| 271 | stage('Copy test results to config node') { |
| 272 | test.copyTempestResults(saltMaster) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | stage('Finalize') { |
| 277 | if (STACK_INSTALL != '') { |
| 278 | try { |
| 279 | salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true) |
| 280 | } catch (Exception e) { |
| 281 | common.warningMsg('State apply failed but we should continue to run') |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } catch (Throwable e) { |
| 286 | currentBuild.result = 'FAILURE' |
| 287 | throw e |
| 288 | } finally { |
| 289 | |
| 290 | |
| 291 | // |
| 292 | // Clean |
| 293 | // |
| 294 | |
| 295 | if (STACK_TYPE == 'heat') { |
| 296 | // send notification |
| 297 | common.sendNotification(currentBuild.result, STACK_NAME, ["slack"]) |
| 298 | |
| 299 | if (STACK_DELETE.toBoolean() == true) { |
| 300 | common.errorMsg('Heat job cleanup triggered') |
| 301 | stage('Trigger cleanup job') { |
Ales Komarek | 5eb8b10 | 2017-05-24 14:00:22 +0200 | [diff] [blame^] | 302 | build job: 'deploy-heat-cleanup', parameters: [[$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME]] |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 303 | } |
| 304 | } else { |
| 305 | if (currentBuild.result == 'FAILURE') { |
| 306 | common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.") |
| 307 | if (SALT_MASTER_URL) { |
| 308 | common.errorMsg("Salt master URL: ${SALT_MASTER_URL}") |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |