blob: 910c50eba9ec5952637ca0b3110e4a7e6b691844 [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
9window.angular && (function (angular) {
10 'use strict';
11
12 angular
Iftekharul Islamcd789502017-04-19 14:37:55 -050013 .module('app.serverControl')
Iftekharul Islam99d199f2017-03-24 15:28:25 -050014 .controller('powerOperationsController', [
Gunnar Millseedefd32018-02-28 17:02:34 -060015 '$scope',
16 'APIUtils',
17 'dataService',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060018 'Constants',
Gunnar Millseedefd32018-02-28 17:02:34 -060019 '$timeout',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060020 '$interval',
CamVan Nguyend80c2802018-04-17 19:25:16 -050021 '$interpolate',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060022 '$q',
CamVan Nguyend80c2802018-04-17 19:25:16 -050023 function($scope, APIUtils, dataService, Constants, $timeout,
24 $interval, $interpolate, $q){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050025 $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;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060032 $scope.loading = false;
33
34 var pollChassisStatusTimer = undefined;
35 var pollHostStatusTimer = undefined;
36 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050037
38 //@TODO: call api and get proper state
39 $scope.toggleState = function(){
40 dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
41 }
42
CamVan Nguyend80c2802018-04-17 19:25:16 -050043 $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, description: error.statusText}) :
59 error });
60 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050061 });
62 }
63 $scope.powerOnConfirm = function(){
64 if($scope.confirm) {
65 return;
66 }
67 $scope.confirm = true;
68 $scope.power_confirm = true;
69 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -060070
CamVan Nguyend80c2802018-04-17 19:25:16 -050071 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
Iftekharul Islama1d238f2018-02-26 12:29:45 -060081 function pollChassisStatusTillOff(){
82 var deferred = $q.defer();
83 pollChassisStatusTimer = $interval(function(){
84 var now = new Date();
85 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF){
86 $interval.cancel(pollChassisStatusTimer);
87 pollChassisStatusTimer = undefined;
CamVan Nguyend80c2802018-04-17 19:25:16 -050088 deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
Iftekharul Islama1d238f2018-02-26 12:29:45 -060089 }
90 APIUtils.getChassisState().then(function(state){
91 if(state === Constants.CHASSIS_POWER_STATE.off_code){
92 $interval.cancel(pollChassisStatusTimer);
93 pollChassisStatusTimer = undefined;
94 deferred.resolve(state);
95 }
96 }).catch(function(error){
97 $interval.cancel(pollChassisStatusTimer);
98 pollChassisStatusTimer = undefined;
99 deferred.reject(error);
100 });
101 }, Constants.POLL_INTERVALS.POWER_OP);
102
103 return deferred.promise;
104 }
105 function pollHostStatusTillOn(){
106 var deferred = $q.defer();
107 pollHostStatusTimer = $interval(function(){
108 var now = new Date();
109 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON){
110 $interval.cancel(pollHostStatusTimer);
111 pollHostStatusTimer = undefined;
CamVan Nguyend80c2802018-04-17 19:25:16 -0500112 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600113 }
114 APIUtils.getHostState().then(function(state){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500115 setHostState(state);
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600116 if(state === Constants.HOST_STATE_TEXT.on_code){
117 $interval.cancel(pollHostStatusTimer);
118 pollHostStatusTimer = undefined;
119 deferred.resolve(state);
CamVan Nguyend80c2802018-04-17 19:25:16 -0500120 }else if(state === Constants.HOST_STATE_TEXT.error_code){
121 $interval.cancel(pollHostStatusTimer);
122 pollHostStatusTimer = undefined;
123 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
124 }
125 }).catch(function(error){
126 $interval.cancel(pollHostStatusTimer);
127 pollHostStatusTimer = undefined;
128 deferred.reject(error);
129 });
130 }, Constants.POLL_INTERVALS.POWER_OP);
131
132 return deferred.promise;
133 }
134 function pollHostStatusTillOff(){
135 var deferred = $q.defer();
136 pollHostStatusTimer = $interval(function(){
137 var now = new Date();
138 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_OFF){
139 $interval.cancel(pollHostStatusTimer);
140 pollHostStatusTimer = undefined;
141 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
142 }
143 APIUtils.getHostState().then(function(state){
144 setHostState(state);
145 if(state === Constants.HOST_STATE_TEXT.off_code){
146 $interval.cancel(pollHostStatusTimer);
147 pollHostStatusTimer = undefined;
148 deferred.resolve(state);
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600149 }
150 }).catch(function(error){
151 $interval.cancel(pollHostStatusTimer);
152 pollHostStatusTimer = undefined;
153 deferred.reject(error);
154 });
155 }, Constants.POLL_INTERVALS.POWER_OP);
156
157 return deferred.promise;
158 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500159 $scope.warmReboot = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500160 $scope.loading = true;
161 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600162 APIUtils.hostReboot().then(function(response){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500163 return response;
164 }).then(function(lastStatus) {
165 pollStartTime = new Date();
166 return pollHostStatusTillOff();
167 }).then(function(hostState) {
168 pollStartTime = new Date();
169 return pollHostStatusTillOn();
170 }).then(function(hostState) {
171 $scope.loading = false;
172 }).catch(function(error){
173 dataService.activateErrorModal(
174 {title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
175 description: error.statusText ?
176 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
177 {status: error.status, description: error.statusText}) :
178 error });
179 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500180 });
181 };
182 $scope.testState = function(){
183 $timeout(function(){
184 dataService.setPowerOffState();
185 $timeout(function(){
186 dataService.setPowerOnState();
187 }, 2000);
188 }, 1000);
189 };
190 $scope.warmRebootConfirm = function(){
191 if($scope.confirm) {
192 return;
193 }
194 $scope.confirm = true;
195 $scope.warmboot_confirm = true;
196 };
197
198 $scope.coldReboot = function(){
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600199 $scope.loading = true;
CamVan Nguyend80c2802018-04-17 19:25:16 -0500200 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600201 APIUtils.chassisPowerOff().then(function(state){
202 return state;
203 }).then(function(lastState) {
204 pollStartTime = new Date();
205 return pollChassisStatusTillOff();
206 }).then(function(chassisState) {
CamVan Nguyend80c2802018-04-17 19:25:16 -0500207 return APIUtils.hostPowerOn().then(function(hostState){
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600208 return hostState;
209 })
210 }).then(function(hostState) {
211 pollStartTime = new Date();
212 return pollHostStatusTillOn();
213 }).then(function(state) {
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600214 $scope.loading = false;
215 }).catch(function(error){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500216 dataService.activateErrorModal(
217 {title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
218 description: error.statusText ?
219 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
220 {status: error.status, description: error.statusText}) :
221 error });
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600222 $scope.loading = false;
223 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500224 };
225 $scope.coldRebootConfirm = function(){
226 if($scope.confirm) {
227 return;
228 }
229 $scope.confirm = true;
230 $scope.coldboot_confirm = true;
231 };
232
233 $scope.orderlyShutdown = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500234 $scope.loading = true;
235 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600236 APIUtils.hostPowerOff().then(function(response){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500237 return response;
238 }).then(function(lastStatus) {
239 pollStartTime = new Date();
240 return pollHostStatusTillOff()
241 }).then(function(hostState) {
242 pollStartTime = new Date();
243 return pollChassisStatusTillOff();
244 }).then(function(chassisState) {
245 $scope.loading = false;
246 }).catch(function(error){
247 dataService.activateErrorModal(
248 {title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
249 description: error.statusText ?
250 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
251 {status: error.status, description: error.statusText}) :
252 error });
253 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500254 });
255 };
256 $scope.orderlyShutdownConfirm = function(){
257 if($scope.confirm) {
258 return;
259 }
260 $scope.confirm = true;
261 $scope.orderly_confirm = true;
262 };
263
264 $scope.immediateShutdown = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500265 $scope.loading = true;
266 dataService.setUnreachableState();
267 APIUtils.chassisPowerOff().then(function(response){
268 return response;
269 }).then(function(lastStatus) {
270 pollStartTime = new Date();
271 return pollChassisStatusTillOff();
272 }).then(function(chassisState) {
273 dataService.setPowerOffState();
274 $scope.loading = false;
275 }).catch(function(error){
276 dataService.activateErrorModal(
277 {title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
278 description: error.statusText ?
279 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
280 {status: error.status, description: error.statusText}) :
281 error });
282 $scope.loading = false;
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600283 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500284 };
285 $scope.immediateShutdownConfirm = function(){
286 if($scope.confirm) {
287 return;
288 }
289 $scope.confirm = true;
290 $scope.immediately_confirm = true;
291 };
292 }
293 ]
294 );
295
296})(angular);