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