blob: 70037a4f758d02e5a6294fa52d1cbb326a81e968 [file] [log] [blame]
Jiri Broulikbdfa2fb2017-07-17 16:26:12 +02001/**
2 * Update packages on given nodes
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_SUBSET_TEST Number of nodes to list package updates, empty string means all targetted nodes.
9 * TARGET_SUBSET_LIVE Number of selected nodes to live apply selected package update.
10 *
11**/
12
13def common = new com.mirantis.mk.Common()
14def salt = new com.mirantis.mk.Salt()
15
16def saltMaster
17def targetTestSubset
18def targetLiveSubset
19def targetLiveAll
20def minions
21def result
22def args
23def command
24def commandKwargs
25def probe = 1
26
27node() {
28 try {
29
30 stage('Connect to Salt master') {
31 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
32 }
33
34 stage('List target servers') {
35 minions = salt.getMinions(saltMaster, TARGET_SERVERS)
36
37 if (minions.isEmpty()) {
38 throw new Exception("No minion was targeted")
39 }
40
41 if (TARGET_SUBSET_TEST != "") {
42 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
43 } else {
44 targetTestSubset = minions.join(' or ')
45 }
46 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
47 targetTestSubsetProbe = minions.subList(0, probe).join(' or ')
48 targetLiveSubsetProbe = minions.subList(0, probe).join(' or ')
49
50 targetLiveAll = minions.join(' or ')
51 common.infoMsg("Found nodes: ${targetLiveAll}")
52 common.infoMsg("Selected test nodes: ${targetTestSubset}")
53 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
54 }
55
56
57 stage("Add new repos on test nodes") {
58 salt.enforceState(saltMaster, targetTestSubset, 'linux.system.repo')
59 }
60
61 stage("List package upgrades") {
62 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
63 }
64
65 stage('Confirm upgrade on sample nodes') {
66 input message: "Please verify the list of packages that you want to be upgraded. Do you want to continue with upgrade?"
67 }
68
69 stage("Add new repos on sample nodes") {
70 salt.enforceState(saltMaster, targetLiveSubset, 'linux.system.repo')
71 }
72
73 args = "apt-get -y -s -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade"
74
75 stage('Test upgrade on sample') {
76 try {
77 salt.cmdRun(saltMaster, targetLiveSubset, args)
78 } catch (Exception er) {
79 print(er)
80 }
81 }
82
83 stage('Confirm upgrade on sample') {
84 input message: "Please verify if there are packages that it wants to downgrade. If so, execute apt-cache policy on them and verify if everything is fine. Do you want to continue with upgrade?"
85 }
86
87 command = "cmd.run"
88 args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;'
89
90 stage('Apply package upgrades on sample') {
91 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
92 salt.printSaltCommandResult(out)
93 }
94
95 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
96
97 stage('Start ovs on sample nodes') {
98 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
99 salt.printSaltCommandResult(out)
100 }
101 stage("Run Neutron state on sample nodes") {
102 salt.enforceState(saltMaster, targetLiveSubset, ['neutron'])
103 }
104
105 stage("Run Highstate on sample nodes") {
106 try {
107 salt.enforceHighstate(saltMaster, targetLiveSubset)
108 } catch (Exception er) {
109 common.errorMsg("Highstate was executed on ${targetLiveSubset} but something failed. Please check it and fix it accordingly.")
110 }
111 }
112
113 stage('Confirm upgrade on all targeted nodes') {
114 timeout(time: 2, unit: 'HOURS') {
115 input message: "Verify that the upgraded sample nodes are working correctly. If so, do you want to approve live upgrade on ${targetLiveAll} nodes?"
116 }
117 }
118
119 stage("Add new repos on all targeted nodes") {
120 salt.enforceState(saltMaster, targetLiveAll, 'linux.system.repo')
121 }
122
123 args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;'
124
125 stage('Apply package upgrades on all targeted nodes') {
126 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
127 salt.printSaltCommandResult(out)
128 }
129
130 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
131
132 stage('Start ovs on all targeted nodes') {
133 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
134 salt.printSaltCommandResult(out)
135 }
136 stage("Run Neutron state on all targeted nodes") {
137 salt.enforceState(saltMaster, targetLiveAll, ['neutron'])
138 }
139
140 stage("Run Highstate on all targeted nodes") {
141 try {
142 salt.enforceHighstate(saltMaster, targetLiveAll)
143 } catch (Exception er) {
144 common.errorMsg("Highstate was executed ${targetLiveAll} but something failed. Please check it and fix it accordingly.")
145 }
146 }
147
148 } catch (Throwable e) {
149 // If there was an error or exception thrown, the build failed
150 currentBuild.result = "FAILURE"
151 throw e
152 }
153}
154