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', 'ngToast', 15 function( 16 $scope, APIUtils, dataService, Constants, $timeout, $interval, 17 $interpolate, $q, ngToast) { 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; 25 $scope.loading = true; 26 27 var pollChassisStatusTimer = undefined; 28 var pollStartTime = null; 29 30 //@TODO: call api and get proper state 31 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; 46 }); 47 48 $scope.toggleState = function() { 49 dataService.server_state = 50 (dataService.server_state == 'Running') ? 'Off' : 'Running'; 51 }; 52 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) { 61 return APIUtils.pollHostStatusTillOn(); 62 }) 63 .then(function(hostState) { 64 $scope.loading = false; 65 }) 66 .catch(function(error) { 67 ngToast.danger(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED); 68 $scope.loading = false; 69 }); 70 }; 71 $scope.powerOnConfirm = function() { 72 if ($scope.confirm) { 73 return; 74 } 75 $scope.confirm = true; 76 $scope.power_confirm = true; 77 }; 78 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) { 99 $interval.cancel(pollChassisStatusTimer); 100 pollChassisStatusTimer = undefined; 101 deferred.reject(error); 102 }); 103 }, Constants.POLL_INTERVALS.POWER_OP); 104 105 return deferred.promise; 106 } 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) { 115 return APIUtils.pollHostStatusTilReboot(); 116 }) 117 .then(function(hostState) { 118 $scope.loading = false; 119 }) 120 .catch(function(error) { 121 ngToast.danger(Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED); 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) { 158 return APIUtils.pollHostStatusTillOn(); 159 }) 160 .then(function(state) { 161 $scope.loading = false; 162 }) 163 .catch(function(error) { 164 ngToast.danger(Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED); 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) { 184 return APIUtils.pollHostStatusTillOff(); 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) { 194 ngToast.danger( 195 Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED); 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) { 223 ngToast.danger( 224 Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED); 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 ]); 237})(angular); 238