blob: 6c6f68cb3fddfc84bba7a4d799324526c401407f [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;
Gunnar Mills6add8322018-09-05 15:16:12 -050025 $scope.loading = true;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060026
Andrew Geisslerd27bb132018-05-24 11:07:27 -070027 var pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050029
Andrew Geisslerd27bb132018-05-24 11:07:27 -070030 //@TODO: call api and get proper state
beccabroek56744252018-08-03 11:25:11 -050031
Gunnar Mills6add8322018-09-05 15:16:12 -050032 APIUtils.getLastPowerTime()
33 .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 })
44 .finally(function() {
45 $scope.loading = false;
beccabroek56744252018-08-03 11:25:11 -050046 });
Gunnar Mills6add8322018-09-05 15:16:12 -050047
Andrew Geisslerd27bb132018-05-24 11:07:27 -070048 $scope.toggleState = function() {
49 dataService.server_state =
50 (dataService.server_state == 'Running') ? 'Off' : 'Running';
51 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050052
Andrew Geisslerd27bb132018-05-24 11:07:27 -070053 $scope.powerOn = function() {
54 $scope.loading = true;
55 dataService.setUnreachableState();
56 APIUtils.hostPowerOn()
57 .then(function(response) {
58 return response;
59 })
60 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -050061 return APIUtils.pollHostStatusTillOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -070062 })
63 .then(function(hostState) {
64 $scope.loading = false;
65 })
66 .catch(function(error) {
67 dataService.activateErrorModal({
68 title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
69 description: error.statusText ?
70 $interpolate(
71 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
72 {status: error.status, description: error.statusText}) :
73 error
74 });
75 $scope.loading = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070076 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070077 };
78 $scope.powerOnConfirm = function() {
79 if ($scope.confirm) {
80 return;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070081 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070082 $scope.confirm = true;
83 $scope.power_confirm = true;
84 };
CamVan Nguyend80c2802018-04-17 19:25:16 -050085
Andrew Geisslerd27bb132018-05-24 11:07:27 -070086 function pollChassisStatusTillOff() {
87 var deferred = $q.defer();
88 pollChassisStatusTimer = $interval(function() {
89 var now = new Date();
90 if ((now.getTime() - pollStartTime.getTime()) >=
91 Constants.TIMEOUT.CHASSIS_OFF) {
92 $interval.cancel(pollChassisStatusTimer);
93 pollChassisStatusTimer = undefined;
94 deferred.reject(
95 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
96 }
97 APIUtils.getChassisState()
98 .then(function(state) {
99 if (state === Constants.CHASSIS_POWER_STATE.off_code) {
100 $interval.cancel(pollChassisStatusTimer);
101 pollChassisStatusTimer = undefined;
102 deferred.resolve(state);
103 }
104 })
105 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700106 $interval.cancel(pollChassisStatusTimer);
107 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700108 deferred.reject(error);
109 });
110 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700111
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700112 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700113 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700114 $scope.warmReboot = function() {
115 $scope.loading = true;
116 dataService.setUnreachableState();
117 APIUtils.hostReboot()
118 .then(function(response) {
119 return response;
120 })
121 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500122 return APIUtils.pollHostStatusTilReboot();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700123 })
124 .then(function(hostState) {
125 $scope.loading = false;
126 })
127 .catch(function(error) {
128 dataService.activateErrorModal({
129 title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
130 description: error.statusText ?
131 $interpolate(
132 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
133 {status: error.status, description: error.statusText}) :
134 error
135 });
136 $scope.loading = false;
137 });
138 };
139 $scope.testState = function() {
140 $timeout(function() {
141 dataService.setPowerOffState();
142 $timeout(function() {
143 dataService.setPowerOnState();
144 }, 2000);
145 }, 1000);
146 };
147 $scope.warmRebootConfirm = function() {
148 if ($scope.confirm) {
149 return;
150 }
151 $scope.confirm = true;
152 $scope.warmboot_confirm = true;
153 };
154
155 $scope.coldReboot = function() {
156 $scope.loading = true;
157 dataService.setUnreachableState();
158 APIUtils.chassisPowerOff()
159 .then(function(state) {
160 return state;
161 })
162 .then(function(lastState) {
163 pollStartTime = new Date();
164 return pollChassisStatusTillOff();
165 })
166 .then(function(chassisState) {
167 return APIUtils.hostPowerOn().then(function(hostState) {
168 return hostState;
169 });
170 })
171 .then(function(hostState) {
beccabroekc3abaa92018-08-14 13:47:18 -0500172 return APIUtils.pollHostStatusTillOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700173 })
174 .then(function(state) {
175 $scope.loading = false;
176 })
177 .catch(function(error) {
178 dataService.activateErrorModal({
179 title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
180 description: error.statusText ?
181 $interpolate(
182 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
183 {status: error.status, description: error.statusText}) :
184 error
185 });
186 $scope.loading = false;
187 });
188 };
189 $scope.coldRebootConfirm = function() {
190 if ($scope.confirm) {
191 return;
192 }
193 $scope.confirm = true;
194 $scope.coldboot_confirm = true;
195 };
196
197 $scope.orderlyShutdown = function() {
198 $scope.loading = true;
199 dataService.setUnreachableState();
200 APIUtils.hostPowerOff()
201 .then(function(response) {
202 return response;
203 })
204 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500205 return APIUtils.pollHostStatusTillOff();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700206 })
207 .then(function(hostState) {
208 pollStartTime = new Date();
209 return pollChassisStatusTillOff();
210 })
211 .then(function(chassisState) {
212 $scope.loading = false;
213 })
214 .catch(function(error) {
215 dataService.activateErrorModal({
216 title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
217 description: error.statusText ?
218 $interpolate(
219 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
220 {status: error.status, description: error.statusText}) :
221 error
222 });
223 $scope.loading = false;
224 });
225 };
226 $scope.orderlyShutdownConfirm = function() {
227 if ($scope.confirm) {
228 return;
229 }
230 $scope.confirm = true;
231 $scope.orderly_confirm = true;
232 };
233
234 $scope.immediateShutdown = function() {
235 $scope.loading = true;
236 dataService.setUnreachableState();
237 APIUtils.chassisPowerOff()
238 .then(function(response) {
239 return response;
240 })
241 .then(function(lastStatus) {
242 pollStartTime = new Date();
243 return pollChassisStatusTillOff();
244 })
245 .then(function(chassisState) {
246 dataService.setPowerOffState();
247 $scope.loading = false;
248 })
249 .catch(function(error) {
250 dataService.activateErrorModal({
251 title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
252 description: error.statusText ?
253 $interpolate(
254 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
255 {status: error.status, description: error.statusText}) :
256 error
257 });
258 $scope.loading = false;
259 });
260 };
261 $scope.immediateShutdownConfirm = function() {
262 if ($scope.confirm) {
263 return;
264 }
265 $scope.confirm = true;
266 $scope.immediately_confirm = true;
267 };
268 }
269 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500270})(angular);