blob: df7655b7f0d3ec4dc299c38fd0636e4318eca5c9 [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.
Ales Komarek1fe5b8f2017-03-06 11:07:54 +01009 *
10**/
Denis Egorenkoa9909262019-04-15 16:50:51 +040011
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020012pepperEnv = "pepperEnv"
13salt = new com.mirantis.mk.Salt()
Denis Egorenkoa9909262019-04-15 16:50:51 +040014common = new com.mirantis.mk.Common()
Denis Egorenko4b54e7f2019-02-26 16:05:03 +040015
16def installSaltStack(target, pkgs, masterUpdate = false){
17 salt.cmdRun(pepperEnv, "I@salt:master", "salt -C '${target}' --async pkg.install force_yes=True pkgs='$pkgs'")
18 def minions_reachable = target
19 if (masterUpdate) {
20 // in case of update Salt Master packages - check all minions are good
21 minions_reachable = '*'
22 }
Denis Egorenkoa9909262019-04-15 16:50:51 +040023 salt.checkTargetMinionsReady(['saltId': pepperEnv, 'target': target, 'target_reachable': minions_reachable])
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020024}
25
Jakub Josefa63f9862018-01-11 17:58:38 +010026timeout(time: 12, unit: 'HOURS') {
27 node() {
28 try {
Denis Egorenkoa9909262019-04-15 16:50:51 +040029 def python = new com.mirantis.mk.Python()
30 def command = 'pkg.upgrade'
31 def commandKwargs = null
32 def packages = null
Jakub Josefa63f9862018-01-11 17:58:38 +010033 stage('Setup virtualenv for Pepper') {
34 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Tomáš Kukráld73cef02017-04-05 15:24:57 +020035 }
36
Denis Egorenkoa9909262019-04-15 16:50:51 +040037 def targetLiveAll = ''
38 stage('Get target servers') {
39 def minions = salt.getMinions(pepperEnv, TARGET_SERVERS)
Jakub Josefa63f9862018-01-11 17:58:38 +010040 if (minions.isEmpty()) {
41 throw new Exception("No minion was targeted")
42 }
Jakub Josefa63f9862018-01-11 17:58:38 +010043 targetLiveAll = minions.join(' or ')
44 common.infoMsg("Found nodes: ${targetLiveAll}")
Ales Komarek374cc382017-03-16 08:49:01 +010045 }
Tomáš Kukráld73cef02017-04-05 15:24:57 +020046
Jakub Josefa63f9862018-01-11 17:58:38 +010047 stage("List package upgrades") {
Denis Egorenkoa9909262019-04-15 16:50:51 +040048 common.infoMsg("Listing all the packages that have a new update available on nodes: ${targetLiveAll}")
49 salt.runSaltProcessStep(pepperEnv, targetLiveAll, 'pkg.list_upgrades', [], null, true)
50 if (TARGET_PACKAGES != '' && TARGET_PACKAGES != '*') {
51 common.warningMsg("Note that only the \"${TARGET_PACKAGES}\" would be installed from the above list of available updates on the ${targetLiveAll}")
52 command = "pkg.install"
53 packages = TARGET_PACKAGES.tokenize(' ')
54 commandKwargs = ['only_upgrade': 'true']
Martin Polreich6cb53622018-08-15 16:45:29 +020055 }
Jakub Josefa63f9862018-01-11 17:58:38 +010056 }
57
58 stage('Confirm package upgrades on all nodes') {
Jakub Josef4a013752017-03-16 17:37:51 +010059 timeout(time: 2, unit: 'HOURS') {
Denis Egorenkoa9909262019-04-15 16:50:51 +040060 input message: "Approve package upgrades on ${targetLiveAll} nodes?"
Jakub Josef4a013752017-03-16 17:37:51 +010061 }
Ales Komarek374cc382017-03-16 08:49:01 +010062 }
Jakub Josefc5407c42017-03-16 18:31:10 +010063
Jakub Josefa63f9862018-01-11 17:58:38 +010064 stage('Apply package upgrades on all nodes') {
Denis Egorenkoa9909262019-04-15 16:50:51 +040065 if (packages == null || packages.contains("salt-master") || packages.contains("salt-common") || packages.contains("salt-minion") || packages.contains("salt-api")) {
66 common.warningMsg('Detected update for some Salt package (master or minion). Updating it first.')
67 def saltTargets = (targetLiveAll.split(' or ').collect { it as String })
68 for (int i = 0; i < saltTargets.size(); i++ ) {
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020069 common.retry(10, 5) {
Denis Egorenkoa9909262019-04-15 16:50:51 +040070 if (salt.getMinions(pepperEnv, "I@salt:master and ${saltTargets[i]}")) {
Denis Egorenko4b54e7f2019-02-26 16:05:03 +040071 installSaltStack("I@salt:master and ${saltTargets[i]}", '["salt-master", "salt-common", "salt-api", "salt-minion"]', true)
Denis Egorenkoa9909262019-04-15 16:50:51 +040072 } else if (salt.getMinions(pepperEnv, "I@salt:minion and not I@salt:master and ${saltTargets[i]}")) {
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020073 installSaltStack("I@salt:minion and not I@salt:master and ${saltTargets[i]}", '["salt-minion"]')
Denis Egorenkoa9909262019-04-15 16:50:51 +040074 } else {
75 error("Minion ${saltTargets[i]} is not reachable!")
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020076 }
77 }
78 }
79 }
Denis Egorenkoa9909262019-04-15 16:50:51 +040080 common.infoMsg('Starting package upgrades...')
Jakub Josefa63f9862018-01-11 17:58:38 +010081 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
82 salt.printSaltCommandResult(out)
Martin Polreich6cb53622018-08-15 16:45:29 +020083 for(value in out.get("return")[0].values()){
84 if (value.containsKey('result') && value.result == false) {
Denis Egorenkoa9909262019-04-15 16:50:51 +040085 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 +020086 }
87 }
Ales Komarek374cc382017-03-16 08:49:01 +010088 }
Ales Komarek374cc382017-03-16 08:49:01 +010089
Denis Egorenkoa9909262019-04-15 16:50:51 +040090 common.warningMsg("Pipeline has finished successfully, but please, check if any packages have been kept back.")
91
Jakub Josefa63f9862018-01-11 17:58:38 +010092 } catch (Throwable e) {
Jakub Josefa63f9862018-01-11 17:58:38 +010093 currentBuild.result = "FAILURE"
94 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
95 throw e
Ales Komarek374cc382017-03-16 08:49:01 +010096 }
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010097 }
98}