blob: 4a04531acfa869005c39375dbf76ef93776a5c85 [file] [log] [blame]
Jiri Broulik9b73d6c2017-06-02 12:27:05 +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].
Jiri Broulik9b73d6c2017-06-02 12:27:05 +02008 * 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.
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020010 *
11**/
12
13def common = new com.mirantis.mk.Common()
14def salt = new com.mirantis.mk.Salt()
15
16def saltMaster
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020017def targetTestSubset
18def targetLiveSubset
19def targetLiveAll
20def minions
21def result
Jiri Broulik52a6b832017-06-16 13:05:08 +020022def args
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020023def command
24def commandKwargs
Jiri Broulik52a6b832017-06-16 13:05:08 +020025def probe = 1
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020026
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') {
Tomáš Kukrálc86c8b42017-07-13 10:26:51 +020035 minions = salt.getMinions(saltMaster, TARGET_SERVERS)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020036
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 ')
Jiri Broulik52a6b832017-06-16 13:05:08 +020047 targetTestSubsetProbe = minions.subList(0, probe).join(' or ')
48 targetLiveSubsetProbe = minions.subList(0, probe).join(' or ')
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020049
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
Jiri Broulik52a6b832017-06-16 13:05:08 +020056
57 stage("Add new repos on test nodes") {
58 salt.enforceState(saltMaster, targetTestSubset, 'linux.system.repo')
59 }
60
61
62 opencontrail = null
63
64 try {
65 opencontrail = salt.cmdRun(saltMaster, targetTestSubsetProbe, "salt-call grains.item roles | grep opencontrail.compute")
66 print(opencontrail)
67 } catch (Exception er) {
68 common.infoMsg("opencontrail is not used")
69 }
70
71 if(opencontrail != null) {
72 stage('Remove OC component from repos on test nodes') {
Jiri Broulikbdfa2fb2017-07-17 16:26:12 +020073 salt.cmdRun(saltMaster, targetTestSubset, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g;s/ oc([0-9]*\$)//g'")
Jiri Broulik52a6b832017-06-16 13:05:08 +020074 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.refresh_db', [], null, true)
75 }
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020076 }
77
78 stage("List package upgrades") {
79 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
80 }
81
Jiri Broulik52a6b832017-06-16 13:05:08 +020082 stage('Confirm upgrade on sample nodes') {
83 input message: "Please verify the list of packages that you want to be upgraded. Do you want to continue with upgrade?"
84 }
85
86 stage("Add new repos on sample nodes") {
87 salt.enforceState(saltMaster, targetLiveSubset, 'linux.system.repo')
88 }
89
90 if(opencontrail != null) {
91 stage('Remove OC component from repos on sample nodes') {
Jiri Broulikbdfa2fb2017-07-17 16:26:12 +020092 salt.cmdRun(saltMaster, targetLiveSubset, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g;s/ oc([0-9]*\$)//g'")
Jiri Broulik52a6b832017-06-16 13:05:08 +020093 salt.runSaltProcessStep(saltMaster, targetLiveSubset, 'pkg.refresh_db', [], null, true)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020094 }
95 }
96
Jiri Broulik52a6b832017-06-16 13:05:08 +020097 args = "apt-get -y -s -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade"
98
99 stage('Test upgrade on sample') {
100 try {
101 salt.cmdRun(saltMaster, targetLiveSubset, args)
102 } catch (Exception er) {
103 print(er)
104 }
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200105 }
106
Jiri Broulik52a6b832017-06-16 13:05:08 +0200107 stage('Confirm upgrade on sample') {
108 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?"
109 }
110
111 command = "cmd.run"
Jiri Broulikf555b1f2017-06-20 23:04:08 +0200112 args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;'
Jiri Broulik52a6b832017-06-16 13:05:08 +0200113
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200114 stage('Apply package upgrades on sample') {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200115 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200116 salt.printSaltCommandResult(out)
117 }
118
Jiri Broulik52a6b832017-06-16 13:05:08 +0200119 openvswitch = null
120
121 try {
122 openvswitch = salt.cmdRun(saltMaster, targetLiveSubsetProbe, "salt-call grains.item roles | grep neutron.compute")
123 } catch (Exception er) {
124 common.infoMsg("openvswitch is not used")
125 }
126
127 if(openvswitch != null) {
128 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
129
130 stage('Start ovs on sample nodes') {
131 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
132 salt.printSaltCommandResult(out)
133 }
Jiri Broulikf20c7672017-06-19 21:20:52 +0200134 stage("Run salt states on sample nodes") {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200135 salt.enforceState(saltMaster, targetLiveSubset, ['nova', 'neutron'])
Jiri Broulik52a6b832017-06-16 13:05:08 +0200136 }
137 } else {
138 stage("Run salt states on sample nodes") {
Jiri Broulikf20c7672017-06-19 21:20:52 +0200139 salt.enforceState(saltMaster, targetLiveSubset, ['nova', 'linux.system.repo'])
Jiri Broulik07292b42017-06-22 18:45:49 +0200140 }
141 }
142
143 stage("Run Highstate on sample nodes") {
144 try {
145 salt.enforceHighstate(saltMaster, targetLiveSubset)
146 } catch (Exception er) {
147 common.errorMsg("Highstate was executed on ${targetLiveSubset} but something failed. Please check it and fix it accordingly.")
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200148 }
149 }
150
Jiri Broulikf20c7672017-06-19 21:20:52 +0200151 stage('Confirm upgrade on all targeted nodes') {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200152 timeout(time: 2, unit: 'HOURS') {
153 input message: "Verify that the upgraded sample nodes are working correctly. If so, do you want to approve live upgrade on ${targetLiveAll} nodes?"
154 }
155 }
156
Jiri Broulik77e7c432017-06-22 17:58:09 +0200157 stage("Add new repos on all targeted nodes") {
158 salt.enforceState(saltMaster, targetLiveAll, 'linux.system.repo')
159 }
160
Jiri Broulik52a6b832017-06-16 13:05:08 +0200161 if(opencontrail != null) {
Jiri Broulikf20c7672017-06-19 21:20:52 +0200162 stage('Remove OC component from repos on all targeted nodes') {
Jiri Broulikbdfa2fb2017-07-17 16:26:12 +0200163 salt.cmdRun(saltMaster, targetLiveAll, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g;s/ oc([0-9]*\$)//g'")
Jiri Broulik52a6b832017-06-16 13:05:08 +0200164 salt.runSaltProcessStep(saltMaster, targetLiveAll, 'pkg.refresh_db', [], null, true)
165 }
166 }
167
Jiri Broulikf555b1f2017-06-20 23:04:08 +0200168 args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;'
Jiri Broulik52a6b832017-06-16 13:05:08 +0200169
Jiri Broulikf20c7672017-06-19 21:20:52 +0200170 stage('Apply package upgrades on all targeted nodes') {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200171 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200172 salt.printSaltCommandResult(out)
173 }
174
Jiri Broulik52a6b832017-06-16 13:05:08 +0200175 if(openvswitch != null) {
176 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
177
Jiri Broulikf20c7672017-06-19 21:20:52 +0200178 stage('Start ovs on all targeted nodes') {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200179 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
180 salt.printSaltCommandResult(out)
181 }
Jiri Broulikf20c7672017-06-19 21:20:52 +0200182 stage("Run salt states on all targeted nodes") {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200183 salt.enforceState(saltMaster, targetLiveAll, ['nova', 'neutron'])
Jiri Broulik52a6b832017-06-16 13:05:08 +0200184 }
185 } else {
Jiri Broulikf20c7672017-06-19 21:20:52 +0200186 stage("Run salt states on all targeted nodes") {
187 salt.enforceState(saltMaster, targetLiveAll, ['nova', 'linux.system.repo'])
Jiri Broulik07292b42017-06-22 18:45:49 +0200188 }
189 }
190
191 stage("Run Highstate on all targeted nodes") {
192 try {
193 salt.enforceHighstate(saltMaster, targetLiveAll)
194 } catch (Exception er) {
195 common.errorMsg("Highstate was executed ${targetLiveAll} but something failed. Please check it and fix it accordingly.")
Jiri Broulik52a6b832017-06-16 13:05:08 +0200196 }
197 }
198
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200199 } catch (Throwable e) {
200 // If there was an error or exception thrown, the build failed
201 currentBuild.result = "FAILURE"
202 throw e
203 }
204}
Jiri Broulik52a6b832017-06-16 13:05:08 +0200205