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