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