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