| /** |
| * Controller for firmware |
| * |
| * @module app/configuration |
| * @exports firmwareController |
| * @name firmwareController |
| * @version 0.1.0 |
| */ |
| |
| window.angular && (function (angular) { |
| 'use strict'; |
| |
| angular |
| .module('app.configuration') |
| .controller('firmwareController', [ |
| '$scope', |
| '$window', |
| 'APIUtils', |
| 'dataService', |
| '$location', |
| '$anchorScroll', |
| 'Constants', |
| '$interval', |
| '$q', |
| function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q) { |
| $scope.dataService = dataService; |
| |
| //Scroll to target anchor |
| $scope.gotoAnchor = function () { |
| $location.hash('upload'); |
| $anchorScroll(); |
| }; |
| |
| $scope.firmwares = []; |
| $scope.bmcActiveVersion = ""; |
| $scope.hostActiveVersion = ""; |
| $scope.display_error = false; |
| $scope.activate_confirm = false; |
| $scope.delete_image_id = ""; |
| $scope.delete_image_version = ""; |
| $scope.activate_image_id = ""; |
| $scope.activate_image_version = ""; |
| $scope.activate_image_type = ""; |
| $scope.priority_image_id = ""; |
| $scope.priority_image_version = ""; |
| $scope.priority_from = -1; |
| $scope.priority_to = -1; |
| $scope.confirm_priority = false; |
| $scope.file_empty = true; |
| $scope.uploading = false; |
| $scope.activate = { reboot: true }; |
| $scope.download_error_msg = ""; |
| |
| var pollActivationTimer = undefined; |
| |
| $scope.error = { |
| modal_title: "", |
| title: "", |
| desc: "", |
| type: "warning" |
| }; |
| |
| $scope.activateImage = function(imageId, imageVersion, imageType){ |
| $scope.activate_image_id = imageId; |
| $scope.activate_image_version = imageVersion; |
| $scope.activate_image_type = imageType; |
| $scope.activate_confirm = true; |
| } |
| |
| $scope.displayError = function(data){ |
| $scope.error = data; |
| $scope.display_error = true; |
| } |
| |
| function waitForActive(imageId){ |
| var deferred = $q.defer(); |
| var startTime = new Date(); |
| pollActivationTimer = $interval(function(){ |
| APIUtils.getActivation(imageId).then(function(state){ |
| //@TODO: display an error message if image "Failed" |
| if(((/\.Active$/).test(state.data)) || ((/\.Failed$/).test(state.data))){ |
| $interval.cancel(pollActivationTimer); |
| pollActivationTimer = undefined; |
| deferred.resolve(state); |
| } |
| }, function(error){ |
| $interval.cancel(pollActivationTimer); |
| pollActivationTimer = undefined; |
| console.log(error); |
| deferred.reject(error); |
| }); |
| var now = new Date(); |
| if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.ACTIVATION){ |
| $interval.cancel(pollActivationTimer); |
| pollActivationTimer = undefined; |
| console.log("Time out activating image, " + imageId); |
| deferred.reject("Time out. Image did not activate in allotted time."); |
| } |
| }, Constants.POLL_INTERVALS.ACTIVATION); |
| return deferred.promise; |
| } |
| |
| $scope.activateConfirmed = function(){ |
| APIUtils.activateImage($scope.activate_image_id).then(function(state){ |
| $scope.loadFirmwares(); |
| return state; |
| }, function(error){ |
| $scope.displayError({ |
| modal_title: 'Error during activation call', |
| title: 'Error during activation call', |
| desc: JSON.stringify(error.data), |
| type: 'Error' |
| }); |
| }).then(function(activationState){ |
| waitForActive($scope.activate_image_id).then(function(state){ |
| $scope.loadFirmwares(); |
| }, function(error){ |
| $scope.displayError({ |
| modal_title: 'Error during image activation', |
| title: 'Error during image activation', |
| desc: JSON.stringify(error.data), |
| type: 'Error' |
| }); |
| }).then(function(state){ |
| if($scope.activate.reboot){ |
| APIUtils.bmcReboot(function(response){}, function(error){ |
| $scope.displayError({ |
| modal_title: 'Error during BMC reboot', |
| title: 'Error during BMC reboot', |
| desc: JSON.stringify(error.data), |
| type: 'Error' |
| }); |
| }); |
| } |
| }); |
| }); |
| $scope.activate_confirm = false; |
| } |
| |
| $scope.upload = function(){ |
| if($scope.file) { |
| $scope.uploading = true; |
| APIUtils.uploadImage($scope.file).then(function(response){ |
| $scope.uploading = false; |
| if(response.status == 'error'){ |
| $scope.displayError({ |
| modal_title: response.data.description, |
| title: response.data.description, |
| desc: response.data.exception, |
| type: 'Error' |
| }); |
| }else{ |
| $scope.loadFirmwares(); |
| } |
| }); |
| } |
| } |
| |
| $scope.download = function(){ |
| $scope.download_error_msg = ""; |
| if(!$scope.download_host || !$scope.download_filename){ |
| $scope.download_error_msg = "Field is required!"; |
| return false; |
| } |
| $scope.downloading = true; |
| APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){ |
| $scope.downloading = false; |
| // TODO: refresh firmware page to display new image |
| }, function(error){ |
| $scope.downloading = false; |
| $scope.displayError({ |
| modal_title: 'Error during downloading Image', |
| title: 'Error during downloading Image', |
| desc: JSON.stringify(error), |
| type: 'Error' |
| }); |
| }); |
| } |
| |
| $scope.changePriority = function(imageId, imageVersion, from, to){ |
| $scope.priority_image_id = imageId; |
| $scope.priority_image_version = imageVersion; |
| $scope.priority_from = from; |
| $scope.priority_to = to; |
| $scope.confirm_priority = true; |
| } |
| |
| $scope.confirmChangePriority = function(){ |
| $scope.loading = true; |
| APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){ |
| $scope.loading = false; |
| if(response.status == 'error'){ |
| $scope.displayError({ |
| modal_title: response.data.description, |
| title: response.data.description, |
| desc: response.data.exception, |
| type: 'Error' |
| }); |
| }else{ |
| $scope.loadFirmwares(); |
| } |
| }); |
| $scope.confirm_priority = false; |
| } |
| $scope.deleteImage = function(imageId, imageVersion){ |
| $scope.delete_image_id = imageId; |
| $scope.delete_image_version = imageVersion; |
| $scope.confirm_delete = true; |
| } |
| $scope.confirmDeleteImage = function(){ |
| $scope.loading = true; |
| APIUtils.deleteImage($scope.delete_image_id).then(function(response){ |
| $scope.loading = false; |
| if(response.status == 'error'){ |
| $scope.displayError({ |
| modal_title: response.data.description, |
| title: response.data.description, |
| desc: response.data.exception, |
| type: 'Error' |
| }); |
| }else{ |
| $scope.loadFirmwares(); |
| } |
| }); |
| $scope.confirm_delete = false; |
| } |
| $scope.fileNameChanged = function(){ |
| $scope.file_empty = false; |
| } |
| |
| $scope.filters = { |
| bmc: { |
| imageType: 'BMC' |
| }, |
| host: { |
| imageType: 'Host' |
| } |
| }; |
| |
| $scope.loadFirmwares = function(){ |
| APIUtils.getFirmwares().then(function(result){ |
| $scope.firmwares = result.data; |
| $scope.bmcActiveVersion = result.bmcActiveVersion; |
| $scope.hostActiveVersion = result.hostActiveVersion; |
| }); |
| } |
| |
| $scope.loadFirmwares(); |
| } |
| ] |
| ); |
| |
| })(angular); |