blob: 4ae732fe07e7850d90d9b15b8771f9e7603af324 [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',
14 '$interpolate', '$q',
15 function(
16 $scope, APIUtils, dataService, Constants, $timeout, $interval,
17 $interpolate, $q) {
18 $scope.dataService = dataService;
19 $scope.confirm = false;
20 $scope.power_confirm = false;
21 $scope.warmboot_confirm = false;
22 $scope.coldboot_confirm = false;
23 $scope.orderly_confirm = false;
24 $scope.immediately_confirm = false;
25 $scope.loading = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060026
Andrew Geisslerd27bb132018-05-24 11:07:27 -070027 var pollChassisStatusTimer = undefined;
28 var pollHostStatusTimer = undefined;
29 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050030
Andrew Geisslerd27bb132018-05-24 11:07:27 -070031 //@TODO: call api and get proper state
32 $scope.toggleState = function() {
33 dataService.server_state =
34 (dataService.server_state == 'Running') ? 'Off' : 'Running';
35 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050036
Andrew Geisslerd27bb132018-05-24 11:07:27 -070037 $scope.powerOn = function() {
38 $scope.loading = true;
39 dataService.setUnreachableState();
40 APIUtils.hostPowerOn()
41 .then(function(response) {
42 return response;
43 })
44 .then(function(lastStatus) {
45 pollStartTime = new Date();
46 return pollHostStatusTillOn();
47 })
48 .then(function(hostState) {
49 $scope.loading = false;
50 })
51 .catch(function(error) {
52 dataService.activateErrorModal({
53 title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
54 description: error.statusText ?
55 $interpolate(
56 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
57 {status: error.status, description: error.statusText}) :
58 error
59 });
60 $scope.loading = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070061 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070062 };
63 $scope.powerOnConfirm = function() {
64 if ($scope.confirm) {
65 return;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070066 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070067 $scope.confirm = true;
68 $scope.power_confirm = true;
69 };
CamVan Nguyend80c2802018-04-17 19:25:16 -050070
Andrew Geisslerd27bb132018-05-24 11:07:27 -070071 function setHostState(state) {
72 if (state == Constants.HOST_STATE_TEXT.off_code) {
73 dataService.setPowerOffState();
74 } else if (state == Constants.HOST_STATE_TEXT.on_code) {
75 dataService.setPowerOnState();
76 } else {
77 dataService.setErrorState();
78 }
79 }
80
81 function pollChassisStatusTillOff() {
82 var deferred = $q.defer();
83 pollChassisStatusTimer = $interval(function() {
84 var now = new Date();
85 if ((now.getTime() - pollStartTime.getTime()) >=
86 Constants.TIMEOUT.CHASSIS_OFF) {
87 $interval.cancel(pollChassisStatusTimer);
88 pollChassisStatusTimer = undefined;
89 deferred.reject(
90 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
91 }
92 APIUtils.getChassisState()
93 .then(function(state) {
94 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
95 $interval.cancel(pollChassisStatusTimer);
96 pollChassisStatusTimer = undefined;
97 deferred.resolve(state);
98 }
99 })
100 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700101 $interval.cancel(pollChassisStatusTimer);
102 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700103 deferred.reject(error);
104 });
105 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700106
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700107 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700108 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700109
110 function pollHostStatusTillOn() {
111 var deferred = $q.defer();
112 pollHostStatusTimer = $interval(function() {
113 var now = new Date();
114 if ((now.getTime() - pollStartTime.getTime()) >=
115 Constants.TIMEOUT.HOST_ON) {
116 $interval.cancel(pollHostStatusTimer);
117 pollHostStatusTimer = undefined;
118 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
119 }
120 APIUtils.getHostState()
121 .then(function(state) {
122 setHostState(state);
123 if (state === Constants.HOST_STATE_TEXT.on_code) {
124 $interval.cancel(pollHostStatusTimer);
125 pollHostStatusTimer = undefined;
126 deferred.resolve(state);
127 } else if (state === Constants.HOST_STATE_TEXT.error_code) {
128 $interval.cancel(pollHostStatusTimer);
129 pollHostStatusTimer = undefined;
130 deferred.reject(
131 new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
132 }
133 })
134 .catch(function(error) {
135 $interval.cancel(pollHostStatusTimer);
136 pollHostStatusTimer = undefined;
137 deferred.reject(error);
138 });
139 }, Constants.POLL_INTERVALS.POWER_OP);
140
141 return deferred.promise;
142 }
143
144 function pollHostStatusTillOff() {
145 var deferred = $q.defer();
146 pollHostStatusTimer = $interval(function() {
147 var now = new Date();
148 if ((now.getTime() - pollStartTime.getTime()) >=
149 Constants.TIMEOUT.HOST_OFF) {
150 $interval.cancel(pollHostStatusTimer);
151 pollHostStatusTimer = undefined;
152 deferred.reject(
153 new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
154 }
155 APIUtils.getHostState()
156 .then(function(state) {
157 setHostState(state);
158 if (state === Constants.HOST_STATE_TEXT.off_code) {
159 $interval.cancel(pollHostStatusTimer);
160 pollHostStatusTimer = undefined;
161 deferred.resolve(state);
162 }
163 })
164 .catch(function(error) {
165 $interval.cancel(pollHostStatusTimer);
166 pollHostStatusTimer = undefined;
167 deferred.reject(error);
168 });
169 }, Constants.POLL_INTERVALS.POWER_OP);
170
171 return deferred.promise;
172 }
173 $scope.warmReboot = function() {
174 $scope.loading = true;
175 dataService.setUnreachableState();
176 APIUtils.hostReboot()
177 .then(function(response) {
178 return response;
179 })
180 .then(function(lastStatus) {
181 pollStartTime = new Date();
182 return pollHostStatusTillOff();
183 })
184 .then(function(hostState) {
185 pollStartTime = new Date();
186 return pollHostStatusTillOn();
187 })
188 .then(function(hostState) {
189 $scope.loading = false;
190 })
191 .catch(function(error) {
192 dataService.activateErrorModal({
193 title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
194 description: error.statusText ?
195 $interpolate(
196 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
197 {status: error.status, description: error.statusText}) :
198 error
199 });
200 $scope.loading = false;
201 });
202 };
203 $scope.testState = function() {
204 $timeout(function() {
205 dataService.setPowerOffState();
206 $timeout(function() {
207 dataService.setPowerOnState();
208 }, 2000);
209 }, 1000);
210 };
211 $scope.warmRebootConfirm = function() {
212 if ($scope.confirm) {
213 return;
214 }
215 $scope.confirm = true;
216 $scope.warmboot_confirm = true;
217 };
218
219 $scope.coldReboot = function() {
220 $scope.loading = true;
221 dataService.setUnreachableState();
222 APIUtils.chassisPowerOff()
223 .then(function(state) {
224 return state;
225 })
226 .then(function(lastState) {
227 pollStartTime = new Date();
228 return pollChassisStatusTillOff();
229 })
230 .then(function(chassisState) {
231 return APIUtils.hostPowerOn().then(function(hostState) {
232 return hostState;
233 });
234 })
235 .then(function(hostState) {
236 pollStartTime = new Date();
237 return pollHostStatusTillOn();
238 })
239 .then(function(state) {
240 $scope.loading = false;
241 })
242 .catch(function(error) {
243 dataService.activateErrorModal({
244 title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
245 description: error.statusText ?
246 $interpolate(
247 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
248 {status: error.status, description: error.statusText}) :
249 error
250 });
251 $scope.loading = false;
252 });
253 };
254 $scope.coldRebootConfirm = function() {
255 if ($scope.confirm) {
256 return;
257 }
258 $scope.confirm = true;
259 $scope.coldboot_confirm = true;
260 };
261
262 $scope.orderlyShutdown = function() {
263 $scope.loading = true;
264 dataService.setUnreachableState();
265 APIUtils.hostPowerOff()
266 .then(function(response) {
267 return response;
268 })
269 .then(function(lastStatus) {
270 pollStartTime = new Date();
271 return pollHostStatusTillOff();
272 })
273 .then(function(hostState) {
274 pollStartTime = new Date();
275 return pollChassisStatusTillOff();
276 })
277 .then(function(chassisState) {
278 $scope.loading = false;
279 })
280 .catch(function(error) {
281 dataService.activateErrorModal({
282 title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
283 description: error.statusText ?
284 $interpolate(
285 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
286 {status: error.status, description: error.statusText}) :
287 error
288 });
289 $scope.loading = false;
290 });
291 };
292 $scope.orderlyShutdownConfirm = function() {
293 if ($scope.confirm) {
294 return;
295 }
296 $scope.confirm = true;
297 $scope.orderly_confirm = true;
298 };
299
300 $scope.immediateShutdown = function() {
301 $scope.loading = true;
302 dataService.setUnreachableState();
303 APIUtils.chassisPowerOff()
304 .then(function(response) {
305 return response;
306 })
307 .then(function(lastStatus) {
308 pollStartTime = new Date();
309 return pollChassisStatusTillOff();
310 })
311 .then(function(chassisState) {
312 dataService.setPowerOffState();
313 $scope.loading = false;
314 })
315 .catch(function(error) {
316 dataService.activateErrorModal({
317 title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
318 description: error.statusText ?
319 $interpolate(
320 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
321 {status: error.status, description: error.statusText}) :
322 error
323 });
324 $scope.loading = false;
325 });
326 };
327 $scope.immediateShutdownConfirm = function() {
328 if ($scope.confirm) {
329 return;
330 }
331 $scope.confirm = true;
332 $scope.immediately_confirm = true;
333 };
334 }
335 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500336
337})(angular);