blob: af1fe8b96a17912ec5798382a82569263e619d00 [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
19def targetAll = ['expression': TARGET_SERVERS, 'type': 'compound']
20def targetTestSubset
21def targetLiveSubset
22def targetLiveAll
23def minions
24def result
25def states
26
27node() {
28 try {
29
30 if (TARGET_STATES != "") {
31 states = TARGET_STATES
32 }
33 else {
34 states = null
35 }
36
37 stage('Connect to Salt master') {
38 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
39 }
40
41 stage('List target servers') {
42 minions = salt.getMinions(saltMaster, targetAll)
Jakub Josefcd15ce72017-04-25 18:55:23 +020043 if (minions.isEmpty()) {
44 throw new Exception("No minion was targeted")
45 }
Ales Komarek374cc382017-03-16 08:49:01 +010046 if (TARGET_SUBSET_TEST != "") {
47 targetTestSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or '), 'type': 'compound']
48 }
49 else {
50 targetTestSubset = ['expression': minions.join(' or '), 'type': 'compound']
51 }
52 targetLiveSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or '), 'type': 'compound']
53 targetLiveAll = ['expression': minions.join(' or '), 'type': 'compound']
54 common.infoMsg("Found nodes: ${targetLiveAll.expression}")
55 common.infoMsg("Selected test nodes: ${targetTestSubset.expression}")
56 common.infoMsg("Selected sample nodes: ${targetLiveSubset.expression}")
57 }
58
59 stage('Test config changes') {
60 def kwargs = [
61 'test': true
62 ]
63 result = salt.runSaltCommand(saltMaster, 'local', targetTestSubset, 'state.apply', null, states, kwargs)
Jakub Josefcd15ce72017-04-25 18:55:23 +020064 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010065 }
66
67 stage('Confirm live changes on sample') {
68 timeout(time: 2, unit: 'HOURS') {
69 input message: "Approve live config change on ${targetLiveSubset.expression} nodes?"
70 }
71 }
72
73 stage('Apply config changes on sample') {
74 result = salt.runSaltCommand(saltMaster, 'local', targetLiveSubset, 'state.apply', null, states)
Jakub Josefcd15ce72017-04-25 18:55:23 +020075 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010076 }
77
78 stage('Confirm live changes on all nodes') {
79 timeout(time: 2, unit: 'HOURS') {
80 input message: "Approve live config change on ${targetLiveAll.expression} nodes?"
81 }
82 }
83
84 stage('Apply config changes on all nodes') {
85 result = salt.runSaltCommand(saltMaster, 'local', targetLiveAll, 'state.apply', null, states)
Jakub Josefcd15ce72017-04-25 18:55:23 +020086 salt.checkResult(result)
Ales Komarek374cc382017-03-16 08:49:01 +010087 }
88
89 } catch (Throwable e) {
90 currentBuild.result = 'FAILURE'
91 throw e
Ales Komarek374cc382017-03-16 08:49:01 +010092 }
93}