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