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, |
Leo Xu | e2c716a | 2024-07-29 05:54:18 +0300 | [diff] [blame^] | 12 | multipartHttpPushUri: null, |
greenfil | 6b424f9 | 2023-03-10 10:47:41 +0300 | [diff] [blame] | 13 | httpPushUri: null, |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 14 | }, |
| 15 | getters: { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 16 | isSingleFileUploadEnabled: (state) => state.hostFirmware.length === 0, |
| 17 | activeBmcFirmware: (state) => { |
| 18 | return state.bmcFirmware.find( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 19 | (firmware) => firmware.id === state.bmcActiveFirmwareId, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 20 | ); |
| 21 | }, |
| 22 | activeHostFirmware: (state) => { |
| 23 | return state.hostFirmware.find( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 24 | (firmware) => firmware.id === state.hostActiveFirmwareId, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 25 | ); |
| 26 | }, |
| 27 | backupBmcFirmware: (state) => { |
| 28 | return state.bmcFirmware.find( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 29 | (firmware) => firmware.id !== state.bmcActiveFirmwareId, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 30 | ); |
| 31 | }, |
| 32 | backupHostFirmware: (state) => { |
| 33 | return state.hostFirmware.find( |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 34 | (firmware) => firmware.id !== state.hostActiveFirmwareId, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 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), |
greenfil | 6b424f9 | 2023-03-10 10:47:41 +0300 | [diff] [blame] | 44 | setHttpPushUri: (state, httpPushUri) => (state.httpPushUri = httpPushUri), |
Leo Xu | e2c716a | 2024-07-29 05:54:18 +0300 | [diff] [blame^] | 45 | setMultipartHttpPushUri: (state, multipartHttpPushUri) => |
| 46 | (state.multipartHttpPushUri = multipartHttpPushUri), |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 47 | }, |
| 48 | actions: { |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 49 | async getFirmwareInformation({ dispatch }) { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 50 | dispatch('getActiveHostFirmware'); |
| 51 | dispatch('getActiveBmcFirmware'); |
| 52 | return await dispatch('getFirmwareInventory'); |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 53 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 54 | async getActiveBmcFirmware({ commit }) { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 55 | return api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 56 | .get(`${await this.dispatch('global/getBmcPath')}`) |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 57 | .then(({ data: { Links } }) => { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 58 | const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop(); |
| 59 | commit('setActiveBmcFirmwareId', id); |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 60 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 61 | .catch((error) => console.log(error)); |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 62 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 63 | async getActiveHostFirmware({ commit }) { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 64 | return api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 65 | .get(`${await this.dispatch('global/getSystemPath')}/Bios`) |
Yoshie Muranaka | 98bb24e | 2020-10-06 10:00:19 -0700 | [diff] [blame] | 66 | .then(({ data: { Links } }) => { |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 67 | const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop(); |
| 68 | commit('setActiveHostFirmwareId', id); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 69 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 70 | .catch((error) => console.log(error)); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 71 | }, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 72 | async getFirmwareInventory({ commit }) { |
| 73 | const inventoryList = await api |
| 74 | .get('/redfish/v1/UpdateService/FirmwareInventory') |
| 75 | .then(({ data: { Members = [] } = {} }) => |
Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 76 | Members.map((item) => api.get(item['@odata.id'])), |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 77 | ) |
| 78 | .catch((error) => console.log(error)); |
| 79 | await api |
| 80 | .all(inventoryList) |
| 81 | .then((response) => { |
| 82 | const bmcFirmware = []; |
| 83 | const hostFirmware = []; |
| 84 | response.forEach(({ data }) => { |
| 85 | const firmwareType = data?.RelatedItem?.[0]?.['@odata.id'] |
| 86 | .split('/') |
| 87 | .pop(); |
| 88 | const item = { |
| 89 | version: data?.Version, |
| 90 | id: data?.Id, |
| 91 | location: data?.['@odata.id'], |
| 92 | status: data?.Status?.Health, |
| 93 | }; |
| 94 | if (firmwareType === 'bmc') { |
| 95 | bmcFirmware.push(item); |
| 96 | } else if (firmwareType === 'Bios') { |
| 97 | hostFirmware.push(item); |
| 98 | } |
| 99 | }); |
| 100 | commit('setBmcFirmware', bmcFirmware); |
| 101 | commit('setHostFirmware', hostFirmware); |
| 102 | }) |
| 103 | .catch((error) => { |
| 104 | console.log(error); |
| 105 | }); |
| 106 | }, |
| 107 | getUpdateServiceSettings({ commit }) { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 108 | api |
| 109 | .get('/redfish/v1/UpdateService') |
| 110 | .then(({ data }) => { |
| 111 | const applyTime = |
| 112 | data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime; |
| 113 | commit('setApplyTime', applyTime); |
greenfil | 6b424f9 | 2023-03-10 10:47:41 +0300 | [diff] [blame] | 114 | const httpPushUri = data.HttpPushUri; |
| 115 | commit('setHttpPushUri', httpPushUri); |
Leo Xu | e2c716a | 2024-07-29 05:54:18 +0300 | [diff] [blame^] | 116 | const multipartHttpPushUri = data.MultipartHttpPushUri; |
| 117 | commit('setMultipartHttpPushUri', multipartHttpPushUri); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 118 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 119 | .catch((error) => console.log(error)); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 120 | }, |
Leo Xu | e2c716a | 2024-07-29 05:54:18 +0300 | [diff] [blame^] | 121 | async uploadFirmware({ state, dispatch }, params) { |
| 122 | if (state.multipartHttpPushUri != null) { |
| 123 | return dispatch('uploadFirmwareMultipartHttpPush', params); |
| 124 | } else if (state.httpPushUri != null) { |
| 125 | return dispatch('uploadFirmwareHttpPush', params); |
| 126 | } else { |
| 127 | console.log('Do not support firmware push update'); |
| 128 | } |
| 129 | }, |
| 130 | async uploadFirmwareHttpPush({ state }, { image }) { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 131 | return await api |
greenfil | 6b424f9 | 2023-03-10 10:47:41 +0300 | [diff] [blame] | 132 | .post(state.httpPushUri, image, { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 133 | headers: { 'Content-Type': 'application/octet-stream' }, |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 134 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 135 | .catch((error) => { |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 136 | console.log(error); |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 137 | throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware')); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 138 | }); |
| 139 | }, |
Leo Xu | e2c716a | 2024-07-29 05:54:18 +0300 | [diff] [blame^] | 140 | async uploadFirmwareMultipartHttpPush({ state }, { image, targets }) { |
| 141 | const formData = new FormData(); |
| 142 | formData.append('UpdateFile', image); |
| 143 | let params = {}; |
| 144 | if (targets != null && targets.length > 0) params.Targets = targets; |
| 145 | formData.append('UpdateParameters', JSON.stringify(params)); |
| 146 | return await api |
| 147 | .post(state.multipartHttpPushUri, formData, { |
| 148 | headers: { 'Content-Type': 'multipart/form-data' }, |
| 149 | }) |
| 150 | .catch((error) => { |
| 151 | console.log(error); |
| 152 | throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware')); |
| 153 | }); |
| 154 | }, |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 155 | async switchBmcFirmwareAndReboot({ getters }) { |
| 156 | const backupLocation = getters.backupBmcFirmware.location; |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 157 | const data = { |
| 158 | Links: { |
| 159 | ActiveSoftwareImage: { |
Yoshie Muranaka | 1dedbdf | 2020-11-30 07:04:01 -0800 | [diff] [blame] | 160 | '@odata.id': backupLocation, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 161 | }, |
| 162 | }, |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 163 | }; |
| 164 | return await api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame] | 165 | .patch(`${await this.dispatch('global/getBmcPath')}`, data) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 166 | .catch((error) => { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 167 | console.log(error); |
Yoshie Muranaka | 33d755f | 2021-02-18 15:24:14 -0800 | [diff] [blame] | 168 | throw new Error(i18n.t('pageFirmware.toast.errorSwitchImages')); |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 169 | }); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 170 | }, |
| 171 | }, |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | export default FirmwareStore; |