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 | * |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 20 | * STACK_COMPUTE_COUNT Number of compute nodes to launch |
| 21 | * |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 22 | * AWS_STACK_REGION CloudFormation AWS region |
| 23 | * AWS_API_CREDENTIALS AWS Access key ID with AWS secret access key |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 24 | * AWS_SSH_KEY AWS key pair name (used for SSH access) |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 25 | * |
| 26 | * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters |
| 27 | * HEAT_STACK_ZONE Heat stack availability zone |
| 28 | * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool |
| 29 | * OPENSTACK_API_URL OpenStack API address |
| 30 | * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API |
| 31 | * OPENSTACK_API_PROJECT OpenStack project to connect to |
| 32 | * OPENSTACK_API_CLIENT Versions of OpenStack python clients |
| 33 | * OPENSTACK_API_VERSION Version of the OpenStack API (2/3) |
| 34 | * |
| 35 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| 36 | * SALT_MASTER_URL URL of Salt master |
| 37 | * |
| 38 | * K8S_API_SERVER Kubernetes API address |
| 39 | * K8S_CONFORMANCE_IMAGE Path to docker image with conformance e2e tests |
| 40 | * |
| 41 | * TEMPEST_IMAGE_LINK Tempest image link |
| 42 | * |
| 43 | */ |
| 44 | common = new com.mirantis.mk.Common() |
| 45 | git = new com.mirantis.mk.Git() |
| 46 | openstack = new com.mirantis.mk.Openstack() |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 47 | aws = new com.mirantis.mk.Aws() |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 48 | orchestrate = new com.mirantis.mk.Orchestrate() |
| 49 | salt = new com.mirantis.mk.Salt() |
| 50 | test = new com.mirantis.mk.Test() |
| 51 | |
| 52 | _MAX_PERMITTED_STACKS = 2 |
| 53 | overwriteFile = "/srv/salt/reclass/classes/cluster/override.yml" |
| 54 | |
Tomáš Kukrál | bc4be25 | 2017-06-06 11:28:40 +0200 | [diff] [blame^] | 55 | venv_path = 'aws_venv' |
| 56 | |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 57 | timestamps { |
| 58 | node { |
| 59 | try { |
| 60 | // |
| 61 | // Prepare machines |
| 62 | // |
| 63 | stage ('Create infrastructure') { |
| 64 | |
| 65 | if (STACK_TYPE == 'heat') { |
| 66 | // value defaults |
| 67 | def openstackCloud |
| 68 | def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty' |
| 69 | def openstackEnv = "${env.WORKSPACE}/venv" |
| 70 | |
| 71 | if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') { |
| 72 | error("If you want to reuse existing stack you need to provide it's name") |
| 73 | } |
| 74 | |
| 75 | if (STACK_REUSE.toBoolean() == false) { |
| 76 | // Don't allow to set custom heat stack name |
| 77 | wrap([$class: 'BuildUser']) { |
| 78 | if (env.BUILD_USER_ID) { |
| 79 | STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}" |
| 80 | } else { |
| 81 | STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}" |
| 82 | } |
| 83 | currentBuild.description = STACK_NAME |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // set description |
| 88 | currentBuild.description = "${STACK_NAME}" |
| 89 | |
| 90 | // get templates |
| 91 | git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS) |
| 92 | |
| 93 | // create openstack env |
| 94 | openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion) |
| 95 | openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT) |
| 96 | openstack.getKeystoneToken(openstackCloud, openstackEnv) |
| 97 | // |
| 98 | // Verify possibility of create stack for given user and stack type |
| 99 | // |
| 100 | wrap([$class: 'BuildUser']) { |
| 101 | if (env.BUILD_USER_ID && !env.BUILD_USER_ID.equals("jenkins") && !STACK_REUSE.toBoolean()) { |
| 102 | def existingStacks = openstack.getStacksForNameContains(openstackCloud, "${env.BUILD_USER_ID}-${JOB_NAME}", openstackEnv) |
| 103 | if(existingStacks.size() >= _MAX_PERMITTED_STACKS){ |
| 104 | STACK_DELETE = "false" |
| 105 | throw new Exception("You cannot create new stack, you already have ${_MAX_PERMITTED_STACKS} stacks of this type (${JOB_NAME}). \nStack names: ${existingStacks}") |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | // launch stack |
| 110 | if (STACK_REUSE.toBoolean() == false) { |
| 111 | stage('Launch new Heat stack') { |
| 112 | // create stack |
| 113 | envParams = [ |
| 114 | 'instance_zone': HEAT_STACK_ZONE, |
| 115 | 'public_net': HEAT_STACK_PUBLIC_NET |
| 116 | ] |
Ales Komarek | 5eb8b10 | 2017-05-24 14:00:22 +0200 | [diff] [blame] | 117 | openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv) |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | // get SALT_MASTER_URL |
| 122 | saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', openstackEnv) |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 123 | currentBuild.description = "${STACK_NAME} ${saltMasterHost}" |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 124 | |
| 125 | SALT_MASTER_URL = "http://${saltMasterHost}:6969" |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 126 | } else if (STACK_TYPE == 'aws') { |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 127 | |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 128 | |
| 129 | if (STACK_REUSE.toBoolean() == true && STACK_NAME == '') { |
| 130 | error("If you want to reuse existing stack you need to provide it's name") |
| 131 | } |
| 132 | |
| 133 | if (STACK_REUSE.toBoolean() == false) { |
| 134 | // Don't allow to set custom stack name |
| 135 | wrap([$class: 'BuildUser']) { |
| 136 | if (env.BUILD_USER_ID) { |
| 137 | STACK_NAME = "${env.BUILD_USER_ID}-${JOB_NAME}-${BUILD_NUMBER}" |
| 138 | } else { |
| 139 | STACK_NAME = "jenkins-${JOB_NAME}-${BUILD_NUMBER}" |
| 140 | } |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 141 | } |
Tomáš Kukrál | dbf8ca8 | 2017-05-25 15:13:20 +0200 | [diff] [blame] | 142 | |
| 143 | // no underscore in STACK_NAME |
| 144 | STACK_NAME = STACK_NAME.replaceAll('_', '-') |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // set description |
Tomáš Kukrál | dbf8ca8 | 2017-05-25 15:13:20 +0200 | [diff] [blame] | 148 | currentBuild.description = STACK_NAME |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 149 | |
| 150 | // prepare configuration |
| 151 | def env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION) |
| 152 | |
| 153 | if (STACK_REUSE.toBoolean() == false) { |
| 154 | // get templates |
| 155 | git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS) |
| 156 | |
| 157 | // setup environment |
| 158 | aws.setupVirtualEnv(venv_path) |
| 159 | |
| 160 | // start stack |
Tomáš Kukrál | ad79d32 | 2017-06-05 15:40:14 +0200 | [diff] [blame] | 161 | def stack_params = [ |
| 162 | "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY, |
| 163 | "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT |
| 164 | ] |
Tomáš Kukrál | dbb5840 | 2017-06-02 07:08:53 +0200 | [diff] [blame] | 165 | def template_file = 'cfn/' + STACK_TEMPLATE + '.yml' |
Tomáš Kukrál | d4c0fed | 2017-06-01 17:20:11 +0200 | [diff] [blame] | 166 | aws.createStack(venv_path, env_vars, template_file, STACK_NAME, stack_params) |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // wait for stack to be ready |
| 170 | aws.waitForStatus(venv_path, env_vars, STACK_NAME, 'CREATE_COMPLETE') |
| 171 | |
| 172 | // get outputs |
| 173 | saltMasterHost = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'SaltMasterIP') |
| 174 | currentBuild.description = "${STACK_NAME} ${saltMasterHost}" |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 175 | SALT_MASTER_URL = "http://${saltMasterHost}:6969" |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 176 | |
Tomáš Kukrál | 7a8a4fa | 2017-05-25 14:54:24 +0200 | [diff] [blame] | 177 | } else { |
| 178 | throw new Exception("STACK_TYPE ${STACK_TYPE} is not supported") |
| 179 | } |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // |
| 183 | // Connect to Salt master |
| 184 | // |
| 185 | |
| 186 | def saltMaster |
| 187 | stage('Connect to Salt API') { |
| 188 | saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 189 | } |
| 190 | |
| 191 | // |
| 192 | // Install |
| 193 | // |
| 194 | |
| 195 | if (common.checkContains('STACK_INSTALL', 'core')) { |
| 196 | stage('Install core infrastructure') { |
| 197 | orchestrate.installFoundationInfra(saltMaster) |
| 198 | |
| 199 | if (common.checkContains('STACK_INSTALL', 'kvm')) { |
| 200 | orchestrate.installInfraKvm(saltMaster) |
| 201 | orchestrate.installFoundationInfra(saltMaster) |
| 202 | } |
| 203 | |
| 204 | orchestrate.validateFoundationInfra(saltMaster) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // install k8s |
| 209 | if (common.checkContains('STACK_INSTALL', 'k8s')) { |
| 210 | stage('Install Kubernetes infra') { |
| 211 | orchestrate.installKubernetesInfra(saltMaster) |
| 212 | } |
| 213 | |
| 214 | stage('Install Kubernetes control') { |
| 215 | |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 216 | orchestrate.installKubernetesControl(saltMaster) |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 217 | |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 220 | stage('Scale Kubernetes computes') { |
Tomáš Kukrál | 8130a56 | 2017-06-06 11:09:22 +0200 | [diff] [blame] | 221 | if (STACK_TYPE == 'aws') { |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 222 | // get stack info |
| 223 | def scaling_group = aws.getOutputs(venv_path, env_vars, STACK_NAME, 'ComputesScalingGroup') |
| 224 | |
| 225 | //update autoscaling group |
| 226 | aws.updateAutoscalingGroup(venv_path, evn_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT]) |
Tomáš Kukrál | 8130a56 | 2017-06-06 11:09:22 +0200 | [diff] [blame] | 227 | |
| 228 | // wait for computes to boot up |
| 229 | sleep(120) |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Tomáš Kukrál | 8130a56 | 2017-06-06 11:09:22 +0200 | [diff] [blame] | 232 | orchestrate.installKubernetesCompute(saltMaster) |
| 233 | |
Tomáš Kukrál | 74f9286 | 2017-06-05 19:18:43 +0200 | [diff] [blame] | 234 | } |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 235 | |
| 236 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 237 | state('Install Contrail for Kubernetes') { |
| 238 | orchestrate.installContrailNetwork(saltMaster) |
| 239 | orchestrate.installContrailCompute(saltMaster) |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // install openstack |
| 245 | if (common.checkContains('STACK_INSTALL', 'openstack')) { |
| 246 | // install Infra and control, tests, ... |
| 247 | |
| 248 | stage('Install OpenStack infra') { |
| 249 | orchestrate.installOpenstackInfra(saltMaster) |
| 250 | } |
| 251 | |
| 252 | stage('Install OpenStack control') { |
| 253 | orchestrate.installOpenstackControl(saltMaster) |
| 254 | } |
| 255 | |
| 256 | stage('Install OpenStack network') { |
| 257 | |
| 258 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 259 | orchestrate.installContrailNetwork(saltMaster) |
| 260 | } else if (common.checkContains('STACK_INSTALL', 'ovs')) { |
| 261 | orchestrate.installOpenstackNetwork(saltMaster) |
| 262 | } |
| 263 | |
| 264 | salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; neutron net-list']) |
| 265 | salt.runSaltProcessStep(saltMaster, 'I@keystone:server', 'cmd.run', ['. /root/keystonerc; nova net-list']) |
| 266 | } |
| 267 | |
| 268 | stage('Install OpenStack compute') { |
| 269 | orchestrate.installOpenstackCompute(saltMaster) |
| 270 | |
| 271 | if (common.checkContains('STACK_INSTALL', 'contrail')) { |
| 272 | orchestrate.installContrailCompute(saltMaster) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | |
| 279 | if (common.checkContains('STACK_INSTALL', 'stacklight')) { |
| 280 | stage('Install StackLight') { |
| 281 | orchestrate.installStacklightControl(saltMaster) |
| 282 | orchestrate.installStacklightClient(saltMaster) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // |
| 287 | // Test |
| 288 | // |
| 289 | def artifacts_dir = '_artifacts/' |
| 290 | |
| 291 | if (common.checkContains('STACK_TEST', 'k8s')) { |
| 292 | stage('Run k8s bootstrap tests') { |
| 293 | def image = 'tomkukral/k8s-scripts' |
| 294 | def output_file = image.replaceAll('/', '-') + '.output' |
| 295 | |
| 296 | // run image |
| 297 | test.runConformanceTests(saltMaster, K8S_API_SERVER, image) |
| 298 | |
| 299 | // collect output |
| 300 | sh "mkdir -p ${artifacts_dir}" |
| 301 | file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file) |
| 302 | writeFile file: "${artifacts_dir}${output_file}", text: file_content |
| 303 | sh "cat ${artifacts_dir}${output_file}" |
| 304 | |
| 305 | // collect artifacts |
| 306 | archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| 307 | } |
| 308 | |
| 309 | stage('Run k8s conformance e2e tests') { |
| 310 | //test.runConformanceTests(saltMaster, K8S_API_SERVER, K8S_CONFORMANCE_IMAGE) |
| 311 | |
| 312 | def image = K8S_CONFORMANCE_IMAGE |
| 313 | def output_file = image.replaceAll('/', '-') + '.output' |
| 314 | |
| 315 | // run image |
| 316 | test.runConformanceTests(saltMaster, K8S_API_SERVER, image) |
| 317 | |
| 318 | // collect output |
| 319 | sh "mkdir -p ${artifacts_dir}" |
| 320 | file_content = salt.getFileContent(saltMaster, 'ctl01*', '/tmp/' + output_file) |
| 321 | writeFile file: "${artifacts_dir}${output_file}", text: file_content |
| 322 | sh "cat ${artifacts_dir}${output_file}" |
| 323 | |
| 324 | // collect artifacts |
| 325 | archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (common.checkContains('STACK_TEST', 'openstack')) { |
| 330 | stage('Run deployment tests') { |
| 331 | test.runTempestTests(saltMaster, TEMPEST_IMAGE_LINK) |
| 332 | } |
| 333 | |
| 334 | stage('Copy test results to config node') { |
| 335 | test.copyTempestResults(saltMaster) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | stage('Finalize') { |
| 340 | if (STACK_INSTALL != '') { |
| 341 | try { |
| 342 | salt.runSaltProcessStep(saltMaster, '*', 'state.apply', [], null, true) |
| 343 | } catch (Exception e) { |
| 344 | common.warningMsg('State apply failed but we should continue to run') |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } catch (Throwable e) { |
| 349 | currentBuild.result = 'FAILURE' |
| 350 | throw e |
| 351 | } finally { |
| 352 | |
| 353 | |
| 354 | // |
| 355 | // Clean |
| 356 | // |
| 357 | |
Tomáš Kukrál | 439c62e | 2017-06-02 11:42:36 +0200 | [diff] [blame] | 358 | if (STACK_NAME && STACK_NAME != '') { |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 359 | // send notification |
| 360 | common.sendNotification(currentBuild.result, STACK_NAME, ["slack"]) |
Tomáš Kukrál | 439c62e | 2017-06-02 11:42:36 +0200 | [diff] [blame] | 361 | } |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 362 | |
Tomáš Kukrál | 439c62e | 2017-06-02 11:42:36 +0200 | [diff] [blame] | 363 | if (STACK_DELETE.toBoolean() == true) { |
| 364 | stage('Trigger cleanup job') { |
| 365 | common.errorMsg('Stack cleanup job triggered') |
| 366 | build(job: STACK_CLEANUP_JOB, parameters: [ |
| 367 | [$class: 'StringParameterValue', name: 'STACK_NAME', value: STACK_NAME], |
| 368 | [$class: 'StringParameterValue', name: 'STACK_TYPE', value: STACK_TYPE] |
| 369 | ]) |
| 370 | } |
| 371 | } else { |
| 372 | if (currentBuild.result == 'FAILURE') { |
| 373 | common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.") |
| 374 | if (SALT_MASTER_URL) { |
| 375 | common.errorMsg("Salt master URL: ${SALT_MASTER_URL}") |
Ales Komarek | 2409740 | 2017-05-22 16:28:10 +0200 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |