| 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 |  | 
|  | 4 | const FirmwareStore = { | 
|  | 5 | namespaced: true, | 
|  | 6 | state: { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 7 | bmcFirmware: [], | 
|  | 8 | hostFirmware: [], | 
|  | 9 | bmcActiveFirmwareId: null, | 
|  | 10 | hostActiveFirmwareId: null, | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 11 | applyTime: null, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 12 | tftpAvailable: false, | 
| Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 13 | }, | 
|  | 14 | getters: { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 15 | isTftpUploadAvailable: (state) => state.tftpAvailable, | 
|  | 16 | isSingleFileUploadEnabled: (state) => state.hostFirmware.length === 0, | 
|  | 17 | activeBmcFirmware: (state) => { | 
|  | 18 | return state.bmcFirmware.find( | 
|  | 19 | (firmware) => firmware.id === state.bmcActiveFirmwareId | 
|  | 20 | ); | 
|  | 21 | }, | 
|  | 22 | activeHostFirmware: (state) => { | 
|  | 23 | return state.hostFirmware.find( | 
|  | 24 | (firmware) => firmware.id === state.hostActiveFirmwareId | 
|  | 25 | ); | 
|  | 26 | }, | 
|  | 27 | backupBmcFirmware: (state) => { | 
|  | 28 | return state.bmcFirmware.find( | 
|  | 29 | (firmware) => firmware.id !== state.bmcActiveFirmwareId | 
|  | 30 | ); | 
|  | 31 | }, | 
|  | 32 | backupHostFirmware: (state) => { | 
|  | 33 | return state.hostFirmware.find( | 
|  | 34 | (firmware) => firmware.id !== state.hostActiveFirmwareId | 
|  | 35 | ); | 
|  | 36 | }, | 
| Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 37 | }, | 
|  | 38 | mutations: { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 39 | setActiveBmcFirmwareId: (state, id) => (state.bmcActiveFirmwareId = id), | 
|  | 40 | setActiveHostFirmwareId: (state, id) => (state.hostActiveFirmwareId = id), | 
|  | 41 | setBmcFirmware: (state, firmware) => (state.bmcFirmware = firmware), | 
|  | 42 | setHostFirmware: (state, firmware) => (state.hostFirmware = firmware), | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 43 | setApplyTime: (state, applyTime) => (state.applyTime = applyTime), | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 44 | setTftpUploadAvailable: (state, tftpAvailable) => | 
|  | 45 | (state.tftpAvailable = tftpAvailable), | 
| Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 46 | }, | 
|  | 47 | actions: { | 
| Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 48 | async getFirmwareInformation({ dispatch }) { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 49 | dispatch('getActiveHostFirmware'); | 
|  | 50 | dispatch('getActiveBmcFirmware'); | 
|  | 51 | return await dispatch('getFirmwareInventory'); | 
| Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 52 | }, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 53 | getActiveBmcFirmware({ commit }) { | 
|  | 54 | return api | 
| Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 55 | .get('/redfish/v1/Managers/bmc') | 
| Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 56 | .then(({ data: { Links } }) => { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 57 | const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop(); | 
|  | 58 | commit('setActiveBmcFirmwareId', id); | 
| Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 59 | }) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 60 | .catch((error) => console.log(error)); | 
| Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 61 | }, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 62 | getActiveHostFirmware({ commit }) { | 
|  | 63 | return api | 
| Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 64 | .get('/redfish/v1/Systems/system/Bios') | 
|  | 65 | .then(({ data: { Links } }) => { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 66 | const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop(); | 
|  | 67 | commit('setActiveHostFirmwareId', id); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 68 | }) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 69 | .catch((error) => console.log(error)); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 70 | }, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 71 | async getFirmwareInventory({ commit }) { | 
|  | 72 | const inventoryList = await api | 
|  | 73 | .get('/redfish/v1/UpdateService/FirmwareInventory') | 
|  | 74 | .then(({ data: { Members = [] } = {} }) => | 
|  | 75 | Members.map((item) => api.get(item['@odata.id'])) | 
|  | 76 | ) | 
|  | 77 | .catch((error) => console.log(error)); | 
|  | 78 | await api | 
|  | 79 | .all(inventoryList) | 
|  | 80 | .then((response) => { | 
|  | 81 | const bmcFirmware = []; | 
|  | 82 | const hostFirmware = []; | 
|  | 83 | response.forEach(({ data }) => { | 
|  | 84 | const firmwareType = data?.RelatedItem?.[0]?.['@odata.id'] | 
|  | 85 | .split('/') | 
|  | 86 | .pop(); | 
|  | 87 | const item = { | 
|  | 88 | version: data?.Version, | 
|  | 89 | id: data?.Id, | 
|  | 90 | location: data?.['@odata.id'], | 
|  | 91 | status: data?.Status?.Health, | 
|  | 92 | }; | 
|  | 93 | if (firmwareType === 'bmc') { | 
|  | 94 | bmcFirmware.push(item); | 
|  | 95 | } else if (firmwareType === 'Bios') { | 
|  | 96 | hostFirmware.push(item); | 
|  | 97 | } | 
|  | 98 | }); | 
|  | 99 | commit('setBmcFirmware', bmcFirmware); | 
|  | 100 | commit('setHostFirmware', hostFirmware); | 
|  | 101 | }) | 
|  | 102 | .catch((error) => { | 
|  | 103 | console.log(error); | 
|  | 104 | }); | 
|  | 105 | }, | 
|  | 106 | getUpdateServiceSettings({ commit }) { | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 107 | api | 
|  | 108 | .get('/redfish/v1/UpdateService') | 
|  | 109 | .then(({ data }) => { | 
|  | 110 | const applyTime = | 
|  | 111 | data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime; | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 112 | const allowableActions = | 
|  | 113 | data?.Actions?.['#UpdateService.SimpleUpdate']?.[ | 
|  | 114 | 'TransferProtocol@Redfish.AllowableValues' | 
|  | 115 | ]; | 
|  | 116 |  | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 117 | commit('setApplyTime', applyTime); | 
| Derick Montague | f7cd5e0 | 2021-05-11 17:02:00 -0500 | [diff] [blame] | 118 | if (allowableActions?.includes('TFTP')) { | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 119 | commit('setTftpUploadAvailable', true); | 
|  | 120 | } | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 121 | }) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 122 | .catch((error) => console.log(error)); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 123 | }, | 
|  | 124 | setApplyTimeImmediate({ commit }) { | 
|  | 125 | const data = { | 
|  | 126 | HttpPushUriOptions: { | 
|  | 127 | HttpPushUriApplyTime: { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 128 | ApplyTime: 'Immediate', | 
|  | 129 | }, | 
|  | 130 | }, | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 131 | }; | 
|  | 132 | return api | 
|  | 133 | .patch('/redfish/v1/UpdateService', data) | 
|  | 134 | .then(() => commit('setApplyTime', 'Immediate')) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 135 | .catch((error) => console.log(error)); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 136 | }, | 
|  | 137 | async uploadFirmware({ state, dispatch }, image) { | 
|  | 138 | if (state.applyTime !== 'Immediate') { | 
|  | 139 | // ApplyTime must be set to Immediate before making | 
|  | 140 | // request to update firmware | 
|  | 141 | await dispatch('setApplyTimeImmediate'); | 
|  | 142 | } | 
|  | 143 | return await api | 
|  | 144 | .post('/redfish/v1/UpdateService', image, { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 145 | headers: { 'Content-Type': 'application/octet-stream' }, | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 146 | }) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 147 | .catch((error) => { | 
| Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 148 | console.log(error); | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 149 | throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware')); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 150 | }); | 
|  | 151 | }, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 152 | async uploadFirmwareTFTP({ state, dispatch }, fileAddress) { | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 153 | const data = { | 
|  | 154 | TransferProtocol: 'TFTP', | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 155 | ImageURI: fileAddress, | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 156 | }; | 
|  | 157 | if (state.applyTime !== 'Immediate') { | 
|  | 158 | // ApplyTime must be set to Immediate before making | 
|  | 159 | // request to update firmware | 
|  | 160 | await dispatch('setApplyTimeImmediate'); | 
|  | 161 | } | 
|  | 162 | return await api | 
| Gunnar Mills | 60713b0 | 2020-08-18 10:21:05 -0500 | [diff] [blame] | 163 | .post( | 
|  | 164 | '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate', | 
|  | 165 | data | 
|  | 166 | ) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 167 | .catch((error) => { | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 168 | console.log(error); | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 169 | throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware')); | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 170 | }); | 
|  | 171 | }, | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 172 | async switchBmcFirmwareAndReboot({ getters }) { | 
|  | 173 | const backupLocation = getters.backupBmcFirmware.location; | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 174 | const data = { | 
|  | 175 | Links: { | 
|  | 176 | ActiveSoftwareImage: { | 
| Yoshie Muranaka | 1dedbdf | 2020-11-30 07:04:01 -0800 | [diff] [blame] | 177 | '@odata.id': backupLocation, | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 178 | }, | 
|  | 179 | }, | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 180 | }; | 
|  | 181 | return await api | 
|  | 182 | .patch('/redfish/v1/Managers/bmc', data) | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 183 | .catch((error) => { | 
| Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 184 | console.log(error); | 
| Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 185 | throw new Error(i18n.t('pageFirmware.toast.errorSwitchImages')); | 
| Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 186 | }); | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 187 | }, | 
|  | 188 | }, | 
| Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 189 | }; | 
|  | 190 |  | 
|  | 191 | export default FirmwareStore; |