Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for firmware |
| 3 | * |
| 4 | * @module app/configuration |
| 5 | * @exports firmwareController |
| 6 | * @name firmwareController |
| 7 | * @version 0.1.0 |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function (angular) { |
| 11 | 'use strict'; |
| 12 | |
| 13 | angular |
| 14 | .module('app.configuration') |
| 15 | .controller('firmwareController', [ |
| 16 | '$scope', |
| 17 | '$window', |
| 18 | 'APIUtils', |
| 19 | 'dataService', |
| 20 | '$location', |
| 21 | '$anchorScroll', |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 22 | 'Constants', |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 23 | '$interval', |
| 24 | '$q', |
| 25 | function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q) { |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 26 | $scope.dataService = dataService; |
| 27 | |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 28 | //Scroll to target anchor |
| 29 | $scope.gotoAnchor = function () { |
| 30 | $location.hash('upload'); |
| 31 | $anchorScroll(); |
| 32 | }; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 33 | |
| 34 | $scope.firmwares = []; |
| 35 | $scope.bmcActiveVersion = ""; |
| 36 | $scope.hostActiveVersion = ""; |
| 37 | $scope.display_error = false; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 38 | $scope.activate_confirm = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 39 | $scope.delete_image_id = ""; |
Gunnar Mills | 607a120 | 2018-03-01 16:26:50 -0600 | [diff] [blame] | 40 | $scope.delete_image_version = ""; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 41 | $scope.activate_image_id = ""; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 42 | $scope.activate_image_version = ""; |
Gunnar Mills | ee6efd8 | 2018-03-21 15:31:56 -0500 | [diff] [blame] | 43 | $scope.activate_image_type = ""; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 44 | $scope.priority_image_id = ""; |
Gunnar Mills | 6473a41 | 2018-03-01 16:19:37 -0600 | [diff] [blame] | 45 | $scope.priority_image_version = ""; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 46 | $scope.priority_from = -1; |
| 47 | $scope.priority_to = -1; |
| 48 | $scope.confirm_priority = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 49 | $scope.file_empty = true; |
| 50 | $scope.uploading = false; |
Gunnar Mills | 6d9ef5a | 2018-03-26 15:34:31 -0500 | [diff] [blame] | 51 | $scope.activate = { reboot: true }; |
Gunnar Mills | 6d7b4a8 | 2018-04-02 15:25:36 -0500 | [diff] [blame^] | 52 | $scope.download_error_msg = ""; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 53 | |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 54 | var pollActivationTimer = undefined; |
| 55 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 56 | $scope.error = { |
| 57 | modal_title: "", |
| 58 | title: "", |
| 59 | desc: "", |
| 60 | type: "warning" |
| 61 | }; |
| 62 | |
Gunnar Mills | ee6efd8 | 2018-03-21 15:31:56 -0500 | [diff] [blame] | 63 | $scope.activateImage = function(imageId, imageVersion, imageType){ |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 64 | $scope.activate_image_id = imageId; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 65 | $scope.activate_image_version = imageVersion; |
Gunnar Mills | ee6efd8 | 2018-03-21 15:31:56 -0500 | [diff] [blame] | 66 | $scope.activate_image_type = imageType; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 67 | $scope.activate_confirm = true; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | $scope.displayError = function(data){ |
| 71 | $scope.error = data; |
| 72 | $scope.display_error = true; |
| 73 | } |
| 74 | |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 75 | function waitForActive(imageId){ |
| 76 | var deferred = $q.defer(); |
| 77 | var startTime = new Date(); |
| 78 | pollActivationTimer = $interval(function(){ |
| 79 | APIUtils.getActivation(imageId).then(function(state){ |
| 80 | //@TODO: display an error message if image "Failed" |
| 81 | if(((/\.Active$/).test(state.data)) || ((/\.Failed$/).test(state.data))){ |
| 82 | $interval.cancel(pollActivationTimer); |
| 83 | pollActivationTimer = undefined; |
| 84 | deferred.resolve(state); |
| 85 | } |
| 86 | }, function(error){ |
| 87 | $interval.cancel(pollActivationTimer); |
| 88 | pollActivationTimer = undefined; |
| 89 | console.log(error); |
| 90 | deferred.reject(error); |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 91 | }); |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 92 | var now = new Date(); |
| 93 | if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.ACTIVATION){ |
| 94 | $interval.cancel(pollActivationTimer); |
| 95 | pollActivationTimer = undefined; |
| 96 | console.log("Time out activating image, " + imageId); |
| 97 | deferred.reject("Time out. Image did not activate in allotted time."); |
| 98 | } |
| 99 | }, Constants.POLL_INTERVALS.ACTIVATION); |
| 100 | return deferred.promise; |
| 101 | } |
| 102 | |
| 103 | $scope.activateConfirmed = function(){ |
| 104 | APIUtils.activateImage($scope.activate_image_id).then(function(state){ |
| 105 | $scope.loadFirmwares(); |
| 106 | return state; |
| 107 | }, function(error){ |
| 108 | $scope.displayError({ |
| 109 | modal_title: 'Error during activation call', |
| 110 | title: 'Error during activation call', |
| 111 | desc: JSON.stringify(error.data), |
| 112 | type: 'Error' |
| 113 | }); |
| 114 | }).then(function(activationState){ |
| 115 | waitForActive($scope.activate_image_id).then(function(state){ |
| 116 | $scope.loadFirmwares(); |
| 117 | }, function(error){ |
| 118 | $scope.displayError({ |
| 119 | modal_title: 'Error during image activation', |
| 120 | title: 'Error during image activation', |
| 121 | desc: JSON.stringify(error.data), |
| 122 | type: 'Error' |
| 123 | }); |
Gunnar Mills | 6d9ef5a | 2018-03-26 15:34:31 -0500 | [diff] [blame] | 124 | }).then(function(state){ |
| 125 | if($scope.activate.reboot){ |
| 126 | APIUtils.bmcReboot(function(response){}, function(error){ |
| 127 | $scope.displayError({ |
| 128 | modal_title: 'Error during BMC reboot', |
| 129 | title: 'Error during BMC reboot', |
| 130 | desc: JSON.stringify(error.data), |
| 131 | type: 'Error' |
| 132 | }); |
| 133 | }); |
| 134 | } |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 135 | }); |
| 136 | }); |
| 137 | $scope.activate_confirm = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 140 | $scope.upload = function(){ |
Gunnar Mills | dddb149 | 2018-02-19 16:27:27 -0600 | [diff] [blame] | 141 | if($scope.file) { |
| 142 | $scope.uploading = true; |
| 143 | APIUtils.uploadImage($scope.file).then(function(response){ |
| 144 | $scope.uploading = false; |
| 145 | if(response.status == 'error'){ |
| 146 | $scope.displayError({ |
| 147 | modal_title: response.data.description, |
| 148 | title: response.data.description, |
| 149 | desc: response.data.exception, |
| 150 | type: 'Error' |
| 151 | }); |
| 152 | }else{ |
| 153 | $scope.loadFirmwares(); |
| 154 | } |
| 155 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 156 | } |
| 157 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 158 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 159 | $scope.download = function(){ |
Gunnar Mills | 6d7b4a8 | 2018-04-02 15:25:36 -0500 | [diff] [blame^] | 160 | $scope.download_error_msg = ""; |
| 161 | if(!$scope.download_host || !$scope.download_filename){ |
| 162 | $scope.download_error_msg = "Field is required!"; |
| 163 | return false; |
| 164 | } |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 165 | $scope.downloading = true; |
| 166 | APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){ |
| 167 | var data = response.data; |
| 168 | $scope.downloading = false; |
| 169 | var headers = response.headers(); |
| 170 | |
| 171 | var filename = headers['x-filename']; |
| 172 | var contentType = headers['content-type']; |
| 173 | |
| 174 | if(!headers['x-filename']){ |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 175 | filename = $scope.download_filename; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | var linkElement = document.createElement('a'); |
| 179 | try { |
| 180 | var blob = new Blob([data], { type: contentType }); |
| 181 | var url = window.URL.createObjectURL(blob); |
| 182 | |
| 183 | linkElement.setAttribute('href', url); |
| 184 | linkElement.setAttribute("download", filename); |
| 185 | |
| 186 | var clickEvent = new MouseEvent("click", { |
| 187 | "view": window, |
| 188 | "bubbles": true, |
| 189 | "cancelable": false |
| 190 | }); |
| 191 | linkElement.dispatchEvent(clickEvent); |
| 192 | } catch (ex) { |
| 193 | console.log(ex); |
| 194 | } |
| 195 | }); |
| 196 | } |
| 197 | |
Gunnar Mills | 6473a41 | 2018-03-01 16:19:37 -0600 | [diff] [blame] | 198 | $scope.changePriority = function(imageId, imageVersion, from, to){ |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 199 | $scope.priority_image_id = imageId; |
Gunnar Mills | 6473a41 | 2018-03-01 16:19:37 -0600 | [diff] [blame] | 200 | $scope.priority_image_version = imageVersion; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 201 | $scope.priority_from = from; |
| 202 | $scope.priority_to = to; |
Gunnar Mills | f4d9bc4 | 2018-03-06 15:42:40 -0600 | [diff] [blame] | 203 | $scope.confirm_priority = true; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | $scope.confirmChangePriority = function(){ |
| 207 | $scope.loading = true; |
| 208 | APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){ |
| 209 | $scope.loading = false; |
| 210 | if(response.status == 'error'){ |
| 211 | $scope.displayError({ |
| 212 | modal_title: response.data.description, |
| 213 | title: response.data.description, |
| 214 | desc: response.data.exception, |
| 215 | type: 'Error' |
| 216 | }); |
| 217 | }else{ |
| 218 | $scope.loadFirmwares(); |
| 219 | } |
| 220 | }); |
| 221 | $scope.confirm_priority = false; |
| 222 | } |
Gunnar Mills | 607a120 | 2018-03-01 16:26:50 -0600 | [diff] [blame] | 223 | $scope.deleteImage = function(imageId, imageVersion){ |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 224 | $scope.delete_image_id = imageId; |
Gunnar Mills | 607a120 | 2018-03-01 16:26:50 -0600 | [diff] [blame] | 225 | $scope.delete_image_version = imageVersion; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 226 | $scope.confirm_delete = true; |
| 227 | } |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 228 | $scope.confirmDeleteImage = function(){ |
| 229 | $scope.loading = true; |
| 230 | APIUtils.deleteImage($scope.delete_image_id).then(function(response){ |
| 231 | $scope.loading = false; |
| 232 | if(response.status == 'error'){ |
| 233 | $scope.displayError({ |
| 234 | modal_title: response.data.description, |
| 235 | title: response.data.description, |
| 236 | desc: response.data.exception, |
| 237 | type: 'Error' |
| 238 | }); |
| 239 | }else{ |
| 240 | $scope.loadFirmwares(); |
| 241 | } |
| 242 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 243 | $scope.confirm_delete = false; |
| 244 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 245 | $scope.fileNameChanged = function(){ |
| 246 | $scope.file_empty = false; |
| 247 | } |
| 248 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 249 | $scope.filters = { |
| 250 | bmc: { |
| 251 | imageType: 'BMC' |
| 252 | }, |
| 253 | host: { |
| 254 | imageType: 'Host' |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | $scope.loadFirmwares = function(){ |
Michael Davis | df3bd12 | 2017-08-10 11:03:42 -0500 | [diff] [blame] | 259 | APIUtils.getFirmwares().then(function(result){ |
| 260 | $scope.firmwares = result.data; |
| 261 | $scope.bmcActiveVersion = result.bmcActiveVersion; |
| 262 | $scope.hostActiveVersion = result.hostActiveVersion; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 263 | }); |
| 264 | } |
| 265 | |
| 266 | $scope.loadFirmwares(); |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 267 | } |
| 268 | ] |
| 269 | ); |
| 270 | |
| 271 | })(angular); |