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. |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 9 | * TARGET_SUBSET_TEST Number of nodes to list package updates, empty string means all targetted nodes. |
| 10 | * TARGET_SUBSET_LIVE Number of selected nodes to live apply selected package update. |
| 11 | * TARGET_BATCH_LIVE Batch size for the complete live package update on all nodes, empty string means apply to all targetted nodes. |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 12 | * |
| 13 | **/ |
| 14 | |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 15 | def common = new com.mirantis.mk.Common() |
| 16 | def salt = new com.mirantis.mk.Salt() |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 17 | def python = new com.mirantis.mk.Python() |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 18 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 19 | def pepperEnv = "pepperEnv" |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 20 | def targetTestSubset |
| 21 | def targetLiveSubset |
| 22 | def targetLiveAll |
| 23 | def minions |
| 24 | def result |
| 25 | def packages |
Jakub Josef | f080c14 | 2017-03-16 18:22:18 +0100 | [diff] [blame] | 26 | def command |
Filip Pytloun | 43896ed | 2017-03-29 14:23:32 +0200 | [diff] [blame] | 27 | def commandKwargs |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 28 | timeout(time: 12, unit: 'HOURS') { |
| 29 | node() { |
| 30 | try { |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 31 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 32 | stage('Setup virtualenv for Pepper') { |
| 33 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Tomáš Kukrál | d73cef0 | 2017-04-05 15:24:57 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 36 | stage('List target servers') { |
| 37 | minions = salt.getMinions(pepperEnv, TARGET_SERVERS) |
| 38 | |
| 39 | if (minions.isEmpty()) { |
| 40 | throw new Exception("No minion was targeted") |
| 41 | } |
| 42 | |
| 43 | if (TARGET_SUBSET_TEST != "") { |
| 44 | targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ') |
| 45 | } else { |
| 46 | targetTestSubset = minions.join(' or ') |
| 47 | } |
| 48 | targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ') |
| 49 | |
| 50 | targetLiveAll = minions.join(' or ') |
| 51 | common.infoMsg("Found nodes: ${targetLiveAll}") |
| 52 | common.infoMsg("Selected test nodes: ${targetTestSubset}") |
| 53 | common.infoMsg("Selected sample nodes: ${targetLiveSubset}") |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 54 | } |
Tomáš Kukrál | d73cef0 | 2017-04-05 15:24:57 +0200 | [diff] [blame] | 55 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 56 | stage("List package upgrades") { |
| 57 | common.infoMsg("Listing all the packages that have a new update available on test nodes: ${targetTestSubset}") |
| 58 | salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.list_upgrades', [], null, true) |
| 59 | if(TARGET_PACKAGES != "" && TARGET_PACKAGES != "*"){ |
| 60 | common.infoMsg("Note that only the ${TARGET_PACKAGES} would be installed from the above list of available updates on the ${targetTestSubset}") |
| 61 | } |
Sam Stoelinga | f52a042 | 2017-08-01 12:26:21 -0700 | [diff] [blame] | 62 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 63 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 64 | stage('Confirm live package upgrades on sample') { |
| 65 | if(TARGET_PACKAGES==""){ |
| 66 | timeout(time: 2, unit: 'HOURS') { |
| 67 | def userInput = input( |
| 68 | id: 'userInput', message: 'Insert package names for update', parameters: [ |
| 69 | [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages'] |
| 70 | ]) |
| 71 | if(userInput!= "" && userInput!= "*"){ |
| 72 | TARGET_PACKAGES = userInput |
| 73 | } |
| 74 | } |
| 75 | }else{ |
| 76 | timeout(time: 2, unit: 'HOURS') { |
| 77 | input message: "Approve live package upgrades on ${targetLiveSubset} nodes?" |
Jakub Josef | 4a01375 | 2017-03-16 17:37:51 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (TARGET_PACKAGES != "") { |
| 83 | command = "pkg.install" |
| 84 | packages = TARGET_PACKAGES.tokenize(' ') |
| 85 | commandKwargs = ['only_upgrade': 'true'] |
| 86 | }else { |
| 87 | command = "pkg.upgrade" |
| 88 | packages = null |
| 89 | } |
| 90 | |
| 91 | stage('Apply package upgrades on sample') { |
| 92 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs) |
| 93 | salt.printSaltCommandResult(out) |
| 94 | } |
| 95 | |
| 96 | stage('Confirm package upgrades on all nodes') { |
Jakub Josef | 4a01375 | 2017-03-16 17:37:51 +0100 | [diff] [blame] | 97 | timeout(time: 2, unit: 'HOURS') { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 98 | input message: "Approve live package upgrades on ${targetLiveAll} nodes?" |
Jakub Josef | 4a01375 | 2017-03-16 17:37:51 +0100 | [diff] [blame] | 99 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 100 | } |
Jakub Josef | c5407c4 | 2017-03-16 18:31:10 +0100 | [diff] [blame] | 101 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 102 | stage('Apply package upgrades on all nodes') { |
| 103 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs) |
| 104 | salt.printSaltCommandResult(out) |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 105 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 106 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 107 | } catch (Throwable e) { |
| 108 | // If there was an error or exception thrown, the build failed |
| 109 | currentBuild.result = "FAILURE" |
| 110 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 111 | throw e |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 112 | } |
Ales Komarek | 1fe5b8f | 2017-03-06 11:07:54 +0100 | [diff] [blame] | 113 | } |
| 114 | } |