blob: 989ca1b052a763f95d6ce3ef6c4e8e90dc351661 [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
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
Iftekharul Islamcd789502017-04-19 14:37:55 -050014 .module('app.serverControl')
Iftekharul Islam99d199f2017-03-24 15:28:25 -050015 .controller('powerOperationsController', [
Gunnar Millseedefd32018-02-28 17:02:34 -060016 '$scope',
17 'APIUtils',
18 'dataService',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060019 'Constants',
Gunnar Millseedefd32018-02-28 17:02:34 -060020 '$timeout',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060021 '$interval',
CamVan Nguyend80c2802018-04-17 19:25:16 -050022 '$interpolate',
Iftekharul Islama1d238f2018-02-26 12:29:45 -060023 '$q',
CamVan Nguyend80c2802018-04-17 19:25:16 -050024 function($scope, APIUtils, dataService, Constants, $timeout,
25 $interval, $interpolate, $q){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050026 $scope.dataService = dataService;
27 $scope.confirm = false;
28 $scope.power_confirm = false;
29 $scope.warmboot_confirm = false;
30 $scope.coldboot_confirm = false;
31 $scope.orderly_confirm = false;
32 $scope.immediately_confirm = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060033 $scope.loading = false;
34
35 var pollChassisStatusTimer = undefined;
36 var pollHostStatusTimer = undefined;
37 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050038
39 //@TODO: call api and get proper state
40 $scope.toggleState = function(){
41 dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
42 }
43
CamVan Nguyend80c2802018-04-17 19:25:16 -050044 $scope.powerOn = function(){
45 $scope.loading = true;
46 dataService.setUnreachableState();
47 APIUtils.hostPowerOn().then(function(response){
48 return response;
49 }).then(function(lastStatus) {
50 pollStartTime = new Date();
51 return pollHostStatusTillOn();
52 }).then(function(hostState) {
53 $scope.loading = false;
54 }).catch(function(error){
55 dataService.activateErrorModal(
56 {title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
57 description: error.statusText ?
58 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
59 {status: error.status, description: error.statusText}) :
60 error });
61 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050062 });
63 }
64 $scope.powerOnConfirm = function(){
65 if($scope.confirm) {
66 return;
67 }
68 $scope.confirm = true;
69 $scope.power_confirm = true;
70 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -060071
CamVan Nguyend80c2802018-04-17 19:25:16 -050072 function setHostState(state){
73 if(state == Constants.HOST_STATE_TEXT.off_code){
74 dataService.setPowerOffState();
75 }else if(state == Constants.HOST_STATE_TEXT.on_code){
76 dataService.setPowerOnState();
77 }else{
78 dataService.setErrorState();
79 }
80 }
81
Iftekharul Islama1d238f2018-02-26 12:29:45 -060082 function pollChassisStatusTillOff(){
83 var deferred = $q.defer();
84 pollChassisStatusTimer = $interval(function(){
85 var now = new Date();
86 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF){
87 $interval.cancel(pollChassisStatusTimer);
88 pollChassisStatusTimer = undefined;
CamVan Nguyend80c2802018-04-17 19:25:16 -050089 deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
Iftekharul Islama1d238f2018-02-26 12:29:45 -060090 }
91 APIUtils.getChassisState().then(function(state){
92 if(state === Constants.CHASSIS_POWER_STATE.off_code){
93 $interval.cancel(pollChassisStatusTimer);
94 pollChassisStatusTimer = undefined;
95 deferred.resolve(state);
96 }
97 }).catch(function(error){
98 $interval.cancel(pollChassisStatusTimer);
99 pollChassisStatusTimer = undefined;
100 deferred.reject(error);
101 });
102 }, Constants.POLL_INTERVALS.POWER_OP);
103
104 return deferred.promise;
105 }
106 function pollHostStatusTillOn(){
107 var deferred = $q.defer();
108 pollHostStatusTimer = $interval(function(){
109 var now = new Date();
110 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON){
111 $interval.cancel(pollHostStatusTimer);
112 pollHostStatusTimer = undefined;
CamVan Nguyend80c2802018-04-17 19:25:16 -0500113 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600114 }
115 APIUtils.getHostState().then(function(state){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500116 setHostState(state);
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600117 if(state === Constants.HOST_STATE_TEXT.on_code){
118 $interval.cancel(pollHostStatusTimer);
119 pollHostStatusTimer = undefined;
120 deferred.resolve(state);
CamVan Nguyend80c2802018-04-17 19:25:16 -0500121 }else if(state === Constants.HOST_STATE_TEXT.error_code){
122 $interval.cancel(pollHostStatusTimer);
123 pollHostStatusTimer = undefined;
124 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
125 }
126 }).catch(function(error){
127 $interval.cancel(pollHostStatusTimer);
128 pollHostStatusTimer = undefined;
129 deferred.reject(error);
130 });
131 }, Constants.POLL_INTERVALS.POWER_OP);
132
133 return deferred.promise;
134 }
135 function pollHostStatusTillOff(){
136 var deferred = $q.defer();
137 pollHostStatusTimer = $interval(function(){
138 var now = new Date();
139 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_OFF){
140 $interval.cancel(pollHostStatusTimer);
141 pollHostStatusTimer = undefined;
142 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
143 }
144 APIUtils.getHostState().then(function(state){
145 setHostState(state);
146 if(state === Constants.HOST_STATE_TEXT.off_code){
147 $interval.cancel(pollHostStatusTimer);
148 pollHostStatusTimer = undefined;
149 deferred.resolve(state);
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600150 }
151 }).catch(function(error){
152 $interval.cancel(pollHostStatusTimer);
153 pollHostStatusTimer = undefined;
154 deferred.reject(error);
155 });
156 }, Constants.POLL_INTERVALS.POWER_OP);
157
158 return deferred.promise;
159 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500160 $scope.warmReboot = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500161 $scope.loading = true;
162 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600163 APIUtils.hostReboot().then(function(response){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500164 return response;
165 }).then(function(lastStatus) {
166 pollStartTime = new Date();
167 return pollHostStatusTillOff();
168 }).then(function(hostState) {
169 pollStartTime = new Date();
170 return pollHostStatusTillOn();
171 }).then(function(hostState) {
172 $scope.loading = false;
173 }).catch(function(error){
174 dataService.activateErrorModal(
175 {title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
176 description: error.statusText ?
177 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
178 {status: error.status, description: error.statusText}) :
179 error });
180 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500181 });
182 };
183 $scope.testState = function(){
184 $timeout(function(){
185 dataService.setPowerOffState();
186 $timeout(function(){
187 dataService.setPowerOnState();
188 }, 2000);
189 }, 1000);
190 };
191 $scope.warmRebootConfirm = function(){
192 if($scope.confirm) {
193 return;
194 }
195 $scope.confirm = true;
196 $scope.warmboot_confirm = true;
197 };
198
199 $scope.coldReboot = function(){
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600200 $scope.loading = true;
CamVan Nguyend80c2802018-04-17 19:25:16 -0500201 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600202 APIUtils.chassisPowerOff().then(function(state){
203 return state;
204 }).then(function(lastState) {
205 pollStartTime = new Date();
206 return pollChassisStatusTillOff();
207 }).then(function(chassisState) {
CamVan Nguyend80c2802018-04-17 19:25:16 -0500208 return APIUtils.hostPowerOn().then(function(hostState){
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600209 return hostState;
210 })
211 }).then(function(hostState) {
212 pollStartTime = new Date();
213 return pollHostStatusTillOn();
214 }).then(function(state) {
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600215 $scope.loading = false;
216 }).catch(function(error){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500217 dataService.activateErrorModal(
218 {title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
219 description: error.statusText ?
220 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
221 {status: error.status, description: error.statusText}) :
222 error });
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600223 $scope.loading = false;
224 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500225 };
226 $scope.coldRebootConfirm = function(){
227 if($scope.confirm) {
228 return;
229 }
230 $scope.confirm = true;
231 $scope.coldboot_confirm = true;
232 };
233
234 $scope.orderlyShutdown = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500235 $scope.loading = true;
236 dataService.setUnreachableState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600237 APIUtils.hostPowerOff().then(function(response){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500238 return response;
239 }).then(function(lastStatus) {
240 pollStartTime = new Date();
241 return pollHostStatusTillOff()
242 }).then(function(hostState) {
243 pollStartTime = new Date();
244 return pollChassisStatusTillOff();
245 }).then(function(chassisState) {
246 $scope.loading = false;
247 }).catch(function(error){
248 dataService.activateErrorModal(
249 {title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
250 description: error.statusText ?
251 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
252 {status: error.status, description: error.statusText}) :
253 error });
254 $scope.loading = false;
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500255 });
256 };
257 $scope.orderlyShutdownConfirm = function(){
258 if($scope.confirm) {
259 return;
260 }
261 $scope.confirm = true;
262 $scope.orderly_confirm = true;
263 };
264
265 $scope.immediateShutdown = function(){
CamVan Nguyend80c2802018-04-17 19:25:16 -0500266 $scope.loading = true;
267 dataService.setUnreachableState();
268 APIUtils.chassisPowerOff().then(function(response){
269 return response;
270 }).then(function(lastStatus) {
271 pollStartTime = new Date();
272 return pollChassisStatusTillOff();
273 }).then(function(chassisState) {
274 dataService.setPowerOffState();
275 $scope.loading = false;
276 }).catch(function(error){
277 dataService.activateErrorModal(
278 {title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
279 description: error.statusText ?
280 $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
281 {status: error.status, description: error.statusText}) :
282 error });
283 $scope.loading = false;
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600284 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500285 };
286 $scope.immediateShutdownConfirm = function(){
287 if($scope.confirm) {
288 return;
289 }
290 $scope.confirm = true;
291 $scope.immediately_confirm = true;
292 };
293 }
294 ]
295 );
296
297})(angular);