blob: 1bb0a81494a8e048e9298fa45b97f572eccb3633 [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',
beccabroek92d13b62019-01-08 14:24:29 -060014 '$interpolate', '$q', 'ngToast',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 function(
16 $scope, APIUtils, dataService, Constants, $timeout, $interval,
beccabroek92d13b62019-01-08 14:24:29 -060017 $interpolate, $q, ngToast) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $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) {
beccabroek92d13b62019-01-08 14:24:29 -060067 ngToast.danger(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070068 $scope.loading = false;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070069 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070070 };
71 $scope.powerOnConfirm = function() {
72 if ($scope.confirm) {
73 return;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070074 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070075 $scope.confirm = true;
76 $scope.power_confirm = true;
77 };
CamVan Nguyend80c2802018-04-17 19:25:16 -050078
Andrew Geisslerd27bb132018-05-24 11:07:27 -070079 function pollChassisStatusTillOff() {
80 var deferred = $q.defer();
81 pollChassisStatusTimer = $interval(function() {
82 var now = new Date();
83 if ((now.getTime() - pollStartTime.getTime()) >=
84 Constants.TIMEOUT.CHASSIS_OFF) {
85 $interval.cancel(pollChassisStatusTimer);
86 pollChassisStatusTimer = undefined;
87 deferred.reject(
88 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
89 }
90 APIUtils.getChassisState()
91 .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 })
98 .catch(function(error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070099 $interval.cancel(pollChassisStatusTimer);
100 pollChassisStatusTimer = undefined;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700101 deferred.reject(error);
102 });
103 }, Constants.POLL_INTERVALS.POWER_OP);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700104
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700105 return deferred.promise;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700106 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700107 $scope.warmReboot = function() {
108 $scope.loading = true;
109 dataService.setUnreachableState();
110 APIUtils.hostReboot()
111 .then(function(response) {
112 return response;
113 })
114 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500115 return APIUtils.pollHostStatusTilReboot();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700116 })
117 .then(function(hostState) {
118 $scope.loading = false;
119 })
120 .catch(function(error) {
beccabroek92d13b62019-01-08 14:24:29 -0600121 ngToast.danger(Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700122 $scope.loading = false;
123 });
124 };
125 $scope.testState = function() {
126 $timeout(function() {
127 dataService.setPowerOffState();
128 $timeout(function() {
129 dataService.setPowerOnState();
130 }, 2000);
131 }, 1000);
132 };
133 $scope.warmRebootConfirm = function() {
134 if ($scope.confirm) {
135 return;
136 }
137 $scope.confirm = true;
138 $scope.warmboot_confirm = true;
139 };
140
141 $scope.coldReboot = function() {
142 $scope.loading = true;
143 dataService.setUnreachableState();
144 APIUtils.chassisPowerOff()
145 .then(function(state) {
146 return state;
147 })
148 .then(function(lastState) {
149 pollStartTime = new Date();
150 return pollChassisStatusTillOff();
151 })
152 .then(function(chassisState) {
153 return APIUtils.hostPowerOn().then(function(hostState) {
154 return hostState;
155 });
156 })
157 .then(function(hostState) {
beccabroekc3abaa92018-08-14 13:47:18 -0500158 return APIUtils.pollHostStatusTillOn();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700159 })
160 .then(function(state) {
161 $scope.loading = false;
162 })
163 .catch(function(error) {
beccabroek92d13b62019-01-08 14:24:29 -0600164 ngToast.danger(Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700165 $scope.loading = false;
166 });
167 };
168 $scope.coldRebootConfirm = function() {
169 if ($scope.confirm) {
170 return;
171 }
172 $scope.confirm = true;
173 $scope.coldboot_confirm = true;
174 };
175
176 $scope.orderlyShutdown = function() {
177 $scope.loading = true;
178 dataService.setUnreachableState();
179 APIUtils.hostPowerOff()
180 .then(function(response) {
181 return response;
182 })
183 .then(function(lastStatus) {
beccabroekc3abaa92018-08-14 13:47:18 -0500184 return APIUtils.pollHostStatusTillOff();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700185 })
186 .then(function(hostState) {
187 pollStartTime = new Date();
188 return pollChassisStatusTillOff();
189 })
190 .then(function(chassisState) {
191 $scope.loading = false;
192 })
193 .catch(function(error) {
beccabroek92d13b62019-01-08 14:24:29 -0600194 ngToast.danger(
195 Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700196 $scope.loading = false;
197 });
198 };
199 $scope.orderlyShutdownConfirm = function() {
200 if ($scope.confirm) {
201 return;
202 }
203 $scope.confirm = true;
204 $scope.orderly_confirm = true;
205 };
206
207 $scope.immediateShutdown = function() {
208 $scope.loading = true;
209 dataService.setUnreachableState();
210 APIUtils.chassisPowerOff()
211 .then(function(response) {
212 return response;
213 })
214 .then(function(lastStatus) {
215 pollStartTime = new Date();
216 return pollChassisStatusTillOff();
217 })
218 .then(function(chassisState) {
219 dataService.setPowerOffState();
220 $scope.loading = false;
221 })
222 .catch(function(error) {
beccabroek92d13b62019-01-08 14:24:29 -0600223 ngToast.danger(
224 Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700225 $scope.loading = false;
226 });
227 };
228 $scope.immediateShutdownConfirm = function() {
229 if ($scope.confirm) {
230 return;
231 }
232 $scope.confirm = true;
233 $scope.immediately_confirm = true;
234 };
235 }
236 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500237})(angular);