blob: c6639ff7618d553ada1ba7d98cabb3fa4dedd237 [file] [log] [blame]
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -07001import api from '@/store/api';
2import i18n from '@/i18n';
Dixsie Wolmersf65ee342020-01-22 19:47:56 -06003
4const FirmwareStore = {
5 namespaced: true,
6 state: {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -08007 bmcFirmware: [],
8 hostFirmware: [],
9 bmcActiveFirmwareId: null,
10 hostActiveFirmwareId: null,
Derick Montague602e98a2020-10-21 16:20:00 -050011 applyTime: null,
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080012 tftpAvailable: false,
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060013 },
14 getters: {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080015 isTftpUploadAvailable: (state) => state.tftpAvailable,
16 isSingleFileUploadEnabled: (state) => state.hostFirmware.length === 0,
17 activeBmcFirmware: (state) => {
18 return state.bmcFirmware.find(
19 (firmware) => firmware.id === state.bmcActiveFirmwareId
20 );
21 },
22 activeHostFirmware: (state) => {
23 return state.hostFirmware.find(
24 (firmware) => firmware.id === state.hostActiveFirmwareId
25 );
26 },
27 backupBmcFirmware: (state) => {
28 return state.bmcFirmware.find(
29 (firmware) => firmware.id !== state.bmcActiveFirmwareId
30 );
31 },
32 backupHostFirmware: (state) => {
33 return state.hostFirmware.find(
34 (firmware) => firmware.id !== state.hostActiveFirmwareId
35 );
36 },
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060037 },
38 mutations: {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080039 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 Montague602e98a2020-10-21 16:20:00 -050043 setApplyTime: (state, applyTime) => (state.applyTime = applyTime),
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080044 setTftpUploadAvailable: (state, tftpAvailable) =>
45 (state.tftpAvailable = tftpAvailable),
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060046 },
47 actions: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070048 async getFirmwareInformation({ dispatch }) {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080049 dispatch('getActiveHostFirmware');
50 dispatch('getActiveBmcFirmware');
51 return await dispatch('getFirmwareInventory');
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070052 },
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080053 getActiveBmcFirmware({ commit }) {
54 return api
Dixsie Wolmers46a87442020-02-26 15:26:30 -060055 .get('/redfish/v1/Managers/bmc')
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070056 .then(({ data: { Links } }) => {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080057 const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop();
58 commit('setActiveBmcFirmwareId', id);
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070059 })
Derick Montague602e98a2020-10-21 16:20:00 -050060 .catch((error) => console.log(error));
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070061 },
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080062 getActiveHostFirmware({ commit }) {
63 return api
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070064 .get('/redfish/v1/Systems/system/Bios')
65 .then(({ data: { Links } }) => {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080066 const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop();
67 commit('setActiveHostFirmwareId', id);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070068 })
Derick Montague602e98a2020-10-21 16:20:00 -050069 .catch((error) => console.log(error));
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070070 },
Yoshie Muranaka33d755f2021-02-18 15:24:14 -080071 async getFirmwareInventory({ commit }) {
72 const inventoryList = await api
73 .get('/redfish/v1/UpdateService/FirmwareInventory')
74 .then(({ data: { Members = [] } = {} }) =>
75 Members.map((item) => api.get(item['@odata.id']))
76 )
77 .catch((error) => console.log(error));
78 await api
79 .all(inventoryList)
80 .then((response) => {
81 const bmcFirmware = [];
82 const hostFirmware = [];
83 response.forEach(({ data }) => {
84 const firmwareType = data?.RelatedItem?.[0]?.['@odata.id']
85 .split('/')
86 .pop();
87 const item = {
88 version: data?.Version,
89 id: data?.Id,
90 location: data?.['@odata.id'],
91 status: data?.Status?.Health,
92 };
93 if (firmwareType === 'bmc') {
94 bmcFirmware.push(item);
95 } else if (firmwareType === 'Bios') {
96 hostFirmware.push(item);
97 }
98 });
99 commit('setBmcFirmware', bmcFirmware);
100 commit('setHostFirmware', hostFirmware);
101 })
102 .catch((error) => {
103 console.log(error);
104 });
105 },
106 getUpdateServiceSettings({ commit }) {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700107 api
108 .get('/redfish/v1/UpdateService')
109 .then(({ data }) => {
110 const applyTime =
111 data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800112 const allowableActions =
113 data?.Actions?.['#UpdateService.SimpleUpdate']?.[
114 'TransferProtocol@Redfish.AllowableValues'
115 ];
116
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700117 commit('setApplyTime', applyTime);
Derick Montaguef7cd5e02021-05-11 17:02:00 -0500118 if (allowableActions?.includes('TFTP')) {
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800119 commit('setTftpUploadAvailable', true);
120 }
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700121 })
Derick Montague602e98a2020-10-21 16:20:00 -0500122 .catch((error) => console.log(error));
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700123 },
124 setApplyTimeImmediate({ commit }) {
125 const data = {
126 HttpPushUriOptions: {
127 HttpPushUriApplyTime: {
Derick Montague602e98a2020-10-21 16:20:00 -0500128 ApplyTime: 'Immediate',
129 },
130 },
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700131 };
132 return api
133 .patch('/redfish/v1/UpdateService', data)
134 .then(() => commit('setApplyTime', 'Immediate'))
Derick Montague602e98a2020-10-21 16:20:00 -0500135 .catch((error) => console.log(error));
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700136 },
137 async uploadFirmware({ state, dispatch }, image) {
138 if (state.applyTime !== 'Immediate') {
139 // ApplyTime must be set to Immediate before making
140 // request to update firmware
141 await dispatch('setApplyTimeImmediate');
142 }
143 return await api
144 .post('/redfish/v1/UpdateService', image, {
Derick Montague602e98a2020-10-21 16:20:00 -0500145 headers: { 'Content-Type': 'application/octet-stream' },
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700146 })
Derick Montague602e98a2020-10-21 16:20:00 -0500147 .catch((error) => {
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600148 console.log(error);
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800149 throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware'));
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700150 });
151 },
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800152 async uploadFirmwareTFTP({ state, dispatch }, fileAddress) {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700153 const data = {
154 TransferProtocol: 'TFTP',
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800155 ImageURI: fileAddress,
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700156 };
157 if (state.applyTime !== 'Immediate') {
158 // ApplyTime must be set to Immediate before making
159 // request to update firmware
160 await dispatch('setApplyTimeImmediate');
161 }
162 return await api
Gunnar Mills60713b02020-08-18 10:21:05 -0500163 .post(
164 '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate',
165 data
166 )
Derick Montague602e98a2020-10-21 16:20:00 -0500167 .catch((error) => {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700168 console.log(error);
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800169 throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware'));
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700170 });
171 },
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800172 async switchBmcFirmwareAndReboot({ getters }) {
173 const backupLocation = getters.backupBmcFirmware.location;
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700174 const data = {
175 Links: {
176 ActiveSoftwareImage: {
Yoshie Muranaka1dedbdf2020-11-30 07:04:01 -0800177 '@odata.id': backupLocation,
Derick Montague602e98a2020-10-21 16:20:00 -0500178 },
179 },
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700180 };
181 return await api
182 .patch('/redfish/v1/Managers/bmc', data)
Derick Montague602e98a2020-10-21 16:20:00 -0500183 .catch((error) => {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700184 console.log(error);
Yoshie Muranaka33d755f2021-02-18 15:24:14 -0800185 throw new Error(i18n.t('pageFirmware.toast.errorSwitchImages'));
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600186 });
Derick Montague602e98a2020-10-21 16:20:00 -0500187 },
188 },
Dixsie Wolmersf65ee342020-01-22 19:47:56 -0600189};
190
191export default FirmwareStore;