blob: ead51994487f8a6e32fa57f74f581a2b921279c5 [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 Muranaka92a0a4a2020-07-15 10:30:31 -07007 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 Wolmersf65ee342020-01-22 19:47:56 -060019 },
20 getters: {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070021 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 Wolmersf65ee342020-01-22 19:47:56 -060026 },
27 mutations: {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070028 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 Wolmersf65ee342020-01-22 19:47:56 -060040 },
41 actions: {
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070042 async getSystemFirwareVersion({ commit, state }) {
Yoshie Muranaka598bf7e2020-05-01 12:26:00 -070043 return await api
Dixsie Wolmers46a87442020-02-26 15:26:30 -060044 .get('/redfish/v1/Managers/bmc')
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070045 .then(({ data: { Links: { ActiveSoftwareImage } } }) => {
46 const location = ActiveSoftwareImage['@odata.id'];
47 return api.get(location);
Dixsie Wolmers46a87442020-02-26 15:26:30 -060048 })
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070049 .then(({ data }) => {
50 const version = data.Version;
51 const id = data.Id;
52 const location = data['@odata.id'];
53 commit('setActiveFirmware', { version, id, location });
54 // TODO: temporary workaround to get 'Backup' Firmware
55 // information
56 return api.get('/redfish/v1/UpdateService/FirmwareInventory');
57 })
58 .then(({ data: { Members } }) => {
59 // TODO: temporary workaround to get 'Backup' Firmware
60 // information
61 // Check FirmwareInventory list for not ActiveSoftwareImage id
62 const backupLocation = Members.map(item => item['@odata.id']).find(
63 location => {
64 const id = location.split('/').pop();
65 return id !== state.activeFirmware.id;
66 }
67 );
68 if (backupLocation) {
69 return api.get(backupLocation);
70 }
71 })
72 .then(({ data } = {}) => {
73 if (!data) return;
74 const version = data.Version;
75 const id = data.Id;
76 const location = data['@odata.id'];
77 const status = data.Status ? data.Status.State : '--';
78 commit('setBackupFirmware', { version, id, location, status });
79 })
80 .catch(error => console.log(error));
81 },
82 getUpdateServiceApplyTime({ commit }) {
83 api
84 .get('/redfish/v1/UpdateService')
85 .then(({ data }) => {
86 const applyTime =
87 data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
88 commit('setApplyTime', applyTime);
89 })
90 .catch(error => console.log(error));
91 },
92 setApplyTimeImmediate({ commit }) {
93 const data = {
94 HttpPushUriOptions: {
95 HttpPushUriApplyTime: {
96 ApplyTime: 'Immediate'
97 }
98 }
99 };
100 return api
101 .patch('/redfish/v1/UpdateService', data)
102 .then(() => commit('setApplyTime', 'Immediate'))
103 .catch(error => console.log(error));
104 },
105 async uploadFirmware({ state, dispatch }, image) {
106 if (state.applyTime !== 'Immediate') {
107 // ApplyTime must be set to Immediate before making
108 // request to update firmware
109 await dispatch('setApplyTimeImmediate');
110 }
111 return await api
112 .post('/redfish/v1/UpdateService', image, {
113 headers: { 'Content-Type': 'application/octet-stream' }
114 })
115 .then(() => dispatch('getSystemFirwareVersion'))
116 .then(() => i18n.t('pageFirmware.toast.successUploadMessage'))
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600117 .catch(error => {
118 console.log(error);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700119 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
120 });
121 },
122 async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) {
123 const data = {
124 TransferProtocol: 'TFTP',
125 ImageURI: `${address}/${filename}`
126 };
127 if (state.applyTime !== 'Immediate') {
128 // ApplyTime must be set to Immediate before making
129 // request to update firmware
130 await dispatch('setApplyTimeImmediate');
131 }
132 return await api
133 .post('/redfish/v1/UpdateService', data)
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 Wolmers46a87442020-02-26 15:26:30 -0600156 });
Dixsie Wolmersf65ee342020-01-22 19:47:56 -0600157 }
158 }
159};
160
161export default FirmwareStore;