blob: 9713d21cc1bb53032573d8a7f2df894f13889801 [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', [
13 '$scope', 'APIUtils', 'dataService', 'Constants', '$timeout', '$interval',
Gunnar Millsa1b5fb62019-05-17 16:36:10 -050014 '$q', 'toastService',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 function(
Gunnar Millsa1b5fb62019-05-17 16:36:10 -050016 $scope, APIUtils, dataService, Constants, $timeout, $interval, $q,
17 toastService) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.dataService = dataService;
Gunnar Millsaa4734d2019-05-17 15:56:18 -050019 // Is a || of the other 4 "confirm" variables to ensure only
20 // one confirm is shown at a time.
Andrew Geisslerd27bb132018-05-24 11:07:27 -070021 $scope.confirm = false;
Gunnar Mills6af5d292019-05-17 14:39:23 -050022 $scope.confirmWarmReboot = false;
23 $scope.confirmColdReboot = false;
24 $scope.confirmOrderlyShutdown = false;
25 $scope.confirmImmediateShutdown = false;
Gunnar Mills6add8322018-09-05 15:16:12 -050026 $scope.loading = true;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060027
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 var pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070029 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050030
Yoshie Muranakaafcfda72019-06-21 09:19:32 -050031 /**
32 * Checks the host status provided by the dataService using an
33 * interval timer
34 * @param {string} statusType : host status type to check for
35 * @param {number} timeout : timeout limit
36 * @param {string} error : error message
37 * @returns {Promise} : returns a deferred promise that will be fulfilled
38 * if the status is met or be rejected if the timeout is reached
39 */
40 var checkHostStatus = (statusType, timeout, error = 'Time out.') => {
41 const deferred = $q.defer();
42 const start = new Date();
43 const checkHostStatusInverval = $interval(() => {
44 let now = new Date();
45 let timePassed = now.getTime() - start.getTime();
46 if (timePassed > timeout) {
47 deferred.reject(error);
48 $interval.cancel(checkHostStatusInverval);
49 }
50 if (dataService.server_state === statusType) {
51 deferred.resolve();
52 $interval.cancel(checkHostStatusInverval);
53 }
54 }, Constants.POLL_INTERVALS.POWER_OP);
55 return deferred.promise;
56 };
57
Gunnar Mills6add8322018-09-05 15:16:12 -050058 APIUtils.getLastPowerTime()
59 .then(
60 function(data) {
61 if (data.data == 0) {
Gunnar Mills6af5d292019-05-17 14:39:23 -050062 $scope.powerTime = 'not available';
Gunnar Mills6add8322018-09-05 15:16:12 -050063 } else {
Gunnar Mills6af5d292019-05-17 14:39:23 -050064 $scope.powerTime = data.data;
Gunnar Mills6add8322018-09-05 15:16:12 -050065 }
66 },
67 function(error) {
68 console.log(JSON.stringify(error));
69 })
70 .finally(function() {
71 $scope.loading = false;
beccabroek56744252018-08-03 11:25:11 -050072 });
Gunnar Mills6add8322018-09-05 15:16:12 -050073
Andrew Geisslerd27bb132018-05-24 11:07:27 -070074 $scope.toggleState = function() {
75 dataService.server_state =
76 (dataService.server_state == 'Running') ? 'Off' : 'Running';
77 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050078
Andrew Geisslerd27bb132018-05-24 11:07:27 -070079 $scope.powerOn = function() {
80 $scope.loading = true;
81 dataService.setUnreachableState();
82 APIUtils.hostPowerOn()
83 .then(function(response) {
84 return response;
85 })
86 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -050087 return APIUtils.pollHostStatusTillOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -070088 })
89 .then(function(hostState) {
90 $scope.loading = false;
91 })
92 .catch(function(error) {
beccabroek27ce84d2019-02-05 15:43:17 -060093 toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070094 $scope.loading = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070095 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070096 };
CamVan Nguyend80c2802018-04-17 19:25:16 -050097
Andrew Geisslerd27bb132018-05-24 11:07:27 -070098 function pollChassisStatusTillOff() {
99 var deferred = $q.defer();
100 pollChassisStatusTimer = $interval(function() {
101 var now = new Date();
102 if ((now.getTime() - pollStartTime.getTime()) >=
103 Constants.TIMEOUT.CHASSIS_OFF) {
104 $interval.cancel(pollChassisStatusTimer);
105 pollChassisStatusTimer = undefined;
106 deferred.reject(
107 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
108 }
109 APIUtils.getChassisState()
110 .then(function(state) {
111 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
112 $interval.cancel(pollChassisStatusTimer);
113 pollChassisStatusTimer = undefined;
114 deferred.resolve(state);
115 }
116 })
117 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700118 $interval.cancel(pollChassisStatusTimer);
119 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700120 deferred.reject(error);
121 });
122 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700123
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700124 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700125 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700126 $scope.warmReboot = function() {
127 $scope.loading = true;
128 dataService.setUnreachableState();
129 APIUtils.hostReboot()
130 .then(function(response) {
131 return response;
132 })
133 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500134 return APIUtils.pollHostStatusTilReboot();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700135 })
136 .then(function(hostState) {
137 $scope.loading = false;
138 })
139 .catch(function(error) {
beccabroek27ce84d2019-02-05 15:43:17 -0600140 toastService.error(
141 Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 $scope.loading = false;
143 });
144 };
Gunnar Mills834eb012019-05-17 14:58:06 -0500145
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700146 $scope.warmRebootConfirm = function() {
147 if ($scope.confirm) {
Gunnar Millsaa4734d2019-05-17 15:56:18 -0500148 // If another "confirm" is already shown return
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700149 return;
150 }
151 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500152 $scope.confirmWarmReboot = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700153 };
154
155 $scope.coldReboot = function() {
156 $scope.loading = true;
157 dataService.setUnreachableState();
158 APIUtils.chassisPowerOff()
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500159 .then(function() {
160 return checkHostStatus(
161 Constants.HOST_STATE_TEXT.off,
162 Constants.TIMEOUT.HOST_OFF_IMMEDIATE,
163 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700164 })
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500165 .then(function() {
166 return APIUtils.hostPowerOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700167 })
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500168 .then(function() {
169 return checkHostStatus(
170 Constants.HOST_STATE_TEXT.on, Constants.TIMEOUT.HOST_ON,
171 Constants.MESSAGES.POLL.HOST_ON_TIMEOUT);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700172 })
173 .catch(function(error) {
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500174 console.log(error);
beccabroek27ce84d2019-02-05 15:43:17 -0600175 toastService.error(
176 Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500177 })
178 .finally(function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700179 $scope.loading = false;
Yoshie Muranakaafcfda72019-06-21 09:19:32 -0500180 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700181 };
182 $scope.coldRebootConfirm = function() {
183 if ($scope.confirm) {
184 return;
185 }
186 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500187 $scope.confirmColdReboot = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700188 };
189
190 $scope.orderlyShutdown = function() {
191 $scope.loading = true;
192 dataService.setUnreachableState();
193 APIUtils.hostPowerOff()
194 .then(function(response) {
195 return response;
196 })
197 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500198 return APIUtils.pollHostStatusTillOff();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700199 })
200 .then(function(hostState) {
201 pollStartTime = new Date();
202 return pollChassisStatusTillOff();
203 })
204 .then(function(chassisState) {
205 $scope.loading = false;
206 })
207 .catch(function(error) {
beccabroek27ce84d2019-02-05 15:43:17 -0600208 toastService.error(
beccabroek92d13b62019-01-08 14:24:29 -0600209 Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700210 $scope.loading = false;
211 });
212 };
213 $scope.orderlyShutdownConfirm = function() {
214 if ($scope.confirm) {
215 return;
216 }
217 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500218 $scope.confirmOrderlyShutdown = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700219 };
220
221 $scope.immediateShutdown = function() {
222 $scope.loading = true;
223 dataService.setUnreachableState();
224 APIUtils.chassisPowerOff()
225 .then(function(response) {
226 return response;
227 })
228 .then(function(lastStatus) {
229 pollStartTime = new Date();
230 return pollChassisStatusTillOff();
231 })
232 .then(function(chassisState) {
233 dataService.setPowerOffState();
234 $scope.loading = false;
235 })
236 .catch(function(error) {
beccabroek27ce84d2019-02-05 15:43:17 -0600237 toastService.error(
beccabroek92d13b62019-01-08 14:24:29 -0600238 Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700239 $scope.loading = false;
240 });
241 };
242 $scope.immediateShutdownConfirm = function() {
243 if ($scope.confirm) {
244 return;
245 }
246 $scope.confirm = true;
Gunnar Mills6af5d292019-05-17 14:39:23 -0500247 $scope.confirmImmediateShutdown = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700248 };
249 }
250 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500251})(angular);