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 | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 42 | async getSystemFirwareVersion({ commit }) { |
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 | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 45 | .then(({ data: { Links } }) => { |
| 46 | const currentLocation = Links.ActiveSoftwareImage['@odata.id']; |
| 47 | // Check SoftwareImages list for not ActiveSoftwareImage id |
| 48 | const backupLocation = Links.SoftwareImages.map( |
| 49 | item => item['@odata.id'] |
| 50 | ).find(location => { |
| 51 | const id = location.split('/').pop(); |
| 52 | const currentId = currentLocation.split('/').pop(); |
| 53 | return id !== currentId; |
| 54 | }); |
| 55 | return { currentLocation, backupLocation }; |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 56 | }) |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 57 | .then(async ({ currentLocation, backupLocation }) => { |
| 58 | const currentData = await api.get(currentLocation); |
| 59 | let backupData = {}; |
| 60 | |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 61 | if (backupLocation) { |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 62 | backupData = await api.get(backupLocation); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 63 | } |
Yoshie Muranaka | d227f1c | 2020-10-06 13:46:58 -0700 | [diff] [blame] | 64 | |
| 65 | commit('setActiveFirmware', { |
| 66 | version: currentData?.data?.Version, |
| 67 | id: currentData?.data?.Id, |
| 68 | location: currentData?.data?.['@odata.id'] |
| 69 | }); |
| 70 | commit('setBackupFirmware', { |
| 71 | version: backupData.data?.Version, |
| 72 | id: backupData.data?.Id, |
| 73 | location: backupData.data?.['@odata.id'], |
| 74 | status: backupData.data?.Status?.State |
| 75 | }); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 76 | }) |
| 77 | .catch(error => console.log(error)); |
| 78 | }, |
| 79 | getUpdateServiceApplyTime({ commit }) { |
| 80 | api |
| 81 | .get('/redfish/v1/UpdateService') |
| 82 | .then(({ data }) => { |
| 83 | const applyTime = |
| 84 | data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime; |
| 85 | commit('setApplyTime', applyTime); |
| 86 | }) |
| 87 | .catch(error => console.log(error)); |
| 88 | }, |
| 89 | setApplyTimeImmediate({ commit }) { |
| 90 | const data = { |
| 91 | HttpPushUriOptions: { |
| 92 | HttpPushUriApplyTime: { |
| 93 | ApplyTime: 'Immediate' |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | return api |
| 98 | .patch('/redfish/v1/UpdateService', data) |
| 99 | .then(() => commit('setApplyTime', 'Immediate')) |
| 100 | .catch(error => console.log(error)); |
| 101 | }, |
| 102 | async uploadFirmware({ state, dispatch }, image) { |
| 103 | if (state.applyTime !== 'Immediate') { |
| 104 | // ApplyTime must be set to Immediate before making |
| 105 | // request to update firmware |
| 106 | await dispatch('setApplyTimeImmediate'); |
| 107 | } |
| 108 | return await api |
| 109 | .post('/redfish/v1/UpdateService', image, { |
| 110 | headers: { 'Content-Type': 'application/octet-stream' } |
| 111 | }) |
| 112 | .then(() => dispatch('getSystemFirwareVersion')) |
| 113 | .then(() => i18n.t('pageFirmware.toast.successUploadMessage')) |
Dixsie Wolmers | 46a8744 | 2020-02-26 15:26:30 -0600 | [diff] [blame] | 114 | .catch(error => { |
| 115 | console.log(error); |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 116 | throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot')); |
| 117 | }); |
| 118 | }, |
| 119 | async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) { |
| 120 | const data = { |
| 121 | TransferProtocol: 'TFTP', |
| 122 | ImageURI: `${address}/${filename}` |
| 123 | }; |
| 124 | if (state.applyTime !== 'Immediate') { |
| 125 | // ApplyTime must be set to Immediate before making |
| 126 | // request to update firmware |
| 127 | await dispatch('setApplyTimeImmediate'); |
| 128 | } |
| 129 | return await api |
Gunnar Mills | 60713b0 | 2020-08-18 10:21:05 -0500 | [diff] [blame] | 130 | .post( |
| 131 | '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate', |
| 132 | data |
| 133 | ) |
Yoshie Muranaka | 92a0a4a | 2020-07-15 10:30:31 -0700 | [diff] [blame] | 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; |