blob: 91bcb9feb2354be4a130bea8b82bcfdf53a956ba [file] [log] [blame]
Yoshie Muranaka0b980db2020-10-06 09:24:14 -07001import api from '@/store/api';
2import i18n from '@/i18n';
3
4const FirmwareSingleImageStore = {
5 namespaced: true,
6 state: {
7 activeFirmware: {
8 version: '--',
9 id: null,
Derick Montague602e98a2020-10-21 16:20:00 -050010 location: null,
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070011 },
12 backupFirmware: {
13 version: '--',
14 id: null,
15 location: null,
Yoshie Muranaka6f712842021-02-04 11:23:03 -080016 status: null,
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070017 },
Derick Montague602e98a2020-10-21 16:20:00 -050018 applyTime: null,
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070019 },
20 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050021 systemFirmwareVersion: (state) => state.activeFirmware.version,
22 backupFirmwareVersion: (state) => state.backupFirmware.version,
23 backupFirmwareStatus: (state) => state.backupFirmware.status,
24 isRebootFromBackupAvailable: (state) =>
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070025 state.backupFirmware.id ? true : false,
Derick Montague602e98a2020-10-21 16:20:00 -050026 bmcFirmwareCurrentVersion: (state) => state.activeFirmware.version, //this getter is needed for the Overview page
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070027 },
28 mutations: {
29 setActiveFirmware: (state, { version, id, location }) => {
30 state.activeFirmware.version = version;
31 state.activeFirmware.id = id;
32 state.activeFirmware.location = location;
33 },
34 setBackupFirmware: (state, { version, id, location, status }) => {
35 state.backupFirmware.version = version;
36 state.backupFirmware.id = id;
37 state.backupFirmware.location = location;
38 state.backupFirmware.status = status;
39 },
Derick Montague602e98a2020-10-21 16:20:00 -050040 setApplyTime: (state, applyTime) => (state.applyTime = applyTime),
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070041 },
42 actions: {
Yoshie Muranaka98bb24e2020-10-06 10:00:19 -070043 async getFirmwareInformation({ commit }) {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070044 return await api
45 .get('/redfish/v1/Managers/bmc')
46 .then(({ data: { Links } }) => {
47 const currentLocation = Links.ActiveSoftwareImage['@odata.id'];
48 // Check SoftwareImages list for not ActiveSoftwareImage id
49 const backupLocation = Links.SoftwareImages.map(
Derick Montague602e98a2020-10-21 16:20:00 -050050 (item) => item['@odata.id']
51 ).find((location) => {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070052 const id = location.split('/').pop();
53 const currentId = currentLocation.split('/').pop();
54 return id !== currentId;
55 });
56 return { currentLocation, backupLocation };
57 })
58 .then(async ({ currentLocation, backupLocation }) => {
59 const currentData = await api.get(currentLocation);
60 let backupData = {};
61
62 if (backupLocation) {
63 backupData = await api.get(backupLocation);
64 }
65
66 commit('setActiveFirmware', {
67 version: currentData?.data?.Version,
68 id: currentData?.data?.Id,
Derick Montague602e98a2020-10-21 16:20:00 -050069 location: currentData?.data?.['@odata.id'],
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070070 });
71 commit('setBackupFirmware', {
72 version: backupData.data?.Version,
73 id: backupData.data?.Id,
74 location: backupData.data?.['@odata.id'],
Yoshie Muranaka6f712842021-02-04 11:23:03 -080075 status: backupData.data?.Status?.Health,
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070076 });
77 })
Derick Montague602e98a2020-10-21 16:20:00 -050078 .catch((error) => console.log(error));
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070079 },
80 getUpdateServiceApplyTime({ commit }) {
81 api
82 .get('/redfish/v1/UpdateService')
83 .then(({ data }) => {
84 const applyTime =
85 data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
86 commit('setApplyTime', applyTime);
87 })
Derick Montague602e98a2020-10-21 16:20:00 -050088 .catch((error) => console.log(error));
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070089 },
90 setApplyTimeImmediate({ commit }) {
91 const data = {
92 HttpPushUriOptions: {
93 HttpPushUriApplyTime: {
Derick Montague602e98a2020-10-21 16:20:00 -050094 ApplyTime: 'Immediate',
95 },
96 },
Yoshie Muranaka0b980db2020-10-06 09:24:14 -070097 };
98 return api
99 .patch('/redfish/v1/UpdateService', data)
100 .then(() => commit('setApplyTime', 'Immediate'))
Derick Montague602e98a2020-10-21 16:20:00 -0500101 .catch((error) => console.log(error));
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700102 },
103 async uploadFirmware({ state, dispatch }, image) {
104 if (state.applyTime !== 'Immediate') {
105 // ApplyTime must be set to Immediate before making
106 // request to update firmware
107 await dispatch('setApplyTimeImmediate');
108 }
109 return await api
110 .post('/redfish/v1/UpdateService', image, {
Derick Montague602e98a2020-10-21 16:20:00 -0500111 headers: { 'Content-Type': 'application/octet-stream' },
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700112 })
Derick Montague602e98a2020-10-21 16:20:00 -0500113 .catch((error) => {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700114 console.log(error);
115 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
116 });
117 },
Yoshie Muranaka6f712842021-02-04 11:23:03 -0800118 async uploadFirmwareTFTP({ state, dispatch }, fileAddress) {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700119 const data = {
120 TransferProtocol: 'TFTP',
Yoshie Muranaka6f712842021-02-04 11:23:03 -0800121 ImageURI: fileAddress,
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700122 };
123 if (state.applyTime !== 'Immediate') {
124 // ApplyTime must be set to Immediate before making
125 // request to update firmware
126 await dispatch('setApplyTimeImmediate');
127 }
128 return await api
129 .post(
130 '/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate',
131 data
132 )
Derick Montague602e98a2020-10-21 16:20:00 -0500133 .catch((error) => {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700134 console.log(error);
135 throw new Error(i18n.t('pageFirmware.toast.errorUploadAndReboot'));
136 });
137 },
138 async switchFirmwareAndReboot({ state }) {
139 const backupLoaction = state.backupFirmware.location;
140 const data = {
141 Links: {
142 ActiveSoftwareImage: {
Derick Montague602e98a2020-10-21 16:20:00 -0500143 '@odata.id': backupLoaction,
144 },
145 },
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700146 };
147 return await api
148 .patch('/redfish/v1/Managers/bmc', data)
Derick Montague602e98a2020-10-21 16:20:00 -0500149 .catch((error) => {
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700150 console.log(error);
Yoshie Muranaka6f712842021-02-04 11:23:03 -0800151 throw new Error(
152 i18n.t('pageFirmware.singleFileUpload.toast.errorSwitchImages')
153 );
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700154 });
Derick Montague602e98a2020-10-21 16:20:00 -0500155 },
156 },
Yoshie Muranaka0b980db2020-10-06 09:24:14 -0700157};
158
159export default FirmwareSingleImageStore;