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', |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 14 | '$interpolate', '$q', 'ngToast', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 15 | function( |
| 16 | $scope, APIUtils, dataService, Constants, $timeout, $interval, |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 17 | $interpolate, $q, ngToast) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 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) { |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 67 | ngToast.danger(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 68 | $scope.loading = false; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 69 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 70 | }; |
| 71 | $scope.powerOnConfirm = function() { |
| 72 | if ($scope.confirm) { |
| 73 | return; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 74 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 75 | $scope.confirm = true; |
| 76 | $scope.power_confirm = true; |
| 77 | }; |
CamVan Nguyen | d80c280 | 2018-04-17 19:25:16 -0500 | [diff] [blame] | 78 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 79 | function pollChassisStatusTillOff() { |
| 80 | var deferred = $q.defer(); |
| 81 | pollChassisStatusTimer = $interval(function() { |
| 82 | var now = new Date(); |
| 83 | if ((now.getTime() - pollStartTime.getTime()) >= |
| 84 | Constants.TIMEOUT.CHASSIS_OFF) { |
| 85 | $interval.cancel(pollChassisStatusTimer); |
| 86 | pollChassisStatusTimer = undefined; |
| 87 | deferred.reject( |
| 88 | new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT)); |
| 89 | } |
| 90 | APIUtils.getChassisState() |
| 91 | .then(function(state) { |
| 92 | if (state === Constants.CHASSIS_POWER_STATE.off_code) { |
| 93 | $interval.cancel(pollChassisStatusTimer); |
| 94 | pollChassisStatusTimer = undefined; |
| 95 | deferred.resolve(state); |
| 96 | } |
| 97 | }) |
| 98 | .catch(function(error) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 99 | $interval.cancel(pollChassisStatusTimer); |
| 100 | pollChassisStatusTimer = undefined; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 101 | deferred.reject(error); |
| 102 | }); |
| 103 | }, Constants.POLL_INTERVALS.POWER_OP); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 104 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 105 | return deferred.promise; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 106 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 107 | $scope.warmReboot = function() { |
| 108 | $scope.loading = true; |
| 109 | dataService.setUnreachableState(); |
| 110 | APIUtils.hostReboot() |
| 111 | .then(function(response) { |
| 112 | return response; |
| 113 | }) |
| 114 | .then(function(lastStatus) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 115 | return APIUtils.pollHostStatusTilReboot(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 116 | }) |
| 117 | .then(function(hostState) { |
| 118 | $scope.loading = false; |
| 119 | }) |
| 120 | .catch(function(error) { |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 121 | ngToast.danger(Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 122 | $scope.loading = false; |
| 123 | }); |
| 124 | }; |
| 125 | $scope.testState = function() { |
| 126 | $timeout(function() { |
| 127 | dataService.setPowerOffState(); |
| 128 | $timeout(function() { |
| 129 | dataService.setPowerOnState(); |
| 130 | }, 2000); |
| 131 | }, 1000); |
| 132 | }; |
| 133 | $scope.warmRebootConfirm = function() { |
| 134 | if ($scope.confirm) { |
| 135 | return; |
| 136 | } |
| 137 | $scope.confirm = true; |
| 138 | $scope.warmboot_confirm = true; |
| 139 | }; |
| 140 | |
| 141 | $scope.coldReboot = function() { |
| 142 | $scope.loading = true; |
| 143 | dataService.setUnreachableState(); |
| 144 | APIUtils.chassisPowerOff() |
| 145 | .then(function(state) { |
| 146 | return state; |
| 147 | }) |
| 148 | .then(function(lastState) { |
| 149 | pollStartTime = new Date(); |
| 150 | return pollChassisStatusTillOff(); |
| 151 | }) |
| 152 | .then(function(chassisState) { |
| 153 | return APIUtils.hostPowerOn().then(function(hostState) { |
| 154 | return hostState; |
| 155 | }); |
| 156 | }) |
| 157 | .then(function(hostState) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 158 | return APIUtils.pollHostStatusTillOn(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 159 | }) |
| 160 | .then(function(state) { |
| 161 | $scope.loading = false; |
| 162 | }) |
| 163 | .catch(function(error) { |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 164 | ngToast.danger(Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 165 | $scope.loading = false; |
| 166 | }); |
| 167 | }; |
| 168 | $scope.coldRebootConfirm = function() { |
| 169 | if ($scope.confirm) { |
| 170 | return; |
| 171 | } |
| 172 | $scope.confirm = true; |
| 173 | $scope.coldboot_confirm = true; |
| 174 | }; |
| 175 | |
| 176 | $scope.orderlyShutdown = function() { |
| 177 | $scope.loading = true; |
| 178 | dataService.setUnreachableState(); |
| 179 | APIUtils.hostPowerOff() |
| 180 | .then(function(response) { |
| 181 | return response; |
| 182 | }) |
| 183 | .then(function(lastStatus) { |
beccabroek | c3abaa9 | 2018-08-14 13:47:18 -0500 | [diff] [blame] | 184 | return APIUtils.pollHostStatusTillOff(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 185 | }) |
| 186 | .then(function(hostState) { |
| 187 | pollStartTime = new Date(); |
| 188 | return pollChassisStatusTillOff(); |
| 189 | }) |
| 190 | .then(function(chassisState) { |
| 191 | $scope.loading = false; |
| 192 | }) |
| 193 | .catch(function(error) { |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 194 | ngToast.danger( |
| 195 | Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 196 | $scope.loading = false; |
| 197 | }); |
| 198 | }; |
| 199 | $scope.orderlyShutdownConfirm = function() { |
| 200 | if ($scope.confirm) { |
| 201 | return; |
| 202 | } |
| 203 | $scope.confirm = true; |
| 204 | $scope.orderly_confirm = true; |
| 205 | }; |
| 206 | |
| 207 | $scope.immediateShutdown = function() { |
| 208 | $scope.loading = true; |
| 209 | dataService.setUnreachableState(); |
| 210 | APIUtils.chassisPowerOff() |
| 211 | .then(function(response) { |
| 212 | return response; |
| 213 | }) |
| 214 | .then(function(lastStatus) { |
| 215 | pollStartTime = new Date(); |
| 216 | return pollChassisStatusTillOff(); |
| 217 | }) |
| 218 | .then(function(chassisState) { |
| 219 | dataService.setPowerOffState(); |
| 220 | $scope.loading = false; |
| 221 | }) |
| 222 | .catch(function(error) { |
beccabroek | 92d13b6 | 2019-01-08 14:24:29 -0600 | [diff] [blame^] | 223 | ngToast.danger( |
| 224 | Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 225 | $scope.loading = false; |
| 226 | }); |
| 227 | }; |
| 228 | $scope.immediateShutdownConfirm = function() { |
| 229 | if ($scope.confirm) { |
| 230 | return; |
| 231 | } |
| 232 | $scope.confirm = true; |
| 233 | $scope.immediately_confirm = true; |
| 234 | }; |
| 235 | } |
| 236 | ]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 237 | })(angular); |