blob: 986ac3b8f7e474dce4d510444e09ca907a5f49b8 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * Controller for power-operations
3 *
Iftekharul Islamcd789502017-04-19 14:37:55 -05004 * @module app/serverControl
Iftekharul Islam99d199f2017-03-24 15:28:25 -05005 * @exports powerOperationsController
6 * @name powerOperationsController
Iftekharul Islam99d199f2017-03-24 15:28:25 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.serverControl').controller('powerOperationsController', [
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050013 '$scope', 'APIUtils', 'dataService', 'Constants', '$interval', '$q',
14 'toastService',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 function(
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050016 $scope, APIUtils, dataService, Constants, $interval, $q, toastService) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070017 $scope.dataService = dataService;
Gunnar Millsaa4734d2019-05-17 15:56:18 -050018 // Is a || of the other 4 "confirm" variables to ensure only
19 // one confirm is shown at a time.
Andrew Geisslerd27bb132018-05-24 11:07:27 -070020 $scope.confirm = false;
Gunnar Mills6af5d292019-05-17 14:39:23 -050021 $scope.confirmWarmReboot = false;
22 $scope.confirmColdReboot = false;
23 $scope.confirmOrderlyShutdown = false;
24 $scope.confirmImmediateShutdown = false;
Gunnar Mills6add8322018-09-05 15:16:12 -050025 $scope.loading = true;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060026
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050027 // When a power operation is in progress, set to true,
28 // when a power operation completes (success/fail) set to false.
29 // This property is used to show/hide the 'in progress' message
30 // in markup.
31 $scope.operationPending = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050032
Yoshie Muranakaafcfda72019-06-21 09:19:32 -050033 /**
34 * Checks the host status provided by the dataService using an
35 * interval timer
36 * @param {string} statusType : host status type to check for
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050037 * @param {number} timeout : timeout limit, defaults to 5 minutes
38 * @param {string} error : error message, defaults to 'Time out'
Yoshie Muranakaafcfda72019-06-21 09:19:32 -050039 * @returns {Promise} : returns a deferred promise that will be fulfilled
40 * if the status is met or be rejected if the timeout is reached
41 */
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050042 var checkHostStatus =
43 (statusType, timeout = 300000, error = 'Time out.') => {
44 const deferred = $q.defer();
45 const start = new Date();
46 const checkHostStatusInverval = $interval(() => {
47 let now = new Date();
48 let timePassed = now.getTime() - start.getTime();
49 if (timePassed > timeout) {
50 deferred.reject(error);
51 $interval.cancel(checkHostStatusInverval);
52 }
53 if (dataService.server_state === statusType) {
54 deferred.resolve();
55 $interval.cancel(checkHostStatusInverval);
56 }
57 }, Constants.POLL_INTERVALS.POWER_OP);
58 return deferred.promise;
59 };
Yoshie Muranakaafcfda72019-06-21 09:19:32 -050060
Gunnar Mills6add8322018-09-05 15:16:12 -050061 APIUtils.getLastPowerTime()
62 .then(
63 function(data) {
64 if (data.data == 0) {
Gunnar Mills6af5d292019-05-17 14:39:23 -050065 $scope.powerTime = 'not available';
Gunnar Mills6add8322018-09-05 15:16:12 -050066 } else {
Gunnar Mills6af5d292019-05-17 14:39:23 -050067 $scope.powerTime = data.data;
Gunnar Mills6add8322018-09-05 15:16:12 -050068 }
69 },
70 function(error) {
71 console.log(JSON.stringify(error));
72 })
73 .finally(function() {
74 $scope.loading = false;
beccabroek56744252018-08-03 11:25:11 -050075 });
Gunnar Mills6add8322018-09-05 15:16:12 -050076
Andrew Geisslerd27bb132018-05-24 11:07:27 -070077 $scope.toggleState = function() {
78 dataService.server_state =
79 (dataService.server_state == 'Running') ? 'Off' : 'Running';
80 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050081
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050082 /**
83 * Initiate Power on
84 */
85 $scope.powerOn = () => {
86 $scope.operationPending = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070087 dataService.setUnreachableState();
88 APIUtils.hostPowerOn()
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050089 .then(() => {
90 // Check for on state
91 return checkHostStatus(
92 Constants.HOST_STATE_TEXT.on, Constants.TIMEOUT.HOST_ON,
93 Constants.MESSAGES.POLL.HOST_ON_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070094 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050095 .catch((error) => {
96 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -060097 toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
Yoshie Muranaka5ff98782019-07-18 15:36:39 -050098 })
99 .finally(() => {
100 $scope.operationPending = false;
101 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700102 };
CamVan Nguyend80c2802018-04-17 19:25:16 -0500103
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500104 /**
105 * Initiate Orderly reboot
106 * Attempts to stop all software
107 */
108 $scope.warmReboot = () => {
109 $scope.operationPending = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700110 dataService.setUnreachableState();
111 APIUtils.hostReboot()
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500112 .then(() => {
113 // Check for off state
114 return checkHostStatus(
115 Constants.HOST_STATE_TEXT.off, Constants.TIMEOUT.HOST_OFF,
116 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700117 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500118 .then(() => {
119 // Check for on state
120 return checkHostStatus(
121 Constants.HOST_STATE_TEXT.on, Constants.TIMEOUT.HOST_ON,
122 Constants.MESSAGES.POLL.HOST_ON_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700123 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500124 .catch((error) => {
125 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -0600126 toastService.error(
127 Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500128 })
129 .finally(() => {
130 $scope.operationPending = false;
131 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700132 };
Gunnar Mills834eb012019-05-17 14:58:06 -0500133
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700134 $scope.warmRebootConfirm = function() {
135 if ($scope.confirm) {
Gunnar Millsaa4734d2019-05-17 15:56:18 -0500136 // If another "confirm" is already shown return
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700137 return;
138 }
139 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500140 $scope.confirmWarmReboot = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700141 };
142
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500143 /**
144 * Initiate Immediate reboot
145 * Does not attempt to stop all software
146 */
147 $scope.coldReboot = () => {
148 $scope.operationPending = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700149 dataService.setUnreachableState();
150 APIUtils.chassisPowerOff()
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500151 .then(() => {
152 // Check for off state
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500153 return checkHostStatus(
154 Constants.HOST_STATE_TEXT.off,
155 Constants.TIMEOUT.HOST_OFF_IMMEDIATE,
156 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700157 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500158 .then(() => {
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500159 return APIUtils.hostPowerOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700160 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500161 .then(() => {
162 // Check for on state
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500163 return checkHostStatus(
164 Constants.HOST_STATE_TEXT.on, Constants.TIMEOUT.HOST_ON,
165 Constants.MESSAGES.POLL.HOST_ON_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700166 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500167 .catch((error) => {
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500168 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -0600169 toastService.error(
170 Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500171 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500172 .finally(() => {
173 $scope.operationPending = false;
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500174 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700175 };
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500176
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700177 $scope.coldRebootConfirm = function() {
178 if ($scope.confirm) {
179 return;
180 }
181 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500182 $scope.confirmColdReboot = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700183 };
184
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500185 /**
186 * Initiate Orderly shutdown
187 * Attempts to stop all software
188 */
189 $scope.orderlyShutdown = () => {
190 $scope.operationPending = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700191 dataService.setUnreachableState();
192 APIUtils.hostPowerOff()
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500193 .then(() => {
194 // Check for off state
195 return checkHostStatus(
196 Constants.HOST_STATE_TEXT.off, Constants.TIMEOUT.HOST_OFF,
197 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700198 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500199 .catch((error) => {
200 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -0600201 toastService.error(
beccabroek92d13b62019-01-08 14:24:29 -0600202 Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500203 })
204 .finally(() => {
205 $scope.operationPending = false;
206 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700207 };
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500208
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700209 $scope.orderlyShutdownConfirm = function() {
210 if ($scope.confirm) {
211 return;
212 }
213 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500214 $scope.confirmOrderlyShutdown = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700215 };
216
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500217 /**
218 * Initiate Immediate shutdown
219 * Does not attempt to stop all software
220 */
221 $scope.immediateShutdown = () => {
222 $scope.operationPending = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700223 dataService.setUnreachableState();
224 APIUtils.chassisPowerOff()
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500225 .then(() => {
226 // Check for off state
227 return checkHostStatus(
228 Constants.HOST_STATE_TEXT.off,
229 Constants.TIMEOUT.HOST_OFF_IMMEDIATE,
230 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700231 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500232 .then(() => {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700233 dataService.setPowerOffState();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700234 })
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500235 .catch((error) => {
236 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -0600237 toastService.error(
beccabroek92d13b62019-01-08 14:24:29 -0600238 Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500239 })
240 .finally(() => {
241 $scope.operationPending = false;
242 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700243 };
Yoshie Muranaka5ff98782019-07-18 15:36:39 -0500244
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700245 $scope.immediateShutdownConfirm = function() {
246 if ($scope.confirm) {
247 return;
248 }
249 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500250 $scope.confirmImmediateShutdown = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700251 };
252 }
253 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500254})(angular);