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 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 20 | timeout(time: 12, unit: 'HOURS') { |
| 21 | node("python&&docker") { |
| 22 | def templateEnv = "${env.WORKSPACE}/template" |
| 23 | def modelEnv = "${env.WORKSPACE}/model" |
| 24 | def testEnv = "${env.WORKSPACE}/test" |
| 25 | def pipelineEnv = "${env.WORKSPACE}/pipelines" |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 26 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 27 | try { |
| 28 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
| 29 | def clusterDomain = templateContext.default_context.cluster_domain |
| 30 | def clusterName = templateContext.default_context.cluster_name |
| 31 | def saltMaster = templateContext.default_context.salt_master_hostname |
| 32 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 33 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 34 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 35 | def targetBranch = "feature/${clusterName}" |
| 36 | def templateBaseDir = "${env.WORKSPACE}/template" |
| 37 | def templateDir = "${templateEnv}/template/dir" |
| 38 | def templateOutputDir = templateBaseDir |
| 39 | def user |
| 40 | wrap([$class: 'BuildUser']) { |
| 41 | user = env.BUILD_USER_ID |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 44 | currentBuild.description = clusterName |
| 45 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 46 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 47 | stage ('Download Cookiecutter template') { |
Richard Felkl | c73f2ee | 2018-01-18 11:09:48 +0100 | [diff] [blame] | 48 | if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/')) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 49 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 50 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 51 | dir(templateEnv) { |
| 52 | ssh.agentSh("git fetch ${COOKIECUTTER_TEMPLATE_URL} ${COOKIECUTTER_TEMPLATE_BRANCH} && git checkout FETCH_HEAD") |
| 53 | } |
| 54 | } else { |
| 55 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 56 | } |
Richard Felkl | 9543faa | 2018-01-11 09:47:35 +0100 | [diff] [blame] | 57 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | stage ('Create empty reclass model') { |
| 61 | dir(path: modelEnv) { |
| 62 | sh "rm -rfv .git" |
| 63 | sh "git init" |
| 64 | |
| 65 | if (SHARED_RECLASS_URL != '') { |
| 66 | ssh.agentSh "git submodule add \"${SHARED_RECLASS_URL}\" \"classes/system\"" |
| 67 | |
| 68 | def mcpVersion = templateContext['default_context']['mcp_version'] |
| 69 | if(mcpVersion != "stable" && mcpVersion != "nightly" && mcpVersion != "testing"){ |
| 70 | ssh.agentSh "cd \"classes/system\";git fetch --tags;git checkout ${mcpVersion}" |
| 71 | } |
| 72 | |
| 73 | git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}") |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"] |
| 79 | for (product in productList) { |
| 80 | |
| 81 | // get templateOutputDir and productDir |
| 82 | if (product.startsWith("stacklight")) { |
| 83 | templateOutputDir = "${env.WORKSPACE}/output/stacklight" |
| 84 | |
| 85 | def stacklightVersion |
| 86 | try { |
| 87 | stacklightVersion = templateContext.default_context['stacklight_version'] |
| 88 | } catch (Throwable e) { |
| 89 | common.warningMsg('Stacklight version loading failed') |
Richard Felkl | 9543faa | 2018-01-11 09:47:35 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 92 | if (stacklightVersion) { |
| 93 | productDir = "stacklight" + stacklightVersion |
| 94 | } else { |
| 95 | productDir = "stacklight1" |
| 96 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 97 | |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 98 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 99 | templateOutputDir = "${env.WORKSPACE}/output/${product}" |
| 100 | productDir = product |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 101 | } |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 102 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 103 | if (product == "infra" || (templateContext.default_context["${product}_enabled"] |
| 104 | && templateContext.default_context["${product}_enabled"].toBoolean())) { |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 105 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 106 | templateDir = "${templateEnv}/cluster_product/${productDir}" |
| 107 | common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 108 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 109 | sh "rm -rf ${templateOutputDir} || true" |
| 110 | sh "mkdir -p ${templateOutputDir}" |
| 111 | sh "mkdir -p ${outputDestination}" |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 112 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 113 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 114 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
| 115 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Jiri Broulik | d1375f7 | 2018-01-09 10:14:59 +0100 | [diff] [blame] | 116 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 117 | common.warningMsg("Product " + product + " is disabled") |
Jiri Broulik | d1375f7 | 2018-01-09 10:14:59 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 120 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 121 | stage('Generate new SaltMaster node') { |
| 122 | def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml" |
| 123 | def nodeString = """classes: |
Leontii Istomin | 56b6b11 | 2018-01-15 12:14:24 +0300 | [diff] [blame] | 124 | - cluster.${clusterName}.infra.config |
| 125 | parameters: |
| 126 | _param: |
| 127 | linux_system_codename: xenial |
| 128 | reclass_data_revision: master |
| 129 | linux: |
| 130 | system: |
| 131 | name: ${saltMaster} |
| 132 | domain: ${clusterDomain} |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 133 | """ |
| 134 | sh "mkdir -p ${modelEnv}/nodes/" |
| 135 | writeFile(file: nodeFile, text: nodeString) |
| 136 | |
| 137 | git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}") |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 138 | } |
| 139 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 140 | stage("Test") { |
| 141 | if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) { |
| 142 | sh("cp -r ${modelEnv} ${testEnv}") |
| 143 | saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv) |
| 144 | } |
| 145 | } |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 146 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 147 | stage("Generate config drives") { |
| 148 | // apt package genisoimage is required for this stage |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 149 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 150 | // download create-config-drive |
| 151 | // FIXME: that should be refactored, to use git clone - to be able download it from custom repo. |
| 152 | def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/create_config_drive.sh" |
| 153 | def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/master_config.sh" |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 154 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 155 | sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive" |
| 156 | sh "wget -O user_data.sh ${user_data_script_url}" |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 157 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 158 | sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines" |
| 159 | sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library" |
| 160 | args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso" |
| 161 | |
| 162 | // load data from model |
| 163 | def smc = [:] |
| 164 | smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}" |
| 165 | smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 166 | smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway'] |
| 167 | smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 168 | smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01'] |
| 169 | if (templateContext['default_context']['local_repositories'] == 'True'){ |
| 170 | smc['PIPELINES_FROM_ISO'] = 'false' |
| 171 | smc['PIPELINE_REPO_URL'] = 'http://' + templateContext['default_context']['aptly_server_deploy_address'] + ':8088' |
| 172 | } |
| 173 | if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){ |
| 174 | if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){ |
| 175 | smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 176 | smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 177 | } else { |
| 178 | smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 179 | smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 180 | } |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 183 | for (i in common.entries(smc)) { |
| 184 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh" |
| 185 | } |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 186 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 187 | // create cfg config-drive |
| 188 | sh "./create-config-drive ${args}" |
| 189 | sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/") |
| 190 | |
| 191 | // save cfg iso to artifacts |
| 192 | archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso" |
| 193 | |
| 194 | if (templateContext['default_context']['local_repositories'] == 'True'){ |
| 195 | def aptlyServerHostname = templateContext.default_context.aptly_server_hostname |
| 196 | def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh" |
| 197 | sh "wget -O mirror_config.sh ${user_data_script_apt_url}" |
| 198 | |
| 199 | def smc_apt = [:] |
| 200 | smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 201 | smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address'] |
| 202 | smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 203 | smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}" |
| 204 | |
| 205 | for (i in common.entries(smc_apt)) { |
| 206 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh" |
| 207 | } |
| 208 | |
| 209 | // create apt config-drive |
| 210 | sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso" |
| 211 | sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/") |
| 212 | |
| 213 | // save apt iso to artifacts |
| 214 | archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso" |
| 215 | } |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 216 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 217 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 218 | stage ('Save changes reclass model') { |
| 219 | sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .") |
| 220 | archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz" |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 221 | |
| 222 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 223 | if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") { |
| 224 | emailext(to: EMAIL_ADDRESS, |
| 225 | attachmentsPattern: "output-${clusterName}/*", |
| 226 | body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis", |
| 227 | subject: "Your Salt model ${clusterName}") |
| 228 | } |
| 229 | dir("output-${clusterName}"){ |
| 230 | deleteDir() |
| 231 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 232 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 233 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 234 | } catch (Throwable e) { |
| 235 | // If there was an error or exception thrown, the build failed |
| 236 | currentBuild.result = "FAILURE" |
| 237 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 238 | throw e |
| 239 | } finally { |
| 240 | stage ('Clean workspace directories') { |
| 241 | sh(returnStatus: true, script: "rm -rf ${templateEnv}") |
| 242 | sh(returnStatus: true, script: "rm -rf ${modelEnv}") |
| 243 | sh(returnStatus: true, script: "rm -rf ${pipelineEnv}") |
| 244 | } |
| 245 | // common.sendNotification(currentBuild.result,"",["slack"]) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 246 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 247 | } |
Mikhail Ivanov | 9f81292 | 2017-11-07 18:52:02 +0400 | [diff] [blame] | 248 | } |