blob: 46634bcbbbdc05b754be92e851736856611fa9ef [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()
chnyda625f4b42017-10-11 14:10:31 +020015def python = new com.mirantis.mk.Python()
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020016
chnyda625f4b42017-10-11 14:10:31 +020017def pepperEnv = "pepperEnv"
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020018def targetTestSubset
19def targetLiveSubset
20def targetLiveAll
21def minions
22def result
Jiri Broulik52a6b832017-06-16 13:05:08 +020023def args
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020024def command
25def commandKwargs
Jiri Broulik52a6b832017-06-16 13:05:08 +020026def probe = 1
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020027
28node() {
29 try {
30
chnyda625f4b42017-10-11 14:10:31 +020031 stage('Setup virtualenv for Pepper') {
Dmitrii Kabanovf31c8962017-10-12 21:00:30 -070032 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020033 }
34
35 stage('List target servers') {
chnyda625f4b42017-10-11 14:10:31 +020036 minions = salt.getMinions(pepperEnv, TARGET_SERVERS)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020037
38 if (minions.isEmpty()) {
39 throw new Exception("No minion was targeted")
40 }
41
42 if (TARGET_SUBSET_TEST != "") {
43 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
44 } else {
45 targetTestSubset = minions.join(' or ')
46 }
47 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
Jiri Broulik52a6b832017-06-16 13:05:08 +020048 targetTestSubsetProbe = minions.subList(0, probe).join(' or ')
49 targetLiveSubsetProbe = minions.subList(0, probe).join(' or ')
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020050
51 targetLiveAll = minions.join(' or ')
52 common.infoMsg("Found nodes: ${targetLiveAll}")
53 common.infoMsg("Selected test nodes: ${targetTestSubset}")
54 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
55 }
56
Jiri Broulik52a6b832017-06-16 13:05:08 +020057
58 stage("Add new repos on test nodes") {
chnyda625f4b42017-10-11 14:10:31 +020059 salt.enforceState(pepperEnv, targetTestSubset, 'linux.system.repo')
Jiri Broulik52a6b832017-06-16 13:05:08 +020060 }
61
62
63 opencontrail = null
64
65 try {
chnyda625f4b42017-10-11 14:10:31 +020066 opencontrail = salt.cmdRun(pepperEnv, targetTestSubsetProbe, "salt-call grains.item roles | grep opencontrail.compute")
Jiri Broulik52a6b832017-06-16 13:05:08 +020067 print(opencontrail)
68 } catch (Exception er) {
69 common.infoMsg("opencontrail is not used")
70 }
71
72 if(opencontrail != null) {
73 stage('Remove OC component from repos on test nodes') {
chnyda625f4b42017-10-11 14:10:31 +020074 salt.cmdRun(pepperEnv, 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'")
75 salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.refresh_db', [], null, true)
Jiri Broulik52a6b832017-06-16 13:05:08 +020076 }
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020077 }
78
79 stage("List package upgrades") {
chnyda625f4b42017-10-11 14:10:31 +020080 salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.list_upgrades', [], null, true)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020081 }
82
Jiri Broulik52a6b832017-06-16 13:05:08 +020083 stage('Confirm upgrade on sample nodes') {
84 input message: "Please verify the list of packages that you want to be upgraded. Do you want to continue with upgrade?"
85 }
86
87 stage("Add new repos on sample nodes") {
chnyda625f4b42017-10-11 14:10:31 +020088 salt.enforceState(pepperEnv, targetLiveSubset, 'linux.system.repo')
Jiri Broulik52a6b832017-06-16 13:05:08 +020089 }
90
91 if(opencontrail != null) {
92 stage('Remove OC component from repos on sample nodes') {
chnyda625f4b42017-10-11 14:10:31 +020093 salt.cmdRun(pepperEnv, 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'")
94 salt.runSaltProcessStep(pepperEnv, targetLiveSubset, 'pkg.refresh_db', [], null, true)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +020095 }
96 }
97
Jiri Broulik52a6b832017-06-16 13:05:08 +020098 args = "apt-get -y -s -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade"
99
100 stage('Test upgrade on sample') {
101 try {
chnyda625f4b42017-10-11 14:10:31 +0200102 salt.cmdRun(pepperEnv, targetLiveSubset, args)
Jiri Broulik52a6b832017-06-16 13:05:08 +0200103 } catch (Exception er) {
104 print(er)
105 }
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200106 }
107
Jiri Broulik52a6b832017-06-16 13:05:08 +0200108 stage('Confirm upgrade on sample') {
109 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?"
110 }
111
112 command = "cmd.run"
Jiri Broulikf555b1f2017-06-20 23:04:08 +0200113 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 +0200114
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200115 stage('Apply package upgrades on sample') {
chnyda625f4b42017-10-11 14:10:31 +0200116 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200117 salt.printSaltCommandResult(out)
118 }
119
Jiri Broulik52a6b832017-06-16 13:05:08 +0200120 openvswitch = null
121
122 try {
chnyda625f4b42017-10-11 14:10:31 +0200123 openvswitch = salt.cmdRun(pepperEnv, targetLiveSubsetProbe, "salt-call grains.item roles | grep neutron.compute")
Jiri Broulik52a6b832017-06-16 13:05:08 +0200124 } catch (Exception er) {
125 common.infoMsg("openvswitch is not used")
126 }
127
128 if(openvswitch != null) {
129 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
130
131 stage('Start ovs on sample nodes') {
chnyda625f4b42017-10-11 14:10:31 +0200132 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik52a6b832017-06-16 13:05:08 +0200133 salt.printSaltCommandResult(out)
134 }
Jiri Broulikf20c7672017-06-19 21:20:52 +0200135 stage("Run salt states on sample nodes") {
chnyda625f4b42017-10-11 14:10:31 +0200136 salt.enforceState(pepperEnv, targetLiveSubset, ['nova', 'neutron'])
Jiri Broulik52a6b832017-06-16 13:05:08 +0200137 }
138 } else {
139 stage("Run salt states on sample nodes") {
chnyda625f4b42017-10-11 14:10:31 +0200140 salt.enforceState(pepperEnv, targetLiveSubset, ['nova', 'linux.system.repo'])
Jiri Broulik07292b42017-06-22 18:45:49 +0200141 }
142 }
143
144 stage("Run Highstate on sample nodes") {
145 try {
chnyda625f4b42017-10-11 14:10:31 +0200146 salt.enforceHighstate(pepperEnv, targetLiveSubset)
Jiri Broulik07292b42017-06-22 18:45:49 +0200147 } catch (Exception er) {
148 common.errorMsg("Highstate was executed on ${targetLiveSubset} but something failed. Please check it and fix it accordingly.")
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200149 }
150 }
151
Jiri Broulikf20c7672017-06-19 21:20:52 +0200152 stage('Confirm upgrade on all targeted nodes') {
Jiri Broulik52a6b832017-06-16 13:05:08 +0200153 timeout(time: 2, unit: 'HOURS') {
154 input message: "Verify that the upgraded sample nodes are working correctly. If so, do you want to approve live upgrade on ${targetLiveAll} nodes?"
155 }
156 }
157
Jiri Broulik77e7c432017-06-22 17:58:09 +0200158 stage("Add new repos on all targeted nodes") {
chnyda625f4b42017-10-11 14:10:31 +0200159 salt.enforceState(pepperEnv, targetLiveAll, 'linux.system.repo')
Jiri Broulik77e7c432017-06-22 17:58:09 +0200160 }
161
Jiri Broulik52a6b832017-06-16 13:05:08 +0200162 if(opencontrail != null) {
Jiri Broulikf20c7672017-06-19 21:20:52 +0200163 stage('Remove OC component from repos on all targeted nodes') {
chnyda625f4b42017-10-11 14:10:31 +0200164 salt.cmdRun(pepperEnv, 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'")
165 salt.runSaltProcessStep(pepperEnv, targetLiveAll, 'pkg.refresh_db', [], null, true)
Jiri Broulik52a6b832017-06-16 13:05:08 +0200166 }
167 }
168
Jiri Broulikf555b1f2017-06-20 23:04:08 +0200169 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 +0200170
Jiri Broulikf20c7672017-06-19 21:20:52 +0200171 stage('Apply package upgrades on all targeted nodes') {
chnyda625f4b42017-10-11 14:10:31 +0200172 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200173 salt.printSaltCommandResult(out)
174 }
175
Jiri Broulik52a6b832017-06-16 13:05:08 +0200176 if(openvswitch != null) {
177 args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start"
178
Jiri Broulikf20c7672017-06-19 21:20:52 +0200179 stage('Start ovs on all targeted nodes') {
chnyda625f4b42017-10-11 14:10:31 +0200180 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs)
Jiri Broulik52a6b832017-06-16 13:05:08 +0200181 salt.printSaltCommandResult(out)
182 }
Jiri Broulikf20c7672017-06-19 21:20:52 +0200183 stage("Run salt states on all targeted nodes") {
chnyda625f4b42017-10-11 14:10:31 +0200184 salt.enforceState(pepperEnv, targetLiveAll, ['nova', 'neutron'])
Jiri Broulik52a6b832017-06-16 13:05:08 +0200185 }
186 } else {
Jiri Broulikf20c7672017-06-19 21:20:52 +0200187 stage("Run salt states on all targeted nodes") {
chnyda625f4b42017-10-11 14:10:31 +0200188 salt.enforceState(pepperEnv, targetLiveAll, ['nova', 'linux.system.repo'])
Jiri Broulik07292b42017-06-22 18:45:49 +0200189 }
190 }
191
192 stage("Run Highstate on all targeted nodes") {
193 try {
chnyda625f4b42017-10-11 14:10:31 +0200194 salt.enforceHighstate(pepperEnv, targetLiveAll)
Jiri Broulik07292b42017-06-22 18:45:49 +0200195 } catch (Exception er) {
196 common.errorMsg("Highstate was executed ${targetLiveAll} but something failed. Please check it and fix it accordingly.")
Jiri Broulik52a6b832017-06-16 13:05:08 +0200197 }
198 }
199
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200200 } catch (Throwable e) {
201 // If there was an error or exception thrown, the build failed
202 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200203 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Jiri Broulik9b73d6c2017-06-02 12:27:05 +0200204 throw e
205 }
206}
Jiri Broulik52a6b832017-06-16 13:05:08 +0200207