blob: 69f75194acbd075325b3dd11f61469d190baae97 [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)
43 if (TARGET_SUBSET_TEST != "") {
44 targetTestSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or '), 'type': 'compound']
45 }
46 else {
47 targetTestSubset = ['expression': minions.join(' or '), 'type': 'compound']
48 }
49 targetLiveSubset = ['expression': minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or '), 'type': 'compound']
50 targetLiveAll = ['expression': minions.join(' or '), 'type': 'compound']
51 common.infoMsg("Found nodes: ${targetLiveAll.expression}")
52 common.infoMsg("Selected test nodes: ${targetTestSubset.expression}")
53 common.infoMsg("Selected sample nodes: ${targetLiveSubset.expression}")
54 }
55
56 stage('Test config changes') {
57 def kwargs = [
58 'test': true
59 ]
60 result = salt.runSaltCommand(saltMaster, 'local', targetTestSubset, 'state.apply', null, states, kwargs)
61 salt.printSaltStateResult(result)
62 }
63
64 stage('Confirm live changes on sample') {
65 timeout(time: 2, unit: 'HOURS') {
66 input message: "Approve live config change on ${targetLiveSubset.expression} nodes?"
67 }
68 }
69
70 stage('Apply config changes on sample') {
71 result = salt.runSaltCommand(saltMaster, 'local', targetLiveSubset, 'state.apply', null, states)
72 salt.printSaltStateResult(result)
73 }
74
75 stage('Confirm live changes on all nodes') {
76 timeout(time: 2, unit: 'HOURS') {
77 input message: "Approve live config change on ${targetLiveAll.expression} nodes?"
78 }
79 }
80
81 stage('Apply config changes on all nodes') {
82 result = salt.runSaltCommand(saltMaster, 'local', targetLiveAll, 'state.apply', null, states)
83 salt.printSaltStateResult(result)
84 }
85
86 } catch (Throwable e) {
87 currentBuild.result = 'FAILURE'
88 throw e
89 } finally {
90 // send notification
91 //common.sendNotification(currentBuild.result,HEAT_STACK_NAME,["slack"])
92 }
93}