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 | }, |
| 74 | async rebootBmc({ dispatch }) { |
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 | ) |
Dixsie Wolmers | 50cf2f7 | 2020-08-17 17:38:46 -0500 | [diff] [blame] | 81 | .then(() => dispatch('getLastBmcRebootTime')) |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 82 | .then(() => i18n.t('pageRebootBmc.toast.successRebootStart')) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 83 | .catch((error) => { |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 84 | console.log(error); |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 85 | throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart')); |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 86 | }); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 87 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 88 | async serverPowerOn({ dispatch, commit }) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 89 | const data = { ResetType: 'On' }; |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 90 | dispatch('serverPowerChange', data); |
| 91 | await checkForServerStatus.bind(this, 'on')(); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 92 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame] | 93 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 94 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 95 | async serverSoftReboot({ dispatch, commit }) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 96 | const data = { ResetType: 'GracefulRestart' }; |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 97 | dispatch('serverPowerChange', data); |
| 98 | await checkForServerStatus.bind(this, 'on')(); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 99 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame] | 100 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 101 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 102 | async serverHardReboot({ dispatch, commit }) { |
Gunnar Mills | 5494112 | 2020-07-27 13:22:05 -0500 | [diff] [blame] | 103 | const data = { ResetType: 'ForceRestart' }; |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 104 | dispatch('serverPowerChange', data); |
| 105 | await checkForServerStatus.bind(this, 'on')(); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 106 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame] | 107 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 108 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 109 | async serverSoftPowerOff({ dispatch, commit }) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 110 | const data = { ResetType: 'GracefulShutdown' }; |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 111 | dispatch('serverPowerChange', data); |
| 112 | await checkForServerStatus.bind(this, 'off')(); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 113 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame] | 114 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 115 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 116 | async serverHardPowerOff({ dispatch, commit }) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 117 | const data = { ResetType: 'ForceOff' }; |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 118 | dispatch('serverPowerChange', data); |
| 119 | await checkForServerStatus.bind(this, 'off')(); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 120 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame] | 121 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 122 | }, |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 123 | async serverPowerChange({ commit }, data) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 124 | commit('setOperationInProgress', true); |
| 125 | api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 126 | .post( |
| 127 | `${await this.dispatch('global/getSystemPath')}/Actions/ComputerSystem.Reset`, |
| 128 | data, |
| 129 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 130 | .catch((error) => { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 131 | console.log(error); |
| 132 | commit('setOperationInProgress', false); |
| 133 | }); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 134 | }, |
| 135 | }, |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | export default ControlStore; |