1import api from '@/store/api'; 2import i18n from '@/i18n'; 3 4/** 5 * Watch for serverStatus changes in GlobalStore module 6 * to set isOperationInProgress state 7 * Stop watching status changes and resolve Promise when 8 * serverStatus value matches passed argument or after 5 minutes 9 * @param {string} serverStatus 10 * @returns {Promise} 11 */ 12const checkForServerStatus = function (serverStatus) { 13 return new Promise((resolve) => { 14 const timer = setTimeout(() => { 15 resolve(); 16 unwatch(); 17 }, 300000 /*5mins*/); 18 const unwatch = this.watch( 19 (state) => state.global.serverStatus, 20 (value) => { 21 if (value === serverStatus) { 22 resolve(); 23 unwatch(); 24 clearTimeout(timer); 25 } 26 }, 27 ); 28 }); 29}; 30 31const ControlStore = { 32 namespaced: true, 33 state: { 34 isOperationInProgress: false, 35 lastPowerOperationTime: null, 36 lastBmcRebootTime: null, 37 }, 38 getters: { 39 isOperationInProgress: (state) => state.isOperationInProgress, 40 lastPowerOperationTime: (state) => state.lastPowerOperationTime, 41 lastBmcRebootTime: (state) => state.lastBmcRebootTime, 42 }, 43 mutations: { 44 setOperationInProgress: (state, inProgress) => 45 (state.isOperationInProgress = inProgress), 46 setLastPowerOperationTime: (state, lastPowerOperationTime) => 47 (state.lastPowerOperationTime = lastPowerOperationTime), 48 setLastBmcRebootTime: (state, lastBmcRebootTime) => 49 (state.lastBmcRebootTime = lastBmcRebootTime), 50 }, 51 actions: { 52 async getLastPowerOperationTime({ commit }) { 53 return await api 54 .get(`${await this.dispatch('global/getSystemPath')}`) 55 .then((response) => { 56 const lastReset = response.data.LastResetTime; 57 if (lastReset) { 58 const lastPowerOperationTime = new Date(lastReset); 59 commit('setLastPowerOperationTime', lastPowerOperationTime); 60 } 61 }) 62 .catch((error) => console.log(error)); 63 }, 64 async getLastBmcRebootTime({ commit }) { 65 return api 66 .get(`${await this.dispatch('global/getBmcPath')}`) 67 .then((response) => { 68 const lastBmcReset = response.data.LastResetTime; 69 const lastBmcRebootTime = new Date(lastBmcReset); 70 commit('setLastBmcRebootTime', lastBmcRebootTime); 71 }) 72 .catch((error) => console.log(error)); 73 }, 74 async rebootBmc({ dispatch }) { 75 const data = { ResetType: 'GracefulRestart' }; 76 return await api 77 .post( 78 `${await this.dispatch('global/getBmcPath')}/Actions/Manager.Reset`, 79 data, 80 ) 81 .then(() => dispatch('getLastBmcRebootTime')) 82 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart')) 83 .catch((error) => { 84 console.log(error); 85 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart')); 86 }); 87 }, 88 async serverPowerOn({ dispatch, commit }) { 89 const data = { ResetType: 'On' }; 90 dispatch('serverPowerChange', data); 91 await checkForServerStatus.bind(this, 'on')(); 92 commit('setOperationInProgress', false); 93 dispatch('getLastPowerOperationTime'); 94 }, 95 async serverSoftReboot({ dispatch, commit }) { 96 const data = { ResetType: 'GracefulRestart' }; 97 dispatch('serverPowerChange', data); 98 await checkForServerStatus.bind(this, 'on')(); 99 commit('setOperationInProgress', false); 100 dispatch('getLastPowerOperationTime'); 101 }, 102 async serverHardReboot({ dispatch, commit }) { 103 const data = { ResetType: 'ForceRestart' }; 104 dispatch('serverPowerChange', data); 105 await checkForServerStatus.bind(this, 'on')(); 106 commit('setOperationInProgress', false); 107 dispatch('getLastPowerOperationTime'); 108 }, 109 async serverSoftPowerOff({ dispatch, commit }) { 110 const data = { ResetType: 'GracefulShutdown' }; 111 dispatch('serverPowerChange', data); 112 await checkForServerStatus.bind(this, 'off')(); 113 commit('setOperationInProgress', false); 114 dispatch('getLastPowerOperationTime'); 115 }, 116 async serverHardPowerOff({ dispatch, commit }) { 117 const data = { ResetType: 'ForceOff' }; 118 dispatch('serverPowerChange', data); 119 await checkForServerStatus.bind(this, 'off')(); 120 commit('setOperationInProgress', false); 121 dispatch('getLastPowerOperationTime'); 122 }, 123 async serverPowerChange({ commit }, data) { 124 commit('setOperationInProgress', true); 125 api 126 .post( 127 `${await this.dispatch('global/getSystemPath')}/Actions/ComputerSystem.Reset`, 128 data, 129 ) 130 .catch((error) => { 131 console.log(error); 132 commit('setOperationInProgress', false); 133 }); 134 }, 135 }, 136}; 137 138export default ControlStore; 139