blob: efcdf6279e3805933a58ef7a2ac0cafae0775243 [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/**
Derick Montague71114fe2021-05-06 18:17:34 -05005 * Watch for serverStatus changes in GlobalStore module
Yoshie Muranakafa1512b2020-02-25 15:54:07 -08006 * to set isOperationInProgress state
7 * Stop watching status changes and resolve Promise when
Derick Montague71114fe2021-05-06 18:17:34 -05008 * serverStatus value matches passed argument or after 5 minutes
9 * @param {string} serverStatus
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080010 * @returns {Promise}
11 */
Derick Montague71114fe2021-05-06 18:17:34 -050012const checkForServerStatus = function (serverStatus) {
Derick Montague602e98a2020-10-21 16:20:00 -050013 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 Montague71114fe2021-05-06 18:17:34 -050019 (state) => state.global.serverStatus,
Derick Montague602e98a2020-10-21 16:20:00 -050020 (value) => {
Derick Montague71114fe2021-05-06 18:17:34 -050021 if (value === serverStatus) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080022 resolve();
23 unwatch();
24 clearTimeout(timer);
25 }
Ed Tanous81323992024-02-27 11:26:24 -080026 },
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080027 );
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
Sean Zhang8841b7d2024-06-15 08:42:41 +030054 .get(`${await this.dispatch('global/getSystemPath')}`)
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 },
Sean Zhang8841b7d2024-06-15 08:42:41 +030064 async getLastBmcRebootTime({ commit }) {
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050065 return api
Sean Zhang8841b7d2024-06-15 08:42:41 +030066 .get(`${await this.dispatch('global/getBmcPath')}`)
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
Sean Zhang8841b7d2024-06-15 08:42:41 +030077 .post(
78 `${await this.dispatch('global/getBmcPath')}/Actions/Manager.Reset`,
79 data,
80 )
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050081 .then(() => dispatch('getLastBmcRebootTime'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080082 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
Derick Montague602e98a2020-10-21 16:20:00 -050083 .catch((error) => {
Yoshie Muranakac11d3892020-02-19 08:07:40 -080084 console.log(error);
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080085 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
Yoshie Muranakac11d3892020-02-19 08:07:40 -080086 });
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080087 },
Derick Montague71114fe2021-05-06 18:17:34 -050088 async serverPowerOn({ dispatch, commit }) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080089 const data = { ResetType: 'On' };
Derick Montague71114fe2021-05-06 18:17:34 -050090 dispatch('serverPowerChange', data);
91 await checkForServerStatus.bind(this, 'on')();
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080092 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -050093 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080094 },
Derick Montague71114fe2021-05-06 18:17:34 -050095 async serverSoftReboot({ dispatch, commit }) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080096 const data = { ResetType: 'GracefulRestart' };
Derick Montague71114fe2021-05-06 18:17:34 -050097 dispatch('serverPowerChange', data);
98 await checkForServerStatus.bind(this, 'on')();
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080099 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500100 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800101 },
Derick Montague71114fe2021-05-06 18:17:34 -0500102 async serverHardReboot({ dispatch, commit }) {
Gunnar Mills54941122020-07-27 13:22:05 -0500103 const data = { ResetType: 'ForceRestart' };
Derick Montague71114fe2021-05-06 18:17:34 -0500104 dispatch('serverPowerChange', data);
105 await checkForServerStatus.bind(this, 'on')();
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800106 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500107 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800108 },
Derick Montague71114fe2021-05-06 18:17:34 -0500109 async serverSoftPowerOff({ dispatch, commit }) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800110 const data = { ResetType: 'GracefulShutdown' };
Derick Montague71114fe2021-05-06 18:17:34 -0500111 dispatch('serverPowerChange', data);
112 await checkForServerStatus.bind(this, 'off')();
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800113 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500114 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800115 },
Derick Montague71114fe2021-05-06 18:17:34 -0500116 async serverHardPowerOff({ dispatch, commit }) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800117 const data = { ResetType: 'ForceOff' };
Derick Montague71114fe2021-05-06 18:17:34 -0500118 dispatch('serverPowerChange', data);
119 await checkForServerStatus.bind(this, 'off')();
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800120 commit('setOperationInProgress', false);
Dixsie Wolmersca0be482020-08-26 13:25:43 -0500121 dispatch('getLastPowerOperationTime');
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800122 },
Sean Zhang8841b7d2024-06-15 08:42:41 +0300123 async serverPowerChange({ commit }, data) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800124 commit('setOperationInProgress', true);
125 api
Sean Zhang8841b7d2024-06-15 08:42:41 +0300126 .post(
127 `${await this.dispatch('global/getSystemPath')}/Actions/ComputerSystem.Reset`,
128 data,
129 )
Derick Montague602e98a2020-10-21 16:20:00 -0500130 .catch((error) => {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800131 console.log(error);
132 commit('setOperationInProgress', false);
133 });
Derick Montague602e98a2020-10-21 16:20:00 -0500134 },
135 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -0800136};
137
138export default ControlStore;