1/**
2 * Controller for power-operations
3 *
4 * @module app/serverControl
5 * @exports powerOperationsController
6 * @name powerOperationsController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.serverControl').controller('powerOperationsController', [
13    '$scope', 'APIUtils', 'dataService', 'Constants', '$timeout', '$interval',
14    '$q', 'toastService',
15    function(
16        $scope, APIUtils, dataService, Constants, $timeout, $interval, $q,
17        toastService) {
18      $scope.dataService = dataService;
19      // Is a || of the other 4 "confirm" variables to ensure only
20      // one confirm is shown at a time.
21      $scope.confirm = false;
22      $scope.confirmWarmReboot = false;
23      $scope.confirmColdReboot = false;
24      $scope.confirmOrderlyShutdown = false;
25      $scope.confirmImmediateShutdown = false;
26      $scope.loading = true;
27
28      var pollChassisStatusTimer = undefined;
29      var pollStartTime = null;
30
31      APIUtils.getLastPowerTime()
32          .then(
33              function(data) {
34                if (data.data == 0) {
35                  $scope.powerTime = 'not available';
36                } else {
37                  $scope.powerTime = data.data;
38                }
39              },
40              function(error) {
41                console.log(JSON.stringify(error));
42              })
43          .finally(function() {
44            $scope.loading = false;
45          });
46
47      $scope.toggleState = function() {
48        dataService.server_state =
49            (dataService.server_state == 'Running') ? 'Off' : 'Running';
50      };
51
52      $scope.powerOn = function() {
53        $scope.loading = true;
54        dataService.setUnreachableState();
55        APIUtils.hostPowerOn()
56            .then(function(response) {
57              return response;
58            })
59            .then(function(lastStatus) {
60              return APIUtils.pollHostStatusTillOn();
61            })
62            .then(function(hostState) {
63              $scope.loading = false;
64            })
65            .catch(function(error) {
66              toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
67              $scope.loading = false;
68            });
69      };
70
71      function pollChassisStatusTillOff() {
72        var deferred = $q.defer();
73        pollChassisStatusTimer = $interval(function() {
74          var now = new Date();
75          if ((now.getTime() - pollStartTime.getTime()) >=
76              Constants.TIMEOUT.CHASSIS_OFF) {
77            $interval.cancel(pollChassisStatusTimer);
78            pollChassisStatusTimer = undefined;
79            deferred.reject(
80                new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
81          }
82          APIUtils.getChassisState()
83              .then(function(state) {
84                if (state === Constants.CHASSIS_POWER_STATE.off_code) {
85                  $interval.cancel(pollChassisStatusTimer);
86                  pollChassisStatusTimer = undefined;
87                  deferred.resolve(state);
88                }
89              })
90              .catch(function(error) {
91                $interval.cancel(pollChassisStatusTimer);
92                pollChassisStatusTimer = undefined;
93                deferred.reject(error);
94              });
95        }, Constants.POLL_INTERVALS.POWER_OP);
96
97        return deferred.promise;
98      }
99      $scope.warmReboot = function() {
100        $scope.loading = true;
101        dataService.setUnreachableState();
102        APIUtils.hostReboot()
103            .then(function(response) {
104              return response;
105            })
106            .then(function(lastStatus) {
107              return APIUtils.pollHostStatusTilReboot();
108            })
109            .then(function(hostState) {
110              $scope.loading = false;
111            })
112            .catch(function(error) {
113              toastService.error(
114                  Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
115              $scope.loading = false;
116            });
117      };
118
119      $scope.warmRebootConfirm = function() {
120        if ($scope.confirm) {
121          // If another "confirm" is already shown return
122          return;
123        }
124        $scope.confirm = true;
125        $scope.confirmWarmReboot = true;
126      };
127
128      $scope.coldReboot = function() {
129        $scope.loading = true;
130        dataService.setUnreachableState();
131        APIUtils.chassisPowerOff()
132            .then(function(state) {
133              return state;
134            })
135            .then(function(lastState) {
136              pollStartTime = new Date();
137              return pollChassisStatusTillOff();
138            })
139            .then(function(chassisState) {
140              return APIUtils.hostPowerOn().then(function(hostState) {
141                return hostState;
142              });
143            })
144            .then(function(hostState) {
145              return APIUtils.pollHostStatusTillOn();
146            })
147            .then(function(state) {
148              $scope.loading = false;
149            })
150            .catch(function(error) {
151              toastService.error(
152                  Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
153              $scope.loading = false;
154            });
155      };
156      $scope.coldRebootConfirm = function() {
157        if ($scope.confirm) {
158          return;
159        }
160        $scope.confirm = true;
161        $scope.confirmColdReboot = true;
162      };
163
164      $scope.orderlyShutdown = function() {
165        $scope.loading = true;
166        dataService.setUnreachableState();
167        APIUtils.hostPowerOff()
168            .then(function(response) {
169              return response;
170            })
171            .then(function(lastStatus) {
172              return APIUtils.pollHostStatusTillOff();
173            })
174            .then(function(hostState) {
175              pollStartTime = new Date();
176              return pollChassisStatusTillOff();
177            })
178            .then(function(chassisState) {
179              $scope.loading = false;
180            })
181            .catch(function(error) {
182              toastService.error(
183                  Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
184              $scope.loading = false;
185            });
186      };
187      $scope.orderlyShutdownConfirm = function() {
188        if ($scope.confirm) {
189          return;
190        }
191        $scope.confirm = true;
192        $scope.confirmOrderlyShutdown = true;
193      };
194
195      $scope.immediateShutdown = function() {
196        $scope.loading = true;
197        dataService.setUnreachableState();
198        APIUtils.chassisPowerOff()
199            .then(function(response) {
200              return response;
201            })
202            .then(function(lastStatus) {
203              pollStartTime = new Date();
204              return pollChassisStatusTillOff();
205            })
206            .then(function(chassisState) {
207              dataService.setPowerOffState();
208              $scope.loading = false;
209            })
210            .catch(function(error) {
211              toastService.error(
212                  Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
213              $scope.loading = false;
214            });
215      };
216      $scope.immediateShutdownConfirm = function() {
217        if ($scope.confirm) {
218          return;
219        }
220        $scope.confirm = true;
221        $scope.confirmImmediateShutdown = true;
222      };
223    }
224  ]);
225})(angular);
226