Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 1 | import api from '@/store/api'; |
| 2 | import i18n from '@/i18n'; |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 3 | |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 4 | /** |
| 5 | * Get backup firmware image from SoftwareImages |
| 6 | * The backup is whichever image is not the current |
| 7 | * or "ActiveSoftwareImage" |
| 8 | * @param {Array} list |
| 9 | * @param {String} currentLocation |
| 10 | */ |
| 11 | function getBackupFirmwareLocation(list, currentLocation) { |
| 12 | return list |
| 13 | .map(item => item['@odata.id']) |
| 14 | .find(location => { |
| 15 | const id = location.split('/').pop(); |
| 16 | const currentId = currentLocation.split('/').pop(); |
| 17 | return id !== currentId; |
| 18 | }); |
| 19 | } |
| 20 | |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 21 | const FirmwareStore = { |
| 22 | namespaced: true, |
| 23 | state: { |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 24 | bmcFirmware: { |
| 25 | currentVersion: null, |
| 26 | currentState: null, |
| 27 | currentLocation: null, |
| 28 | backupVersion: null, |
| 29 | backupState: null, |
| 30 | backupLocation: null |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 31 | }, |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 32 | hostFirmware: { |
| 33 | currentVersion: null, |
| 34 | currentState: null, |
| 35 | currentLocation: null, |
| 36 | backupVersion: null, |
| 37 | backupState: null, |
| 38 | backupLocation: null |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 39 | }, |
| 40 | applyTime: null |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 41 | }, |
| 42 | getters: { |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 43 | bmcFirmwareCurrentVersion: state => state.bmcFirmware.currentVersion, |
| 44 | bmcFirmwareCurrentState: state => state.bmcFirmware.currentState, |
| 45 | bmcFirmwareBackupVersion: state => state.bmcFirmware.backupVersion, |
| 46 | bmcFirmwareBackupState: state => state.bmcFirmware.backupState, |
| 47 | hostFirmwareCurrentVersion: state => state.hostFirmware.currentVersion, |
| 48 | hostFirmwareCurrentState: state => state.hostFirmware.currentState, |
| 49 | hostFirmwareBackupVersion: state => state.hostFirmware.backupVersion, |
| 50 | hostFirmwareBackupState: state => state.hostFirmware.backupState |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 51 | }, |
| 52 | mutations: { |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 53 | setBmcFirmwareCurrent: (state, { version, location, status }) => { |
| 54 | state.bmcFirmware.currentVersion = version; |
| 55 | state.bmcFirmware.currentState = status; |
| 56 | state.bmcFirmware.currentLocation = location; |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 57 | }, |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 58 | setBmcFirmwareBackup: (state, { version, location, status }) => { |
| 59 | state.bmcFirmware.backupVersion = version; |
| 60 | state.bmcFirmware.backupState = status; |
| 61 | state.bmcFirmware.backupLocation = location; |
| 62 | }, |
| 63 | setHostFirmwareCurrent: (state, { version, location, status }) => { |
| 64 | state.hostFirmware.currentVersion = version; |
| 65 | state.hostFirmware.currentState = status; |
| 66 | state.hostFirmware.currentLocation = location; |
| 67 | }, |
| 68 | setHostFirmwareBackup: (state, { version, location, status }) => { |
| 69 | state.hostFirmware.backupVersion = version; |
| 70 | state.hostFirmware.backupState = status; |
| 71 | state.hostFirmware.backupLocation = location; |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 72 | }, |
| 73 | setApplyTime: (state, applyTime) => (state.applyTime = applyTime) |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 74 | }, |
| 75 | actions: { |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 76 | async getFirmwareInformation({ dispatch }) { |
| 77 | return await api.all([ |
| 78 | dispatch('getBmcFirmware'), |
| 79 | dispatch('getHostFirmware') |
| 80 | ]); |
| 81 | }, |
| 82 | async getBmcFirmware({ commit }) { |
Yoshie Muranaka | 598bf7e | 2020-05-01 12:26:00 -0700 | [diff] [blame] | 83 | return await api |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 84 | .get('/redfish/v1/Managers/bmc') |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 85 | .then(({ data: { Links } }) => { |
| 86 | const currentLocation = Links.ActiveSoftwareImage['@odata.id']; |
| 87 | // Check SoftwareImages list for not ActiveSoftwareImage id |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 88 | const backupLocation = getBackupFirmwareLocation( |
| 89 | Links.SoftwareImages, |
| 90 | currentLocation |
| 91 | ); |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 92 | return { currentLocation, backupLocation }; |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 93 | }) |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 94 | .then(async ({ currentLocation, backupLocation }) => { |
| 95 | const currentData = await api.get(currentLocation); |
| 96 | let backupData = {}; |
| 97 | |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 98 | if (backupLocation) { |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 99 | backupData = await api.get(backupLocation); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 100 | } |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 101 | |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 102 | commit('setBmcFirmwareCurrent', { |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 103 | version: currentData?.data?.Version, |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 104 | location: currentData?.data?.['@odata.id'], |
| 105 | status: currentData?.data?.Status?.State |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 106 | }); |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 107 | commit('setBmcFirmwareBackup', { |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 108 | version: backupData.data?.Version, |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 109 | location: backupData.data?.['@odata.id'], |
| 110 | status: backupData.data?.Status?.State |
| 111 | }); |
| 112 | }) |
| 113 | .catch(error => console.log(error)); |
| 114 | }, |
| 115 | async getHostFirmware({ commit }) { |
| 116 | return await api |
| 117 | .get('/redfish/v1/Systems/system/Bios') |
| 118 | .then(({ data: { Links } }) => { |
| 119 | const currentLocation = Links.ActiveSoftwareImage['@odata.id']; |
| 120 | const backupLocation = getBackupFirmwareLocation( |
| 121 | Links.SoftwareImages, |
| 122 | currentLocation |
| 123 | ); |
| 124 | return { currentLocation, backupLocation }; |
| 125 | }) |
| 126 | .then(async ({ currentLocation, backupLocation }) => { |
| 127 | const currentData = await api.get(currentLocation); |
| 128 | let backupData = {}; |
| 129 | |
| 130 | if (backupLocation) { |
| 131 | backupData = await api.get(backupLocation); |
| 132 | } |
| 133 | |
| 134 | commit('setHostFirmwareCurrent', { |
| 135 | version: currentData?.data?.Version, |
| 136 | location: currentData?.data?.['@odata.id'], |
| 137 | status: currentData?.data?.Status?.State |
| 138 | }); |
| 139 | commit('setHostFirmwareBackup', { |
| 140 | version: backupData.data?.Version, |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 141 | location: backupData.data?.['@odata.id'], |
| 142 | status: backupData.data?.Status?.State |
| 143 | }); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 144 | }) |
| 145 | .catch(error => console.log(error)); |
| 146 | }, |
| 147 | getUpdateServiceApplyTime({ commit }) { |
| 148 | api |
| 149 | .get('/redfish/v1/UpdateService') |
| 150 | .then(({ data }) => { |
| 151 | const applyTime = |
| 152 | data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime; |
| 153 | commit('setApplyTime', applyTime); |
| 154 | }) |
| 155 | .catch(error => console.log(error)); |
| 156 | }, |
| 157 | setApplyTimeImmediate({ commit }) { |
| 158 | const data = { |
| 159 | HttpPushUriOptions: { |
| 160 | HttpPushUriApplyTime: { |
| 161 | ApplyTime: 'Immediate' |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | return api |
| 166 | .patch('/redfish/v1/UpdateService', data) |
| 167 | .then(() => commit('setApplyTime', 'Immediate')) |
| 168 | .catch(error => console.log(error)); |
| 169 | }, |
| 170 | async uploadFirmware({ state, dispatch }, image) { |
| 171 | if (state.applyTime !== 'Immediate') { |
| 172 | // ApplyTime must be set to Immediate before making |
| 173 | // request to update firmware |
| 174 | await dispatch('setApplyTimeImmediate'); |
| 175 | } |
| 176 | return await api |
| 177 | .post('/redfish/v1/UpdateService', image, { |
| 178 | headers: { 'Content-Type': 'application/octet-stream' } |
| 179 | }) |
| 180 | .then(() => dispatch('getSystemFirwareVersion')) |
| 181 | .then(() => i18n.t('pageFirmware.toast.successUploadMessage')) |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 182 | .catch(error => { |
| 183 | console.log(error); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 184 | throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot')); |
| 185 | }); |
| 186 | }, |
| 187 | async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) { |
| 188 | const data = { |
| 189 | TransferProtocol: 'TFTP', |
| 190 | ImageURI: `${address}/${filename}` |
| 191 | }; |
| 192 | if (state.applyTime !== 'Immediate') { |
| 193 | // ApplyTime must be set to Immediate before making |
| 194 | // request to update firmware |
| 195 | await dispatch('setApplyTimeImmediate'); |
| 196 | } |
| 197 | return await api |
Gunnar Mills | 60713b0 | 2020-08-18 10:21:05 -0500 | [diff] [blame] | 198 | .post( |
| 199 | '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate', |
| 200 | data |
| 201 | ) |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 202 | .then(() => dispatch('getSystemFirwareVersion')) |
| 203 | .then(() => i18n.t('pageFirmware.toast.successUploadMessage')) |
| 204 | .catch(error => { |
| 205 | console.log(error); |
| 206 | throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot')); |
| 207 | }); |
| 208 | }, |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame^] | 209 | async swtichBmcFirmware({ state }) { |
| 210 | const backupLoaction = state.bmcFirmware.backupLoaction; |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 211 | const data = { |
| 212 | Links: { |
| 213 | ActiveSoftwareImage: { |
| 214 | '@odata.id': backupLoaction |
| 215 | } |
| 216 | } |
| 217 | }; |
| 218 | return await api |
| 219 | .patch('/redfish/v1/Managers/bmc', data) |
| 220 | .then(() => i18n.t('pageFirmware.toast.successRebootFromBackup')) |
| 221 | .catch(error => { |
| 222 | console.log(error); |
| 223 | throw new Error(i18n.t('pageFirmware.toast.errorRebootFromBackup')); |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 224 | }); |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | export default FirmwareStore; |