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() |
| 15 | |
| 16 | def saltMaster |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 17 | def targetTestSubset |
| 18 | def targetLiveSubset |
| 19 | def targetLiveAll |
| 20 | def minions |
| 21 | def result |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 22 | def args |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 23 | def command |
| 24 | def commandKwargs |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 25 | def probe = 1 |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 26 | |
| 27 | node() { |
| 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ál | c86c8b4 | 2017-07-13 10:26:51 +0200 | [diff] [blame^] | 35 | minions = salt.getMinions(saltMaster, TARGET_SERVERS) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 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 ') |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 47 | targetTestSubsetProbe = minions.subList(0, probe).join(' or ') |
| 48 | targetLiveSubsetProbe = minions.subList(0, probe).join(' or ') |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 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 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 56 | |
| 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') { |
| 73 | salt.cmdRun(saltMaster, targetTestSubset, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g'") |
| 74 | salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.refresh_db', [], null, true) |
| 75 | } |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | stage("List package upgrades") { |
| 79 | salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true) |
| 80 | } |
| 81 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 82 | 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') { |
| 92 | salt.cmdRun(saltMaster, targetLiveSubset, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g'") |
| 93 | salt.runSaltProcessStep(saltMaster, targetLiveSubset, 'pkg.refresh_db', [], null, true) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 97 | 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 Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 107 | 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 Broulik | f555b1f | 2017-06-20 23:04:08 +0200 | [diff] [blame] | 112 | 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] | 113 | |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 114 | stage('Apply package upgrades on sample') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 115 | out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 116 | salt.printSaltCommandResult(out) |
| 117 | } |
| 118 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 119 | 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 Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 134 | stage("Run salt states on sample nodes") { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 135 | salt.enforceState(saltMaster, targetLiveSubset, ['nova', 'neutron']) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 136 | } |
| 137 | } else { |
| 138 | stage("Run salt states on sample nodes") { |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 139 | salt.enforceState(saltMaster, targetLiveSubset, ['nova', 'linux.system.repo']) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 140 | } |
| 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 Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 151 | stage('Confirm upgrade on all targeted nodes') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 152 | 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 Broulik | 77e7c43 | 2017-06-22 17:58:09 +0200 | [diff] [blame] | 157 | stage("Add new repos on all targeted nodes") { |
| 158 | salt.enforceState(saltMaster, targetLiveAll, 'linux.system.repo') |
| 159 | } |
| 160 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 161 | if(opencontrail != null) { |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 162 | stage('Remove OC component from repos on all targeted nodes') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 163 | salt.cmdRun(saltMaster, targetLiveAll, "find /etc/apt/sources.list* -type f -print0 | xargs -0 sed -i -r -e 's/ oc([0-9]*) / /g'") |
| 164 | salt.runSaltProcessStep(saltMaster, targetLiveAll, 'pkg.refresh_db', [], null, true) |
| 165 | } |
| 166 | } |
| 167 | |
Jiri Broulik | f555b1f | 2017-06-20 23:04:08 +0200 | [diff] [blame] | 168 | 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] | 169 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 170 | stage('Apply package upgrades on all targeted nodes') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 171 | out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs) |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 172 | salt.printSaltCommandResult(out) |
| 173 | } |
| 174 | |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 175 | if(openvswitch != null) { |
| 176 | args = "sudo /usr/share/openvswitch/scripts/ovs-ctl start" |
| 177 | |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 178 | stage('Start ovs on all targeted nodes') { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 179 | out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, args, commandKwargs) |
| 180 | salt.printSaltCommandResult(out) |
| 181 | } |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 182 | stage("Run salt states on all targeted nodes") { |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 183 | salt.enforceState(saltMaster, targetLiveAll, ['nova', 'neutron']) |
Jiri Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 184 | } |
| 185 | } else { |
Jiri Broulik | f20c767 | 2017-06-19 21:20:52 +0200 | [diff] [blame] | 186 | stage("Run salt states on all targeted nodes") { |
| 187 | salt.enforceState(saltMaster, targetLiveAll, ['nova', 'linux.system.repo']) |
Jiri Broulik | 07292b4 | 2017-06-22 18:45:49 +0200 | [diff] [blame] | 188 | } |
| 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 Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Jiri Broulik | 9b73d6c | 2017-06-02 12:27:05 +0200 | [diff] [blame] | 199 | } 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 Broulik | 52a6b83 | 2017-06-16 13:05:08 +0200 | [diff] [blame] | 205 | |