blob: 6908769dd8b47871863bdd7538a574c17ec3deb3 [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,
35 lastPowerOperationTime: null
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080036 },
37 getters: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050038 isOperationInProgress: state => state.isOperationInProgress,
39 lastPowerOperationTime: state => state.lastPowerOperationTime
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080040 },
41 mutations: {
42 setOperationInProgress: (state, inProgress) =>
Dixsie Wolmersbb316062020-08-04 19:17:33 -050043 (state.isOperationInProgress = inProgress),
44 setLastPowerOperationTime: (state, lastPowerOperationTime) =>
45 (state.lastPowerOperationTime = lastPowerOperationTime)
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080046 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -080047 actions: {
Dixsie Wolmersbb316062020-08-04 19:17:33 -050048 async getLastPowerOperationTime({ commit }) {
49 return await api
50 .get('/redfish/v1/Systems/system')
51 .then(response => {
52 const lastReset = response.data.LastResetTime;
53 const lastPowerOperationTime = new Date(lastReset);
54 commit('setLastPowerOperationTime', lastPowerOperationTime);
55 })
56 .catch(error => console.log(error));
57 },
Yoshie Muranakac11d3892020-02-19 08:07:40 -080058 async rebootBmc() {
59 const data = { ResetType: 'GracefulRestart' };
60 return await api
61 .post('/redfish/v1/Managers/bmc/Actions/Manager.Reset', data)
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080062 .then(() => i18n.t('pageRebootBmc.toast.successRebootStart'))
Yoshie Muranakac11d3892020-02-19 08:07:40 -080063 .catch(error => {
64 console.log(error);
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -080065 throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
Yoshie Muranakac11d3892020-02-19 08:07:40 -080066 });
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080067 },
68 async hostPowerOn({ dispatch, commit }) {
69 const data = { ResetType: 'On' };
70 dispatch('hostPowerChange', data);
71 await checkForHostStatus.bind(this, 'on')();
72 commit('setOperationInProgress', false);
73 },
74 async hostSoftReboot({ dispatch, commit }) {
75 const data = { ResetType: 'GracefulRestart' };
76 dispatch('hostPowerChange', data);
77 await checkForHostStatus.bind(this, 'on')();
78 commit('setOperationInProgress', false);
79 },
80 async hostHardReboot({ dispatch, commit }) {
Gunnar Mills54941122020-07-27 13:22:05 -050081 const data = { ResetType: 'ForceRestart' };
82 dispatch('hostPowerChange', data);
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080083 await checkForHostStatus.bind(this, 'on')();
84 commit('setOperationInProgress', false);
85 },
86 async hostSoftPowerOff({ dispatch, commit }) {
87 const data = { ResetType: 'GracefulShutdown' };
88 dispatch('hostPowerChange', data);
89 await checkForHostStatus.bind(this, 'off')();
90 commit('setOperationInProgress', false);
91 },
92 async hostHardPowerOff({ dispatch, commit }) {
93 const data = { ResetType: 'ForceOff' };
94 dispatch('hostPowerChange', data);
95 await checkForHostStatus.bind(this, 'off')();
96 commit('setOperationInProgress', false);
97 },
Dixsie Wolmersbb316062020-08-04 19:17:33 -050098 hostPowerChange({ commit, dispatch }, data) {
Yoshie Muranakafa1512b2020-02-25 15:54:07 -080099 commit('setOperationInProgress', true);
100 api
101 .post('/redfish/v1/Systems/system/Actions/ComputerSystem.Reset', data)
Dixsie Wolmersbb316062020-08-04 19:17:33 -0500102 .then(() => dispatch('getLastPowerOperationTime'))
Yoshie Muranakafa1512b2020-02-25 15:54:07 -0800103 .catch(error => {
104 console.log(error);
105 commit('setOperationInProgress', false);
106 });
Yoshie Muranakac11d3892020-02-19 08:07:40 -0800107 }
108 }
109};
110
111export default ControlStore;