blob: 0b2fb58dcc0b1773cfe2427412dc976e8bd0a2f6 [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
beccabroek56744252018-08-03 11:25:11 -050032
33 APIUtils.getLastPowerTime().then(
34 function(data) {
35 if (data.data == 0) {
36 $scope.power_time = 'not available';
37 } else {
38 $scope.power_time = data.data;
39 }
40 },
41 function(error) {
42 console.log(JSON.stringify(error));
43 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070044 $scope.toggleState = function() {
45 dataService.server_state =
46 (dataService.server_state == 'Running') ? 'Off' : 'Running';
47 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050048
Andrew Geisslerd27bb132018-05-24 11:07:27 -070049 $scope.powerOn = function() {
50 $scope.loading = true;
51 dataService.setUnreachableState();
52 APIUtils.hostPowerOn()
53 .then(function(response) {
54 return response;
55 })
56 .then(function(lastStatus) {
57 pollStartTime = new Date();
58 return pollHostStatusTillOn();
59 })
60 .then(function(hostState) {
61 $scope.loading = false;
62 })
63 .catch(function(error) {
64 dataService.activateErrorModal({
65 title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
66 description: error.statusText ?
67 $interpolate(
68 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
69 {status: error.status, description: error.statusText}) :
70 error
71 });
72 $scope.loading = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070074 };
75 $scope.powerOnConfirm = function() {
76 if ($scope.confirm) {
77 return;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070078 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070079 $scope.confirm = true;
80 $scope.power_confirm = true;
81 };
CamVan Nguyend80c2802018-04-17 19:25:16 -050082
Andrew Geisslerd27bb132018-05-24 11:07:27 -070083 function pollChassisStatusTillOff() {
84 var deferred = $q.defer();
85 pollChassisStatusTimer = $interval(function() {
86 var now = new Date();
87 if ((now.getTime() - pollStartTime.getTime()) >=
88 Constants.TIMEOUT.CHASSIS_OFF) {
89 $interval.cancel(pollChassisStatusTimer);
90 pollChassisStatusTimer = undefined;
91 deferred.reject(
92 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
93 }
94 APIUtils.getChassisState()
95 .then(function(state) {
96 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
97 $interval.cancel(pollChassisStatusTimer);
98 pollChassisStatusTimer = undefined;
99 deferred.resolve(state);
100 }
101 })
102 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700103 $interval.cancel(pollChassisStatusTimer);
104 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700105 deferred.reject(error);
106 });
107 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700108
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700109 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700110 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700111
112 function pollHostStatusTillOn() {
113 var deferred = $q.defer();
114 pollHostStatusTimer = $interval(function() {
115 var now = new Date();
116 if ((now.getTime() - pollStartTime.getTime()) >=
117 Constants.TIMEOUT.HOST_ON) {
118 $interval.cancel(pollHostStatusTimer);
119 pollHostStatusTimer = undefined;
120 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
121 }
122 APIUtils.getHostState()
123 .then(function(state) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700124 if (state === Constants.HOST_STATE_TEXT.on_code) {
125 $interval.cancel(pollHostStatusTimer);
126 pollHostStatusTimer = undefined;
127 deferred.resolve(state);
128 } else if (state === Constants.HOST_STATE_TEXT.error_code) {
129 $interval.cancel(pollHostStatusTimer);
130 pollHostStatusTimer = undefined;
131 deferred.reject(
132 new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
133 }
134 })
135 .catch(function(error) {
136 $interval.cancel(pollHostStatusTimer);
137 pollHostStatusTimer = undefined;
138 deferred.reject(error);
139 });
140 }, Constants.POLL_INTERVALS.POWER_OP);
141
142 return deferred.promise;
143 }
144
145 function pollHostStatusTillOff() {
146 var deferred = $q.defer();
147 pollHostStatusTimer = $interval(function() {
148 var now = new Date();
149 if ((now.getTime() - pollStartTime.getTime()) >=
150 Constants.TIMEOUT.HOST_OFF) {
151 $interval.cancel(pollHostStatusTimer);
152 pollHostStatusTimer = undefined;
153 deferred.reject(
154 new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
155 }
156 APIUtils.getHostState()
157 .then(function(state) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700158 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})(angular);