blob: 5f532d0ed1746a11c3346f8f1056080c30e43edd [file] [log] [blame]
Mateusz Gapski75100462020-07-30 11:01:29 +02001import api from '@/store/api';
SurenNeware61859092020-10-01 09:37:32 +05302import i18n from '@/i18n';
Yoshie Muranakac11d3892020-02-19 08:07:40 -08003
Yoshie Muranakafa1512b2020-02-25 15:54:07 -08004/**
5 * Watch for hostStatus changes in GlobalStore module
6 * to set isOperationInProgress state
7 * Stop watching status changes and resolve Promise when
8 * hostStatus value matches passed argument or after 5 minutes
9 * @param {string} hostStatus
10 * @returns {Promise}
11 */
Derick Montague602e98a2020-10-21 16:20:00 -050012const checkForHostStatus = function (hostStatus) {
13 return new Promise((resolve) => {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080014 const timer = setTimeout(() => {
15 resolve();
16 unwatch();
17 }, 300000 /*5mins*/);
18 const unwatch = this.watch(
Derick Montague602e98a2020-10-21 16:20:00 -050019 (state) => state.global.hostStatus,
20 (value) => {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080021 if (value === hostStatus) {
22 resolve();
23 unwatch();
24 clearTimeout(timer);
25 }
26 }
27 );
28 });
29};
30
Yoshie Muranakac11d3892020-02-19 08:07:40 -080031const ControlStore = {
32 namespaced: true,
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080033 state: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050034 isOperationInProgress: false,
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050035 lastPowerOperationTime: null,
Derick Montague602e98a2020-10-21 16:20:00 -050036 lastBmcRebootTime: null,
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080037 },
38 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050039 isOperationInProgress: (state) => state.isOperationInProgress,
40 lastPowerOperationTime: (state) => state.lastPowerOperationTime,
41 lastBmcRebootTime: (state) => state.lastBmcRebootTime,
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080042 },
43 mutations: {
44 setOperationInProgress: (state, inProgress) =>
Dixsie Wolmersbb316062020-08-04 19:17:33 -050045 (state.isOperationInProgress = inProgress),
46 setLastPowerOperationTime: (state, lastPowerOperationTime) =>
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050047 (state.lastPowerOperationTime = lastPowerOperationTime),
48 setLastBmcRebootTime: (state, lastBmcRebootTime) =>
Derick Montague602e98a2020-10-21 16:20:00 -050049 (state.lastBmcRebootTime = lastBmcRebootTime),
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080050 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -080051 actions: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050052 async getLastPowerOperationTime({ commit }) {
53 return await api
54 .get('/redfish/v1/Systems/system')
Derick Montague602e98a2020-10-21 16:20:00 -050055 .then((response) => {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050056 const lastReset = response.data.LastResetTime;
Yoshie Muranakadada89c2021-02-01 07:48:53 -080057 if (lastReset) {
58 const lastPowerOperationTime = new Date(lastReset);
59 commit('setLastPowerOperationTime', lastPowerOperationTime);
60 }
Dixsie Wolmersbb316062020-08-04 19:17:33 -050061 })
Derick Montague602e98a2020-10-21 16:20:00 -050062 .catch((error) => console.log(error));
Dixsie Wolmersbb316062020-08-04 19:17:33 -050063 },
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050064 getLastBmcRebootTime({ commit }) {
65 return api
66 .get('/redfish/v1/Managers/bmc')
Derick Montague602e98a2020-10-21 16:20:00 -050067 .then((response) => {
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050068 const lastBmcReset = response.data.LastResetTime;
69 const lastBmcRebootTime = new Date(lastBmcReset);
70 commit('setLastBmcRebootTime', lastBmcRebootTime);
71 })
Derick Montague602e98a2020-10-21 16:20:00 -050072 .catch((error) => console.log(error));
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050073 },
74 async rebootBmc({ dispatch }) {
Yoshie Muranakac11d3892020-02-19 08:07:40 -080075 const data = { ResetType: 'GracefulRestart' };
76 return await api
77 .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data)
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050078 .then(() => dispatch('getLastBmcRebootTime'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080079 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
Derick Montague602e98a2020-10-21 16:20:00 -050080 .catch((error) => {
Yoshie Muranakac11d3892020-02-19 08:07:40 -080081 console.log(error);
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080082 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
Yoshie Muranakac11d3892020-02-19 08:07:40 -080083 });
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080084 },
85 async hostPowerOn({ dispatch, commit }) {
86 const data = { ResetType: 'On' };
87 dispatch('hostPowerChange', data);
88 await checkForHostStatus.bind(this, 'on')();
89 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -050090 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080091 },
92 async hostSoftReboot({ dispatch, commit }) {
93 const data = { ResetType: 'GracefulRestart' };
94 dispatch('hostPowerChange', data);
95 await checkForHostStatus.bind(this, 'on')();
96 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -050097 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080098 },
99 async hostHardReboot({ dispatch, commit }) {
Gunnar Mills54941122020-07-27 13:22:05 -0500100 const data = { ResetType: 'ForceRestart' };
101 dispatch('hostPowerChange', data);
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800102 await checkForHostStatus.bind(this, 'on')();
103 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500104 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800105 },
106 async hostSoftPowerOff({ dispatch, commit }) {
107 const data = { ResetType: 'GracefulShutdown' };
108 dispatch('hostPowerChange', data);
109 await checkForHostStatus.bind(this, 'off')();
110 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500111 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800112 },
113 async hostHardPowerOff({ dispatch, commit }) {
114 const data = { ResetType: 'ForceOff' };
115 dispatch('hostPowerChange', data);
116 await checkForHostStatus.bind(this, 'off')();
117 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500118 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800119 },
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500120 hostPowerChange({ commit }, data) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800121 commit('setOperationInProgress', true);
122 api
123 .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
Derick Montague602e98a2020-10-21 16:20:00 -0500124 .catch((error) => {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800125 console.log(error);
126 commit('setOperationInProgress', false);
127 });
Derick Montague602e98a2020-10-21 16:20:00 -0500128 },
129 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -0800130};
131
132export default ControlStore;