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