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