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