blob: c00e93e6e6ec2a693333b7d5d59ca2725cef519d [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 Egorenkod13ad132018-12-17 16:56:02 +040036 // wait 2 mins when salt-* packages are updated which leads to salt-* services restart
37 common.retry(2, 120) {
Richard Felkl0e80d892018-06-20 13:44:54 +020038 salt.runSaltProcessStep(venvPepper, target, 'pkg.install', ["force_yes=True", "pkgs='$pkgs'"], null, true, 5)
azvyagintsev6dac3802018-11-09 15:11:30 +020039 }
Richard Felkl0e80d892018-06-20 13:44:54 +020040
azvyagintsev6dac3802018-11-09 15:11:30 +020041 common.retry(20, 60) {
Richard Felkl0e80d892018-06-20 13:44:54 +020042 salt.minionsReachable(venvPepper, 'I@salt:master', '*')
43 def running = salt.runSaltProcessStep(venvPepper, target, 'saltutil.running', [], null, true, 5)
azvyagintsev6dac3802018-11-09 15:11:30 +020044 for (value in running.get("return")[0].values()) {
45 if (value != []) {
Richard Felkl0e80d892018-06-20 13:44:54 +020046 throw new Exception("Not all salt-minions are ready for execution")
47 }
48 }
49 }
50
azvyagintsevf3bf77a2018-11-15 19:30:04 +020051 def saltVersion = salt.getPillar(venvPepper, 'I@salt:master', '_param:salt_version').get('return')[0].values()[0]
Richard Felklf23257e2018-08-06 09:24:23 +020052 def saltMinionVersions = salt.cmdRun(venvPepper, target, "apt-cache policy salt-common | awk '/Installed/ && /$saltVersion/'").get("return")
Richard Felkl0e80d892018-06-20 13:44:54 +020053 def saltMinionVersion = ""
54
azvyagintsev6dac3802018-11-09 15:11:30 +020055 for (minion in saltMinionVersions[0].keySet()) {
56 saltMinionVersion = saltMinionVersions[0].get(minion).replace("Salt command execution success", "").trim()
57 if (saltMinionVersion == "") {
Richard Felkl0e80d892018-06-20 13:44:54 +020058 error("Installed version of Salt on $minion doesn't match specified version in the model.")
59 }
60 }
61}
62
azvyagintsev6dac3802018-11-09 15:11:30 +020063def archiveReclassInventory(filename) {
Richard Felkl0e80d892018-06-20 13:44:54 +020064 def ret = salt.cmdRun(venvPepper, 'I@salt:master', "reclass -i", true, null, false)
65 def reclassInv = ret.values()[0]
66 writeFile file: filename, text: reclassInv.toString()
67 archiveArtifacts artifacts: "$filename"
68}
69
Denis Egorenkod13ad132018-12-17 16:56:02 +040070def validateReclassModel(ArrayList saltMinions, String suffix) {
71 try {
72 for(String minion in saltMinions) {
73 common.infoMsg("Reclass model validation for minion ${minion}...")
74 def ret = salt.cmdRun(venvPepper, 'I@salt:master', "reclass -n ${minion}", true, null, false)
75 def reclassInv = ret.values()[0]
76 writeFile file: "inventory-${minion}-${suffix}.out", text: reclassInv.toString()
77 }
78 } catch (Exception e) {
79 common.errorMsg('Can not validate current Reclass model. Inspect failed minion manually.')
80 error(e)
81 }
82}
83
84def archiveReclassModelChanges(ArrayList saltMinions, String oldSuffix='before', String newSuffix='after') {
85 for(String minion in saltMinions) {
86 def fileName = "reclass-model-${minion}-diff.out"
87 sh "diff -u inventory-${minion}-${oldSuffix}.out inventory-${minion}-${newSuffix}.out > ${fileName} || true"
88 archiveArtifacts artifacts: "${fileName}"
89 }
90}
91
azvyagintsevf3bf77a2018-11-15 19:30:04 +020092if (common.validInputParam('PIPELINE_TIMEOUT') && env.PIPELINE_TIMEOUT.isInteger()) {
93 pipelineTimeout = env.PIPELINE_TIMEOUT.toInteger()
Martin Polreich965c9032018-08-09 16:33:57 +020094}
95
96timeout(time: pipelineTimeout, unit: 'HOURS') {
Jakub Josefa63f9862018-01-11 17:58:38 +010097 node("python") {
98 try {
Richard Felkl0e80d892018-06-20 13:44:54 +020099 workspace = common.getWorkspace()
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200100 targetMcpVersion = null
101 if (!common.validInputParam('TARGET_MCP_VERSION') && !common.validInputParam('MCP_VERSION')) {
102 error('You must specify MCP version in TARGET_MCP_VERSION|MCP_VERSION variable')
azvyagintsev6dac3802018-11-09 15:11:30 +0200103 }
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200104 // bw comp. for 2018.X => 2018.11 release
105 if (common.validInputParam('MCP_VERSION')){
106 targetMcpVersion = env.MCP_VERSION
107 common.warningMsg("targetMcpVersion has been changed to:${targetMcpVersion}, which was taken from deprecated pipeline viriable:MCP_VERSION")
108 }
109 else {
110 targetMcpVersion = env.TARGET_MCP_VERSION
111 }
112 // end bw comp. for 2018.X => 2018.11 release
Denis Egorenkod13ad132018-12-17 16:56:02 +0400113 def gitTargetMcpVersion = env.getProperty('GIT_REFSPEC')
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200114 if (targetMcpVersion == 'testing') {
115 gitTargetMcpVersion = 'master'
116 common.warningMsg("gitTargetMcpVersion has been changed to:${gitTargetMcpVersion}")
azvyagintsev6dac3802018-11-09 15:11:30 +0200117 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400118 def driteTrainParamsYaml = env.getProperty('DRIVE_TRAIN_PARAMS')
119 def driteTrainParams = readYaml text: driteTrainParamsYaml
120 def saltMastURL = driteTrainParams.get('SALT_MASTER_URL')
121 def saltMastCreds = driteTrainParams.get('SALT_MASTER_CREDENTIALS')
122 def upgradeSaltStack = driteTrainParams.get('UPGRADE_SALTSTACK', false).toBoolean()
123 def updateClusterModel = driteTrainParams.get('UPDATE_CLUSTER_MODEL', false).toBoolean()
124 def updatePipelines = driteTrainParams.get('UPDATE_PIPELINES', false).toBoolean()
125 def updateLocalRepos = driteTrainParams.get('UPDATE_LOCAL_REPOS', false).toBoolean()
126 def reclassSystemBranch = driteTrainParams.get('RECLASS_SYSTEM_BRANCH', gitTargetMcpVersion)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100127
Denis Egorenkod13ad132018-12-17 16:56:02 +0400128 python.setupPepperVirtualenv(venvPepper, saltMastURL, saltMastCreds)
129
130 def inventoryBeforeFilename = "reclass-inventory-before.out"
131 def inventoryAfterFilename = "reclass-inventory-after.out"
132
133 def minions = salt.getMinions(venvPepper, '*')
134
135 stage("Update Reclass and Salt-Formulas ") {
136 validateReclassModel(minions, 'before')
137
Richard Felkl970e0082018-06-12 18:00:51 +0200138 def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
azvyagintsev6dac3802018-11-09 15:11:30 +0200139 try {
Sergey382581a2018-10-25 12:22:26 +0400140 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/ && git diff-index --quiet HEAD --")
Richard Felkl970e0082018-06-12 18:00:51 +0200141 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200142 catch (Exception ex) {
Sergey382581a2018-10-25 12:22:26 +0400143 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 +0200144 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400145 if (updateClusterModel) {
azvyagintsev6dac3802018-11-09 15:11:30 +0200146 common.infoMsg('Perform: UPDATE_CLUSTER_MODEL')
Sergey382581a2018-10-25 12:22:26 +0400147 def dateTime = common.getDatetime()
148 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200149 "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 +0200150 // Do the same, for deprecated variable-duplicate
151 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && " +
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200152 "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 Egorenkod13ad132018-12-17 16:56:02 +0400153 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/system && git checkout ${reclassSystemBranch}")
Sergey382581a2018-10-25 12:22:26 +0400154 // Add new defaults
155 common.infoMsg("Add new defaults")
Denis Egorenkod13ad132018-12-17 16:56:02 +0400156 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^ mcp_version: ' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
157 "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 +0400158 salt.cmdRun(venvPepper, 'I@salt:master', "grep '^- system.defaults\$' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml || " +
azvyagintsev6dac3802018-11-09 15:11:30 +0200159 "sed -i 's/^classes:/classes:\\n- system.defaults/' /srv/salt/reclass/classes/cluster/$cluster_name/infra/init.yml")
Sergey382581a2018-10-25 12:22:26 +0400160 common.infoMsg("The following changes were made to the cluster model and will be commited. " +
azvyagintsev6dac3802018-11-09 15:11:30 +0200161 "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 +0400162 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git diff")
163 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && git status && " +
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200164 "git add -u && git commit --allow-empty -m 'Cluster model update to the release $targetMcpVersion on $dateTime'")
Sergey382581a2018-10-25 12:22:26 +0400165 }
Denis Egorenkod13ad132018-12-17 16:56:02 +0400166
167 try {
168 common.infoMsg('Perform: UPDATE Salt Formulas')
169 salt.enforceState(venvPepper, 'I@salt:master', 'linux.system.repo')
170 def saltEnv = salt.getPillar(venvPepper, 'I@salt:master', "_param:salt_master_base_environment").get("return")[0].values()[0]
171 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'state.sls_id', ["salt_master_${saltEnv}_pkg_formulas",'salt.master.env'])
172 } catch (Exception updateErr) {
173 common.warningMsg(updateErr)
174 common.warningMsg('Failed to update Salt Formulas repos/packages. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
175 input message: 'Continue anyway?'
176 }
177
178 archiveReclassInventory(inventoryBeforeFilename)
179
180 try {
181 common.infoMsg('Perform: UPDATE Reclass package')
182 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'pkg.install', ["reclass"])
183 } catch (Exception updateErr) {
184 common.warningMsg(updateErr)
185 common.warningMsg('Failed to update Reclass package. Check current available documentation on https://docs.mirantis.com/mcp/latest/, how to update packages.')
186 input message: 'Continue anyway?'
187 }
188
189 salt.fullRefresh(venvPepper, 'I@salt:master')
Sergey1b81c062018-10-16 17:10:21 +0400190 salt.enforceState(venvPepper, 'I@salt:master', 'reclass.storage', true)
Denis Egorenkod13ad132018-12-17 16:56:02 +0400191 try {
192 salt.enforceState(venvPepper, "I@salt:master", 'reclass', true)
193 }
194 catch (Exception ex) {
195 error("Reclass fails rendering. Pay attention to your cluster model.")
196 }
197
198 salt.fullRefresh(venvPepper, '*')
199
200 try {
201 salt.cmdRun(venvPepper, 'I@salt:master', "reclass-salt --top")
202 }
203 catch (Exception ex) {
204 error("Reclass fails rendering. Pay attention to your cluster model.")
205 }
206
207 archiveReclassInventory(inventoryAfterFilename)
208
209 sh "diff -u $inventoryBeforeFilename $inventoryAfterFilename > reclass-inventory-diff.out || true"
210 archiveArtifacts artifacts: "reclass-inventory-diff.out"
211
212 validateReclassModel(minions, 'after')
213 archiveReclassModelChanges(minions)
Jakub Josefa63f9862018-01-11 17:58:38 +0100214 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100215
Denis Egorenkod13ad132018-12-17 16:56:02 +0400216 if (updateLocalRepos) {
Sergey1b81c062018-10-16 17:10:21 +0400217 def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0]
azvyagintsev6dac3802018-11-09 15:11:30 +0200218 stage("Update local repos") {
Jakub Josefa63f9862018-01-11 17:58:38 +0100219 common.infoMsg("Updating local repositories")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700220
Sergey1b81c062018-10-16 17:10:21 +0400221 def engine = salt.getPillar(venvPepper, 'I@aptly:publisher', "aptly:publisher:source:engine")
Sam Stoelingaaab79702018-04-09 18:49:39 -0700222 runningOnDocker = engine.get("return")[0].containsValue("docker")
223
224 if (runningOnDocker) {
225 common.infoMsg("Aptly is running as Docker container")
azvyagintsev6dac3802018-11-09 15:11:30 +0200226 } else {
Sam Stoelingaaab79702018-04-09 18:49:39 -0700227 common.infoMsg("Aptly isn't running as Docker container. Going to use aptly user for executing aptly commands")
228 }
229
azvyagintsev6dac3802018-11-09 15:11:30 +0200230 if (runningOnDocker) {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200231 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 +0200232 } else {
azvyagintsevf3bf77a2018-11-15 19:30:04 +0200233 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 +0100234 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100235
Sergey1b81c062018-10-16 17:10:21 +0400236 salt.enforceState(venvPepper, 'I@aptly:publisher', 'aptly', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100237
azvyagintsev6dac3802018-11-09 15:11:30 +0200238 if (runningOnDocker) {
Sergey1b81c062018-10-16 17:10:21 +0400239 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv"], null, true)
240 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 +0200241 } else {
Sergey1b81c062018-10-16 17:10:21 +0400242 salt.runSaltProcessStep(venvPepper, 'I@aptly:publisher', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
243 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 +0100244 }
Richard Felkl79d7df12018-01-05 16:40:11 +0100245
Sergey1b81c062018-10-16 17:10:21 +0400246 salt.enforceState(venvPepper, 'I@aptly:publisher', 'docker.client.registry', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100247
Sergey1b81c062018-10-16 17:10:21 +0400248 salt.enforceState(venvPepper, 'I@aptly:publisher', 'debmirror', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100249
Sergey1b81c062018-10-16 17:10:21 +0400250 salt.enforceState(venvPepper, 'I@aptly:publisher', 'git.server', true)
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100251
Sergey1b81c062018-10-16 17:10:21 +0400252 salt.enforceState(venvPepper, 'I@aptly:publisher', 'linux.system.file', true)
Jakub Josefa63f9862018-01-11 17:58:38 +0100253 }
254 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100255
azvyagintsev6dac3802018-11-09 15:11:30 +0200256 stage("Update Drivetrain") {
Denis Egorenkod13ad132018-12-17 16:56:02 +0400257 if (upgradeSaltStack) {
258 updateSaltStack("I@salt:master", '["salt-master", "salt-common", "salt-api", "salt-minion"]')
Richard Felkl0e80d892018-06-20 13:44:54 +0200259
Richard Felkl0e80d892018-06-20 13:44:54 +0200260 salt.enforceState(venvPepper, "I@linux:system", 'linux.system.repo', true)
Richard Felkl0e80d892018-06-20 13:44:54 +0200261 updateSaltStack("I@salt:minion and not I@salt:master", '["salt-minion"]')
262 }
263
Denis Egorenkod13ad132018-12-17 16:56:02 +0400264 if (updatePipelines) {
Richard Felkl970e0082018-06-12 18:00:51 +0200265 triggerMirrorJob("git-mirror-downstream-mk-pipelines")
266 triggerMirrorJob("git-mirror-downstream-pipeline-library")
267 }
268
269 salt.enforceState(venvPepper, "I@jenkins:client", 'jenkins.client', true)
270
271 salt.cmdRun(venvPepper, "I@salt:master", "salt -C 'I@jenkins:client and I@docker:client' state.sls docker.client --async")
272
273 sleep(180)
274
275 common.infoMsg("Checking if Docker containers are up")
276
azvyagintsev6dac3802018-11-09 15:11:30 +0200277 try {
278 common.retry(10, 30) {
Richard Felkl970e0082018-06-12 18:00:51 +0200279 salt.cmdRun(venvPepper, 'I@jenkins:client and I@docker:client', "! docker service ls | tail -n +2 | grep -v -E '\\s([0-9])/\\1\\s'")
280 }
281 }
azvyagintsev6dac3802018-11-09 15:11:30 +0200282 catch (Exception ex) {
Richard Felkl970e0082018-06-12 18:00:51 +0200283 error("Docker containers for CI/CD services are having troubles with starting.")
284 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100285 }
286 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100287 catch (Throwable e) {
288 // If there was an error or exception thrown, the build failed
289 currentBuild.result = "FAILURE"
290 throw e
Richard Felkl26cae4d2017-12-19 00:19:16 +0100291 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100292 }
Jakub Josef2c21c6c2018-02-08 18:51:42 +0100293}