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 |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | window.angular && (function (angular) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 13 | .module('app.serverControl') |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 14 | .controller('powerOperationsController', [ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 15 | '$scope', |
| 16 | 'APIUtils', |
| 17 | 'dataService', |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 18 | 'Constants', |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 19 | '$timeout', |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 20 | '$interval', |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 21 | '$interpolate', |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 22 | '$q', |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 23 | function($scope, APIUtils, dataService, Constants, $timeout, |
| 24 | $interval, $interpolate, $q){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 25 | $scope.dataService = dataService; |
| 26 | $scope.confirm = false; |
| 27 | $scope.power_confirm = false; |
| 28 | $scope.warmboot_confirm = false; |
| 29 | $scope.coldboot_confirm = false; |
| 30 | $scope.orderly_confirm = false; |
| 31 | $scope.immediately_confirm = false; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 32 | $scope.loading = false; |
| 33 | |
| 34 | var pollChassisStatusTimer = undefined; |
| 35 | var pollHostStatusTimer = undefined; |
| 36 | var pollStartTime = null; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 37 | |
| 38 | //@TODO: call api and get proper state |
| 39 | $scope.toggleState = function(){ |
| 40 | dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running'; |
| 41 | } |
| 42 | |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 43 | $scope.powerOn = function(){ |
| 44 | $scope.loading = true; |
| 45 | dataService.setUnreachableState(); |
| 46 | APIUtils.hostPowerOn().then(function(response){ |
| 47 | return response; |
| 48 | }).then(function(lastStatus) { |
| 49 | pollStartTime = new Date(); |
| 50 | return pollHostStatusTillOn(); |
| 51 | }).then(function(hostState) { |
| 52 | $scope.loading = false; |
| 53 | }).catch(function(error){ |
| 54 | dataService.activateErrorModal( |
| 55 | {title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED, |
| 56 | description: error.statusText ? |
| 57 | $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( |
| 58 | {status: error.status, description: error.statusText}) : |
| 59 | error }); |
| 60 | $scope.loading = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 61 | }); |
| 62 | } |
| 63 | $scope.powerOnConfirm = function(){ |
| 64 | if($scope.confirm) { |
| 65 | return; |
| 66 | } |
| 67 | $scope.confirm = true; |
| 68 | $scope.power_confirm = true; |
| 69 | }; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 70 | |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 71 | function setHostState(state){ |
| 72 | if(state == Constants.HOST_STATE_TEXT.off_code){ |
| 73 | dataService.setPowerOffState(); |
| 74 | }else if(state == Constants.HOST_STATE_TEXT.on_code){ |
| 75 | dataService.setPowerOnState(); |
| 76 | }else{ |
| 77 | dataService.setErrorState(); |
| 78 | } |
| 79 | } |
| 80 | |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 81 | function pollChassisStatusTillOff(){ |
| 82 | var deferred = $q.defer(); |
| 83 | pollChassisStatusTimer = $interval(function(){ |
| 84 | var now = new Date(); |
| 85 | if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF){ |
| 86 | $interval.cancel(pollChassisStatusTimer); |
| 87 | pollChassisStatusTimer = undefined; |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 88 | deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT)); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 89 | } |
| 90 | APIUtils.getChassisState().then(function(state){ |
| 91 | if(state === Constants.CHASSIS_POWER_STATE.off_code){ |
| 92 | $interval.cancel(pollChassisStatusTimer); |
| 93 | pollChassisStatusTimer = undefined; |
| 94 | deferred.resolve(state); |
| 95 | } |
| 96 | }).catch(function(error){ |
| 97 | $interval.cancel(pollChassisStatusTimer); |
| 98 | pollChassisStatusTimer = undefined; |
| 99 | deferred.reject(error); |
| 100 | }); |
| 101 | }, Constants.POLL_INTERVALS.POWER_OP); |
| 102 | |
| 103 | return deferred.promise; |
| 104 | } |
| 105 | function pollHostStatusTillOn(){ |
| 106 | var deferred = $q.defer(); |
| 107 | pollHostStatusTimer = $interval(function(){ |
| 108 | var now = new Date(); |
| 109 | if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON){ |
| 110 | $interval.cancel(pollHostStatusTimer); |
| 111 | pollHostStatusTimer = undefined; |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 112 | deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT)); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 113 | } |
| 114 | APIUtils.getHostState().then(function(state){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 115 | setHostState(state); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 116 | if(state === Constants.HOST_STATE_TEXT.on_code){ |
| 117 | $interval.cancel(pollHostStatusTimer); |
| 118 | pollHostStatusTimer = undefined; |
| 119 | deferred.resolve(state); |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 120 | }else if(state === Constants.HOST_STATE_TEXT.error_code){ |
| 121 | $interval.cancel(pollHostStatusTimer); |
| 122 | pollHostStatusTimer = undefined; |
| 123 | deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED)); |
| 124 | } |
| 125 | }).catch(function(error){ |
| 126 | $interval.cancel(pollHostStatusTimer); |
| 127 | pollHostStatusTimer = undefined; |
| 128 | deferred.reject(error); |
| 129 | }); |
| 130 | }, Constants.POLL_INTERVALS.POWER_OP); |
| 131 | |
| 132 | return deferred.promise; |
| 133 | } |
| 134 | function pollHostStatusTillOff(){ |
| 135 | var deferred = $q.defer(); |
| 136 | pollHostStatusTimer = $interval(function(){ |
| 137 | var now = new Date(); |
| 138 | if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_OFF){ |
| 139 | $interval.cancel(pollHostStatusTimer); |
| 140 | pollHostStatusTimer = undefined; |
| 141 | deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT)); |
| 142 | } |
| 143 | APIUtils.getHostState().then(function(state){ |
| 144 | setHostState(state); |
| 145 | if(state === Constants.HOST_STATE_TEXT.off_code){ |
| 146 | $interval.cancel(pollHostStatusTimer); |
| 147 | pollHostStatusTimer = undefined; |
| 148 | deferred.resolve(state); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 149 | } |
| 150 | }).catch(function(error){ |
| 151 | $interval.cancel(pollHostStatusTimer); |
| 152 | pollHostStatusTimer = undefined; |
| 153 | deferred.reject(error); |
| 154 | }); |
| 155 | }, Constants.POLL_INTERVALS.POWER_OP); |
| 156 | |
| 157 | return deferred.promise; |
| 158 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 159 | $scope.warmReboot = function(){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 160 | $scope.loading = true; |
| 161 | dataService.setUnreachableState(); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 162 | APIUtils.hostReboot().then(function(response){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 163 | return response; |
| 164 | }).then(function(lastStatus) { |
| 165 | pollStartTime = new Date(); |
| 166 | return pollHostStatusTillOff(); |
| 167 | }).then(function(hostState) { |
| 168 | pollStartTime = new Date(); |
| 169 | return pollHostStatusTillOn(); |
| 170 | }).then(function(hostState) { |
| 171 | $scope.loading = false; |
| 172 | }).catch(function(error){ |
| 173 | dataService.activateErrorModal( |
| 174 | {title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED, |
| 175 | description: error.statusText ? |
| 176 | $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( |
| 177 | {status: error.status, description: error.statusText}) : |
| 178 | error }); |
| 179 | $scope.loading = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 180 | }); |
| 181 | }; |
| 182 | $scope.testState = function(){ |
| 183 | $timeout(function(){ |
| 184 | dataService.setPowerOffState(); |
| 185 | $timeout(function(){ |
| 186 | dataService.setPowerOnState(); |
| 187 | }, 2000); |
| 188 | }, 1000); |
| 189 | }; |
| 190 | $scope.warmRebootConfirm = function(){ |
| 191 | if($scope.confirm) { |
| 192 | return; |
| 193 | } |
| 194 | $scope.confirm = true; |
| 195 | $scope.warmboot_confirm = true; |
| 196 | }; |
| 197 | |
| 198 | $scope.coldReboot = function(){ |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 199 | $scope.loading = true; |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 200 | dataService.setUnreachableState(); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 201 | APIUtils.chassisPowerOff().then(function(state){ |
| 202 | return state; |
| 203 | }).then(function(lastState) { |
| 204 | pollStartTime = new Date(); |
| 205 | return pollChassisStatusTillOff(); |
| 206 | }).then(function(chassisState) { |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 207 | return APIUtils.hostPowerOn().then(function(hostState){ |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 208 | return hostState; |
| 209 | }) |
| 210 | }).then(function(hostState) { |
| 211 | pollStartTime = new Date(); |
| 212 | return pollHostStatusTillOn(); |
| 213 | }).then(function(state) { |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 214 | $scope.loading = false; |
| 215 | }).catch(function(error){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 216 | dataService.activateErrorModal( |
| 217 | {title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED, |
| 218 | description: error.statusText ? |
| 219 | $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( |
| 220 | {status: error.status, description: error.statusText}) : |
| 221 | error }); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 222 | $scope.loading = false; |
| 223 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 224 | }; |
| 225 | $scope.coldRebootConfirm = function(){ |
| 226 | if($scope.confirm) { |
| 227 | return; |
| 228 | } |
| 229 | $scope.confirm = true; |
| 230 | $scope.coldboot_confirm = true; |
| 231 | }; |
| 232 | |
| 233 | $scope.orderlyShutdown = function(){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 234 | $scope.loading = true; |
| 235 | dataService.setUnreachableState(); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 236 | APIUtils.hostPowerOff().then(function(response){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 237 | return response; |
| 238 | }).then(function(lastStatus) { |
| 239 | pollStartTime = new Date(); |
| 240 | return pollHostStatusTillOff() |
| 241 | }).then(function(hostState) { |
| 242 | pollStartTime = new Date(); |
| 243 | return pollChassisStatusTillOff(); |
| 244 | }).then(function(chassisState) { |
| 245 | $scope.loading = false; |
| 246 | }).catch(function(error){ |
| 247 | dataService.activateErrorModal( |
| 248 | {title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED, |
| 249 | description: error.statusText ? |
| 250 | $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( |
| 251 | {status: error.status, description: error.statusText}) : |
| 252 | error }); |
| 253 | $scope.loading = false; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 254 | }); |
| 255 | }; |
| 256 | $scope.orderlyShutdownConfirm = function(){ |
| 257 | if($scope.confirm) { |
| 258 | return; |
| 259 | } |
| 260 | $scope.confirm = true; |
| 261 | $scope.orderly_confirm = true; |
| 262 | }; |
| 263 | |
| 264 | $scope.immediateShutdown = function(){ |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 265 | $scope.loading = true; |
| 266 | dataService.setUnreachableState(); |
| 267 | APIUtils.chassisPowerOff().then(function(response){ |
| 268 | return response; |
| 269 | }).then(function(lastStatus) { |
| 270 | pollStartTime = new Date(); |
| 271 | return pollChassisStatusTillOff(); |
| 272 | }).then(function(chassisState) { |
| 273 | dataService.setPowerOffState(); |
| 274 | $scope.loading = false; |
| 275 | }).catch(function(error){ |
| 276 | dataService.activateErrorModal( |
| 277 | {title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED, |
| 278 | description: error.statusText ? |
| 279 | $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( |
| 280 | {status: error.status, description: error.statusText}) : |
| 281 | error }); |
| 282 | $scope.loading = false; |
Gunnar Mills | 3aa8b53 | 2018-02-06 15:42:29 -0600 | [diff] [blame] | 283 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 284 | }; |
| 285 | $scope.immediateShutdownConfirm = function(){ |
| 286 | if($scope.confirm) { |
| 287 | return; |
| 288 | } |
| 289 | $scope.confirm = true; |
| 290 | $scope.immediately_confirm = true; |
| 291 | }; |
| 292 | } |
| 293 | ] |
| 294 | ); |
| 295 | |
| 296 | })(angular); |