blob: f3955e6bdc6a80823c9b31072adb61218d7dc363 [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 pollChassisStatusTillOff() {
72 var deferred = $q.defer();
73 pollChassisStatusTimer = $interval(function() {
74 var now = new Date();
75 if ((now.getTime() - pollStartTime.getTime()) >=
76 Constants.TIMEOUT.CHASSIS_OFF) {
77 $interval.cancel(pollChassisStatusTimer);
78 pollChassisStatusTimer = undefined;
79 deferred.reject(
80 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
81 }
82 APIUtils.getChassisState()
83 .then(function(state) {
84 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
85 $interval.cancel(pollChassisStatusTimer);
86 pollChassisStatusTimer = undefined;
87 deferred.resolve(state);
88 }
89 })
90 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070091 $interval.cancel(pollChassisStatusTimer);
92 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070093 deferred.reject(error);
94 });
95 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070096
Andrew Geisslerd27bb132018-05-24 11:07:27 -070097 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070098 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070099
100 function pollHostStatusTillOn() {
101 var deferred = $q.defer();
102 pollHostStatusTimer = $interval(function() {
103 var now = new Date();
104 if ((now.getTime() - pollStartTime.getTime()) >=
105 Constants.TIMEOUT.HOST_ON) {
106 $interval.cancel(pollHostStatusTimer);
107 pollHostStatusTimer = undefined;
108 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
109 }
110 APIUtils.getHostState()
111 .then(function(state) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700112 if (state === Constants.HOST_STATE_TEXT.on_code) {
113 $interval.cancel(pollHostStatusTimer);
114 pollHostStatusTimer = undefined;
115 deferred.resolve(state);
116 } else if (state === Constants.HOST_STATE_TEXT.error_code) {
117 $interval.cancel(pollHostStatusTimer);
118 pollHostStatusTimer = undefined;
119 deferred.reject(
120 new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
121 }
122 })
123 .catch(function(error) {
124 $interval.cancel(pollHostStatusTimer);
125 pollHostStatusTimer = undefined;
126 deferred.reject(error);
127 });
128 }, Constants.POLL_INTERVALS.POWER_OP);
129
130 return deferred.promise;
131 }
132
133 function pollHostStatusTillOff() {
134 var deferred = $q.defer();
135 pollHostStatusTimer = $interval(function() {
136 var now = new Date();
137 if ((now.getTime() - pollStartTime.getTime()) >=
138 Constants.TIMEOUT.HOST_OFF) {
139 $interval.cancel(pollHostStatusTimer);
140 pollHostStatusTimer = undefined;
141 deferred.reject(
142 new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
143 }
144 APIUtils.getHostState()
145 .then(function(state) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700146 if (state === Constants.HOST_STATE_TEXT.off_code) {
147 $interval.cancel(pollHostStatusTimer);
148 pollHostStatusTimer = undefined;
149 deferred.resolve(state);
150 }
151 })
152 .catch(function(error) {
153 $interval.cancel(pollHostStatusTimer);
154 pollHostStatusTimer = undefined;
155 deferred.reject(error);
156 });
157 }, Constants.POLL_INTERVALS.POWER_OP);
158
159 return deferred.promise;
160 }
161 $scope.warmReboot = function() {
162 $scope.loading = true;
163 dataService.setUnreachableState();
164 APIUtils.hostReboot()
165 .then(function(response) {
166 return response;
167 })
168 .then(function(lastStatus) {
169 pollStartTime = new Date();
170 return pollHostStatusTillOff();
171 })
172 .then(function(hostState) {
173 pollStartTime = new Date();
174 return pollHostStatusTillOn();
175 })
176 .then(function(hostState) {
177 $scope.loading = false;
178 })
179 .catch(function(error) {
180 dataService.activateErrorModal({
181 title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
182 description: error.statusText ?
183 $interpolate(
184 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
185 {status: error.status, description: error.statusText}) :
186 error
187 });
188 $scope.loading = false;
189 });
190 };
191 $scope.testState = function() {
192 $timeout(function() {
193 dataService.setPowerOffState();
194 $timeout(function() {
195 dataService.setPowerOnState();
196 }, 2000);
197 }, 1000);
198 };
199 $scope.warmRebootConfirm = function() {
200 if ($scope.confirm) {
201 return;
202 }
203 $scope.confirm = true;
204 $scope.warmboot_confirm = true;
205 };
206
207 $scope.coldReboot = function() {
208 $scope.loading = true;
209 dataService.setUnreachableState();
210 APIUtils.chassisPowerOff()
211 .then(function(state) {
212 return state;
213 })
214 .then(function(lastState) {
215 pollStartTime = new Date();
216 return pollChassisStatusTillOff();
217 })
218 .then(function(chassisState) {
219 return APIUtils.hostPowerOn().then(function(hostState) {
220 return hostState;
221 });
222 })
223 .then(function(hostState) {
224 pollStartTime = new Date();
225 return pollHostStatusTillOn();
226 })
227 .then(function(state) {
228 $scope.loading = false;
229 })
230 .catch(function(error) {
231 dataService.activateErrorModal({
232 title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
233 description: error.statusText ?
234 $interpolate(
235 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
236 {status: error.status, description: error.statusText}) :
237 error
238 });
239 $scope.loading = false;
240 });
241 };
242 $scope.coldRebootConfirm = function() {
243 if ($scope.confirm) {
244 return;
245 }
246 $scope.confirm = true;
247 $scope.coldboot_confirm = true;
248 };
249
250 $scope.orderlyShutdown = function() {
251 $scope.loading = true;
252 dataService.setUnreachableState();
253 APIUtils.hostPowerOff()
254 .then(function(response) {
255 return response;
256 })
257 .then(function(lastStatus) {
258 pollStartTime = new Date();
259 return pollHostStatusTillOff();
260 })
261 .then(function(hostState) {
262 pollStartTime = new Date();
263 return pollChassisStatusTillOff();
264 })
265 .then(function(chassisState) {
266 $scope.loading = false;
267 })
268 .catch(function(error) {
269 dataService.activateErrorModal({
270 title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
271 description: error.statusText ?
272 $interpolate(
273 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
274 {status: error.status, description: error.statusText}) :
275 error
276 });
277 $scope.loading = false;
278 });
279 };
280 $scope.orderlyShutdownConfirm = function() {
281 if ($scope.confirm) {
282 return;
283 }
284 $scope.confirm = true;
285 $scope.orderly_confirm = true;
286 };
287
288 $scope.immediateShutdown = function() {
289 $scope.loading = true;
290 dataService.setUnreachableState();
291 APIUtils.chassisPowerOff()
292 .then(function(response) {
293 return response;
294 })
295 .then(function(lastStatus) {
296 pollStartTime = new Date();
297 return pollChassisStatusTillOff();
298 })
299 .then(function(chassisState) {
300 dataService.setPowerOffState();
301 $scope.loading = false;
302 })
303 .catch(function(error) {
304 dataService.activateErrorModal({
305 title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
306 description: error.statusText ?
307 $interpolate(
308 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
309 {status: error.status, description: error.statusText}) :
310 error
311 });
312 $scope.loading = false;
313 });
314 };
315 $scope.immediateShutdownConfirm = function() {
316 if ($scope.confirm) {
317 return;
318 }
319 $scope.confirm = true;
320 $scope.immediately_confirm = true;
321 };
322 }
323 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500324
325})(angular);