blob: 851c37600c0c002d52c333decc64d2e8590901e6 [file] [log] [blame]
Ales Komarek1fe5b8f2017-03-06 11:07:54 +01001/**
Ales Komarek374cc382017-03-16 08:49:01 +01002 * Update packages on given nodes
Ales Komarek1fe5b8f2017-03-06 11:07:54 +01003 *
4 * Expected parameters:
Ales Komarek374cc382017-03-16 08:49:01 +01005 * SALT_MASTER_CREDENTIALS Credentials to the Salt API.
6 * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000].
7 * TARGET_SERVERS Salt compound target to match nodes to be updated [*, G@osfamily:debian].
8 * TARGET_PACKAGES Space delimited list of packages to be updates [package1=version package2=version], empty string means all updating all packages to the latest version.
Denis Egorenko70002bc2019-08-19 19:41:27 +04009 * BATCH_SIZE Use batching for large amount of target nodes
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010010 *
11**/
Denis Egorenko4fe9c292019-04-15 16:50:51 +040012
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020013pepperEnv = "pepperEnv"
14salt = new com.mirantis.mk.Salt()
Denis Egorenko4fe9c292019-04-15 16:50:51 +040015common = new com.mirantis.mk.Common()
Denis Egorenko0d0c65f2019-02-26 16:05:03 +040016
Denis Egorenko70002bc2019-08-19 19:41:27 +040017def batch_size = ''
18if (common.validInputParam('BATCH_SIZE')) {
19 batch_size = "${BATCH_SIZE}"
20}
21
22def installSaltStack(target, pkgs, batch, masterUpdate = false){
Denis Egorenko0d0c65f2019-02-26 16:05:03 +040023 salt.cmdRun(pepperEnv, "I@salt:master", "salt -C '${target}' --async pkg.install force_yes=True pkgs='$pkgs'")
24 def minions_reachable = target
25 if (masterUpdate) {
26 // in case of update Salt Master packages - check all minions are good
27 minions_reachable = '*'
28 }
Denis Egorenko70002bc2019-08-19 19:41:27 +040029 salt.checkTargetMinionsReady(['saltId': pepperEnv, 'target': target, 'target_reachable': minions_reachable, 'batch': batch])
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020030}
31
Jakub Josefa63f9862018-01-11 17:58:38 +010032timeout(time: 12, unit: 'HOURS') {
33 node() {
34 try {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040035 def python = new com.mirantis.mk.Python()
36 def command = 'pkg.upgrade'
37 def commandKwargs = null
38 def packages = null
Jakub Josefa63f9862018-01-11 17:58:38 +010039 stage('Setup virtualenv for Pepper') {
40 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Tomáš Kukráld73cef02017-04-05 15:24:57 +020041 }
42
Denis Egorenko4fe9c292019-04-15 16:50:51 +040043 def targetLiveAll = ''
44 stage('Get target servers') {
45 def minions = salt.getMinions(pepperEnv, TARGET_SERVERS)
Jakub Josefa63f9862018-01-11 17:58:38 +010046 if (minions.isEmpty()) {
47 throw new Exception("No minion was targeted")
48 }
Jakub Josefa63f9862018-01-11 17:58:38 +010049 targetLiveAll = minions.join(' or ')
50 common.infoMsg("Found nodes: ${targetLiveAll}")
Ales Komarek374cc382017-03-16 08:49:01 +010051 }
Tomáš Kukráld73cef02017-04-05 15:24:57 +020052
Jakub Josefa63f9862018-01-11 17:58:38 +010053 stage("List package upgrades") {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040054 common.infoMsg("Listing all the packages that have a new update available on nodes: ${targetLiveAll}")
Denis Egorenko70002bc2019-08-19 19:41:27 +040055 salt.runSaltProcessStep(pepperEnv, targetLiveAll, 'pkg.list_upgrades', [], batch_size, true)
Denis Egorenko4fe9c292019-04-15 16:50:51 +040056 if (TARGET_PACKAGES != '' && TARGET_PACKAGES != '*') {
57 common.warningMsg("Note that only the \"${TARGET_PACKAGES}\" would be installed from the above list of available updates on the ${targetLiveAll}")
58 command = "pkg.install"
59 packages = TARGET_PACKAGES.tokenize(' ')
60 commandKwargs = ['only_upgrade': 'true']
Martin Polreich6cb53622018-08-15 16:45:29 +020061 }
Jakub Josefa63f9862018-01-11 17:58:38 +010062 }
63
64 stage('Confirm package upgrades on all nodes') {
Jakub Josef4a013752017-03-16 17:37:51 +010065 timeout(time: 2, unit: 'HOURS') {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040066 input message: "Approve package upgrades on ${targetLiveAll} nodes?"
Jakub Josef4a013752017-03-16 17:37:51 +010067 }
Ales Komarek374cc382017-03-16 08:49:01 +010068 }
Jakub Josefc5407c42017-03-16 18:31:10 +010069
Jakub Josefa63f9862018-01-11 17:58:38 +010070 stage('Apply package upgrades on all nodes') {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040071 if (packages == null || packages.contains("salt-master") || packages.contains("salt-common") || packages.contains("salt-minion") || packages.contains("salt-api")) {
72 common.warningMsg('Detected update for some Salt package (master or minion). Updating it first.')
73 def saltTargets = (targetLiveAll.split(' or ').collect { it as String })
74 for (int i = 0; i < saltTargets.size(); i++ ) {
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020075 common.retry(10, 5) {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040076 if (salt.getMinions(pepperEnv, "I@salt:master and ${saltTargets[i]}")) {
Denis Egorenko70002bc2019-08-19 19:41:27 +040077 installSaltStack("I@salt:master and ${saltTargets[i]}", '["salt-master", "salt-common", "salt-api", "salt-minion"]', null, true)
Denis Egorenko4fe9c292019-04-15 16:50:51 +040078 } else if (salt.getMinions(pepperEnv, "I@salt:minion and not I@salt:master and ${saltTargets[i]}")) {
Denis Egorenko70002bc2019-08-19 19:41:27 +040079 installSaltStack("I@salt:minion and not I@salt:master and ${saltTargets[i]}", '["salt-minion"]', batch_size)
Denis Egorenko4fe9c292019-04-15 16:50:51 +040080 } else {
81 error("Minion ${saltTargets[i]} is not reachable!")
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020082 }
83 }
84 }
85 }
Denis Egorenko4fe9c292019-04-15 16:50:51 +040086 common.infoMsg('Starting package upgrades...')
Denis Egorenko70002bc2019-08-19 19:41:27 +040087 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, batch_size, packages, commandKwargs)
Jakub Josefa63f9862018-01-11 17:58:38 +010088 salt.printSaltCommandResult(out)
Martin Polreich6cb53622018-08-15 16:45:29 +020089 for(value in out.get("return")[0].values()){
90 if (value.containsKey('result') && value.result == false) {
Denis Egorenko4fe9c292019-04-15 16:50:51 +040091 throw new Exception("The package upgrade on nodes has failed. Please check the Salt run result above for more information.")
Martin Polreich6cb53622018-08-15 16:45:29 +020092 }
93 }
Ales Komarek374cc382017-03-16 08:49:01 +010094 }
Ales Komarek374cc382017-03-16 08:49:01 +010095
Denis Egorenko4fe9c292019-04-15 16:50:51 +040096 common.warningMsg("Pipeline has finished successfully, but please, check if any packages have been kept back.")
97
Jakub Josefa63f9862018-01-11 17:58:38 +010098 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010099 currentBuild.result = "FAILURE"
100 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
101 throw e
Ales Komarek374cc382017-03-16 08:49:01 +0100102 }
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100103 }
104}