Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 1 | /** |
| 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 Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 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. |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 10 | * |
| 11 | **/ |
| 12 | |
| 13 | def common = new com.mirantis.mk.Common() |
| 14 | def salt = new com.mirantis.mk.Salt() |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 15 | def python = new com.mirantis.mk.Python() |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 16 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 17 | def pepperEnv = "pepperEnv" |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 18 | def targetTestSubset |
| 19 | def targetLiveSubset |
| 20 | def targetLiveAll |
| 21 | def minions |
| 22 | def result |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 23 | def args |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 24 | def command |
| 25 | def commandKwargs |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 26 | def probe = 1 |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 27 | |
| 28 | node() { |
| 29 | try { |
| 30 | |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 31 | stage('Setup virtualenv for Pepper') { |
Dmitrii Kabanov | f31c896 | 2017-10-12 21:00:30 -0700 | [diff] [blame] | 32 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | stage('List target servers') { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 36 | minions = salt.getMinions(pepperEnv, TARGET_SERVERS) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 37 | |
| 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 Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 48 | targetTestSubsetProbe = minions.subList(0, probe).join(' or ') |
| 49 | targetLiveSubsetProbe = minions.subList(0, probe).join(' or ') |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 50 | |
| 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 Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 57 | |
| 58 | stage("Add new repos on test nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 59 | salt.enforceState(pepperEnv, targetTestSubset, 'linux.system.repo') |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | opencontrail = null |
| 64 | |
| 65 | try { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 66 | opencontrail = salt.cmdRun(pepperEnv, targetTestSubsetProbe, "salt-call grains.item roles | grep opencontrail.compute") |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 67 | 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') { |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 74 | def contrail_repo_file1 = '' |
| 75 | def contrail_repo_file2 = '' |
| 76 | try { |
| 77 | contrail_repo_file1 = salt.cmdRun(pepperEnv, targetTestSubset, "grep -Eorl \\ oc\\([0-9]*\$\\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 78 | contrail_repo_file2 = salt.cmdRun(pepperEnv, targetTestSubset, "grep -Eorl \\ oc\\([0-9]*\\ \\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 79 | } catch (Exception er) { |
| 80 | common.warningMsg(er) |
| 81 | } |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 82 | 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'") |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 83 | try { |
| 84 | salt.cmdRun(pepperEnv, targetTestSubset, "salt-call pkg.refresh_db") |
| 85 | } catch (Exception er) { |
| 86 | common.warningMsg(er) |
| 87 | // remove the malformed repo entry |
| 88 | salt.cmdRun(pepperEnv, targetTestSubset, "rm ${contrail_repo_file1} ${contrail_repo_file2}") |
| 89 | salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.refresh_db', [], null, true) |
| 90 | } |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 91 | } |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | stage("List package upgrades") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 95 | salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.list_upgrades', [], null, true) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 98 | stage('Confirm upgrade on sample nodes') { |
| 99 | input message: "Please verify the list of packages that you want to be upgraded. Do you want to continue with upgrade?" |
| 100 | } |
| 101 | |
| 102 | stage("Add new repos on sample nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 103 | salt.enforceState(pepperEnv, targetLiveSubset, 'linux.system.repo') |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if(opencontrail != null) { |
| 107 | stage('Remove OC component from repos on sample nodes') { |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 108 | def contrail_repo_file1 = '' |
| 109 | def contrail_repo_file2 = '' |
| 110 | try { |
| 111 | contrail_repo_file1 = salt.cmdRun(pepperEnv, targetLiveSubset, "grep -Eorl \\ oc\\([0-9]*\$\\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 112 | contrail_repo_file2 = salt.cmdRun(pepperEnv, targetLiveSubset, "grep -Eorl \\ oc\\([0-9]*\\ \\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 113 | } catch (Exception er) { |
| 114 | common.warningMsg(er) |
| 115 | } |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 116 | 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'") |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 117 | try { |
| 118 | salt.cmdRun(pepperEnv, targetLiveSubset, "salt-call pkg.refresh_db") |
| 119 | } catch (Exception er) { |
| 120 | common.warningMsg(er) |
| 121 | // remove the malformed repo entry |
| 122 | salt.cmdRun(pepperEnv, targetLiveSubset, "rm ${contrail_repo_file1} ${contrail_repo_file2}") |
| 123 | salt.runSaltProcessStep(pepperEnv, targetLiveSubset, 'pkg.refresh_db', [], null, true) |
| 124 | } |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 128 | args = "apt-get -y -s -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade" |
| 129 | |
| 130 | stage('Test upgrade on sample') { |
| 131 | try { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 132 | salt.cmdRun(pepperEnv, targetLiveSubset, args) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 133 | } catch (Exception er) { |
| 134 | print(er) |
| 135 | } |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 138 | stage('Confirm upgrade on sample') { |
| 139 | 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?" |
| 140 | } |
| 141 | |
| 142 | command = "cmd.run" |
Jiri Broulik | f555b1f | 2017-06-20 23:04:08 +0200 | [diff] [blame] | 143 | args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;' |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 144 | |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 145 | stage('Apply package upgrades on sample') { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 146 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 147 | salt.printSaltCommandResult(out) |
| 148 | } |
| 149 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 150 | openvswitch = null |
| 151 | |
| 152 | try { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 153 | openvswitch = salt.cmdRun(pepperEnv, targetLiveSubsetProbe, "salt-call grains.item roles | grep neutron.compute") |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 154 | } catch (Exception er) { |
| 155 | common.infoMsg("openvswitch is not used") |
| 156 | } |
| 157 | |
| 158 | if(openvswitch != null) { |
| 159 | args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start" |
| 160 | |
| 161 | stage('Start ovs on sample nodes') { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 162 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 163 | salt.printSaltCommandResult(out) |
| 164 | } |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 165 | stage("Run salt states on sample nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 166 | salt.enforceState(pepperEnv, targetLiveSubset, ['nova', 'neutron']) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 167 | } |
| 168 | } else { |
| 169 | stage("Run salt states on sample nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 170 | salt.enforceState(pepperEnv, targetLiveSubset, ['nova', 'linux.system.repo']) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | stage("Run Highstate on sample nodes") { |
| 175 | try { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 176 | salt.enforceHighstate(pepperEnv, targetLiveSubset) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 177 | } catch (Exception er) { |
| 178 | common.errorMsg("Highstate was executed on ${targetLiveSubset} but something failed. Please check it and fix it accordingly.") |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 182 | stage('Confirm upgrade on all targeted nodes') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 183 | timeout(time: 2, unit: 'HOURS') { |
| 184 | input message: "Verify that the upgraded sample nodes are working correctly. If so, do you want to approve live upgrade on ${targetLiveAll} nodes?" |
| 185 | } |
| 186 | } |
| 187 | |
Jiri Broulik | 77e7c43 | 2017-06-22 17:58:09 +0200 | [diff] [blame] | 188 | stage("Add new repos on all targeted nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 189 | salt.enforceState(pepperEnv, targetLiveAll, 'linux.system.repo') |
Jiri Broulik | 77e7c43 | 2017-06-22 17:58:09 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 192 | if(opencontrail != null) { |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 193 | stage('Remove OC component from repos on all targeted nodes') { |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 194 | def contrail_repo_file1 = '' |
| 195 | def contrail_repo_file2 = '' |
| 196 | try { |
| 197 | contrail_repo_file1 = salt.cmdRun(pepperEnv, targetLiveAll, "grep -Eorl \\ oc\\([0-9]*\$\\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 198 | contrail_repo_file2 = salt.cmdRun(pepperEnv, targetLiveAll, "grep -Eorl \\ oc\\([0-9]*\\ \\) /etc/apt/sources.list*")['return'][0].values()[0].split("\n")[0] |
| 199 | } catch (Exception er) { |
| 200 | common.warningMsg(er) |
| 201 | } |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 202 | 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'") |
Jiri Broulik | cbad2f1 | 2017-11-23 17:15:36 +0100 | [diff] [blame] | 203 | try { |
| 204 | salt.cmdRun(pepperEnv, targetLiveAll, "salt-call pkg.refresh_db") |
| 205 | } catch (Exception er) { |
| 206 | common.warningMsg(er) |
| 207 | // remove the malformed repo entry |
| 208 | salt.cmdRun(pepperEnv, targetLiveAll, "rm ${contrail_repo_file1} ${contrail_repo_file2}") |
| 209 | salt.runSaltProcessStep(pepperEnv, targetLiveAll, 'pkg.refresh_db', [], null, true) |
| 210 | } |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Jiri Broulik | f555b1f | 2017-06-20 23:04:08 +0200 | [diff] [blame] | 214 | args = 'export DEBIAN_FRONTEND=noninteractive; apt-get -y -q --allow-downgrades -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" dist-upgrade;' |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 215 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 216 | stage('Apply package upgrades on all targeted nodes') { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 217 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 218 | salt.printSaltCommandResult(out) |
| 219 | } |
| 220 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 221 | if(openvswitch != null) { |
| 222 | args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start" |
| 223 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 224 | stage('Start ovs on all targeted nodes') { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 225 | out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 226 | salt.printSaltCommandResult(out) |
| 227 | } |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 228 | stage("Run salt states on all targeted nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 229 | salt.enforceState(pepperEnv, targetLiveAll, ['nova', 'neutron']) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 230 | } |
| 231 | } else { |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 232 | stage("Run salt states on all targeted nodes") { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 233 | salt.enforceState(pepperEnv, targetLiveAll, ['nova', 'linux.system.repo']) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | stage("Run Highstate on all targeted nodes") { |
| 238 | try { |
chnyda | 625f4b4 | 2017-10-11 14:10:31 +0200 | [diff] [blame] | 239 | salt.enforceHighstate(pepperEnv, targetLiveAll) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 240 | } catch (Exception er) { |
| 241 | common.errorMsg("Highstate was executed ${targetLiveAll} but something failed. Please check it and fix it accordingly.") |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 245 | } catch (Throwable e) { |
| 246 | // If there was an error or exception thrown, the build failed |
| 247 | currentBuild.result = "FAILURE" |
Jakub Josef | d2efd7d | 2017-08-22 17:49:57 +0200 | [diff] [blame] | 248 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 249 | throw e |
| 250 | } |
| 251 | } |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 252 | |