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