blob: d9d3cfb863118f3d8955f74689c3fd1e5a584eb3 [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 Muranakad227f1c2020-10-06 13:46:58 -070042 async getSystemFirwareVersion({ commit }) {
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 Muranakad227f1c2020-10-06 13:46:58 -070045 .then(({ data: { Links } }) => {
46 const currentLocation = Links.ActiveSoftwareImage['@odata.id'];
47 // Check SoftwareImages list for not ActiveSoftwareImage id
48 const backupLocation = Links.SoftwareImages.map(
49 item => item['@odata.id']
50 ).find(location => {
51 const id = location.split('/').pop();
52 const currentId = currentLocation.split('/').pop();
53 return id !== currentId;
54 });
55 return { currentLocation, backupLocation };
Dixsie Wolmers46a87442020-02-26 15:26:30 -060056 })
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070057 .then(async ({ currentLocation, backupLocation }) => {
58 const currentData = await api.get(currentLocation);
59 let backupData = {};
60
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070061 if (backupLocation) {
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070062 backupData = await api.get(backupLocation);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070063 }
Yoshie Muranakad227f1c2020-10-06 13:46:58 -070064
65 commit('setActiveFirmware', {
66 version: currentData?.data?.Version,
67 id: currentData?.data?.Id,
68 location: currentData?.data?.['@odata.id']
69 });
70 commit('setBackupFirmware', {
71 version: backupData.data?.Version,
72 id: backupData.data?.Id,
73 location: backupData.data?.['@odata.id'],
74 status: backupData.data?.Status?.State
75 });
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -070076 })
77 .catch(error => console.log(error));
78 },
79 getUpdateServiceApplyTime({ commit }) {
80 api
81 .get('/redfish/v1/UpdateService')
82 .then(({ data }) => {
83 const applyTime =
84 data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
85 commit('setApplyTime', applyTime);
86 })
87 .catch(error => console.log(error));
88 },
89 setApplyTimeImmediate({ commit }) {
90 const data = {
91 HttpPushUriOptions: {
92 HttpPushUriApplyTime: {
93 ApplyTime: 'Immediate'
94 }
95 }
96 };
97 return api
98 .patch('/redfish/v1/UpdateService', data)
99 .then(() => commit('setApplyTime', 'Immediate'))
100 .catch(error => console.log(error));
101 },
102 async uploadFirmware({ state, dispatch }, image) {
103 if (state.applyTime !== 'Immediate') {
104 // ApplyTime must be set to Immediate before making
105 // request to update firmware
106 await dispatch('setApplyTimeImmediate');
107 }
108 return await api
109 .post('/redfish/v1/UpdateService', image, {
110 headers: { 'Content-Type': 'application/octet-stream' }
111 })
112 .then(() => dispatch('getSystemFirwareVersion'))
113 .then(() => i18n.t('pageFirmware.toast.successUploadMessage'))
Dixsie Wolmers46a87442020-02-26 15:26:30 -0600114 .catch(error => {
115 console.log(error);
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700116 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
117 });
118 },
119 async uploadFirmwareTFTP({ state, dispatch }, { address, filename }) {
120 const data = {
121 TransferProtocol: 'TFTP',
122 ImageURI: `${address}/${filename}`
123 };
124 if (state.applyTime !== 'Immediate') {
125 // ApplyTime must be set to Immediate before making
126 // request to update firmware
127 await dispatch('setApplyTimeImmediate');
128 }
129 return await api
Gunnar Mills60713b02020-08-18 10:21:05 -0500130 .post(
131 '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate',
132 data
133 )
Yoshie Muranaka92a0a4a2020-07-15 10:30:31 -0700134 .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;