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', 15 function( 16 $scope, APIUtils, dataService, Constants, $timeout, $interval, 17 $interpolate, $q) { 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 dataService.activateErrorModal({ 68 title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED, 69 description: error.statusText ? 70 $interpolate( 71 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( 72 {status: error.status, description: error.statusText}) : 73 error 74 }); 75 $scope.loading = false; 76 }); 77 }; 78 $scope.powerOnConfirm = function() { 79 if ($scope.confirm) { 80 return; 81 } 82 $scope.confirm = true; 83 $scope.power_confirm = true; 84 }; 85 86 function pollChassisStatusTillOff() { 87 var deferred = $q.defer(); 88 pollChassisStatusTimer = $interval(function() { 89 var now = new Date(); 90 if ((now.getTime() - pollStartTime.getTime()) >= 91 Constants.TIMEOUT.CHASSIS_OFF) { 92 $interval.cancel(pollChassisStatusTimer); 93 pollChassisStatusTimer = undefined; 94 deferred.reject( 95 new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT)); 96 } 97 APIUtils.getChassisState() 98 .then(function(state) { 99 if (state === Constants.CHASSIS_POWER_STATE.off_code) { 100 $interval.cancel(pollChassisStatusTimer); 101 pollChassisStatusTimer = undefined; 102 deferred.resolve(state); 103 } 104 }) 105 .catch(function(error) { 106 $interval.cancel(pollChassisStatusTimer); 107 pollChassisStatusTimer = undefined; 108 deferred.reject(error); 109 }); 110 }, Constants.POLL_INTERVALS.POWER_OP); 111 112 return deferred.promise; 113 } 114 $scope.warmReboot = function() { 115 $scope.loading = true; 116 dataService.setUnreachableState(); 117 APIUtils.hostReboot() 118 .then(function(response) { 119 return response; 120 }) 121 .then(function(lastStatus) { 122 return APIUtils.pollHostStatusTilReboot(); 123 }) 124 .then(function(hostState) { 125 $scope.loading = false; 126 }) 127 .catch(function(error) { 128 dataService.activateErrorModal({ 129 title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED, 130 description: error.statusText ? 131 $interpolate( 132 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( 133 {status: error.status, description: error.statusText}) : 134 error 135 }); 136 $scope.loading = false; 137 }); 138 }; 139 $scope.testState = function() { 140 $timeout(function() { 141 dataService.setPowerOffState(); 142 $timeout(function() { 143 dataService.setPowerOnState(); 144 }, 2000); 145 }, 1000); 146 }; 147 $scope.warmRebootConfirm = function() { 148 if ($scope.confirm) { 149 return; 150 } 151 $scope.confirm = true; 152 $scope.warmboot_confirm = true; 153 }; 154 155 $scope.coldReboot = function() { 156 $scope.loading = true; 157 dataService.setUnreachableState(); 158 APIUtils.chassisPowerOff() 159 .then(function(state) { 160 return state; 161 }) 162 .then(function(lastState) { 163 pollStartTime = new Date(); 164 return pollChassisStatusTillOff(); 165 }) 166 .then(function(chassisState) { 167 return APIUtils.hostPowerOn().then(function(hostState) { 168 return hostState; 169 }); 170 }) 171 .then(function(hostState) { 172 return APIUtils.pollHostStatusTillOn(); 173 }) 174 .then(function(state) { 175 $scope.loading = false; 176 }) 177 .catch(function(error) { 178 dataService.activateErrorModal({ 179 title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED, 180 description: error.statusText ? 181 $interpolate( 182 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( 183 {status: error.status, description: error.statusText}) : 184 error 185 }); 186 $scope.loading = false; 187 }); 188 }; 189 $scope.coldRebootConfirm = function() { 190 if ($scope.confirm) { 191 return; 192 } 193 $scope.confirm = true; 194 $scope.coldboot_confirm = true; 195 }; 196 197 $scope.orderlyShutdown = function() { 198 $scope.loading = true; 199 dataService.setUnreachableState(); 200 APIUtils.hostPowerOff() 201 .then(function(response) { 202 return response; 203 }) 204 .then(function(lastStatus) { 205 return APIUtils.pollHostStatusTillOff(); 206 }) 207 .then(function(hostState) { 208 pollStartTime = new Date(); 209 return pollChassisStatusTillOff(); 210 }) 211 .then(function(chassisState) { 212 $scope.loading = false; 213 }) 214 .catch(function(error) { 215 dataService.activateErrorModal({ 216 title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED, 217 description: error.statusText ? 218 $interpolate( 219 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( 220 {status: error.status, description: error.statusText}) : 221 error 222 }); 223 $scope.loading = false; 224 }); 225 }; 226 $scope.orderlyShutdownConfirm = function() { 227 if ($scope.confirm) { 228 return; 229 } 230 $scope.confirm = true; 231 $scope.orderly_confirm = true; 232 }; 233 234 $scope.immediateShutdown = function() { 235 $scope.loading = true; 236 dataService.setUnreachableState(); 237 APIUtils.chassisPowerOff() 238 .then(function(response) { 239 return response; 240 }) 241 .then(function(lastStatus) { 242 pollStartTime = new Date(); 243 return pollChassisStatusTillOff(); 244 }) 245 .then(function(chassisState) { 246 dataService.setPowerOffState(); 247 $scope.loading = false; 248 }) 249 .catch(function(error) { 250 dataService.activateErrorModal({ 251 title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED, 252 description: error.statusText ? 253 $interpolate( 254 Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)( 255 {status: error.status, description: error.statusText}) : 256 error 257 }); 258 $scope.loading = false; 259 }); 260 }; 261 $scope.immediateShutdownConfirm = function() { 262 if ($scope.confirm) { 263 return; 264 } 265 $scope.confirm = true; 266 $scope.immediately_confirm = true; 267 }; 268 } 269 ]); 270})(angular); 271