blob: 44832ed77aabbed519ae4ef9fa9aa985aa644fa3 [file] [log] [blame]
Ales Komarek374cc382017-03-16 08:49:01 +01001/**
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
15def common = new com.mirantis.mk.Common()
16def salt = new com.mirantis.mk.Salt()
17
18def saltMaster
Ales Komarek374cc382017-03-16 08:49:01 +010019def targetTestSubset
20def targetLiveSubset
21def targetLiveAll
22def minions
23def result
24def states
25
26node() {
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álc86c8b42017-07-13 10:26:51 +020041 minions = salt.getMinions(saltMaster, TARGET_SERVERS)
Jakub Josefcd15ce72017-04-25 18:55:23 +020042 if (minions.isEmpty()) {
43 throw new Exception("No minion was targeted")
44 }
Ales Komarek374cc382017-03-16 08:49:01 +010045 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 Josefcd15ce72017-04-25 18:55:23 +020063 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010064 }
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 Josefcd15ce72017-04-25 18:55:23 +020074 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010075 }
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 Josefcd15ce72017-04-25 18:55:23 +020085 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010086 }
87
88 } catch (Throwable e) {
89 currentBuild.result = 'FAILURE'
90 throw e
Ales Komarek374cc382017-03-16 08:49:01 +010091 }
92}