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', |
Gunnar Mills | a1b5fb6 | 2019-05-17 16:36:10 -0500 | [diff] [blame] | 14 | '$q', 'toastService', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 15 | function( |
Gunnar Mills | a1b5fb6 | 2019-05-17 16:36:10 -0500 | [diff] [blame] | 16 | $scope, APIUtils, dataService, Constants, $timeout, $interval, $q, |
| 17 | toastService) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 18 | $scope.dataService = dataService; |
Gunnar Mills | aa4734d | 2019-05-17 15:56:18 -0500 | [diff] [blame] | 19 | // Is a || of the other 4 "confirm" variables to ensure only |
| 20 | // one confirm is shown at a time. |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 21 | $scope.confirm = false; |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 22 | $scope.confirmWarmReboot = false; |
| 23 | $scope.confirmColdReboot = false; |
| 24 | $scope.confirmOrderlyShutdown = false; |
| 25 | $scope.confirmImmediateShutdown = false; |
Gunnar Mills | 6add832 | 2018-09-05 15:16:12 -0500 | [diff] [blame] | 26 | $scope.loading = true; |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 27 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 28 | var pollChassisStatusTimer = undefined; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 29 | var pollStartTime = null; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 30 | |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 31 | /** |
| 32 | * Checks the host status provided by the dataService using an |
| 33 | * interval timer |
| 34 | * @param {string} statusType : host status type to check for |
| 35 | * @param {number} timeout : timeout limit |
| 36 | * @param {string} error : error message |
| 37 | * @returns {Promise} : returns a deferred promise that will be fulfilled |
| 38 | * if the status is met or be rejected if the timeout is reached |
| 39 | */ |
| 40 | var checkHostStatus = (statusType, timeout, error = 'Time out.') => { |
| 41 | const deferred = $q.defer(); |
| 42 | const start = new Date(); |
| 43 | const checkHostStatusInverval = $interval(() => { |
| 44 | let now = new Date(); |
| 45 | let timePassed = now.getTime() - start.getTime(); |
| 46 | if (timePassed > timeout) { |
| 47 | deferred.reject(error); |
| 48 | $interval.cancel(checkHostStatusInverval); |
| 49 | } |
| 50 | if (dataService.server_state === statusType) { |
| 51 | deferred.resolve(); |
| 52 | $interval.cancel(checkHostStatusInverval); |
| 53 | } |
| 54 | }, Constants.POLL_INTERVALS.POWER_OP); |
| 55 | return deferred.promise; |
| 56 | }; |
| 57 | |
Gunnar Mills | 6add832 | 2018-09-05 15:16:12 -0500 | [diff] [blame] | 58 | APIUtils.getLastPowerTime() |
| 59 | .then( |
| 60 | function(data) { |
| 61 | if (data.data == 0) { |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 62 | $scope.powerTime = 'not available'; |
Gunnar Mills | 6add832 | 2018-09-05 15:16:12 -0500 | [diff] [blame] | 63 | } else { |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 64 | $scope.powerTime = data.data; |
Gunnar Mills | 6add832 | 2018-09-05 15:16:12 -0500 | [diff] [blame] | 65 | } |
| 66 | }, |
| 67 | function(error) { |
| 68 | console.log(JSON.stringify(error)); |
| 69 | }) |
| 70 | .finally(function() { |
| 71 | $scope.loading = false; |
beccabroek | 5674425 | 2018-08-03 11:25:11 -0500 | [diff] [blame] | 72 | }); |
Gunnar Mills | 6add832 | 2018-09-05 15:16:12 -0500 | [diff] [blame] | 73 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 74 | $scope.toggleState = function() { |
| 75 | dataService.server_state = |
| 76 | (dataService.server_state == 'Running') ? 'Off' : 'Running'; |
| 77 | }; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 78 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 79 | $scope.powerOn = function() { |
| 80 | $scope.loading = true; |
| 81 | dataService.setUnreachableState(); |
| 82 | APIUtils.hostPowerOn() |
| 83 | .then(function(response) { |
| 84 | return response; |
| 85 | }) |
| 86 | .then(function(lastStatus) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 87 | return APIUtils.pollHostStatusTillOn(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 88 | }) |
| 89 | .then(function(hostState) { |
| 90 | $scope.loading = false; |
| 91 | }) |
| 92 | .catch(function(error) { |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 93 | toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 94 | $scope.loading = false; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 95 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 96 | }; |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 98 | function pollChassisStatusTillOff() { |
| 99 | var deferred = $q.defer(); |
| 100 | pollChassisStatusTimer = $interval(function() { |
| 101 | var now = new Date(); |
| 102 | if ((now.getTime() - pollStartTime.getTime()) >= |
| 103 | Constants.TIMEOUT.CHASSIS_OFF) { |
| 104 | $interval.cancel(pollChassisStatusTimer); |
| 105 | pollChassisStatusTimer = undefined; |
| 106 | deferred.reject( |
| 107 | new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT)); |
| 108 | } |
| 109 | APIUtils.getChassisState() |
| 110 | .then(function(state) { |
| 111 | if (state === Constants.CHASSIS_POWER_STATE.off_code) { |
| 112 | $interval.cancel(pollChassisStatusTimer); |
| 113 | pollChassisStatusTimer = undefined; |
| 114 | deferred.resolve(state); |
| 115 | } |
| 116 | }) |
| 117 | .catch(function(error) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 118 | $interval.cancel(pollChassisStatusTimer); |
| 119 | pollChassisStatusTimer = undefined; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 120 | deferred.reject(error); |
| 121 | }); |
| 122 | }, Constants.POLL_INTERVALS.POWER_OP); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 123 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 124 | return deferred.promise; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 125 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 126 | $scope.warmReboot = function() { |
| 127 | $scope.loading = true; |
| 128 | dataService.setUnreachableState(); |
| 129 | APIUtils.hostReboot() |
| 130 | .then(function(response) { |
| 131 | return response; |
| 132 | }) |
| 133 | .then(function(lastStatus) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 134 | return APIUtils.pollHostStatusTilReboot(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 135 | }) |
| 136 | .then(function(hostState) { |
| 137 | $scope.loading = false; |
| 138 | }) |
| 139 | .catch(function(error) { |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 140 | toastService.error( |
| 141 | Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 142 | $scope.loading = false; |
| 143 | }); |
| 144 | }; |
Gunnar Mills | 834eb01 | 2019-05-17 14:58:06 -0500 | [diff] [blame] | 145 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 146 | $scope.warmRebootConfirm = function() { |
| 147 | if ($scope.confirm) { |
Gunnar Mills | aa4734d | 2019-05-17 15:56:18 -0500 | [diff] [blame] | 148 | // If another "confirm" is already shown return |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | $scope.confirm = true; |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 152 | $scope.confirmWarmReboot = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | $scope.coldReboot = function() { |
| 156 | $scope.loading = true; |
| 157 | dataService.setUnreachableState(); |
| 158 | APIUtils.chassisPowerOff() |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 159 | .then(function() { |
| 160 | return checkHostStatus( |
| 161 | Constants.HOST_STATE_TEXT.off, |
| 162 | Constants.TIMEOUT.HOST_OFF_IMMEDIATE, |
| 163 | Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 164 | }) |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 165 | .then(function() { |
| 166 | return APIUtils.hostPowerOn(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 167 | }) |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 168 | .then(function() { |
| 169 | return checkHostStatus( |
| 170 | Constants.HOST_STATE_TEXT.on, Constants.TIMEOUT.HOST_ON, |
| 171 | Constants.MESSAGES.POLL.HOST_ON_TIMEOUT); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 172 | }) |
| 173 | .catch(function(error) { |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 174 | console.log(error); |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 175 | toastService.error( |
| 176 | Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED); |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 177 | }) |
| 178 | .finally(function() { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 179 | $scope.loading = false; |
Yoshie Muranaka | afcfda7 | 2019-06-21 09:19:32 -0500 | [diff] [blame^] | 180 | }) |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 181 | }; |
| 182 | $scope.coldRebootConfirm = function() { |
| 183 | if ($scope.confirm) { |
| 184 | return; |
| 185 | } |
| 186 | $scope.confirm = true; |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 187 | $scope.confirmColdReboot = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | $scope.orderlyShutdown = function() { |
| 191 | $scope.loading = true; |
| 192 | dataService.setUnreachableState(); |
| 193 | APIUtils.hostPowerOff() |
| 194 | .then(function(response) { |
| 195 | return response; |
| 196 | }) |
| 197 | .then(function(lastStatus) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 198 | return APIUtils.pollHostStatusTillOff(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 199 | }) |
| 200 | .then(function(hostState) { |
| 201 | pollStartTime = new Date(); |
| 202 | return pollChassisStatusTillOff(); |
| 203 | }) |
| 204 | .then(function(chassisState) { |
| 205 | $scope.loading = false; |
| 206 | }) |
| 207 | .catch(function(error) { |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 208 | toastService.error( |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame] | 209 | Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 210 | $scope.loading = false; |
| 211 | }); |
| 212 | }; |
| 213 | $scope.orderlyShutdownConfirm = function() { |
| 214 | if ($scope.confirm) { |
| 215 | return; |
| 216 | } |
| 217 | $scope.confirm = true; |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 218 | $scope.confirmOrderlyShutdown = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | $scope.immediateShutdown = function() { |
| 222 | $scope.loading = true; |
| 223 | dataService.setUnreachableState(); |
| 224 | APIUtils.chassisPowerOff() |
| 225 | .then(function(response) { |
| 226 | return response; |
| 227 | }) |
| 228 | .then(function(lastStatus) { |
| 229 | pollStartTime = new Date(); |
| 230 | return pollChassisStatusTillOff(); |
| 231 | }) |
| 232 | .then(function(chassisState) { |
| 233 | dataService.setPowerOffState(); |
| 234 | $scope.loading = false; |
| 235 | }) |
| 236 | .catch(function(error) { |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 237 | toastService.error( |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame] | 238 | Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 239 | $scope.loading = false; |
| 240 | }); |
| 241 | }; |
| 242 | $scope.immediateShutdownConfirm = function() { |
| 243 | if ($scope.confirm) { |
| 244 | return; |
| 245 | } |
| 246 | $scope.confirm = true; |
Gunnar Mills | 6af5d29 | 2019-05-17 14:39:23 -0500 | [diff] [blame] | 247 | $scope.confirmImmediateShutdown = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 248 | }; |
| 249 | } |
| 250 | ]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 251 | })(angular); |