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