xref: /openbmc/phosphor-webui/app/server-control/controllers/power-operations-controller.js (revision d80c280bc69b146b9daf132658b7b0ad07c7e734)
1/**
2 * Controller for power-operations
3 *
4 * @module app/serverControl
5 * @exports powerOperationsController
6 * @name powerOperationsController
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11    'use strict';
12
13    angular
14        .module('app.serverControl')
15        .controller('powerOperationsController', [
16            '$scope',
17            'APIUtils',
18            'dataService',
19            'Constants',
20            '$timeout',
21            '$interval',
22            '$interpolate',
23            '$q',
24            function($scope, APIUtils, dataService, Constants, $timeout,
25                     $interval, $interpolate, $q){
26                $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;
33                $scope.loading = false;
34
35                var pollChassisStatusTimer = undefined;
36                var pollHostStatusTimer = undefined;
37                var pollStartTime = null;
38
39                //@TODO: call api and get proper state
40                $scope.toggleState = function(){
41                    dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
42                }
43
44                $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;
62                    });
63                }
64                $scope.powerOnConfirm = function(){
65                    if($scope.confirm) {
66                        return;
67                    }
68                    $scope.confirm = true;
69                    $scope.power_confirm = true;
70                };
71
72                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
82                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;
89                            deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
90                        }
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;
113                            deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
114                        }
115                        APIUtils.getHostState().then(function(state){
116                            setHostState(state);
117                            if(state === Constants.HOST_STATE_TEXT.on_code){
118                                $interval.cancel(pollHostStatusTimer);
119                                pollHostStatusTimer = undefined;
120                                deferred.resolve(state);
121                            }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);
150                            }
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                }
160                $scope.warmReboot = function(){
161                    $scope.loading = true;
162                    dataService.setUnreachableState();
163                    APIUtils.hostReboot().then(function(response){
164                        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;
181                    });
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(){
200                    $scope.loading = true;
201                    dataService.setUnreachableState();
202                    APIUtils.chassisPowerOff().then(function(state){
203                        return state;
204                    }).then(function(lastState) {
205                        pollStartTime = new Date();
206                        return pollChassisStatusTillOff();
207                    }).then(function(chassisState) {
208                        return APIUtils.hostPowerOn().then(function(hostState){
209                            return hostState;
210                        })
211                    }).then(function(hostState) {
212                        pollStartTime = new Date();
213                        return pollHostStatusTillOn();
214                    }).then(function(state) {
215                        $scope.loading = false;
216                    }).catch(function(error){
217                        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 });
223                        $scope.loading = false;
224                    });
225                };
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(){
235                    $scope.loading = true;
236                    dataService.setUnreachableState();
237                    APIUtils.hostPowerOff().then(function(response){
238                        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;
255                    });
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(){
266                    $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;
284                    });
285                };
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);
298