Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 1 | import api from '../../api'; |
| 2 | import i18n from '../../../i18n'; |
| 3 | |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 4 | /** |
| 5 | * Watch for hostStatus changes in GlobalStore module |
| 6 | * to set isOperationInProgress state |
| 7 | * Stop watching status changes and resolve Promise when |
| 8 | * hostStatus value matches passed argument or after 5 minutes |
| 9 | * @param {string} hostStatus |
| 10 | * @returns {Promise} |
| 11 | */ |
| 12 | const checkForHostStatus = function(hostStatus) { |
| 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.hostStatus, |
| 20 | value => { |
| 21 | if (value === hostStatus) { |
| 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, |
| 36 | lastBmcRebootTime: null |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 37 | }, |
| 38 | getters: { |
Dixsie Wolmers | bb31606 | 2020-08-04 19:17:33 -0500 | [diff] [blame] | 39 | isOperationInProgress: state => state.isOperationInProgress, |
Dixsie Wolmers | 50cf2f7 | 2020-08-17 17:38:46 -0500 | [diff] [blame] | 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) => |
| 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') |
| 55 | .then(response => { |
| 56 | const lastReset = response.data.LastResetTime; |
| 57 | const lastPowerOperationTime = new Date(lastReset); |
| 58 | commit('setLastPowerOperationTime', lastPowerOperationTime); |
| 59 | }) |
| 60 | .catch(error => console.log(error)); |
| 61 | }, |
Dixsie Wolmers | 50cf2f7 | 2020-08-17 17:38:46 -0500 | [diff] [blame] | 62 | getLastBmcRebootTime({ commit }) { |
| 63 | return api |
| 64 | .get('/redfish/v1/Managers/bmc') |
| 65 | .then(response => { |
| 66 | const lastBmcReset = response.data.LastResetTime; |
| 67 | const lastBmcRebootTime = new Date(lastBmcReset); |
| 68 | commit('setLastBmcRebootTime', lastBmcRebootTime); |
| 69 | }) |
| 70 | .catch(error => console.log(error)); |
| 71 | }, |
| 72 | async rebootBmc({ dispatch }) { |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 73 | const data = { ResetType: 'GracefulRestart' }; |
| 74 | return await api |
| 75 | .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data) |
Dixsie Wolmers | 50cf2f7 | 2020-08-17 17:38:46 -0500 | [diff] [blame] | 76 | .then(() => dispatch('getLastBmcRebootTime')) |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 77 | .then(() => i18n.t('pageRebootBmc.toast.successRebootStart')) |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 78 | .catch(error => { |
| 79 | console.log(error); |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 80 | throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart')); |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 81 | }); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 82 | }, |
| 83 | async hostPowerOn({ dispatch, commit }) { |
| 84 | const data = { ResetType: 'On' }; |
| 85 | dispatch('hostPowerChange', data); |
| 86 | await checkForHostStatus.bind(this, 'on')(); |
| 87 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 88 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 89 | }, |
| 90 | async hostSoftReboot({ dispatch, commit }) { |
| 91 | const data = { ResetType: 'GracefulRestart' }; |
| 92 | dispatch('hostPowerChange', data); |
| 93 | await checkForHostStatus.bind(this, 'on')(); |
| 94 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 95 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 96 | }, |
| 97 | async hostHardReboot({ dispatch, commit }) { |
Gunnar Mills | 5494112 | 2020-07-27 13:22:05 -0500 | [diff] [blame] | 98 | const data = { ResetType: 'ForceRestart' }; |
| 99 | dispatch('hostPowerChange', data); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 100 | await checkForHostStatus.bind(this, 'on')(); |
| 101 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 102 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 103 | }, |
| 104 | async hostSoftPowerOff({ dispatch, commit }) { |
| 105 | const data = { ResetType: 'GracefulShutdown' }; |
| 106 | dispatch('hostPowerChange', data); |
| 107 | await checkForHostStatus.bind(this, 'off')(); |
| 108 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 109 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 110 | }, |
| 111 | async hostHardPowerOff({ dispatch, commit }) { |
| 112 | const data = { ResetType: 'ForceOff' }; |
| 113 | dispatch('hostPowerChange', data); |
| 114 | await checkForHostStatus.bind(this, 'off')(); |
| 115 | commit('setOperationInProgress', false); |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 116 | dispatch('getLastPowerOperationTime'); |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 117 | }, |
Dixsie Wolmers | ca0be48 | 2020-08-26 13:25:43 -0500 | [diff] [blame^] | 118 | hostPowerChange({ commit }, data) { |
Yoshie Muranaka | fa1512b | 2020-02-25 15:54:07 -0800 | [diff] [blame] | 119 | commit('setOperationInProgress', true); |
| 120 | api |
| 121 | .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data) |
| 122 | .catch(error => { |
| 123 | console.log(error); |
| 124 | commit('setOperationInProgress', false); |
| 125 | }); |
Yoshie Muranaka | c11d389 | 2020-02-19 08:07:40 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | export default ControlStore; |