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