Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Generate cookiecutter cluster by individual products |
| 3 | * |
| 4 | * Expected parameters: |
| 5 | * COOKIECUTTER_TEMPLATE_CREDENTIALS Credentials to the Cookiecutter template repo. |
| 6 | * COOKIECUTTER_TEMPLATE_URL Cookiecutter template repo address. |
| 7 | * COOKIECUTTER_TEMPLATE_BRANCH Branch for the template. |
| 8 | * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation. |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 9 | * EMAIL_ADDRESS Email to send a created tar file |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 10 | * SHARED_RECLASS_URL Git repository with shared reclass |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 11 | * |
| 12 | **/ |
| 13 | |
| 14 | common = new com.mirantis.mk.Common() |
| 15 | git = new com.mirantis.mk.Git() |
| 16 | python = new com.mirantis.mk.Python() |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 17 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 18 | |
| 19 | timestamps { |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 20 | node("python&&docker") { |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 21 | def templateEnv = "${env.WORKSPACE}/template" |
| 22 | def modelEnv = "${env.WORKSPACE}/model" |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 23 | def testEnv = "${env.WORKSPACE}/test" |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 24 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 25 | try { |
Tomáš Kukrál | f2a74ab | 2017-05-09 17:53:32 +0200 | [diff] [blame] | 26 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 27 | def clusterDomain = templateContext.default_context.cluster_domain |
| 28 | def clusterName = templateContext.default_context.cluster_name |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 29 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 30 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 31 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 32 | def targetBranch = "feature/${clusterName}" |
| 33 | def templateBaseDir = "${env.WORKSPACE}/template" |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 34 | def templateDir = "${templateEnv}/template/dir" |
| 35 | def templateOutputDir = templateBaseDir |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 36 | def user |
| 37 | wrap([$class: 'BuildUser']) { |
| 38 | user = env.BUILD_USER_ID |
| 39 | } |
| 40 | |
| 41 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 42 | |
Tomáš Kukrál | 08bfb95 | 2017-04-12 13:10:52 +0200 | [diff] [blame] | 43 | currentBuild.description = clusterName |
Tomáš Kukrál | 2a8fc32 | 2017-04-12 18:06:20 +0200 | [diff] [blame] | 44 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
| 45 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 46 | stage ('Download Cookiecutter template') { |
| 47 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 48 | } |
| 49 | |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 50 | stage ('Create empty reclass model') { |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 51 | dir(path: modelEnv) { |
| 52 | sh "rm -rfv .git" |
| 53 | sh "git init" |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 54 | |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 55 | if (SHARED_RECLASS_URL != '') { |
| 56 | sh "git submodule add ${SHARED_RECLASS_URL} '${modelEnv}/classes/system'" |
| 57 | git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}") |
| 58 | } |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 59 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 60 | } |
| 61 | |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 62 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"] |
| 63 | for (product in productList) { |
| 64 | def stagename = (product == "infra") ? "Generate base infrastructure" : "Generate product ${product}" |
| 65 | stage(stagename) { |
| 66 | if (product == "infra" || (templateContext.default_context["${product}_enabled"] |
| 67 | && templateContext.default_context["${product}_enabled"].toBoolean())) { |
| 68 | templateDir = "${templateEnv}/cluster_product/${product}" |
| 69 | templateOutputDir = "${env.WORKSPACE}/template/output/${product}" |
| 70 | sh "mkdir -p ${templateOutputDir}" |
| 71 | sh "mkdir -p ${outputDestination}" |
| 72 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 73 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
| 74 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 75 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | stage('Generate new SaltMaster node') { |
| 80 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" |
| 81 | def nodeString = """classes: |
| 82 | - cluster.${clusterName}.infra.config |
| 83 | parameters: |
Tomáš Kukrál | c0bffae | 2017-06-07 14:40:32 +0200 | [diff] [blame] | 84 | _param: |
| 85 | linux_system_codename: xenial |
| 86 | reclass_data_revision: master |
| 87 | linux: |
| 88 | system: |
| 89 | name: cfg01 |
| 90 | domain: ${clusterDomain} |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 91 | """ |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 92 | sh "mkdir -p ${modelEnv}/nodes/" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 93 | writeFile(file: nodeFile, text: nodeString) |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 94 | |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 95 | git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}") |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 96 | } |
| 97 | |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 98 | stage("Test") { |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 99 | if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) { |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 100 | sh("cp -r ${modelEnv} ${testEnv}") |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 101 | saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv) |
| 102 | } |
| 103 | } |
| 104 | |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 105 | stage("Generate config drive") { |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 106 | // apt package genisoimage is required for this stage |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 107 | |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 108 | // download create-config-drive |
| 109 | def config_drive_script_url = "https://raw.githubusercontent.com/pupapaik/virt-utils/master/create-config-drive" |
| 110 | def user_data_script_url = "https://raw.githubusercontent.com/mceloud/scripts/master/master_config.sh" |
| 111 | |
| 112 | sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive" |
| 113 | sh "wget -O user_data.sh ${user_data_script_url}" |
| 114 | |
| 115 | |
| 116 | // load data from model |
| 117 | def smc = [:] |
| 118 | smc['SALT_MASTER_MINION_ID'] = "cfg.${clusterDomain}" |
| 119 | smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 120 | smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway'] |
| 121 | smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 122 | smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01'] |
| 123 | |
| 124 | for (i in common.entries(smc)) { |
| 125 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh" |
| 126 | } |
| 127 | |
| 128 | // create config-drive |
| 129 | sh "./create-config-drive --user-data user_data.sh --hostname cfg --model ${modelEnv} cfg.${clusterDomain}-config.iso" |
| 130 | |
| 131 | // save iso to artifacts |
| 132 | archiveArtifacts artifacts: "cfg.${clusterDomain}-config.iso" |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | stage ('Save changes reclass model') { |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 136 | |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 137 | sh(returnStatus: true, script: "tar -zcf ${clusterName}.tar.gz -C ${modelEnv} .") |
Tomáš Kukrál | 4c98b58 | 2017-03-30 18:49:06 +0200 | [diff] [blame] | 138 | archiveArtifacts artifacts: "${clusterName}.tar.gz" |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 139 | |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 140 | |
| 141 | if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") { |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 142 | emailext(to: EMAIL_ADDRESS, |
| 143 | attachmentsPattern: "${clusterName}.tar.gz", |
| 144 | body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis", |
| 145 | subject: "Your Salt model ${clusterName}") |
| 146 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 149 | } catch (Throwable e) { |
| 150 | // If there was an error or exception thrown, the build failed |
| 151 | currentBuild.result = "FAILURE" |
| 152 | throw e |
| 153 | } finally { |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 154 | stage ('Clean workspace directories') { |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 155 | sh(returnStatus: true, script: "rm -rf ${templateEnv}") |
| 156 | sh(returnStatus: true, script: "rm -rf ${modelEnv}") |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 157 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 158 | // common.sendNotification(currentBuild.result,"",["slack"]) |
| 159 | } |
| 160 | } |
| 161 | } |