Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for power-operations |
| 3 | * |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 4 | * @module app/serverControl |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 5 | * @exports powerOperationsController |
| 6 | * @name powerOperationsController |
| 7 | * @version 0.1.0 |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function (angular) { |
| 11 | 'use strict'; |
| 12 | |
| 13 | angular |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 14 | .module('app.serverControl') |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 15 | .controller('powerOperationsController', [ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 16 | '$scope', |
| 17 | 'APIUtils', |
| 18 | 'dataService', |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 19 | 'Constants', |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 20 | '$timeout', |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 21 | '$interval', |
| 22 | '$q', |
| 23 | function($scope, APIUtils, dataService, Constants, $timeout, $interval, $q){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 24 | $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 Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 31 | $scope.loading = false; |
| 32 | |
| 33 | var pollChassisStatusTimer = undefined; |
| 34 | var pollHostStatusTimer = undefined; |
| 35 | var pollStartTime = null; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 36 | |
| 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 Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 44 | //@TODO: show progress or set class orange |
| 45 | APIUtils[method]().then(function(response){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 46 | //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 Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 67 | |
| 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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 116 | $scope.warmReboot = function(){ |
| 117 | //@TODO:show progress |
| 118 | dataService.setBootingState(); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 119 | APIUtils.hostReboot().then(function(response){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 120 | if(response){ |
Gunnar Mills | cacfa6d | 2018-02-07 12:09:02 -0600 | [diff] [blame] | 121 | dataService.setPowerOnState(); |
| 122 | }else{ |
| 123 | //@TODO:hide progress & show error message |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 124 | } |
| 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 Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 144 | $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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 164 | }; |
| 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 Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame^] | 175 | APIUtils.hostPowerOff().then(function(response){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 176 | 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 Mills | 3aa8b53 | 2018-02-06 15:42:29 -0600 | [diff] [blame] | 192 | //@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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 200 | }; |
| 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); |