Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 1 | /** |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 2 | * Update packages on given nodes |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 3 | * |
| 4 | * Expected parameters: |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 5 | * 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 Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 9 | * BATCH_SIZE Use batching for large amount of target nodes |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 10 | * |
| 11 | **/ |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 12 | |
Pavel Cizinsky | af889c3 | 2018-08-15 15:20:42 +0200 | [diff] [blame] | 13 | pepperEnv = "pepperEnv" |
| 14 | salt = new com.mirantis.mk.Salt() |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 15 | common = new com.mirantis.mk.Common() |
Denis Egorenko | 0d0c65f | 2019-02-26 16:05:03 +0400 | [diff] [blame] | 16 | |
Denis Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 17 | def batch_size = '' |
| 18 | if (common.validInputParam('BATCH_SIZE')) { |
| 19 | batch_size = "${BATCH_SIZE}" |
| 20 | } |
| 21 | |
| 22 | def installSaltStack(target, pkgs, batch, masterUpdate = false){ |
Denis Egorenko | 0d0c65f | 2019-02-26 16:05:03 +0400 | [diff] [blame] | 23 | 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 Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 29 | salt.checkTargetMinionsReady(['saltId': pepperEnv, 'target': target, 'target_reachable': minions_reachable, 'batch': batch]) |
Pavel Cizinsky | af889c3 | 2018-08-15 15:20:42 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 32 | timeout(time: 12, unit: 'HOURS') { |
| 33 | node() { |
| 34 | try { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 35 | def python = new com.mirantis.mk.Python() |
| 36 | def command = 'pkg.upgrade' |
| 37 | def commandKwargs = null |
| 38 | def packages = null |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 39 | stage('Setup virtualenv for Pepper') { |
| 40 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Tomáš Kukrál | d73cef0 | 2017-04-05 15:24:57 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 43 | def targetLiveAll = '' |
| 44 | stage('Get target servers') { |
| 45 | def minions = salt.getMinions(pepperEnv, TARGET_SERVERS) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 46 | if (minions.isEmpty()) { |
| 47 | throw new Exception("No minion was targeted") |
| 48 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 49 | targetLiveAll = minions.join(' or ') |
| 50 | common.infoMsg("Found nodes: ${targetLiveAll}") |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 51 | } |
Tomáš Kukrál | d73cef0 | 2017-04-05 15:24:57 +0200 | [diff] [blame] | 52 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 53 | stage("List package upgrades") { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 54 | common.infoMsg("Listing all the packages that have a new update available on nodes: ${targetLiveAll}") |
Denis Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 55 | salt.runSaltProcessStep(pepperEnv, targetLiveAll, 'pkg.list_upgrades', [], batch_size, true) |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 56 | 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 Polreich | 6cb5362 | 2018-08-15 16:45:29 +0200 | [diff] [blame] | 61 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | stage('Confirm package upgrades on all nodes') { |
Jakub Josef | 4a01375 | 2017-03-16 17:37:51 +0100 | [diff] [blame] | 65 | timeout(time: 2, unit: 'HOURS') { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 66 | input message: "Approve package upgrades on ${targetLiveAll} nodes?" |
Jakub Josef | 4a01375 | 2017-03-16 17:37:51 +0100 | [diff] [blame] | 67 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 68 | } |
Jakub Josef | c5407c4 | 2017-03-16 18:31:10 +0100 | [diff] [blame] | 69 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 70 | stage('Apply package upgrades on all nodes') { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 71 | 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 Cizinsky | af889c3 | 2018-08-15 15:20:42 +0200 | [diff] [blame] | 75 | common.retry(10, 5) { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 76 | if (salt.getMinions(pepperEnv, "I@salt:master and ${saltTargets[i]}")) { |
Denis Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 77 | installSaltStack("I@salt:master and ${saltTargets[i]}", '["salt-master", "salt-common", "salt-api", "salt-minion"]', null, true) |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 78 | } else if (salt.getMinions(pepperEnv, "I@salt:minion and not I@salt:master and ${saltTargets[i]}")) { |
Denis Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 79 | installSaltStack("I@salt:minion and not I@salt:master and ${saltTargets[i]}", '["salt-minion"]', batch_size) |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 80 | } else { |
| 81 | error("Minion ${saltTargets[i]} is not reachable!") |
Pavel Cizinsky | af889c3 | 2018-08-15 15:20:42 +0200 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 86 | common.infoMsg('Starting package upgrades...') |
Denis Egorenko | 70002bc | 2019-08-19 19:41:27 +0400 | [diff] [blame^] | 87 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, batch_size, packages, commandKwargs) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 88 | salt.printSaltCommandResult(out) |
Martin Polreich | 6cb5362 | 2018-08-15 16:45:29 +0200 | [diff] [blame] | 89 | for(value in out.get("return")[0].values()){ |
| 90 | if (value.containsKey('result') && value.result == false) { |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 91 | throw new Exception("The package upgrade on nodes has failed. Please check the Salt run result above for more information.") |
Martin Polreich | 6cb5362 | 2018-08-15 16:45:29 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 94 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 95 | |
Denis Egorenko | 4fe9c29 | 2019-04-15 16:50:51 +0400 | [diff] [blame] | 96 | common.warningMsg("Pipeline has finished successfully, but please, check if any packages have been kept back.") |
| 97 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 98 | } catch (Throwable e) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 99 | currentBuild.result = "FAILURE" |
| 100 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 101 | throw e |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 102 | } |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 103 | } |
| 104 | } |