blob: f27f5a6970a100e92405d9791e21754d9d65167a [file] [log] [blame]
Yoshie Muranakac11d3892020-02-19 08:07:40 -08001import api from '../../api';
2import i18n from '../../../i18n';
3
Yoshie Muranakafa1512b2020-02-25 15:54:07 -08004/**
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 */
12const 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 Muranakac11d3892020-02-19 08:07:40 -080031const ControlStore = {
32 namespaced: true,
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080033 state: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050034 isOperationInProgress: false,
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050035 lastPowerOperationTime: null,
36 lastBmcRebootTime: null
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080037 },
38 getters: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050039 isOperationInProgress: state => state.isOperationInProgress,
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050040 lastPowerOperationTime: state => state.lastPowerOperationTime,
41 lastBmcRebootTime: state => state.lastBmcRebootTime
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080042 },
43 mutations: {
44 setOperationInProgress: (state, inProgress) =>
Dixsie Wolmersbb316062020-08-04 19:17:33 -050045 (state.isOperationInProgress = inProgress),
46 setLastPowerOperationTime: (state, lastPowerOperationTime) =>
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050047 (state.lastPowerOperationTime = lastPowerOperationTime),
48 setLastBmcRebootTime: (state, lastBmcRebootTime) =>
49 (state.lastBmcRebootTime = lastBmcRebootTime)
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080050 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -080051 actions: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050052 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 Wolmers50cf2f72020-08-17 17:38:46 -050062 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 Muranakac11d3892020-02-19 08:07:40 -080073 const data = { ResetType: 'GracefulRestart' };
74 return await api
75 .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data)
Dixsie Wolmers50cf2f72020-08-17 17:38:46 -050076 .then(() => dispatch('getLastBmcRebootTime'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080077 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
Yoshie Muranakac11d3892020-02-19 08:07:40 -080078 .catch(error => {
79 console.log(error);
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080080 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
Yoshie Muranakac11d3892020-02-19 08:07:40 -080081 });
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080082 },
83 async hostPowerOn({ dispatch, commit }) {
84 const data = { ResetType: 'On' };
85 dispatch('hostPowerChange', data);
86 await checkForHostStatus.bind(this, 'on')();
87 commit('setOperationInProgress', false);
88 },
89 async hostSoftReboot({ dispatch, commit }) {
90 const data = { ResetType: 'GracefulRestart' };
91 dispatch('hostPowerChange', data);
92 await checkForHostStatus.bind(this, 'on')();
93 commit('setOperationInProgress', false);
94 },
95 async hostHardReboot({ dispatch, commit }) {
Gunnar Mills54941122020-07-27 13:22:05 -050096 const data = { ResetType: 'ForceRestart' };
97 dispatch('hostPowerChange', data);
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080098 await checkForHostStatus.bind(this, 'on')();
99 commit('setOperationInProgress', false);
100 },
101 async hostSoftPowerOff({ dispatch, commit }) {
102 const data = { ResetType: 'GracefulShutdown' };
103 dispatch('hostPowerChange', data);
104 await checkForHostStatus.bind(this, 'off')();
105 commit('setOperationInProgress', false);
106 },
107 async hostHardPowerOff({ dispatch, commit }) {
108 const data = { ResetType: 'ForceOff' };
109 dispatch('hostPowerChange', data);
110 await checkForHostStatus.bind(this, 'off')();
111 commit('setOperationInProgress', false);
112 },
Dixsie Wolmersbb316062020-08-04 19:17:33 -0500113 hostPowerChange({ commit, dispatch }, data) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800114 commit('setOperationInProgress', true);
115 api
116 .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
Dixsie Wolmersbb316062020-08-04 19:17:33 -0500117 .then(() => dispatch('getLastPowerOperationTime'))
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800118 .catch(error => {
119 console.log(error);
120 commit('setOperationInProgress', false);
121 });
Yoshie Muranakac11d3892020-02-19 08:07:40 -0800122 }
123 }
124};
125
126export default ControlStore;