1/** 2 * Controller for power-operations 3 * 4 * @module app/serverControl 5 * @exports powerOperationsController 6 * @name powerOperationsController 7 */ 8 9window.angular && (function(angular) { 10 'use strict'; 11 12 angular.module('app.serverControl').controller('powerOperationsController', [ 13 '$scope', 'APIUtils', 'dataService', 'Constants', '$timeout', '$interval', 14 '$interpolate', '$q', 'toastService', 15 function( 16 $scope, APIUtils, dataService, Constants, $timeout, $interval, 17 $interpolate, $q, toastService) { 18 $scope.dataService = dataService; 19 $scope.confirm = false; 20 $scope.confirmWarmReboot = false; 21 $scope.confirmColdReboot = false; 22 $scope.confirmOrderlyShutdown = false; 23 $scope.confirmImmediateShutdown = false; 24 $scope.loading = true; 25 26 var pollChassisStatusTimer = undefined; 27 var pollStartTime = null; 28 29 //@TODO: call api and get proper state 30 31 APIUtils.getLastPowerTime() 32 .then( 33 function(data) { 34 if (data.data == 0) { 35 $scope.powerTime = 'not available'; 36 } else { 37 $scope.powerTime = data.data; 38 } 39 }, 40 function(error) { 41 console.log(JSON.stringify(error)); 42 }) 43 .finally(function() { 44 $scope.loading = false; 45 }); 46 47 $scope.toggleState = function() { 48 dataService.server_state = 49 (dataService.server_state == 'Running') ? 'Off' : 'Running'; 50 }; 51 52 $scope.powerOn = function() { 53 $scope.loading = true; 54 dataService.setUnreachableState(); 55 APIUtils.hostPowerOn() 56 .then(function(response) { 57 return response; 58 }) 59 .then(function(lastStatus) { 60 return APIUtils.pollHostStatusTillOn(); 61 }) 62 .then(function(hostState) { 63 $scope.loading = false; 64 }) 65 .catch(function(error) { 66 toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED); 67 $scope.loading = false; 68 }); 69 }; 70 71 function pollChassisStatusTillOff() { 72 var deferred = $q.defer(); 73 pollChassisStatusTimer = $interval(function() { 74 var now = new Date(); 75 if ((now.getTime() - pollStartTime.getTime()) >= 76 Constants.TIMEOUT.CHASSIS_OFF) { 77 $interval.cancel(pollChassisStatusTimer); 78 pollChassisStatusTimer = undefined; 79 deferred.reject( 80 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT)); 81 } 82 APIUtils.getChassisState() 83 .then(function(state) { 84 if (state === Constants.CHASSIS_POWER_STATE.off_code) { 85 $interval.cancel(pollChassisStatusTimer); 86 pollChassisStatusTimer = undefined; 87 deferred.resolve(state); 88 } 89 }) 90 .catch(function(error) { 91 $interval.cancel(pollChassisStatusTimer); 92 pollChassisStatusTimer = undefined; 93 deferred.reject(error); 94 }); 95 }, Constants.POLL_INTERVALS.POWER_OP); 96 97 return deferred.promise; 98 } 99 $scope.warmReboot = function() { 100 $scope.loading = true; 101 dataService.setUnreachableState(); 102 APIUtils.hostReboot() 103 .then(function(response) { 104 return response; 105 }) 106 .then(function(lastStatus) { 107 return APIUtils.pollHostStatusTilReboot(); 108 }) 109 .then(function(hostState) { 110 $scope.loading = false; 111 }) 112 .catch(function(error) { 113 toastService.error( 114 Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED); 115 $scope.loading = false; 116 }); 117 }; 118 $scope.testState = function() { 119 $timeout(function() { 120 dataService.setPowerOffState(); 121 $timeout(function() { 122 dataService.setPowerOnState(); 123 }, 2000); 124 }, 1000); 125 }; 126 $scope.warmRebootConfirm = function() { 127 if ($scope.confirm) { 128 return; 129 } 130 $scope.confirm = true; 131 $scope.confirmWarmReboot = true; 132 }; 133 134 $scope.coldReboot = function() { 135 $scope.loading = true; 136 dataService.setUnreachableState(); 137 APIUtils.chassisPowerOff() 138 .then(function(state) { 139 return state; 140 }) 141 .then(function(lastState) { 142 pollStartTime = new Date(); 143 return pollChassisStatusTillOff(); 144 }) 145 .then(function(chassisState) { 146 return APIUtils.hostPowerOn().then(function(hostState) { 147 return hostState; 148 }); 149 }) 150 .then(function(hostState) { 151 return APIUtils.pollHostStatusTillOn(); 152 }) 153 .then(function(state) { 154 $scope.loading = false; 155 }) 156 .catch(function(error) { 157 toastService.error( 158 Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED); 159 $scope.loading = false; 160 }); 161 }; 162 $scope.coldRebootConfirm = function() { 163 if ($scope.confirm) { 164 return; 165 } 166 $scope.confirm = true; 167 $scope.confirmColdReboot = true; 168 }; 169 170 $scope.orderlyShutdown = function() { 171 $scope.loading = true; 172 dataService.setUnreachableState(); 173 APIUtils.hostPowerOff() 174 .then(function(response) { 175 return response; 176 }) 177 .then(function(lastStatus) { 178 return APIUtils.pollHostStatusTillOff(); 179 }) 180 .then(function(hostState) { 181 pollStartTime = new Date(); 182 return pollChassisStatusTillOff(); 183 }) 184 .then(function(chassisState) { 185 $scope.loading = false; 186 }) 187 .catch(function(error) { 188 toastService.error( 189 Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED); 190 $scope.loading = false; 191 }); 192 }; 193 $scope.orderlyShutdownConfirm = function() { 194 if ($scope.confirm) { 195 return; 196 } 197 $scope.confirm = true; 198 $scope.confirmOrderlyShutdown = true; 199 }; 200 201 $scope.immediateShutdown = function() { 202 $scope.loading = true; 203 dataService.setUnreachableState(); 204 APIUtils.chassisPowerOff() 205 .then(function(response) { 206 return response; 207 }) 208 .then(function(lastStatus) { 209 pollStartTime = new Date(); 210 return pollChassisStatusTillOff(); 211 }) 212 .then(function(chassisState) { 213 dataService.setPowerOffState(); 214 $scope.loading = false; 215 }) 216 .catch(function(error) { 217 toastService.error( 218 Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED); 219 $scope.loading = false; 220 }); 221 }; 222 $scope.immediateShutdownConfirm = function() { 223 if ($scope.confirm) { 224 return; 225 } 226 $scope.confirm = true; 227 $scope.confirmImmediateShutdown = true; 228 }; 229 } 230 ]); 231})(angular); 232