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: |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 5 | * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation. |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 6 | * EMAIL_ADDRESS Email to send a created tar file |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 7 | * |
azvyagintsev | 3ed704f | 2018-07-09 15:49:27 +0300 | [diff] [blame] | 8 | **/ |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 9 | |
| 10 | common = new com.mirantis.mk.Common() |
| 11 | git = new com.mirantis.mk.Git() |
| 12 | python = new com.mirantis.mk.Python() |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 13 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
Tomáš Kukrál | cebc1a0 | 2017-07-25 16:10:37 +0200 | [diff] [blame] | 14 | ssh = new com.mirantis.mk.Ssh() |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 15 | |
azvyagintsev | b8ddb49 | 2018-09-12 14:59:45 +0300 | [diff] [blame] | 16 | slaveNode = env.SLAVE_NODE ?: 'python&&docker' |
azvyagintsev | f252b59 | 2018-08-13 18:39:14 +0300 | [diff] [blame] | 17 | |
| 18 | timeout(time: 2, unit: 'HOURS') { |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 19 | node(slaveNode) { |
| 20 | def templateEnv = "${env.WORKSPACE}/template" |
| 21 | def modelEnv = "${env.WORKSPACE}/model" |
| 22 | def testEnv = "${env.WORKSPACE}/test" |
| 23 | def pipelineEnv = "${env.WORKSPACE}/pipelines" |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 24 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 25 | try { |
| 26 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
| 27 | def mcpVersion = templateContext.default_context.mcp_version |
| 28 | def sharedReclassUrl = templateContext.default_context.shared_reclass_url |
| 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 |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 32 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 33 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 34 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 35 | def systemEnv = "${modelEnv}/classes/system" |
| 36 | def targetBranch = "feature/${clusterName}" |
| 37 | def templateBaseDir = "${env.WORKSPACE}/template" |
| 38 | def templateDir = "${templateEnv}/template/dir" |
| 39 | def templateOutputDir = templateBaseDir |
| 40 | def user |
| 41 | def testResult = false |
| 42 | wrap([$class: 'BuildUser']) { |
| 43 | user = env.BUILD_USER_ID |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 44 | } |
| 45 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 46 | currentBuild.description = clusterName |
| 47 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
Richard Felkl | e77b091 | 2018-01-31 17:12:23 +0100 | [diff] [blame] | 48 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 49 | stage('Download Cookiecutter template') { |
| 50 | sh(script: 'find . -mindepth 1 -delete > /dev/null || true') |
| 51 | def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url |
| 52 | def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch |
| 53 | git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master') |
| 54 | // Use refspec if exists first of all |
| 55 | if (cookiecutterTemplateBranch.toString().startsWith('refs/')) { |
| 56 | dir(templateEnv) { |
| 57 | ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD") |
| 58 | } |
| 59 | } else { |
| 60 | // Use mcpVersion git tag if not specified branch for cookiecutter-templates |
| 61 | if (cookiecutterTemplateBranch == '') { |
| 62 | cookiecutterTemplateBranch = mcpVersion |
| 63 | // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master |
| 64 | if (["nightly", "testing", "stable"].contains(mcpVersion)) { |
| 65 | cookiecutterTemplateBranch = 'master' |
| 66 | } |
| 67 | } |
| 68 | git.changeGitBranch(templateEnv, cookiecutterTemplateBranch) |
| 69 | } |
| 70 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 71 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 72 | stage('Create empty reclass model') { |
| 73 | dir(path: modelEnv) { |
| 74 | sh "rm -rfv .git" |
| 75 | sh "git init" |
| 76 | ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'") |
| 77 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 78 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 79 | def sharedReclassBranch = templateContext.default_context.shared_reclass_branch |
| 80 | // Use refspec if exists first of all |
| 81 | if (sharedReclassBranch.toString().startsWith('refs/')) { |
| 82 | dir(systemEnv) { |
| 83 | ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD") |
| 84 | } |
| 85 | } else { |
| 86 | // Use mcpVersion git tag if not specified branch for reclass-system |
| 87 | if (sharedReclassBranch == '') { |
| 88 | sharedReclassBranch = mcpVersion |
| 89 | // Don't have nightly/testing for reclass-system repo, therefore use master |
| 90 | if (["nightly", "testing", "stable"].contains(mcpVersion)) { |
| 91 | common.warningMsg("Fetching reclass-system from master!") |
| 92 | sharedReclassBranch = 'master' |
| 93 | } |
| 94 | } |
| 95 | git.changeGitBranch(systemEnv, sharedReclassBranch) |
| 96 | } |
| 97 | git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}") |
| 98 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 99 | |
Denis Egorenko | f346909 | 2018-10-17 17:05:50 +0400 | [diff] [blame] | 100 | stage('Generate model') { |
| 101 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 102 | python.generateModel(COOKIECUTTER_TEMPLATE_CONTEXT, 'default_context', saltMaster, cutterEnv, modelEnv, templateEnv, false) |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 103 | git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}") |
| 104 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 105 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 106 | stage("Test") { |
| 107 | if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') { |
azvyagintsev | 1bcadc4 | 2018-10-05 20:13:15 +0300 | [diff] [blame] | 108 | distribRevision = mcpVersion |
| 109 | if (['master'].contains(mcpVersion)) { |
| 110 | distribRevision = 'nightly' |
| 111 | } |
| 112 | if (distribRevision.contains('/')) { |
| 113 | distribRevision = distribRevision.split('/')[-1] |
| 114 | } |
| 115 | // Check if we are going to test bleeding-edge release, which doesn't have binary release yet |
| 116 | if (!common.checkRemoteBinary([apt_mk_version: distribRevision]).linux_system_repo_url) { |
| 117 | common.errorMsg("Binary release: ${distribRevision} not exist. Fallback to 'proposed'! ") |
| 118 | distribRevision = 'proposed' |
| 119 | } |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 120 | sh("cp -r ${modelEnv} ${testEnv}") |
| 121 | def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}" |
azvyagintsev | 1bcadc4 | 2018-10-05 20:13:15 +0300 | [diff] [blame] | 122 | common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}") |
Denis Egorenko | 032b8ca | 2018-09-13 17:00:23 +0400 | [diff] [blame] | 123 | try { |
| 124 | def config = [ |
azvyagintsev | 7cdcc7a | 2018-09-25 16:47:24 +0300 | [diff] [blame] | 125 | 'dockerHostname' : "${saltMaster}.${clusterDomain}", |
| 126 | 'reclassEnv' : testEnv, |
azvyagintsev | 1bcadc4 | 2018-10-05 20:13:15 +0300 | [diff] [blame] | 127 | 'distribRevision' : distribRevision, |
Denis Egorenko | 032b8ca | 2018-09-13 17:00:23 +0400 | [diff] [blame] | 128 | 'dockerContainerName': DockerCName, |
azvyagintsev | 7cdcc7a | 2018-09-25 16:47:24 +0300 | [diff] [blame] | 129 | 'testContext' : 'salt-model-node' |
Denis Egorenko | 032b8ca | 2018-09-13 17:00:23 +0400 | [diff] [blame] | 130 | ] |
| 131 | testResult = saltModelTesting.testNode(config) |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 132 | common.infoMsg("Test finished: SUCCESS") |
Denis Egorenko | 032b8ca | 2018-09-13 17:00:23 +0400 | [diff] [blame] | 133 | } catch (Exception ex) { |
| 134 | common.warningMsg("Test finished: FAILED") |
| 135 | testResult = false |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 136 | } |
| 137 | } else { |
| 138 | common.warningMsg("Test stage has been skipped!") |
| 139 | } |
| 140 | } |
| 141 | stage("Generate config drives") { |
| 142 | // apt package genisoimage is required for this stage |
| 143 | |
| 144 | // download create-config-drive |
| 145 | // FIXME: that should be refactored, to use git clone - to be able download it from custom repo. |
Ivan Berezovskiy | 95ec927 | 2018-09-18 16:41:18 +0400 | [diff] [blame] | 146 | def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch'] |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 147 | if (mcpCommonScriptsBranch == '') { |
| 148 | mcpCommonScriptsBranch = mcpVersion |
| 149 | // Don't have n/t/s for mcp-common-scripts repo, therefore use master |
| 150 | if (["nightly", "testing", "stable"].contains(mcpVersion)) { |
| 151 | common.warningMsg("Fetching mcp-common-scripts from master!") |
| 152 | mcpCommonScriptsBranch = 'master' |
| 153 | } |
| 154 | } |
Ivan Berezovskiy | 95ec927 | 2018-09-18 16:41:18 +0400 | [diff] [blame] | 155 | |
Roman Vyalov | f0c596e | 2018-10-01 17:56:09 +0300 | [diff] [blame] | 156 | def commonScriptsRepoUrl = 'https://gerrit.mcp.mirantis.com/mcp/mcp-common-scripts' |
Ivan Berezovskiy | 95ec927 | 2018-09-18 16:41:18 +0400 | [diff] [blame] | 157 | checkout([ |
azvyagintsev | 7cdcc7a | 2018-09-25 16:47:24 +0300 | [diff] [blame] | 158 | $class : 'GitSCM', |
| 159 | branches : [[name: 'FETCH_HEAD'],], |
| 160 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']], |
| 161 | userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch],], |
Ivan Berezovskiy | 95ec927 | 2018-09-18 16:41:18 +0400 | [diff] [blame] | 162 | ]) |
| 163 | |
| 164 | sh "cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive" |
Ivan Berezovskiy | 3379c7a | 2018-09-14 19:02:46 +0400 | [diff] [blame] | 165 | sh "[ -f mcp-common-scripts/config-drive/master_config.sh ] && cp mcp-common-scripts/config-drive/master_config.sh user_data || cp mcp-common-scripts/config-drive/master_config.yaml user_data" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 166 | |
| 167 | sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines" |
| 168 | sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library" |
Ivan Berezovskiy | 3379c7a | 2018-09-14 19:02:46 +0400 | [diff] [blame] | 169 | args = "--user-data user_data --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 170 | |
| 171 | // load data from model |
| 172 | def smc = [:] |
| 173 | smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}" |
| 174 | smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 175 | smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway'] |
| 176 | smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 177 | if (templateContext['default_context'].get('deploy_network_mtu')) { |
| 178 | smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu'] |
| 179 | } |
| 180 | smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01'] |
| 181 | smc['MCP_VERSION'] = "${mcpVersion}" |
| 182 | if (templateContext['default_context']['local_repositories'] == 'True') { |
| 183 | def localRepoIP = templateContext['default_context']['local_repo_url'] |
| 184 | smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg" |
| 185 | smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial" |
| 186 | smc['PIPELINES_FROM_ISO'] = 'false' |
| 187 | smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088" |
| 188 | smc['LOCAL_REPOS'] = 'true' |
| 189 | } |
| 190 | if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') { |
| 191 | if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') { |
| 192 | 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'] |
| 193 | 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'] |
| 194 | } else { |
| 195 | smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 196 | smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port'] |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | for (i in common.entries(smc)) { |
Ivan Berezovskiy | 3379c7a | 2018-09-14 19:02:46 +0400 | [diff] [blame] | 201 | sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // create cfg config-drive |
| 205 | sh "./create-config-drive ${args}" |
| 206 | sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/") |
| 207 | |
| 208 | // save cfg iso to artifacts |
| 209 | archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso" |
| 210 | |
| 211 | if (templateContext['default_context']['local_repositories'] == 'True') { |
| 212 | def aptlyServerHostname = templateContext.default_context.aptly_server_hostname |
Ivan Berezovskiy | ffff330 | 2018-09-21 19:02:39 +0400 | [diff] [blame] | 213 | sh "[ -f mcp-common-scripts/config-drive/mirror_config.yaml ] && cp mcp-common-scripts/config-drive/mirror_config.yaml mirror_config || cp mcp-common-scripts/config-drive/mirror_config.sh mirror_config" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 214 | |
| 215 | def smc_apt = [:] |
| 216 | smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 217 | smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address'] |
| 218 | smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 219 | smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}" |
| 220 | |
| 221 | for (i in common.entries(smc_apt)) { |
Ivan Berezovskiy | ffff330 | 2018-09-21 19:02:39 +0400 | [diff] [blame] | 222 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // create apt config-drive |
Ivan Berezovskiy | ffff330 | 2018-09-21 19:02:39 +0400 | [diff] [blame] | 226 | sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso" |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 227 | sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/") |
| 228 | |
| 229 | // save apt iso to artifacts |
| 230 | archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso" |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | stage('Save changes reclass model') { |
| 235 | sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .") |
| 236 | archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz" |
| 237 | |
azvyagintsev | 636493c | 2018-09-12 17:17:05 +0300 | [diff] [blame] | 238 | if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") { |
| 239 | emailext(to: EMAIL_ADDRESS, |
| 240 | attachmentsPattern: "output-${clusterName}/*", |
| 241 | body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis", |
| 242 | subject: "Your Salt model ${clusterName}") |
| 243 | } |
| 244 | dir("output-${clusterName}") { |
| 245 | deleteDir() |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Fail, but leave possibility to get failed artifacts |
| 250 | if (!testResult && TEST_MODEL.toBoolean()) { |
| 251 | common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!') |
| 252 | error('Test stage finished: FAILURE') |
| 253 | } |
| 254 | |
| 255 | } catch (Throwable e) { |
| 256 | currentBuild.result = "FAILURE" |
| 257 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 258 | throw e |
| 259 | } finally { |
| 260 | stage('Clean workspace directories') { |
| 261 | sh(script: 'find . -mindepth 1 -delete > /dev/null || true') |
| 262 | } |
| 263 | // common.sendNotification(currentBuild.result,"",["slack"]) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 264 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 265 | } |
Mikhail Ivanov | 9f81292 | 2017-11-07 18:52:02 +0400 | [diff] [blame] | 266 | } |