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