blob: 06fb3712544f9765f9a653a2e5c60c19bb16fe76 [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 Geisslerba5e3f32018-05-24 10:58:00 -070012 angular
13 .module('app.serverControl')
14 .controller('powerOperationsController', [
15 '$scope',
16 'APIUtils',
17 'dataService',
18 'Constants',
19 '$timeout',
20 '$interval',
21 '$interpolate',
22 '$q',
23 function($scope, APIUtils, dataService, Constants, $timeout,
24 $interval, $interpolate, $q) {
25 $scope.dataService = dataService;
26 $scope.confirm = false;
27 $scope.power_confirm = false;
28 $scope.warmboot_confirm = false;
29 $scope.coldboot_confirm = false;
30 $scope.orderly_confirm = false;
31 $scope.immediately_confirm = false;
32 $scope.loading = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060033
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070034 var pollChassisStatusTimer = undefined;
35 var pollHostStatusTimer = undefined;
36 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050037
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070038 //@TODO: call api and get proper state
39 $scope.toggleState = function() {
40 dataService.server_state = (dataService.server_state == 'Running') ? 'Off' : 'Running';
41 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050042
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070043 $scope.powerOn = function() {
44 $scope.loading = true;
45 dataService.setUnreachableState();
46 APIUtils.hostPowerOn().then(function(response) {
47 return response;
48 }).then(function(lastStatus) {
49 pollStartTime = new Date();
50 return pollHostStatusTillOn();
51 }).then(function(hostState) {
52 $scope.loading = false;
53 }).catch(function(error) {
54 dataService.activateErrorModal({
55 title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
56 description: error.statusText ?
57 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)({
58 status: error.status,
59 description: error.statusText
60 }) : error
61 });
62 $scope.loading = false;
63 });
64 };
65 $scope.powerOnConfirm = function() {
66 if ($scope.confirm) {
67 return;
68 }
69 $scope.confirm = true;
70 $scope.power_confirm = true;
71 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -060072
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073 function setHostState(state) {
74 if (state == Constants.HOST_STATE_TEXT.off_code) {
75 dataService.setPowerOffState();
76 }
77 else if (state == Constants.HOST_STATE_TEXT.on_code) {
78 dataService.setPowerOnState();
79 }
80 else {
81 dataService.setErrorState();
82 }
83 }
CamVan Nguyend80c2802018-04-17 19:25:16 -050084
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070085 function pollChassisStatusTillOff() {
86 var deferred = $q.defer();
87 pollChassisStatusTimer = $interval(function() {
88 var now = new Date();
89 if ((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF) {
90 $interval.cancel(pollChassisStatusTimer);
91 pollChassisStatusTimer = undefined;
92 deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
Iftekharul Islam99d199f2017-03-24 15:28:25 -050093 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070094 APIUtils.getChassisState().then(function(state) {
95 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
96 $interval.cancel(pollChassisStatusTimer);
97 pollChassisStatusTimer = undefined;
98 deferred.resolve(state);
99 }
100 }).catch(function(error) {
101 $interval.cancel(pollChassisStatusTimer);
102 pollChassisStatusTimer = undefined;
103 deferred.reject(error);
104 });
105 }, Constants.POLL_INTERVALS.POWER_OP);
106
107 return deferred.promise;
108 }
109
110 function pollHostStatusTillOn() {
111 var deferred = $q.defer();
112 pollHostStatusTimer = $interval(function() {
113 var now = new Date();
114 if ((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON) {
115 $interval.cancel(pollHostStatusTimer);
116 pollHostStatusTimer = undefined;
117 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
118 }
119 APIUtils.getHostState().then(function(state) {
120 setHostState(state);
121 if (state === Constants.HOST_STATE_TEXT.on_code) {
122 $interval.cancel(pollHostStatusTimer);
123 pollHostStatusTimer = undefined;
124 deferred.resolve(state);
125 }
126 else if (state === Constants.HOST_STATE_TEXT.error_code) {
127 $interval.cancel(pollHostStatusTimer);
128 pollHostStatusTimer = undefined;
129 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
130 }
131 }).catch(function(error) {
132 $interval.cancel(pollHostStatusTimer);
133 pollHostStatusTimer = undefined;
134 deferred.reject(error);
135 });
136 }, Constants.POLL_INTERVALS.POWER_OP);
137
138 return deferred.promise;
139 }
140
141 function pollHostStatusTillOff() {
142 var deferred = $q.defer();
143 pollHostStatusTimer = $interval(function() {
144 var now = new Date();
145 if ((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_OFF) {
146 $interval.cancel(pollHostStatusTimer);
147 pollHostStatusTimer = undefined;
148 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
149 }
150 APIUtils.getHostState().then(function(state) {
151 setHostState(state);
152 if (state === Constants.HOST_STATE_TEXT.off_code) {
153 $interval.cancel(pollHostStatusTimer);
154 pollHostStatusTimer = undefined;
155 deferred.resolve(state);
156 }
157 }).catch(function(error) {
158 $interval.cancel(pollHostStatusTimer);
159 pollHostStatusTimer = undefined;
160 deferred.reject(error);
161 });
162 }, Constants.POLL_INTERVALS.POWER_OP);
163
164 return deferred.promise;
165 }
166 $scope.warmReboot = function() {
167 $scope.loading = true;
168 dataService.setUnreachableState();
169 APIUtils.hostReboot().then(function(response) {
170 return response;
171 }).then(function(lastStatus) {
172 pollStartTime = new Date();
173 return pollHostStatusTillOff();
174 }).then(function(hostState) {
175 pollStartTime = new Date();
176 return pollHostStatusTillOn();
177 }).then(function(hostState) {
178 $scope.loading = false;
179 }).catch(function(error) {
180 dataService.activateErrorModal({
181 title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
182 description: error.statusText ?
183 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)({
184 status: error.status,
185 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().then(function(state) {
211 return state;
212 }).then(function(lastState) {
213 pollStartTime = new Date();
214 return pollChassisStatusTillOff();
215 }).then(function(chassisState) {
216 return APIUtils.hostPowerOn().then(function(hostState) {
217 return hostState;
218 });
219 }).then(function(hostState) {
220 pollStartTime = new Date();
221 return pollHostStatusTillOn();
222 }).then(function(state) {
223 $scope.loading = false;
224 }).catch(function(error) {
225 dataService.activateErrorModal({
226 title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
227 description: error.statusText ?
228 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)({
229 status: error.status,
230 description: error.statusText
231 }) : error
232 });
233 $scope.loading = false;
234 });
235 };
236 $scope.coldRebootConfirm = function() {
237 if ($scope.confirm) {
238 return;
239 }
240 $scope.confirm = true;
241 $scope.coldboot_confirm = true;
242 };
243
244 $scope.orderlyShutdown = function() {
245 $scope.loading = true;
246 dataService.setUnreachableState();
247 APIUtils.hostPowerOff().then(function(response) {
248 return response;
249 }).then(function(lastStatus) {
250 pollStartTime = new Date();
251 return pollHostStatusTillOff();
252 }).then(function(hostState) {
253 pollStartTime = new Date();
254 return pollChassisStatusTillOff();
255 }).then(function(chassisState) {
256 $scope.loading = false;
257 }).catch(function(error) {
258 dataService.activateErrorModal({
259 title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
260 description: error.statusText ?
261 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)({
262 status: error.status,
263 description: error.statusText
264 }) : error
265 });
266 $scope.loading = false;
267 });
268 };
269 $scope.orderlyShutdownConfirm = function() {
270 if ($scope.confirm) {
271 return;
272 }
273 $scope.confirm = true;
274 $scope.orderly_confirm = true;
275 };
276
277 $scope.immediateShutdown = function() {
278 $scope.loading = true;
279 dataService.setUnreachableState();
280 APIUtils.chassisPowerOff().then(function(response) {
281 return response;
282 }).then(function(lastStatus) {
283 pollStartTime = new Date();
284 return pollChassisStatusTillOff();
285 }).then(function(chassisState) {
286 dataService.setPowerOffState();
287 $scope.loading = false;
288 }).catch(function(error) {
289 dataService.activateErrorModal({
290 title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
291 description: error.statusText ?
292 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)({
293 status: error.status,
294 description: error.statusText
295 }) : error
296 });
297 $scope.loading = false;
298 });
299 };
300 $scope.immediateShutdownConfirm = function() {
301 if ($scope.confirm) {
302 return;
303 }
304 $scope.confirm = true;
305 $scope.immediately_confirm = true;
306 };
307 }
308 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500309
310})(angular);