blob: 461c1e7faa017de4b5674912ed498ea445061f4a [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',
22 '$q',
23 function($scope, APIUtils, dataService, Constants, $timeout, $interval, $q){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050024 $scope.dataService = dataService;
25 $scope.confirm = false;
26 $scope.power_confirm = false;
27 $scope.warmboot_confirm = false;
28 $scope.coldboot_confirm = false;
29 $scope.orderly_confirm = false;
30 $scope.immediately_confirm = false;
Iftekharul Islama1d238f2018-02-26 12:29:45 -060031 $scope.loading = false;
32
33 var pollChassisStatusTimer = undefined;
34 var pollHostStatusTimer = undefined;
35 var pollStartTime = null;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050036
37 //@TODO: call api and get proper state
38 $scope.toggleState = function(){
39 dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
40 }
41
42 $scope.togglePower = function(){
43 var method = (dataService.server_state == 'Running') ? 'hostPowerOff' : 'hostPowerOn';
Iftekharul Islama1d238f2018-02-26 12:29:45 -060044 //@TODO: show progress or set class orange
45 APIUtils[method]().then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050046 //update state based on response
47 //error case?
48 if(response == null){
49 console.log("Failed request.");
50 }else{
51 //@TODO::need to get the server status
52 if(dataService.server_state == 'Running'){
53 dataService.setPowerOffState();
54 }else{
55 dataService.setPowerOnState();
56 }
57 }
58 });
59 }
60 $scope.powerOnConfirm = function(){
61 if($scope.confirm) {
62 return;
63 }
64 $scope.confirm = true;
65 $scope.power_confirm = true;
66 };
Iftekharul Islama1d238f2018-02-26 12:29:45 -060067
68 function pollChassisStatusTillOff(){
69 var deferred = $q.defer();
70 pollChassisStatusTimer = $interval(function(){
71 var now = new Date();
72 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF){
73 $interval.cancel(pollChassisStatusTimer);
74 pollChassisStatusTimer = undefined;
75 deferred.reject(new Error(Constants.MESSAGES.POLL.TIMEOUT));
76 }
77 APIUtils.getChassisState().then(function(state){
78 if(state === Constants.CHASSIS_POWER_STATE.off_code){
79 $interval.cancel(pollChassisStatusTimer);
80 pollChassisStatusTimer = undefined;
81 deferred.resolve(state);
82 }
83 }).catch(function(error){
84 $interval.cancel(pollChassisStatusTimer);
85 pollChassisStatusTimer = undefined;
86 deferred.reject(error);
87 });
88 }, Constants.POLL_INTERVALS.POWER_OP);
89
90 return deferred.promise;
91 }
92 function pollHostStatusTillOn(){
93 var deferred = $q.defer();
94 pollHostStatusTimer = $interval(function(){
95 var now = new Date();
96 if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON){
97 $interval.cancel(pollHostStatusTimer);
98 pollHostStatusTimer = undefined;
99 deferred.reject(new Error(Constants.MESSAGES.POLL.TIMEOUT));
100 }
101 APIUtils.getHostState().then(function(state){
102 if(state === Constants.HOST_STATE_TEXT.on_code){
103 $interval.cancel(pollHostStatusTimer);
104 pollHostStatusTimer = undefined;
105 deferred.resolve(state);
106 }
107 }).catch(function(error){
108 $interval.cancel(pollHostStatusTimer);
109 pollHostStatusTimer = undefined;
110 deferred.reject(error);
111 });
112 }, Constants.POLL_INTERVALS.POWER_OP);
113
114 return deferred.promise;
115 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500116 $scope.warmReboot = function(){
117 //@TODO:show progress
118 dataService.setBootingState();
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600119 APIUtils.hostReboot().then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500120 if(response){
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600121 dataService.setPowerOnState();
122 }else{
123 //@TODO:hide progress & show error message
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500124 }
125 });
126 };
127 $scope.testState = function(){
128 $timeout(function(){
129 dataService.setPowerOffState();
130 $timeout(function(){
131 dataService.setPowerOnState();
132 }, 2000);
133 }, 1000);
134 };
135 $scope.warmRebootConfirm = function(){
136 if($scope.confirm) {
137 return;
138 }
139 $scope.confirm = true;
140 $scope.warmboot_confirm = true;
141 };
142
143 $scope.coldReboot = function(){
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600144 $scope.loading = true;
145 dataService.setBootingState();
146 APIUtils.chassisPowerOff().then(function(state){
147 return state;
148 }).then(function(lastState) {
149 pollStartTime = new Date();
150 return pollChassisStatusTillOff();
151 }).then(function(chassisState) {
152 APIUtils.hostPowerOn().then(function(hostState){
153 return hostState;
154 })
155 }).then(function(hostState) {
156 pollStartTime = new Date();
157 return pollHostStatusTillOn();
158 }).then(function(state) {
159 dataService.setPowerOnState();
160 $scope.loading = false;
161 }).catch(function(error){
162 $scope.loading = false;
163 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500164 };
165 $scope.coldRebootConfirm = function(){
166 if($scope.confirm) {
167 return;
168 }
169 $scope.confirm = true;
170 $scope.coldboot_confirm = true;
171 };
172
173 $scope.orderlyShutdown = function(){
174 //@TODO:show progress
Iftekharul Islama1d238f2018-02-26 12:29:45 -0600175 APIUtils.hostPowerOff().then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500176 if(response){
177 dataService.setPowerOffState();
178 }else{
179 //@TODO:hide progress & show error message
180 }
181 });
182 };
183 $scope.orderlyShutdownConfirm = function(){
184 if($scope.confirm) {
185 return;
186 }
187 $scope.confirm = true;
188 $scope.orderly_confirm = true;
189 };
190
191 $scope.immediateShutdown = function(){
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600192 //@TODO:show progress
193 APIUtils.chassisPowerOff(function(response){
194 if(response){
195 dataService.setPowerOffState();
196 }else{
197 //@TODO:hide progress & show error message
198 }
199 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500200 };
201 $scope.immediateShutdownConfirm = function(){
202 if($scope.confirm) {
203 return;
204 }
205 $scope.confirm = true;
206 $scope.immediately_confirm = true;
207 };
208 }
209 ]
210 );
211
212})(angular);