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