Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Make change to the node(s) configuration |
| 3 | * |
| 4 | * Expected parameters: |
| 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_STATES States to be applied, empty string means running highstate [linux, linux,openssh, salt.minion.grains]. |
| 9 | * TARGET_SUBSET_TEST Number of nodes to test config changes, empty string means all targetted nodes. |
| 10 | * TARGET_SUBSET_LIVE Number of selected noded to live apply selected config changes. |
| 11 | * TARGET_BATCH_LIVE Batch size for the complete live config changes on all nodes, empty string means apply to all targetted nodes. |
| 12 | * |
| 13 | **/ |
| 14 | |
| 15 | def common = new com.mirantis.mk.Common() |
| 16 | def salt = new com.mirantis.mk.Salt() |
| 17 | |
| 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 states |
| 25 | |
| 26 | node() { |
| 27 | try { |
| 28 | |
| 29 | if (TARGET_STATES != "") { |
| 30 | states = TARGET_STATES |
| 31 | } |
| 32 | else { |
| 33 | states = null |
| 34 | } |
| 35 | |
| 36 | stage('Connect to Salt master') { |
| 37 | saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 38 | } |
| 39 | |
| 40 | stage('List target servers') { |
Tomáš Kukrál | c86c8b4 | 2017-07-13 10:26:51 +0200 | [diff] [blame] | 41 | minions = salt.getMinions(saltMaster, TARGET_SERVERS) |
Jakub Josef | cd15ce7 | 2017-04-25 18:55:23 +0200 | [diff] [blame] | 42 | if (minions.isEmpty()) { |
| 43 | throw new Exception("No minion was targeted") |
| 44 | } |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 45 | if (TARGET_SUBSET_TEST != "") { |
| 46 | targetTestSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or '), 'type': 'compound'] |
| 47 | } |
| 48 | else { |
| 49 | targetTestSubset = ['expression': minions.join(' or '), 'type': 'compound'] |
| 50 | } |
| 51 | targetLiveSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or '), 'type': 'compound'] |
| 52 | targetLiveAll = ['expression': minions.join(' or '), 'type': 'compound'] |
| 53 | common.infoMsg("Found nodes: ${targetLiveAll.expression}") |
| 54 | common.infoMsg("Selected test nodes: ${targetTestSubset.expression}") |
| 55 | common.infoMsg("Selected sample nodes: ${targetLiveSubset.expression}") |
| 56 | } |
| 57 | |
| 58 | stage('Test config changes') { |
| 59 | def kwargs = [ |
| 60 | 'test': true |
| 61 | ] |
| 62 | result = salt.runSaltCommand(saltMaster, 'local', targetTestSubset, 'state.apply', null, states, kwargs) |
Jakub Josef | cd15ce7 | 2017-04-25 18:55:23 +0200 | [diff] [blame] | 63 | salt.checkResult(result) |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | stage('Confirm live changes on sample') { |
| 67 | timeout(time: 2, unit: 'HOURS') { |
| 68 | input message: "Approve live config change on ${targetLiveSubset.expression} nodes?" |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | stage('Apply config changes on sample') { |
| 73 | result = salt.runSaltCommand(saltMaster, 'local', targetLiveSubset, 'state.apply', null, states) |
Jakub Josef | cd15ce7 | 2017-04-25 18:55:23 +0200 | [diff] [blame] | 74 | salt.checkResult(result) |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | stage('Confirm live changes on all nodes') { |
| 78 | timeout(time: 2, unit: 'HOURS') { |
| 79 | input message: "Approve live config change on ${targetLiveAll.expression} nodes?" |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | stage('Apply config changes on all nodes') { |
| 84 | result = salt.runSaltCommand(saltMaster, 'local', targetLiveAll, 'state.apply', null, states) |
Jakub Josef | cd15ce7 | 2017-04-25 18:55:23 +0200 | [diff] [blame] | 85 | salt.checkResult(result) |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } catch (Throwable e) { |
| 89 | currentBuild.result = 'FAILURE' |
| 90 | throw e |
Ales Komarek | 374cc38 | 2017-03-16 08:49:01 +0100 | [diff] [blame] | 91 | } |
| 92 | } |