blob: d1614eb6bf96c1f0e316cf2795903b7cc4affb75 [file] [log] [blame]
Richard Felkl26cae4d2017-12-19 00:19:16 +01001/**
2 *
3 * Update Salt environment pipeline
4 *
5 * Expected parameters:
azvyagintsev6dac3802018-11-09 15:11:30 +02006 * TARGET_MCP_VERSION Version of MCP to upgrade to
Denis Egorenkod13ad132018-12-17 16:56:02 +04007 * GIT_REFSPEC Git repo ref to be used
8 * DRIVE_TRAIN_PARAMS Yaml, DriveTrain releated params:
9 * SALT_MASTER_URL Salt API server location
10 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
11 * UPGRADE_SALTSTACK Upgrade SaltStack packages to new version.
12 * UPDATE_CLUSTER_MODEL Update MCP version parameter in cluster model
13 * UPDATE_PIPELINES Update pipeline repositories on Gerrit
14 * UPDATE_LOCAL_REPOS Update local repositories
Richard Felkl26cae4d2017-12-19 00:19:16 +010015 */
16
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010017salt = new com.mirantis.mk.Salt()
18common = new com.mirantis.mk.Common()
19python = new com.mirantis.mk.Python()
Richard Felkl970e0082018-06-12 18:00:51 +020020jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
azvyagintsevf3bf77a2018-11-15 19:30:04 +020021def pipelineTimeout = 12
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010022venvPepper = "venvPepper"
Richard Felkl0e80d892018-06-20 13:44:54 +020023workspace = ""
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010024
azvyagintsev6dac3802018-11-09 15:11:30 +020025def triggerMirrorJob(jobName) {
Richard Felkl970e0082018-06-12 18:00:51 +020026 params = jenkinsUtils.getJobParameters(jobName)
27 build job: jobName, parameters: [
28 [$class: 'StringParameterValue', name: 'BRANCHES', value: params.get("BRANCHES")],
29 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: params.get("CREDENTIALS_ID")],
30 [$class: 'StringParameterValue', name: 'SOURCE_URL', value: params.get("SOURCE_URL")],
31 [$class: 'StringParameterValue', name: 'TARGET_URL', value: params.get("TARGET_URL")]
32 ]
33}
34
azvyagintsev6dac3802018-11-09 15:11:30 +020035def updateSaltStack(target, pkgs) {
Denis Egorenko0d0c65f2019-02-26 16:05:03 +040036 salt.cmdRun(venvPepper, "I@salt:master", "salt -C '${target}' --async pkg.install force_yes=True pkgs='$pkgs'")
37 // can't use same function from pipeline lib, as at the moment of running upgrade pipeline Jenkins
38 // still using pipeline lib from current old mcp-version
azvyagintsev6dac3802018-11-09 15:11:30 +020039 common.retry(20, 60) {
Richard Felkl0e80d892018-06-20 13:44:54 +020040 salt.minionsReachable(venvPepper, 'I@salt:master', '*')
41 def running = salt.runSaltProcessStep(venvPepper, target, 'saltutil.running', [], null, true, 5)
azvyagintsev6dac3802018-11-09 15:11:30 +020042 for (value in running.get("return")[0].values()) {
43 if (value != []) {
Richard Felkl0e80d892018-06-20 13:44:54 +020044 throw new Exception("Not all salt-minions are ready for execution")
45 }
46 }
47 }
48
azvyagintsevf3bf77a2018-11-15 19:30:04 +020049 def saltVersion = salt.getPillar(venvPepper, 'I@salt:master', '_param:salt_version').get('return')[0].values()[0]
Richard Felklf23257e2018-08-06 09:24:23 +020050 def saltMinionVersions = salt.cmdRun(venvPepper, target, "apt-cache policy salt-common | awk '/Installed/ && /$saltVersion/'").get("return")
Richard Felkl0e80d892018-06-20 13:44:54 +020051 def saltMinionVersion = ""
52
azvyagintsev6dac3802018-11-09 15:11:30 +020053 for (minion in saltMinionVersions[0].keySet()) {
54 saltMinionVersion = saltMinionVersions[0].get(minion).replace("Salt command execution success", "").trim()
55 if (saltMinionVersion == "") {
Richard Felkl0e80d892018-06-20 13:44:54 +020056 error("Installed version of Salt on $minion doesn't match specified version in the model.")
57 }
58 }
59}
60
azvyagintsev6dac3802018-11-09 15:11:30 +020061def archiveReclassInventory(filename) {
Richard Felkl0e80d892018-06-20 13:44:54 +020062 def ret = salt.cmdRun(venvPepper, 'I@salt:master', "reclass -i", true, null, false)
63 def reclassInv = ret.values()[0]
64 writeFile file: filename, text: reclassInv.toString()
65 archiveArtifacts artifacts: "$filename"
66}
67
Denis Egorenkod13ad132018-12-17 16:56:02 +040068def validateReclassModel(ArrayList saltMinions, String suffix) {
69 try {
Denis Egorenko89a3c572019-03-22 17:03:40 +040070 dir(suffix) {
71 for(String minion in saltMinions) {
72 common.infoMsg("Reclass model validation for minion ${minion}...")
73 def ret = salt.cmdRun("${workspace}/${venvPepper}", 'I@salt:master', "reclass -n ${minion}", true, null, false).get('return')[0].values()[0]
74 writeFile file: minion, text: ret.toString()
75 }
Denis Egorenkod13ad132018-12-17 16:56:02 +040076 }
77 } catch (Exception e) {
78 common.errorMsg('Can not validate current Reclass model. Inspect failed minion manually.')
79 error(e)
80 }
81}
82
Denis Egorenko89a3c572019-03-22 17:03:40 +040083def archiveReclassModelChanges(ArrayList saltMinions, String oldSuffix, String newSuffix) {
Denis Egorenkob2213602019-03-29 16:07:26 +040084 def diffDir = 'pillarsDiff'
Denis Egorenko89a3c572019-03-22 17:03:40 +040085 dir(diffDir) {
86 for(String minion in saltMinions) {
87 def fileName = "reclass-model-${minion}-diff.out"
88 sh "diff -u ${workspace}/${oldSuffix}/${minion} ${workspace}/${newSuffix}/${minion} > ${fileName} || true"
89 }
Denis Egorenkod13ad132018-12-17 16:56:02 +040090 }
Denis Egorenko6e5e29d2019-04-15 20:23:22 +040091 archiveArtifacts artifacts: "${oldSuffix}/*"
92 archiveArtifacts artifacts: "${newSuffix}/*"
93 archiveArtifacts artifacts: "${diffDir}/*"
Denis Egorenkod13ad132018-12-17 16:56:02 +040094}
95
Denis Egorenko5a8a7e92018-12-27 14:13:48 +040096if (common.validInputParam('PIPELINE_TIMEOUT')) {
97 try {
98 pipelineTimeout = env.PIPELINE_TIMEOUT.toInteger()
99 } catch(Exception e) {
100 common.warningMsg("Provided PIPELINE_TIMEOUT parameter has invalid value: ${env.PIPELINE_TIMEOUT} - should be interger")
101 }
Martin Polreich965c9032018-08-09 16:33:57 +0200102}
103
104timeout(time: pipelineTimeout, unit: 'HOURS') {
Denis Egorenko89a3c572019-03-22 17:03:40 +0400105 node("python && docker") {
Jakub Josefa63f9862018-01-11 17:58:38 +0100106 try {
Richard Felkl0e80d892018-06-20 13:44:54 +0200107 workspace = common.getWorkspace()
Denis Egorenko89a3c572019-03-22 17:03:40 +0400108 deleteDir()
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200109 targetMcpVersion = null
110 if (!common.validInputParam('TARGET_MCP_VERSION') && !common.validInputParam('MCP_VERSION')) {
111 error('You must specify MCP version in TARGET_MCP_VERSION|MCP_VERSION variable')
azvyagintsev6dac3802018-11-09 15:11:30 +0200112 }
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200113 // bw comp. for 2018.X => 2018.11 release
114 if (common.validInputParam('MCP_VERSION')){
115 targetMcpVersion = env.MCP_VERSION
116 common.warningMsg("targetMcpVersion has been changed to:${targetMcpVersion}, which was taken from deprecated pipeline viriable:MCP_VERSION")
117 }
118 else {
119 targetMcpVersion = env.TARGET_MCP_VERSION
120 }
121 // end bw comp. for 2018.X => 2018.11 release
Denis Egorenkod13ad132018-12-17 16:56:02 +0400122 def gitTargetMcpVersion = env.getProperty('GIT_REFSPEC')
Ivan Berezovskiyd62629c2019-01-16 19:26:18 +0400123 if (targetMcpVersion in ['nightly', 'testing']) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200124 gitTargetMcpVersion = 'master'
Ivan Berezovskiyd62629c2019-01-16 19:26:18 +0400125 } else if (targetMcpVersion == 'proposed') {
126 gitTargetMcpVersion = 'proposed'
Denis Egorenko284690e2018-12-25 15:55:12 +0400127 } else if (!gitTargetMcpVersion) {
128 // backward compatibility for 2018.11.0
129 gitTargetMcpVersion = "release/${targetMcpVersion}"
azvyagintsev6dac3802018-11-09 15:11:30 +0200130 }
Ivan Berezovskiyd62629c2019-01-16 19:26:18 +0400131 common.warningMsg("gitTargetMcpVersion has been changed to:${gitTargetMcpVersion}")
Denis Egorenko284690e2018-12-25 15:55:12 +0400132 def saltMastURL = ''
133 def saltMastCreds = ''
134 def upgradeSaltStack = ''
135 def updateClusterModel = ''
136 def updatePipelines = ''
137 def updateLocalRepos = ''
138 def reclassSystemBranch = ''
Denis Egorenkob559e542019-03-29 13:24:15 +0400139 def reclassSystemBranchDefault = gitTargetMcpVersion
140 if (gitTargetMcpVersion != 'proposed') {
141 reclassSystemBranchDefault = "origin/${gitTargetMcpVersion}"
142 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400143 def driteTrainParamsYaml = env.getProperty('DRIVE_TRAIN_PARAMS')
Denis Egorenko284690e2018-12-25 15:55:12 +0400144 if (driteTrainParamsYaml) {
145 def driteTrainParams = readYaml text: driteTrainParamsYaml
146 saltMastURL = driteTrainParams.get('SALT_MASTER_URL')
Denis Egorenko928b3e82019-01-15 11:47:16 +0400147 saltMastCreds = driteTrainParams.get('SALT_MASTER_CREDENTIALS')
Denis Egorenko284690e2018-12-25 15:55:12 +0400148 upgradeSaltStack = driteTrainParams.get('UPGRADE_SALTSTACK', false).toBoolean()
149 updateClusterModel = driteTrainParams.get('UPDATE_CLUSTER_MODEL', false).toBoolean()
150 updatePipelines = driteTrainParams.get('UPDATE_PIPELINES', false).toBoolean()
151 updateLocalRepos = driteTrainParams.get('UPDATE_LOCAL_REPOS', false).toBoolean()
Denis Egorenkob559e542019-03-29 13:24:15 +0400152 reclassSystemBranch = driteTrainParams.get('RECLASS_SYSTEM_BRANCH', reclassSystemBranchDefault)
Denis Egorenko284690e2018-12-25 15:55:12 +0400153 } else {
154 // backward compatibility for 2018.11.0
155 saltMastURL = env.getProperty('SALT_MASTER_URL')
156 saltMastCreds = env.getProperty('SALT_MASTER_CREDENTIALS')
Denis Egorenkoe5f39142018-12-28 12:30:48 +0400157 upgradeSaltStack = env.getProperty('UPGRADE_SALTSTACK').toBoolean()
158 updateClusterModel = env.getProperty('UPDATE_CLUSTER_MODEL').toBoolean()
159 updatePipelines = env.getProperty('UPDATE_PIPELINES').toBoolean()
160 updateLocalRepos = env.getProperty('UPDATE_LOCAL_REPOS').toBoolean()
Denis Egorenkob559e542019-03-29 13:24:15 +0400161 reclassSystemBranch = reclassSystemBranchDefault
Denis Egorenko284690e2018-12-25 15:55:12 +0400162 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100163
Denis Egorenkod13ad132018-12-17 16:56:02 +0400164 python.setupPepperVirtualenv(venvPepper, saltMastURL, saltMastCreds)
165
Denis Egorenko89a3c572019-03-22 17:03:40 +0400166 def pillarsBeforeSuffix = 'pillarsBefore'
167 def pillarsAfterSuffix = 'pillarsAfter'
Denis Egorenkod13ad132018-12-17 16:56:02 +0400168 def inventoryBeforeFilename = "reclass-inventory-before.out"
169 def inventoryAfterFilename = "reclass-inventory-after.out"
170
171 def minions = salt.getMinions(venvPepper, '*')
Denis Egorenko89a3c572019-03-22 17:03:40 +0400172 def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
Denis Egorenkod13ad132018-12-17 16:56:02 +0400173
174 stage("Update Reclass and Salt-Formulas ") {
Denis Egorenko89a3c572019-03-22 17:03:40 +0400175 validateReclassModel(minions, pillarsBeforeSuffix)
176 archiveReclassInventory(inventoryBeforeFilename)
Denis Egorenkod13ad132018-12-17 16:56:02 +0400177
azvyagintsev6dac3802018-11-09 15:11:30 +0200178 try {
Sergey382581a2018-10-25 12:22:26 +0400179 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/ && git diff-index --quiet HEAD --")
Richard Felkl970e0082018-06-12 18:00:51 +0200180 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200181 catch (Exception ex) {
Sergey382581a2018-10-25 12:22:26 +0400182 error("You have uncommited changes in your Reclass cluster model repository. Please commit or reset them and rerun the pipeline.")
Richard Felkl970e0082018-06-12 18:00:51 +0200183 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400184 if (updateClusterModel) {
azvyagintsev6dac3802018-11-09 15:11:30 +0200185 common.infoMsg('Perform: UPDATE_CLUSTER_MODEL')
Sergey382581a2018-10-25 12:22:26 +0400186 def dateTime = common.getDatetime()
Denis Egorenko00897c52019-03-18 15:47:33 +0400187 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/ && git submodule foreach git fetch")
Sergey382581a2018-10-25 12:22:26 +0400188 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenko928b3e82019-01-15 11:47:16 +0400189 "grep -r --exclude-dir=aptly -l 'mcp_version: .*' * | xargs --no-run-if-empty sed -i 's|mcp_version: .*|mcp_version: \"$targetMcpVersion\"|g'")
azvyagintsev6dac3802018-11-09 15:11:30 +0200190 // Do the same, for deprecated variable-duplicate
191 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenko928b3e82019-01-15 11:47:16 +0400192 "grep -r --exclude-dir=aptly -l 'apt_mk_version: .*' * | xargs --no-run-if-empty sed -i 's|apt_mk_version: .*|apt_mk_version: \"$targetMcpVersion\"|g'")
Denis Egorenko284690e2018-12-25 15:55:12 +0400193 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenko928b3e82019-01-15 11:47:16 +0400194 "grep -r --exclude-dir=aptly -l 'jenkins_pipelines_branch: .*' * | xargs --no-run-if-empty sed -i 's|jenkins_pipelines_branch: .*|jenkins_pipelines_branch: \"$gitTargetMcpVersion\"|g'")
Victor Ryzhenkin202c5062019-01-15 19:58:03 +0400195 // Set new k8s param
196 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
197 "grep -r --exclude-dir=aptly -l 'kubernetes_containerd_enabled: .*' * | xargs --no-run-if-empty sed -i 's|kubernetes_containerd_enabled: .*|kubernetes_containerd_enabled: True|g'")
Denis Egorenko2a39d2c2019-01-16 19:18:31 +0400198 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
199 "grep -r --exclude-dir=aptly -l 'system.linux.system.repo.mcp.salt' * | xargs --no-run-if-empty sed -i 's/system.linux.system.repo.mcp.salt/system.linux.system.repo.mcp.apt_mirantis.salt-formulas/g'")
200 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenkoca871c22019-02-13 13:49:13 +0400201 "grep -r --exclude-dir=aptly -l 'system.linux.system.repo.mcp.contrail' * | xargs --no-run-if-empty sed -i 's/system.linux.system.repo.mcp.contrail/system.linux.system.repo.mcp.apt_mirantis.contrail/g'")
Denis Egorenko2a39d2c2019-01-16 19:18:31 +0400202 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenkoca871c22019-02-13 13:49:13 +0400203 "grep -r --exclude-dir=aptly -l 'system.linux.system.repo.mcp.updates' * | xargs --no-run-if-empty sed -i 's/system.linux.system.repo.mcp.updates/system.linux.system.repo.mcp.apt_mirantis.update/g'")
Anton Samoylov72c2ccf2019-02-13 15:57:13 +0400204 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
205 "grep -r --exclude-dir=aptly -l 'system.linux.system.repo.mcp.extra' * | xargs --no-run-if-empty sed -i 's/system.linux.system.repo.mcp.extra/system.linux.system.repo.mcp.apt_mirantis.extra/g'")
Denis Egorenkod13ad132018-12-17 16:56:02 +0400206 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/system && git checkout ${reclassSystemBranch}")
Sergey Kolekonov6ab15112019-01-23 16:47:43 +0400207 // Add kubernetes-extra repo
208 if (salt.testTarget(venvPepper, "I@kubernetes:master")) {
Sergey Kolekonovac9554c2019-04-11 16:33:54 +0400209 // docker-engine conflicts with the recent containerd versions, so it's removed during upgrade. Thus update source engine
210 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
211 "grep -r -l 'engine: docker_hybrid' kubernetes | xargs --no-run-if-empty sed -i 's/engine: docker_hybrid/engine: archive/g'")
Sergey Kolekonov6ab15112019-01-23 16:47:43 +0400212 common.infoMsg("Add kubernetes-extra repo")
213 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
214 "grep -q system.linux.system.repo.mcp.apt_mirantis.update.kubernetes_extra kubernetes/common.yml || sed -i '/classes:/ a - system.linux.system.repo.mcp.apt_mirantis.update.kubernetes_extra' kubernetes/common.yml")
215 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
216 "grep -q system.linux.system.repo.mcp.apt_mirantis.kubernetes_extra kubernetes/common.yml || sed -i '/classes:/ a - system.linux.system.repo.mcp.apt_mirantis.kubernetes_extra' kubernetes/common.yml")
217 }
Ivan Berezovskiybe630ee2019-04-15 13:23:58 +0400218 // Add all update repositories
219 def repoIncludeBase = '- system.linux.system.repo.mcp.apt_mirantis.'
220 def updateRepoList = [ 'cassandra', 'ceph', 'contrail', 'docker', 'elastic', 'extra', 'openstack', 'percona', 'salt-formulas', 'saltstack', 'ubuntu' ]
221 updateRepoList.each { repo ->
222 def repoNameUpdateInclude = "${repoIncludeBase}update.${repo}"
223 def filesWithInclude = salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && grep -Plr '\\${repoIncludeBase}${repo}\$' . || true", false).get('return')[0].values()[0].trim().tokenize('\n')
224 filesWithInclude.each { file ->
225 def updateRepoIncludeExist = salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && grep -P '\\${repoNameUpdateInclude}\$' ${file} || echo not_found", false, null, true).get('return')[0].values()[0].trim()
226 if (updateRepoIncludeExist == 'not_found') {
227 // Include needs to be added
228 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
229 "sed -i 's/\\( *\\)${repoIncludeBase}${repo}\$/&\\n\\1${repoNameUpdateInclude}/g' ${file}")
230 common.infoMsg("Update repo for ${repo} is added to ${file}")
231 }
232 }
233 }
Sergey382581a2018-10-25 12:22:26 +0400234 // Add new defaults
235 common.infoMsg("Add new defaults")
Denis Egorenkod13ad132018-12-17 16:56:02 +0400236 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^ mcp_version: ' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
Denis Egorenko928b3e82019-01-15 11:47:16 +0400237 "sed -i 's|^ _param:| _param:\\n mcp_version: \"$targetMcpVersion\"|' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml")
Sergey382581a2018-10-25 12:22:26 +0400238 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^- system.defaults\$' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
Denis Egorenko928b3e82019-01-15 11:47:16 +0400239 "sed -i 's|^classes:|classes:\\n- system.defaults|' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml")
Ivan Berezovskiyadb7d832019-04-23 14:32:02 +0400240 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
241 "grep -r -l 'docker_image_jenkins: .*' cicd | xargs --no-run-if-empty sed -i 's|\\s*docker_image_jenkins: .*||g'")
242 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
243 "grep -r -l 'docker_image_jenkins_slave: .*' cicd | xargs --no-run-if-empty sed -i 's|\\s*docker_image_jenkins_slave: .*||g'")
Sergey382581a2018-10-25 12:22:26 +0400244 common.infoMsg("The following changes were made to the cluster model and will be commited. " +
azvyagintsev6dac3802018-11-09 15:11:30 +0200245 "Please consider if you want to push them to the remote repository or not. You have to do this manually when the run is finished.")
Sergey382581a2018-10-25 12:22:26 +0400246 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git diff")
247 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git status && " +
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200248 "git add -u && git commit --allow-empty -m 'Cluster model update to the release $targetMcpVersion on $dateTime'")
Sergey382581a2018-10-25 12:22:26 +0400249 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400250
Denis Egorenko89a3c572019-03-22 17:03:40 +0400251 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'saltutil.refresh_pillar')
252 try {
253 salt.enforceState(venvPepper, 'I@salt:master', 'linux.system.repo')
254 } catch (Exception e) {
255 common.errorMsg("Something wrong with model after UPDATE_CLUSTER_MODEL step. Please check model.")
256 throw e
257 }
258
259 common.infoMsg('Running a check for compatibility with new Reclass/Salt-Formulas packages')
260 def saltModelDir = 'salt-model'
261 def nodesArtifact = 'pillarsFromValidation.tar.gz'
262 def reclassModel = 'reclassModel.tar.gz'
263 def pillarsAfterValidation = 'pillarsFromValidation'
264 try {
265 def repos = salt.getPillar(venvPepper, 'I@salt:master', "linux:system:repo").get("return")[0].values()[0]
266 def cfgInfo = salt.getPillar(venvPepper, 'I@salt:master', "reclass:storage:node:infra_cfg01_node").get("return")[0].values()[0]
267 def docker_image_for_test = salt.getPillar(venvPepper, 'I@salt:master', "_param:docker_image_cvp_sanity_checks").get("return")[0].values()[0]
268 def saltModelTesting = new com.mirantis.mk.SaltModelTesting()
269 def config = [
270 'dockerHostname': "cfg01",
271 'distribRevision': "${targetMcpVersion}",
272 'baseRepoPreConfig': true,
273 'extraRepoMergeStrategy': 'override',
274 'dockerContainerName': 'new-reclass-package-check',
275 'dockerMaxCpus': 1,
276 'image': docker_image_for_test,
277 'dockerExtraOpts': [
278 "-v ${env.WORKSPACE}/${saltModelDir}:/srv/salt/reclass",
279 "--entrypoint ''",
280 ],
281 'extraRepos': ['repo': repos, 'aprConfD': "APT::Get::AllowUnauthenticated 'true';" ],
282 'envOpts': [ "CLUSTER_NAME=${cluster_name}", "NODES_ARTIFACT_NAME=${nodesArtifact}" ]
283 ]
284 def tarName = '/tmp/currentModel.tar.gz'
285 salt.cmdRun(venvPepper, 'I@salt:master', "tar -cf ${tarName} --mode='a+rwX' --directory=/srv/salt/reclass classes")
286 if (cfgInfo == '') {
287 // case for old setups when cfg01 node model was static
288 def node_name = salt.getPillar(venvPepper, 'I@salt:master', "linux:system:name").get("return")[0].values()[0]
289 def node_domain = salt.getPillar(venvPepper, 'I@salt:master', "linux:system:domain").get("return")[0].values()[0]
290 salt.cmdRun(venvPepper, 'I@salt:master', "tar -rf ${tarName} --mode='a+rwX' --directory=/srv/salt/reclass nodes/${node_name}.${node_domain}.yml")
291 config['envOpts'].add("CFG_NODE_NAME=${node_name}.${node_domain}")
292 }
293 def modelHash = salt.cmdRun(venvPepper, 'I@salt:master', "cat ${tarName} | gzip -9 -c | base64", false, null, false).get('return')[0].values()[0]
294 writeFile file: 'modelHash', text: modelHash
295 sh "cat modelHash | base64 -d | gzip -d > ${reclassModel}"
296 sh "mkdir ${saltModelDir} && tar -xf ${reclassModel} -C ${saltModelDir}"
297
298 config['runCommands'] = [
299 '001_Install_Salt_Reclass_Packages': { sh('apt-get install -y reclass salt-formula-*') },
300 '002_Get_new_nodes': {
301 try {
302 sh('''#!/bin/bash
303 new_generated_dir=/srv/salt/_new_nodes
304 new_pillar_dir=/srv/salt/_new_pillar
305 reclass_classes=/srv/salt/reclass/classes/
306 mkdir -p ${new_generated_dir} ${new_pillar_dir}
307 nodegenerator -b ${reclass_classes} -o ${new_generated_dir} ${CLUSTER_NAME}
308 for node in $(ls ${new_generated_dir}); do
309 nodeName=$(basename -s .yml ${node})
310 reclass -n ${nodeName} -c ${reclass_classes} -u ${new_generated_dir} > ${new_pillar_dir}/${nodeName}
311 done
312 if [[ -n "${CFG_NODE_NAME}" ]]; then
313 reclass -n ${CFG_NODE_NAME} -c ${reclass_classes} -u /srv/salt/reclass/nodes > ${new_pillar_dir}/${CFG_NODE_NAME}
314 fi
315 tar -czf /tmp/${NODES_ARTIFACT_NAME} -C ${new_pillar_dir}/ .
316 ''')
317 } catch (Exception e) {
318 print "Test new nodegenerator tool is failed: ${e}"
319 throw e
320 }
321 },
322 ]
323 config['runFinally'] = [ '001_Archive_nodegenerator_artefact': {
324 sh(script: "mv /tmp/${nodesArtifact} ${env.WORKSPACE}/${nodesArtifact}")
325 archiveArtifacts artifacts: nodesArtifact
326 }]
327 saltModelTesting.setupDockerAndTest(config)
Denis Egorenkob2213602019-03-29 16:07:26 +0400328 def pillarsValidationDiff = "${pillarsAfterValidation}/diffFromOriginal"
329 sh "mkdir -p ${pillarsValidationDiff} && tar -xf ${nodesArtifact} --dir ${pillarsAfterValidation}/"
Denis Egorenko89a3c572019-03-22 17:03:40 +0400330 def changesFound = false
331 for(String minion in minions) {
332 try {
Denis Egorenkob2213602019-03-29 16:07:26 +0400333 sh (script:"diff -u -w -I '^Salt command execution success' -I '^ node: ' -I '^ uri: ' -I '^ timestamp: ' ${pillarsBeforeSuffix}/${minion} ${pillarsAfterValidation}/${minion} > ${pillarsValidationDiff}/${minion}", returnStdout: true)
Denis Egorenko89a3c572019-03-22 17:03:40 +0400334 } catch(Exception e) {
335 changesFound = true
Denis Egorenkob2213602019-03-29 16:07:26 +0400336 archiveArtifacts artifacts: "${pillarsValidationDiff}/${minion}"
337 def buildUrl = env.BUILD_URL ? env.BUILD_URL : "${env.JENKINS_URL}/job/${env.JOB_NAME}/${env.BUILD_NUMBER}"
338 common.errorMsg("Found diff changes for ${minion} minion: ${buildUrl}/artifact/${pillarsValidationDiff}/${minion}/*view*/ ")
Denis Egorenko89a3c572019-03-22 17:03:40 +0400339 }
340 }
341 if (changesFound) {
342 common.warningMsg('Found diff changes between current pillar data and updated. Inspect logs above.')
343 input message: 'Continue anyway?'
344 } else {
345 common.infoMsg('Diff between current pillar data and updated one - not found.')
346 }
347 } catch (Exception updateErr) {
348 common.warningMsg(updateErr)
349 common.warningMsg('Failed to validate update Salt Formulas repos/packages.')
350 input message: 'Continue anyway?'
351 } finally {
352 sh "rm -rf ${saltModelDir} ${nodesArtifact} ${pillarsAfterValidation} ${reclassModel}"
353 }
354
Denis Egorenkod13ad132018-12-17 16:56:02 +0400355 try {
356 common.infoMsg('Perform: UPDATE Salt Formulas')
Denis Egorenkod13ad132018-12-17 16:56:02 +0400357 def saltEnv = salt.getPillar(venvPepper, 'I@salt:master', "_param:salt_master_base_environment").get("return")[0].values()[0]
358 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'state.sls_id', ["salt_master_${saltEnv}_pkg_formulas",'salt.master.env'])
359 } catch (Exception updateErr) {
360 common.warningMsg(updateErr)
361 common.warningMsg('Failed to update Salt Formulas repos/packages. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
362 input message: 'Continue anyway?'
363 }
364
Denis Egorenkod13ad132018-12-17 16:56:02 +0400365 try {
366 common.infoMsg('Perform: UPDATE Reclass package')
367 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'pkg.install', ["reclass"])
368 } catch (Exception updateErr) {
369 common.warningMsg(updateErr)
370 common.warningMsg('Failed to update Reclass package. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
371 input message: 'Continue anyway?'
372 }
373
374 salt.fullRefresh(venvPepper, 'I@salt:master')
Sergey1b81c062018-10-16 17:10:21 +0400375 salt.enforceState(venvPepper, 'I@salt:master', 'reclass.storage', true)
Denis Egorenkod13ad132018-12-17 16:56:02 +0400376 try {
377 salt.enforceState(venvPepper, "I@salt:master", 'reclass', true)
378 }
379 catch (Exception ex) {
380 error("Reclass fails rendering. Pay attention to your cluster model.")
381 }
382
383 salt.fullRefresh(venvPepper, '*')
384
385 try {
386 salt.cmdRun(venvPepper, 'I@salt:master', "reclass-salt --top")
387 }
388 catch (Exception ex) {
389 error("Reclass fails rendering. Pay attention to your cluster model.")
390 }
391
392 archiveReclassInventory(inventoryAfterFilename)
393
394 sh "diff -u $inventoryBeforeFilename $inventoryAfterFilename > reclass-inventory-diff.out || true"
395 archiveArtifacts artifacts: "reclass-inventory-diff.out"
396
Denis Egorenko89a3c572019-03-22 17:03:40 +0400397 validateReclassModel(minions, pillarsAfterSuffix)
398 archiveReclassModelChanges(minions, pillarsBeforeSuffix, pillarsAfterSuffix)
Jakub Josefa63f9862018-01-11 17:58:38 +0100399 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100400
Denis Egorenkod13ad132018-12-17 16:56:02 +0400401 if (updateLocalRepos) {
azvyagintsev6dac3802018-11-09 15:11:30 +0200402 stage("Update local repos") {
Jakub Josefa63f9862018-01-11 17:58:38 +0100403 common.infoMsg("Updating local repositories")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700404
Sergey1b81c062018-10-16 17:10:21 +0400405 def engine = salt.getPillar(venvPepper, 'I@aptly:publisher', "aptly:publisher:source:engine")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700406 runningOnDocker = engine.get("return")[0].containsValue("docker")
407
408 if (runningOnDocker) {
409 common.infoMsg("Aptly is running as Docker container")
azvyagintsev6dac3802018-11-09 15:11:30 +0200410 } else {
Sam Stoelingaaab79702018-04-09 18:49:39 -0700411 common.infoMsg("Aptly isn't running as Docker container. Going to use aptly user for executing aptly commands")
412 }
413
azvyagintsev6dac3802018-11-09 15:11:30 +0200414 if (runningOnDocker) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200415 salt.cmdRun(venvPepper, 'I@aptly:publisher', "aptly mirror list --raw | grep -E '*' | xargs --no-run-if-empty -n 1 aptly mirror drop -force", true, null, true)
azvyagintsev6dac3802018-11-09 15:11:30 +0200416 } else {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200417 salt.cmdRun(venvPepper, 'I@aptly:publisher', "aptly mirror list --raw | grep -E '*' | xargs --no-run-if-empty -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly'])
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100418 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100419
Sergey1b81c062018-10-16 17:10:21 +0400420 salt.enforceState(venvPepper, 'I@aptly:publisher', 'aptly', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100421
azvyagintsev6dac3802018-11-09 15:11:30 +0200422 if (runningOnDocker) {
Sergey1b81c062018-10-16 17:10:21 +0400423 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv"], null, true)
424 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-frv -u http://10.99.0.1:8080"], null, true)
azvyagintsev6dac3802018-11-09 15:11:30 +0200425 } else {
Sergey1b81c062018-10-16 17:10:21 +0400426 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
427 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-afrv", 'runas=aptly'], null, true)
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100428 }
Richard Felkl79d7df12018-01-05 16:40:11 +0100429
Sergey1b81c062018-10-16 17:10:21 +0400430 salt.enforceState(venvPepper, 'I@aptly:publisher', 'docker.client.registry', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100431
Sergey1b81c062018-10-16 17:10:21 +0400432 salt.enforceState(venvPepper, 'I@aptly:publisher', 'debmirror', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100433
Sergey1b81c062018-10-16 17:10:21 +0400434 salt.enforceState(venvPepper, 'I@aptly:publisher', 'git.server', true)
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100435
Sergey1b81c062018-10-16 17:10:21 +0400436 salt.enforceState(venvPepper, 'I@aptly:publisher', 'linux.system.file', true)
Jakub Josefa63f9862018-01-11 17:58:38 +0100437 }
438 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100439
azvyagintsev6dac3802018-11-09 15:11:30 +0200440 stage("Update Drivetrain") {
Denis Egorenkod13ad132018-12-17 16:56:02 +0400441 if (upgradeSaltStack) {
442 updateSaltStack("I@salt:master", '["salt-master", "salt-common", "salt-api", "salt-minion"]')
Richard Felkl0e80d892018-06-20 13:44:54 +0200443
Richard Felkl0e80d892018-06-20 13:44:54 +0200444 salt.enforceState(venvPepper, "I@linux:system", 'linux.system.repo', true)
Richard Felkl0e80d892018-06-20 13:44:54 +0200445 updateSaltStack("I@salt:minion and not I@salt:master", '["salt-minion"]')
446 }
447
Denis Egorenkod13ad132018-12-17 16:56:02 +0400448 if (updatePipelines) {
Richard Felkl970e0082018-06-12 18:00:51 +0200449 triggerMirrorJob("git-mirror-downstream-mk-pipelines")
450 triggerMirrorJob("git-mirror-downstream-pipeline-library")
451 }
452
Denis Egorenkod00c17e2019-04-25 20:17:27 +0400453 // update minions certs
Denis Egorenkod04fe942019-04-26 15:13:16 +0400454 // call for `salt.minion.ca` state on related nodes to make sure
455 // mine was updated with required data after salt-minion/salt-master restart salt:minion:ca
456 salt.enforceState(venvPepper, "I@salt:minion:ca", 'salt.minion.ca', true)
Denis Egorenkod00c17e2019-04-25 20:17:27 +0400457 salt.enforceState(venvPepper, "I@salt:minion", 'salt.minion.cert', true)
Ann Kamyshnikovae2531ba2019-05-06 12:56:38 +0400458 // run `salt.minion` to refresh all minion configs (for example _keystone.conf)
459 salt.enforceState([saltId: venvPepper, target: "I@salt:minion ${extra_tgt}", state: ['salt.minion'], read_timeout: 60, retries: 2])
Denis Egorenkoaeaa1de2019-01-15 11:58:12 +0400460 // updating users and keys
461 salt.enforceState(venvPepper, "I@linux:system", 'linux.system.user', true)
462 salt.enforceState(venvPepper, "I@linux:system", 'openssh', true)
463
Richard Felkl970e0082018-06-12 18:00:51 +0200464 salt.enforceState(venvPepper, "I@jenkins:client", 'jenkins.client', true)
465
466 salt.cmdRun(venvPepper, "I@salt:master", "salt -C 'I@jenkins:client and I@docker:client' state.sls docker.client --async")
467
468 sleep(180)
469
470 common.infoMsg("Checking if Docker containers are up")
471
azvyagintsev6dac3802018-11-09 15:11:30 +0200472 try {
473 common.retry(10, 30) {
Richard Felkl970e0082018-06-12 18:00:51 +0200474 salt.cmdRun(venvPepper, 'I@jenkins:client and I@docker:client', "! docker service ls | tail -n +2 | grep -v -E '\\s([0-9])/\\1\\s'")
475 }
476 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200477 catch (Exception ex) {
Richard Felkl970e0082018-06-12 18:00:51 +0200478 error("Docker containers for CI/CD services are having troubles with starting.")
479 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100480 }
481 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100482 catch (Throwable e) {
483 // If there was an error or exception thrown, the build failed
484 currentBuild.result = "FAILURE"
485 throw e
Richard Felkl26cae4d2017-12-19 00:19:16 +0100486 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100487 }
Jakub Josef2c21c6c2018-02-08 18:51:42 +0100488}