blob: c99e7eb8b58e2676eb5a64b3c33c41ea1fa42740 [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
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -07004/**
5 * Get backup firmware image from SoftwareImages
6 * The backup is whichever image is not the current
7 * or "ActiveSoftwareImage"
8 * @param {Array} list
9 * @param {String} currentLocation
10 */
11function getBackupFirmwareLocation(list, currentLocation) {
12 return list
13 .map(item => item['@odata.id'])
14 .find(location => {
15 const id = location.split('/').pop();
16 const currentId = currentLocation.split('/').pop();
17 return id !== currentId;
18 });
19}
20
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060021const FirmwareStore = {
22 namespaced: true,
23 state: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070024 bmcFirmware: {
25 currentVersion: null,
26 currentState: null,
27 currentLocation: null,
28 backupVersion: null,
29 backupState: null,
30 backupLocation: null
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070031 },
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070032 hostFirmware: {
33 currentVersion: null,
34 currentState: null,
35 currentLocation: null,
36 backupVersion: null,
37 backupState: null,
38 backupLocation: null
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070039 },
40 applyTime: null
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060041 },
42 getters: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070043 bmcFirmwareCurrentVersion: state => state.bmcFirmware.currentVersion,
44 bmcFirmwareCurrentState: state => state.bmcFirmware.currentState,
45 bmcFirmwareBackupVersion: state => state.bmcFirmware.backupVersion,
46 bmcFirmwareBackupState: state => state.bmcFirmware.backupState,
47 hostFirmwareCurrentVersion: state => state.hostFirmware.currentVersion,
48 hostFirmwareCurrentState: state => state.hostFirmware.currentState,
49 hostFirmwareBackupVersion: state => state.hostFirmware.backupVersion,
50 hostFirmwareBackupState: state => state.hostFirmware.backupState
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060051 },
52 mutations: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070053 setBmcFirmwareCurrent: (state, { version, location, status }) => {
54 state.bmcFirmware.currentVersion = version;
55 state.bmcFirmware.currentState = status;
56 state.bmcFirmware.currentLocation = location;
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070057 },
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070058 setBmcFirmwareBackup: (state, { version, location, status }) => {
59 state.bmcFirmware.backupVersion = version;
60 state.bmcFirmware.backupState = status;
61 state.bmcFirmware.backupLocation = location;
62 },
63 setHostFirmwareCurrent: (state, { version, location, status }) => {
64 state.hostFirmware.currentVersion = version;
65 state.hostFirmware.currentState = status;
66 state.hostFirmware.currentLocation = location;
67 },
68 setHostFirmwareBackup: (state, { version, location, status }) => {
69 state.hostFirmware.backupVersion = version;
70 state.hostFirmware.backupState = status;
71 state.hostFirmware.backupLocation = location;
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070072 },
73 setApplyTime: (state, applyTime) => (state.applyTime = applyTime)
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060074 },
75 actions: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070076 async getFirmwareInformation({ dispatch }) {
77 return await api.all([
78 dispatch('getBmcFirmware'),
79 dispatch('getHostFirmware')
80 ]);
81 },
82 async getBmcFirmware({ commit }) {
Yoshie Muranaka598bf7e2020-05-01 12:26:00 -070083 return await api
Dixsie Wolmers46a87442020-02-26 15:26:30 -060084 .get('/redfish/v1/Managers/bmc')
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070085 .then(({ data: { Links } }) => {
86 const currentLocation = Links.ActiveSoftwareImage['@odata.id'];
87 // Check SoftwareImages list for not ActiveSoftwareImage id
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070088 const backupLocation = getBackupFirmwareLocation(
89 Links.SoftwareImages,
90 currentLocation
91 );
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070092 return { currentLocation, backupLocation };
Dixsie Wolmers46a87442020-02-26 15:26:30 -060093 })
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070094 .then(async ({ currentLocation, backupLocation }) => {
95 const currentData = await api.get(currentLocation);
96 let backupData = {};
97
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070098 if (backupLocation) {
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070099 backupData = await api.get(backupLocation);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700100 }
Yoshie Muranakad227f1c2020-10-06 13:46:58 -0700101
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -0700102 commit('setBmcFirmwareCurrent', {
Yoshie Muranakad227f1c2020-10-06 13:46:58 -0700103 version: currentData?.data?.Version,
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -0700104 location: currentData?.data?.['@odata.id'],
105 status: currentData?.data?.Status?.State
Yoshie Muranakad227f1c2020-10-06 13:46:58 -0700106 });
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -0700107 commit('setBmcFirmwareBackup', {
Yoshie Muranakad227f1c2020-10-06 13:46:58 -0700108 version: backupData.data?.Version,
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -0700109 location: backupData.data?.['@odata.id'],
110 status: backupData.data?.Status?.State
111 });
112 })
113 .catch(error => console.log(error));
114 },
115 async getHostFirmware({ commit }) {
116 return await api
117 .get('/redfish/v1/Systems/system/Bios')
118 .then(({ data: { Links } }) => {
119 const currentLocation = Links.ActiveSoftwareImage['@odata.id'];
120 const backupLocation = getBackupFirmwareLocation(
121 Links.SoftwareImages,
122 currentLocation
123 );
124 return { currentLocation, backupLocation };
125 })
126 .then(async ({ currentLocation, backupLocation }) => {
127 const currentData = await api.get(currentLocation);
128 let backupData = {};
129
130 if (backupLocation) {
131 backupData = await api.get(backupLocation);
132 }
133
134 commit('setHostFirmwareCurrent', {
135 version: currentData?.data?.Version,
136 location: currentData?.data?.['@odata.id'],
137 status: currentData?.data?.Status?.State
138 });
139 commit('setHostFirmwareBackup', {
140 version: backupData.data?.Version,
Yoshie Muranakad227f1c2020-10-06 13:46:58 -0700141 location: backupData.data?.['@odata.id'],
142 status: backupData.data?.Status?.State
143 });
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700144 })
145 .catch(error => console.log(error));
146 },
147 getUpdateServiceApplyTime({ commit }) {
148 api
149 .get('/redfish/v1/UpdateService')
150 .then(({ data }) => {
151 const applyTime =
152 data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
153 commit('setApplyTime', applyTime);
154 })
155 .catch(error => console.log(error));
156 },
157 setApplyTimeImmediate({ commit }) {
158 const data = {
159 HttpPushUriOptions: {
160 HttpPushUriApplyTime: {
161 ApplyTime: 'Immediate'
162 }
163 }
164 };
165 return api
166 .patch('/redfish/v1/UpdateService', data)
167 .then(() => commit('setApplyTime', 'Immediate'))
168 .catch(error => console.log(error));
169 },
170 async uploadFirmware({ state, dispatch }, image) {
171 if (state.applyTime !== 'Immediate') {
172 // ApplyTime must be set to Immediate before making
173 // request to update firmware
174 await dispatch('setApplyTimeImmediate');
175 }
176 return await api
177 .post('/redfish/v1/UpdateService', image, {
178 headers: { 'Content-Type': 'application/octet-stream' }
179 })
180 .then(() => dispatch('getSystemFirwareVersion'))
181 .then(() => i18n.t('pageFirmware.toast.successUploadMessage'))
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600182 .catch(error => {
183 console.log(error);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700184 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
185 });
186 },
187 async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) {
188 const data = {
189 TransferProtocol: 'TFTP',
190 ImageURI: `${address}/${filename}`
191 };
192 if (state.applyTime !== 'Immediate') {
193 // ApplyTime must be set to Immediate before making
194 // request to update firmware
195 await dispatch('setApplyTimeImmediate');
196 }
197 return await api
Gunnar Mills60713b02020-08-18 10:21:05 -0500198 .post(
199 '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate',
200 data
201 )
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700202 .then(() => dispatch('getSystemFirwareVersion'))
203 .then(() => i18n.t('pageFirmware.toast.successUploadMessage'))
204 .catch(error => {
205 console.log(error);
206 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
207 });
208 },
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -0700209 async swtichBmcFirmware({ state }) {
210 const backupLoaction = state.bmcFirmware.backupLoaction;
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700211 const data = {
212 Links: {
213 ActiveSoftwareImage: {
214 '@odata.id': backupLoaction
215 }
216 }
217 };
218 return await api
219 .patch('/redfish/v1/Managers/bmc', data)
220 .then(() => i18n.t('pageFirmware.toast.successRebootFromBackup'))
221 .catch(error => {
222 console.log(error);
223 throw new Error(i18n.t('pageFirmware.toast.errorRebootFromBackup'));
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600224 });
Dixsie Wolmersf65ee342020-01-22 19:47:56 -0600225 }
226 }
227};
228
229export default FirmwareStore;