blob: 7a1db598653609081166e21a45c5779594f7a9c4 [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()
azvyagintsevbc359d42019-04-10 19:53:22 +030019mcpCommon = new com.mirantis.mcp.Common()
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010020python = new com.mirantis.mk.Python()
Richard Felkl970e0082018-06-12 18:00:51 +020021jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
azvyagintsevf3bf77a2018-11-15 19:30:04 +020022def pipelineTimeout = 12
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010023venvPepper = "venvPepper"
Richard Felkl0e80d892018-06-20 13:44:54 +020024workspace = ""
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010025
azvyagintsevbc359d42019-04-10 19:53:22 +030026def triggerMirrorJob(String jobName, String reclassSystemBranch) {
Richard Felkl970e0082018-06-12 18:00:51 +020027 params = jenkinsUtils.getJobParameters(jobName)
azvyagintsevbc359d42019-04-10 19:53:22 +030028 try {
29 build job: jobName, parameters: [
30 [$class: 'StringParameterValue', name: 'BRANCHES', value: params.get('BRANCHES')],
31 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: params.get('CREDENTIALS_ID')],
32 [$class: 'StringParameterValue', name: 'SOURCE_URL', value: params.get('SOURCE_URL')],
33 [$class: 'StringParameterValue', name: 'TARGET_URL', value: params.get('TARGET_URL')]
34 ]
35 } catch (Exception updateErr) {
36 common.warningMsg(updateErr)
37 common.warningMsg('Attempt to update git repo in failsafe manner')
38 build job: jobName, parameters: [
39 [$class: 'StringParameterValue', name: 'BRANCHES', value: reclassSystemBranch.replace('origin/', '')],
40 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: params.get('CREDENTIALS_ID')],
41 [$class: 'StringParameterValue', name: 'SOURCE_URL', value: params.get('SOURCE_URL')],
42 [$class: 'StringParameterValue', name: 'TARGET_URL', value: params.get('TARGET_URL')]
43 ]
44 }
Richard Felkl970e0082018-06-12 18:00:51 +020045}
46
azvyagintsev6dac3802018-11-09 15:11:30 +020047def updateSaltStack(target, pkgs) {
Denis Egorenko4b54e7f2019-02-26 16:05:03 +040048 salt.cmdRun(venvPepper, "I@salt:master", "salt -C '${target}' --async pkg.install force_yes=True pkgs='$pkgs'")
49 // can't use same function from pipeline lib, as at the moment of running upgrade pipeline Jenkins
50 // still using pipeline lib from current old mcp-version
azvyagintsev6dac3802018-11-09 15:11:30 +020051 common.retry(20, 60) {
Richard Felkl0e80d892018-06-20 13:44:54 +020052 salt.minionsReachable(venvPepper, 'I@salt:master', '*')
53 def running = salt.runSaltProcessStep(venvPepper, target, 'saltutil.running', [], null, true, 5)
azvyagintsev6dac3802018-11-09 15:11:30 +020054 for (value in running.get("return")[0].values()) {
55 if (value != []) {
Richard Felkl0e80d892018-06-20 13:44:54 +020056 throw new Exception("Not all salt-minions are ready for execution")
57 }
58 }
59 }
60
azvyagintsevf3bf77a2018-11-15 19:30:04 +020061 def saltVersion = salt.getPillar(venvPepper, 'I@salt:master', '_param:salt_version').get('return')[0].values()[0]
Richard Felklf23257e2018-08-06 09:24:23 +020062 def saltMinionVersions = salt.cmdRun(venvPepper, target, "apt-cache policy salt-common | awk '/Installed/ && /$saltVersion/'").get("return")
Richard Felkl0e80d892018-06-20 13:44:54 +020063 def saltMinionVersion = ""
64
azvyagintsev6dac3802018-11-09 15:11:30 +020065 for (minion in saltMinionVersions[0].keySet()) {
66 saltMinionVersion = saltMinionVersions[0].get(minion).replace("Salt command execution success", "").trim()
67 if (saltMinionVersion == "") {
Richard Felkl0e80d892018-06-20 13:44:54 +020068 error("Installed version of Salt on $minion doesn't match specified version in the model.")
69 }
70 }
71}
72
azvyagintsevbc359d42019-04-10 19:53:22 +030073def wa29155(ArrayList saltMinions, String cname) {
74 // WA for PROD-29155. Issue cause due patch https://gerrit.mcp.mirantis.com/#/c/37932/
75 // CHeck for existence cmp nodes, and try to render it. Is failed, apply ssh-key wa
76 def ret = ''
77 def patched = false
78 def wa29155ClassName = 'cluster.' + cname + '.infra.secrets_nova_wa29155'
79 def wa29155File = "/srv/salt/reclass/classes/cluster/${cname}/infra/secrets_nova_wa29155.yml"
80
81 try {
82 salt.cmdRun(venvPepper, 'I@salt:master', "test ! -f ${wa29155File}", true, null, false)
83 }
84 catch (Exception ex) {
85 common.infoMsg('Work-around for PROD-29155 already apply, nothing todo')
86 return
87 }
88 salt.fullRefresh(venvPepper, 'cfg*')
89 salt.fullRefresh(venvPepper, 'cmp*')
90 for (String minion in saltMinions) {
91 if (!minion.startsWith('cmp')) {
92 continue
93 }
94 // First attempt, second will be performed in next validateReclassModel() stages
95 try {
96 salt.cmdRun(venvPepper, 'I@salt:master', "reclass -n ${minion}", true, null, false).get('return')[0].values()[0].replaceAll('Salt command execution success','').trim()
97 } catch (Exception e) {
98 common.errorMsg(e.toString())
99 if (patched) {
100 error("Node: ${minion} failed to render after reclass-system upgrade!WA29155 probably didn't help.")
101 }
102 // check, that failed exactly by our case, by key-length check.
103 def missed_key = salt.getPillar(venvPepper, minion, '_param:nova_compute_ssh_private').get("return")[0].values()[0]
104 if (missed_key != '') {
105 error("Node: ${minion} failed to render after reclass-system upgrade!")
106 }
107 common.warningMsg("Perform: Attempt to apply WA for PROD-29155\n" +
108 "See https://gerrit.mcp.mirantis.com/#/c/37932/ for more info")
109 common.warningMsg('WA-PROD-29155 Generating new ssh key at master node')
110 def _tempFile = "/tmp/nova_wa29155_" + UUID.randomUUID().toString().take(8)
111 common.infoMsg('Perform: generation NEW ssh-private key for nova-compute')
112 salt.cmdRun(venvPepper, 'I@salt:master', "ssh-keygen -f ${_tempFile} -N '' -q")
113 def _pub_k = salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'cmd.run', "cat ${_tempFile}.pub").get('return')[0].values()[0].trim()
114 def _priv_k = salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'cmd.run', "cat ${_tempFile}").get('return')[0].values()[0].trim()
115 salt.cmdRun(venvPepper, 'I@salt:master', "rm -fv ${_tempFile}", false, null, false)
116 def novaKeysDict = [
117 "parameters": [
118 "_param": [
119 "nova_compute_ssh_private": _priv_k,
120 "nova_compute_ssh_public" : _pub_k
121 ]
122 ]
123 ]
124 writeYaml file: "${_tempFile}", data: novaKeysDict
125 def yamlData = sh(script: "cat ${_tempFile} | base64", returnStdout: true).trim()
126 salt.cmdRun(venvPepper, 'I@salt:master', "echo '${yamlData}' | base64 -d > ${wa29155File}", false, null, false)
127 common.infoMsg("Add $wa29155ClassName class into secrets.yml")
128
129 // Add 'classes:' directive
130 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cname && " +
131 "grep -q 'classes:' infra/secrets.yml || sed -i '1iclasses:' infra/secrets.yml")
132
133 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cname && " +
134 "grep -q '${wa29155ClassName}' infra/secrets.yml || sed -i '/classes:/ a - $wa29155ClassName' infra/secrets.yml")
135 salt.fullRefresh(venvPepper, 'cfg*')
136 salt.fullRefresh(venvPepper, 'cmp*')
137 patched = true
138 }
139 }
140 if (patched) {
141 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cname && git status && " +
142 "git add ${wa29155File} && git add -u && git commit --allow-empty -m 'Cluster model updated with WA for PROD-29155. Issue cause due patch https://gerrit.mcp.mirantis.com/#/c/37932/ at ${common.getDatetime()}' ")
143 common.infoMsg('Work-around for PROD-29155 successfully applied')
144 }
145
146}
147
azvyagintsev6dac3802018-11-09 15:11:30 +0200148def archiveReclassInventory(filename) {
azvyagintsevbc359d42019-04-10 19:53:22 +0300149 def _tmp_file = '/tmp/' + filename + UUID.randomUUID().toString().take(8)
150 // jenkins may fail at overheap. Compress data with gzip like WA
151 def ret = salt.cmdRun(venvPepper, 'I@salt:master', 'reclass -i 2>/dev/null | gzip -9 -c | base64', true, null, false).get('return')[0].values()[0].replaceAll('Salt command execution success','').trim()
152 def _tmp = sh(script: "echo '$ret' > ${_tmp_file}", returnStdout: false)
153 sh(script: "cat ${_tmp_file} | base64 -d | gzip -d > $filename", returnStdout: false)
154 archiveArtifacts artifacts: filename
155 sh(script: "rm -v ${_tmp_file}|| true")
Richard Felkl0e80d892018-06-20 13:44:54 +0200156}
157
Denis Egorenkod13ad132018-12-17 16:56:02 +0400158def validateReclassModel(ArrayList saltMinions, String suffix) {
159 try {
azvyagintsevbc359d42019-04-10 19:53:22 +0300160 for (String minion in saltMinions) {
Denis Egorenkod13ad132018-12-17 16:56:02 +0400161 common.infoMsg("Reclass model validation for minion ${minion}...")
azvyagintsevbc359d42019-04-10 19:53:22 +0300162 def reclassInv = salt.cmdRun(venvPepper, 'I@salt:master', "reclass -n ${minion}", true, null, false).get('return')[0].values()[0].replaceAll('Salt command execution success','').trim()
Denis Egorenkod13ad132018-12-17 16:56:02 +0400163 writeFile file: "inventory-${minion}-${suffix}.out", text: reclassInv.toString()
164 }
165 } catch (Exception e) {
166 common.errorMsg('Can not validate current Reclass model. Inspect failed minion manually.')
azvyagintsevbc359d42019-04-10 19:53:22 +0300167 error(e.toString())
Denis Egorenkod13ad132018-12-17 16:56:02 +0400168 }
169}
170
azvyagintsevbc359d42019-04-10 19:53:22 +0300171def archiveReclassModelChanges(ArrayList saltMinions, String oldSuffix = 'before', String newSuffix = 'after') {
172 for (String minion in saltMinions) {
Denis Egorenkod13ad132018-12-17 16:56:02 +0400173 def fileName = "reclass-model-${minion}-diff.out"
174 sh "diff -u inventory-${minion}-${oldSuffix}.out inventory-${minion}-${newSuffix}.out > ${fileName} || true"
175 archiveArtifacts artifacts: "${fileName}"
176 }
177}
178
Denis Egorenko5a8a7e92018-12-27 14:13:48 +0400179if (common.validInputParam('PIPELINE_TIMEOUT')) {
180 try {
181 pipelineTimeout = env.PIPELINE_TIMEOUT.toInteger()
azvyagintsevbc359d42019-04-10 19:53:22 +0300182 } catch (Exception e) {
Denis Egorenko5a8a7e92018-12-27 14:13:48 +0400183 common.warningMsg("Provided PIPELINE_TIMEOUT parameter has invalid value: ${env.PIPELINE_TIMEOUT} - should be interger")
184 }
Martin Polreich965c9032018-08-09 16:33:57 +0200185}
186
187timeout(time: pipelineTimeout, unit: 'HOURS') {
Jakub Josefa63f9862018-01-11 17:58:38 +0100188 node("python") {
189 try {
azvyagintsevbc359d42019-04-10 19:53:22 +0300190 def inventoryBeforeFilename = "reclass-inventory-before.out"
191 def inventoryAfterFilename = "reclass-inventory-after.out"
Richard Felkl0e80d892018-06-20 13:44:54 +0200192 workspace = common.getWorkspace()
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200193 targetMcpVersion = null
194 if (!common.validInputParam('TARGET_MCP_VERSION') && !common.validInputParam('MCP_VERSION')) {
195 error('You must specify MCP version in TARGET_MCP_VERSION|MCP_VERSION variable')
azvyagintsev6dac3802018-11-09 15:11:30 +0200196 }
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200197 // bw comp. for 2018.X => 2018.11 release
azvyagintsevbc359d42019-04-10 19:53:22 +0300198 if (common.validInputParam('MCP_VERSION')) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200199 targetMcpVersion = env.MCP_VERSION
200 common.warningMsg("targetMcpVersion has been changed to:${targetMcpVersion}, which was taken from deprecated pipeline viriable:MCP_VERSION")
azvyagintsevbc359d42019-04-10 19:53:22 +0300201 } else {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200202 targetMcpVersion = env.TARGET_MCP_VERSION
203 }
204 // end bw comp. for 2018.X => 2018.11 release
Denis Egorenkod13ad132018-12-17 16:56:02 +0400205 def gitTargetMcpVersion = env.getProperty('GIT_REFSPEC')
Ivan Berezovskiydb325092019-01-16 19:26:18 +0400206 if (targetMcpVersion in ['nightly', 'testing']) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200207 gitTargetMcpVersion = 'master'
Ivan Berezovskiydb325092019-01-16 19:26:18 +0400208 } else if (targetMcpVersion == 'proposed') {
209 gitTargetMcpVersion = 'proposed'
Denis Egorenko284690e2018-12-25 15:55:12 +0400210 } else if (!gitTargetMcpVersion) {
211 // backward compatibility for 2018.11.0
212 gitTargetMcpVersion = "release/${targetMcpVersion}"
azvyagintsev6dac3802018-11-09 15:11:30 +0200213 }
Ivan Berezovskiydb325092019-01-16 19:26:18 +0400214 common.warningMsg("gitTargetMcpVersion has been changed to:${gitTargetMcpVersion}")
Denis Egorenko284690e2018-12-25 15:55:12 +0400215 def saltMastURL = ''
216 def saltMastCreds = ''
217 def upgradeSaltStack = ''
218 def updateClusterModel = ''
219 def updatePipelines = ''
220 def updateLocalRepos = ''
221 def reclassSystemBranch = ''
Denis Egorenko517e06c2019-03-29 13:24:15 +0400222 def reclassSystemBranchDefault = gitTargetMcpVersion
223 if (gitTargetMcpVersion != 'proposed') {
224 reclassSystemBranchDefault = "origin/${gitTargetMcpVersion}"
225 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400226 def driteTrainParamsYaml = env.getProperty('DRIVE_TRAIN_PARAMS')
Denis Egorenko284690e2018-12-25 15:55:12 +0400227 if (driteTrainParamsYaml) {
228 def driteTrainParams = readYaml text: driteTrainParamsYaml
229 saltMastURL = driteTrainParams.get('SALT_MASTER_URL')
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400230 saltMastCreds = driteTrainParams.get('SALT_MASTER_CREDENTIALS')
Denis Egorenko284690e2018-12-25 15:55:12 +0400231 upgradeSaltStack = driteTrainParams.get('UPGRADE_SALTSTACK', false).toBoolean()
232 updateClusterModel = driteTrainParams.get('UPDATE_CLUSTER_MODEL', false).toBoolean()
233 updatePipelines = driteTrainParams.get('UPDATE_PIPELINES', false).toBoolean()
234 updateLocalRepos = driteTrainParams.get('UPDATE_LOCAL_REPOS', false).toBoolean()
Denis Egorenko517e06c2019-03-29 13:24:15 +0400235 reclassSystemBranch = driteTrainParams.get('RECLASS_SYSTEM_BRANCH', reclassSystemBranchDefault)
Denis Egorenko284690e2018-12-25 15:55:12 +0400236 } else {
237 // backward compatibility for 2018.11.0
238 saltMastURL = env.getProperty('SALT_MASTER_URL')
239 saltMastCreds = env.getProperty('SALT_MASTER_CREDENTIALS')
Denis Egorenkoe5f39142018-12-28 12:30:48 +0400240 upgradeSaltStack = env.getProperty('UPGRADE_SALTSTACK').toBoolean()
241 updateClusterModel = env.getProperty('UPDATE_CLUSTER_MODEL').toBoolean()
242 updatePipelines = env.getProperty('UPDATE_PIPELINES').toBoolean()
243 updateLocalRepos = env.getProperty('UPDATE_LOCAL_REPOS').toBoolean()
Denis Egorenko517e06c2019-03-29 13:24:15 +0400244 reclassSystemBranch = reclassSystemBranchDefault
Denis Egorenko284690e2018-12-25 15:55:12 +0400245 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400246 python.setupPepperVirtualenv(venvPepper, saltMastURL, saltMastCreds)
Denis Egorenkod13ad132018-12-17 16:56:02 +0400247 def minions = salt.getMinions(venvPepper, '*')
248
azvyagintsevbc359d42019-04-10 19:53:22 +0300249 stage('Update Reclass and Salt-Formulas') {
250 common.infoMsg('Perform: Full salt sync')
251 salt.fullRefresh(venvPepper, '*')
252 common.infoMsg('Perform: Validate reclass medata before processing')
Denis Egorenkod13ad132018-12-17 16:56:02 +0400253 validateReclassModel(minions, 'before')
254
Richard Felkl970e0082018-06-12 18:00:51 +0200255 def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
azvyagintsev6dac3802018-11-09 15:11:30 +0200256 try {
azvyagintsevbc359d42019-04-10 19:53:22 +0300257 salt.cmdRun(venvPepper, 'I@salt:master', 'cd /srv/salt/reclass/ && git status && git diff-index --quiet HEAD --')
Richard Felkl970e0082018-06-12 18:00:51 +0200258 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200259 catch (Exception ex) {
azvyagintsevbc359d42019-04-10 19:53:22 +0300260 error('You have uncommitted changes in your Reclass cluster model repository. Please commit or reset them and rerun the pipeline.')
Richard Felkl970e0082018-06-12 18:00:51 +0200261 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400262 if (updateClusterModel) {
azvyagintsev6dac3802018-11-09 15:11:30 +0200263 common.infoMsg('Perform: UPDATE_CLUSTER_MODEL')
Sergey382581a2018-10-25 12:22:26 +0400264 def dateTime = common.getDatetime()
Denis Egorenko20254e72019-03-18 15:47:33 +0400265 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/ && git submodule foreach git fetch")
Sergey382581a2018-10-25 12:22:26 +0400266 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400267 "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 +0200268 // Do the same, for deprecated variable-duplicate
269 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400270 "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 +0400271 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400272 "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 Ryzhenkinabbefe62019-01-15 19:58:03 +0400273 // Set new k8s param
274 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
275 "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 Egorenkoe5b49512019-01-16 19:18:31 +0400276 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
277 "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'")
278 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenko301cc8a2019-02-13 13:49:13 +0400279 "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 Egorenkoe5b49512019-01-16 19:18:31 +0400280 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
Denis Egorenko301cc8a2019-02-13 13:49:13 +0400281 "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 Samoylov308c7ca2019-02-13 15:57:13 +0400282 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
azvyagintsevbc359d42019-04-10 19:53:22 +0300283 "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 +0400284 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/system && git checkout ${reclassSystemBranch}")
Sergey Kolekonov5dfbf7a2019-01-23 16:47:43 +0400285 // Add kubernetes-extra repo
286 if (salt.testTarget(venvPepper, "I@kubernetes:master")) {
Sergey Kolekonov3fde8b12019-04-11 16:33:54 +0400287 // docker-engine conflicts with the recent containerd versions, so it's removed during upgrade. Thus update source engine
288 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
289 "grep -r -l 'engine: docker_hybrid' kubernetes | xargs --no-run-if-empty sed -i 's/engine: docker_hybrid/engine: archive/g'")
Sergey Kolekonov5dfbf7a2019-01-23 16:47:43 +0400290 common.infoMsg("Add kubernetes-extra repo")
291 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
292 "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")
293 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
294 "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")
295 }
Ivan Berezovskiy41c76792019-04-15 13:23:58 +0400296 // Add all update repositories
297 def repoIncludeBase = '- system.linux.system.repo.mcp.apt_mirantis.'
azvyagintsevbc359d42019-04-10 19:53:22 +0300298 def updateRepoList = ['cassandra', 'ceph', 'contrail', 'docker', 'elastic', 'extra', 'openstack', 'percona', 'salt-formulas', 'saltstack', 'ubuntu']
Ivan Berezovskiy41c76792019-04-15 13:23:58 +0400299 updateRepoList.each { repo ->
300 def repoNameUpdateInclude = "${repoIncludeBase}update.${repo}"
301 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')
302 filesWithInclude.each { file ->
303 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()
304 if (updateRepoIncludeExist == 'not_found') {
305 // Include needs to be added
306 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
azvyagintsevbc359d42019-04-10 19:53:22 +0300307 "sed -i 's/\\( *\\)${repoIncludeBase}${repo}\$/&\\n\\1${repoNameUpdateInclude}/g' ${file}")
Ivan Berezovskiy41c76792019-04-15 13:23:58 +0400308 common.infoMsg("Update repo for ${repo} is added to ${file}")
309 }
310 }
311 }
Sergey382581a2018-10-25 12:22:26 +0400312 // Add new defaults
313 common.infoMsg("Add new defaults")
Denis Egorenkod13ad132018-12-17 16:56:02 +0400314 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^ mcp_version: ' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400315 "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 +0400316 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^- system.defaults\$' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
Denis Egorenkofcbc6b82019-01-15 11:47:16 +0400317 "sed -i 's|^classes:|classes:\\n- system.defaults|' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml")
Sergey382581a2018-10-25 12:22:26 +0400318 common.infoMsg("The following changes were made to the cluster model and will be commited. " +
azvyagintsev6dac3802018-11-09 15:11:30 +0200319 "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 +0400320 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git diff")
321 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git status && " +
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200322 "git add -u && git commit --allow-empty -m 'Cluster model update to the release $targetMcpVersion on $dateTime'")
Sergey382581a2018-10-25 12:22:26 +0400323 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400324 try {
325 common.infoMsg('Perform: UPDATE Salt Formulas')
326 salt.enforceState(venvPepper, 'I@salt:master', 'linux.system.repo')
327 def saltEnv = salt.getPillar(venvPepper, 'I@salt:master', "_param:salt_master_base_environment").get("return")[0].values()[0]
azvyagintsevbc359d42019-04-10 19:53:22 +0300328 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'state.sls_id', ["salt_master_${saltEnv}_pkg_formulas", 'salt.master.env'])
Denis Egorenkod13ad132018-12-17 16:56:02 +0400329 } catch (Exception updateErr) {
330 common.warningMsg(updateErr)
331 common.warningMsg('Failed to update Salt Formulas repos/packages. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
332 input message: 'Continue anyway?'
333 }
334
azvyagintsevbc359d42019-04-10 19:53:22 +0300335 wa29155(minions, cluster_name)
336 common.infoMsg('Perform: archiveReclassInventory before upgrade')
Denis Egorenkod13ad132018-12-17 16:56:02 +0400337 archiveReclassInventory(inventoryBeforeFilename)
338
339 try {
340 common.infoMsg('Perform: UPDATE Reclass package')
341 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'pkg.install', ["reclass"])
342 } catch (Exception updateErr) {
343 common.warningMsg(updateErr)
344 common.warningMsg('Failed to update Reclass package. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
345 input message: 'Continue anyway?'
346 }
347
348 salt.fullRefresh(venvPepper, 'I@salt:master')
Sergey1b81c062018-10-16 17:10:21 +0400349 salt.enforceState(venvPepper, 'I@salt:master', 'reclass.storage', true)
Denis Egorenkod13ad132018-12-17 16:56:02 +0400350 try {
351 salt.enforceState(venvPepper, "I@salt:master", 'reclass', true)
352 }
353 catch (Exception ex) {
354 error("Reclass fails rendering. Pay attention to your cluster model.")
355 }
356
357 salt.fullRefresh(venvPepper, '*')
Denis Egorenkod13ad132018-12-17 16:56:02 +0400358 try {
359 salt.cmdRun(venvPepper, 'I@salt:master', "reclass-salt --top")
360 }
361 catch (Exception ex) {
azvyagintsevbc359d42019-04-10 19:53:22 +0300362
363 error('Reclass fails rendering. Pay attention to your cluster model.' +
364 'ErrorMessage:' + ex.toString())
Denis Egorenkod13ad132018-12-17 16:56:02 +0400365 }
366
azvyagintsevbc359d42019-04-10 19:53:22 +0300367 common.infoMsg('Perform: archiveReclassInventory AFTER upgrade')
Denis Egorenkod13ad132018-12-17 16:56:02 +0400368 archiveReclassInventory(inventoryAfterFilename)
369
370 sh "diff -u $inventoryBeforeFilename $inventoryAfterFilename > reclass-inventory-diff.out || true"
371 archiveArtifacts artifacts: "reclass-inventory-diff.out"
372
373 validateReclassModel(minions, 'after')
374 archiveReclassModelChanges(minions)
Jakub Josefa63f9862018-01-11 17:58:38 +0100375 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100376
Denis Egorenkod13ad132018-12-17 16:56:02 +0400377 if (updateLocalRepos) {
Sergey1b81c062018-10-16 17:10:21 +0400378 def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
azvyagintsev6dac3802018-11-09 15:11:30 +0200379 stage("Update local repos") {
Jakub Josefa63f9862018-01-11 17:58:38 +0100380 common.infoMsg("Updating local repositories")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700381
Sergey1b81c062018-10-16 17:10:21 +0400382 def engine = salt.getPillar(venvPepper, 'I@aptly:publisher', "aptly:publisher:source:engine")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700383 runningOnDocker = engine.get("return")[0].containsValue("docker")
384
385 if (runningOnDocker) {
386 common.infoMsg("Aptly is running as Docker container")
azvyagintsev6dac3802018-11-09 15:11:30 +0200387 } else {
Sam Stoelingaaab79702018-04-09 18:49:39 -0700388 common.infoMsg("Aptly isn't running as Docker container. Going to use aptly user for executing aptly commands")
389 }
390
azvyagintsev6dac3802018-11-09 15:11:30 +0200391 if (runningOnDocker) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200392 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 +0200393 } else {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200394 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 +0100395 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100396
Sergey1b81c062018-10-16 17:10:21 +0400397 salt.enforceState(venvPepper, 'I@aptly:publisher', 'aptly', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100398
azvyagintsev6dac3802018-11-09 15:11:30 +0200399 if (runningOnDocker) {
Sergey1b81c062018-10-16 17:10:21 +0400400 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv"], null, true)
401 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 +0200402 } else {
Sergey1b81c062018-10-16 17:10:21 +0400403 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
404 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 +0100405 }
Richard Felkl79d7df12018-01-05 16:40:11 +0100406
Sergey1b81c062018-10-16 17:10:21 +0400407 salt.enforceState(venvPepper, 'I@aptly:publisher', 'docker.client.registry', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100408
Sergey1b81c062018-10-16 17:10:21 +0400409 salt.enforceState(venvPepper, 'I@aptly:publisher', 'debmirror', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100410
Sergey1b81c062018-10-16 17:10:21 +0400411 salt.enforceState(venvPepper, 'I@aptly:publisher', 'git.server', true)
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100412
Sergey1b81c062018-10-16 17:10:21 +0400413 salt.enforceState(venvPepper, 'I@aptly:publisher', 'linux.system.file', true)
Jakub Josefa63f9862018-01-11 17:58:38 +0100414 }
415 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100416
azvyagintsevbc359d42019-04-10 19:53:22 +0300417 stage('Update Drivetrain') {
Denis Egorenkod13ad132018-12-17 16:56:02 +0400418 if (upgradeSaltStack) {
419 updateSaltStack("I@salt:master", '["salt-master", "salt-common", "salt-api", "salt-minion"]')
Richard Felkl0e80d892018-06-20 13:44:54 +0200420
Richard Felkl0e80d892018-06-20 13:44:54 +0200421 salt.enforceState(venvPepper, "I@linux:system", 'linux.system.repo', true)
Richard Felkl0e80d892018-06-20 13:44:54 +0200422 updateSaltStack("I@salt:minion and not I@salt:master", '["salt-minion"]')
423 }
424
Denis Egorenkod13ad132018-12-17 16:56:02 +0400425 if (updatePipelines) {
azvyagintsevbc359d42019-04-10 19:53:22 +0300426 common.infoMsg('Perform: UPDATE git repos')
427 triggerMirrorJob('git-mirror-downstream-mk-pipelines', reclassSystemBranch)
428 triggerMirrorJob('git-mirror-downstream-pipeline-library', reclassSystemBranch)
Richard Felkl970e0082018-06-12 18:00:51 +0200429 }
430
azvyagintsevbc359d42019-04-10 19:53:22 +0300431 // Retry needed only for rare race-condition in user appearance
432 common.infoMsg('Perform: updating users and keys')
Denis Egorenko65cf2a42019-01-15 11:58:12 +0400433 salt.enforceState(venvPepper, "I@linux:system", 'linux.system.user', true)
azvyagintsevbc359d42019-04-10 19:53:22 +0300434 common.infoMsg('Perform: updating openssh')
Denis Egorenko65cf2a42019-01-15 11:58:12 +0400435 salt.enforceState(venvPepper, "I@linux:system", 'openssh', true)
436
azvyagintsevbc359d42019-04-10 19:53:22 +0300437 salt.enforceState(venvPepper, 'I@jenkins:client and not I@salt:master', 'jenkins.client', true)
438 salt.cmdRun(venvPepper, "I@salt:master", "salt -C 'I@jenkins:client and I@docker:client and not I@salt:master' state.sls docker.client --async")
Richard Felkl970e0082018-06-12 18:00:51 +0200439
440 sleep(180)
441
azvyagintsevbc359d42019-04-10 19:53:22 +0300442 common.infoMsg('Perform: Checking if Docker containers are up')
Richard Felkl970e0082018-06-12 18:00:51 +0200443
azvyagintsev6dac3802018-11-09 15:11:30 +0200444 try {
445 common.retry(10, 30) {
Richard Felkl970e0082018-06-12 18:00:51 +0200446 salt.cmdRun(venvPepper, 'I@jenkins:client and I@docker:client', "! docker service ls | tail -n +2 | grep -v -E '\\s([0-9])/\\1\\s'")
447 }
448 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200449 catch (Exception ex) {
Richard Felkl970e0082018-06-12 18:00:51 +0200450 error("Docker containers for CI/CD services are having troubles with starting.")
451 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100452 }
453 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100454 catch (Throwable e) {
455 // If there was an error or exception thrown, the build failed
456 currentBuild.result = "FAILURE"
457 throw e
Richard Felkl26cae4d2017-12-19 00:19:16 +0100458 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100459 }
Jakub Josef2c21c6c2018-02-08 18:51:42 +0100460}