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 | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 7 | activeFirmware: { |
| 8 | version: '--', |
| 9 | id: null, |
| 10 | location: null |
| 11 | }, |
| 12 | backupFirmware: { |
| 13 | version: '--', |
| 14 | id: null, |
| 15 | location: null, |
| 16 | status: '--' |
| 17 | }, |
| 18 | applyTime: null |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 19 | }, |
| 20 | getters: { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 21 | systemFirmwareVersion: state => state.activeFirmware.version, |
| 22 | backupFirmwareVersion: state => state.backupFirmware.version, |
| 23 | backupFirmwareStatus: state => state.backupFirmware.status, |
| 24 | isRebootFromBackupAvailable: state => |
| 25 | state.backupFirmware.id ? true : false |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 26 | }, |
| 27 | mutations: { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 28 | setActiveFirmware: (state, { version, id, location }) => { |
| 29 | state.activeFirmware.version = version; |
| 30 | state.activeFirmware.id = id; |
| 31 | state.activeFirmware.location = location; |
| 32 | }, |
| 33 | setBackupFirmware: (state, { version, id, location, status }) => { |
| 34 | state.backupFirmware.version = version; |
| 35 | state.backupFirmware.id = id; |
| 36 | state.backupFirmware.location = location; |
| 37 | state.backupFirmware.status = status; |
| 38 | }, |
| 39 | setApplyTime: (state, applyTime) => (state.applyTime = applyTime) |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 40 | }, |
| 41 | actions: { |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 42 | async getSystemFirwareVersion({ commit, state }) { |
Yoshie Muranaka | 598bf7e | 2020-05-01 12:26:00 -0700 | [diff] [blame] | 43 | return await api |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 44 | .get('/redfish/v1/Managers/bmc') |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 45 | .then(({ data: { Links: { ActiveSoftwareImage } } }) => { |
| 46 | const location = ActiveSoftwareImage['@odata.id']; |
| 47 | return api.get(location); |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 48 | }) |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 49 | .then(({ data }) => { |
| 50 | const version = data.Version; |
| 51 | const id = data.Id; |
| 52 | const location = data['@odata.id']; |
| 53 | commit('setActiveFirmware', { version, id, location }); |
| 54 | // TODO: temporary workaround to get 'Backup' Firmware |
| 55 | // information |
| 56 | return api.get('/redfish/v1/UpdateService/FirmwareInventory'); |
| 57 | }) |
| 58 | .then(({ data: { Members } }) => { |
| 59 | // TODO: temporary workaround to get 'Backup' Firmware |
| 60 | // information |
| 61 | // Check FirmwareInventory list for not ActiveSoftwareImage id |
| 62 | const backupLocation = Members.map(item => item['@odata.id']).find( |
| 63 | location => { |
| 64 | const id = location.split('/').pop(); |
| 65 | return id !== state.activeFirmware.id; |
| 66 | } |
| 67 | ); |
| 68 | if (backupLocation) { |
| 69 | return api.get(backupLocation); |
| 70 | } |
| 71 | }) |
| 72 | .then(({ data } = {}) => { |
| 73 | if (!data) return; |
| 74 | const version = data.Version; |
| 75 | const id = data.Id; |
| 76 | const location = data['@odata.id']; |
| 77 | const status = data.Status ? data.Status.State : '--'; |
| 78 | commit('setBackupFirmware', { version, id, location, status }); |
| 79 | }) |
| 80 | .catch(error => console.log(error)); |
| 81 | }, |
| 82 | getUpdateServiceApplyTime({ commit }) { |
| 83 | api |
| 84 | .get('/redfish/v1/UpdateService') |
| 85 | .then(({ data }) => { |
| 86 | const applyTime = |
| 87 | data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime; |
| 88 | commit('setApplyTime', applyTime); |
| 89 | }) |
| 90 | .catch(error => console.log(error)); |
| 91 | }, |
| 92 | setApplyTimeImmediate({ commit }) { |
| 93 | const data = { |
| 94 | HttpPushUriOptions: { |
| 95 | HttpPushUriApplyTime: { |
| 96 | ApplyTime: 'Immediate' |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | return api |
| 101 | .patch('/redfish/v1/UpdateService', data) |
| 102 | .then(() => commit('setApplyTime', 'Immediate')) |
| 103 | .catch(error => console.log(error)); |
| 104 | }, |
| 105 | async uploadFirmware({ state, dispatch }, image) { |
| 106 | if (state.applyTime !== 'Immediate') { |
| 107 | // ApplyTime must be set to Immediate before making |
| 108 | // request to update firmware |
| 109 | await dispatch('setApplyTimeImmediate'); |
| 110 | } |
| 111 | return await api |
| 112 | .post('/redfish/v1/UpdateService', image, { |
| 113 | headers: { 'Content-Type': 'application/octet-stream' } |
| 114 | }) |
| 115 | .then(() => dispatch('getSystemFirwareVersion')) |
| 116 | .then(() => i18n.t('pageFirmware.toast.successUploadMessage')) |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 117 | .catch(error => { |
| 118 | console.log(error); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 119 | throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot')); |
| 120 | }); |
| 121 | }, |
| 122 | async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) { |
| 123 | const data = { |
| 124 | TransferProtocol: 'TFTP', |
| 125 | ImageURI: `${address}/${filename}` |
| 126 | }; |
| 127 | if (state.applyTime !== 'Immediate') { |
| 128 | // ApplyTime must be set to Immediate before making |
| 129 | // request to update firmware |
| 130 | await dispatch('setApplyTimeImmediate'); |
| 131 | } |
| 132 | return await api |
| 133 | .post('/redfish/v1/UpdateService', data) |
| 134 | .then(() => dispatch('getSystemFirwareVersion')) |
| 135 | .then(() => i18n.t('pageFirmware.toast.successUploadMessage')) |
| 136 | .catch(error => { |
| 137 | console.log(error); |
| 138 | throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot')); |
| 139 | }); |
| 140 | }, |
| 141 | async switchFirmwareAndReboot({ state }) { |
| 142 | const backupLoaction = state.backupFirmware.location; |
| 143 | const data = { |
| 144 | Links: { |
| 145 | ActiveSoftwareImage: { |
| 146 | '@odata.id': backupLoaction |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | return await api |
| 151 | .patch('/redfish/v1/Managers/bmc', data) |
| 152 | .then(() => i18n.t('pageFirmware.toast.successRebootFromBackup')) |
| 153 | .catch(error => { |
| 154 | console.log(error); |
| 155 | throw new Error(i18n.t('pageFirmware.toast.errorRebootFromBackup')); |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 156 | }); |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | export default FirmwareStore; |