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