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 |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | window.angular && (function (angular) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular |
| 13 | .module('app.configuration') |
| 14 | .controller('firmwareController', [ |
| 15 | '$scope', |
| 16 | '$window', |
| 17 | 'APIUtils', |
| 18 | 'dataService', |
| 19 | '$location', |
| 20 | '$anchorScroll', |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 21 | 'Constants', |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 22 | '$interval', |
| 23 | '$q', |
Gunnar Mills | 90f8e69 | 2018-04-03 14:33:52 -0500 | [diff] [blame] | 24 | '$timeout', |
| 25 | function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q, $timeout) { |
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 = ""; |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 53 | $scope.download_success = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 54 | |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 55 | var pollActivationTimer = undefined; |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 56 | var pollDownloadTimer = undefined; |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 57 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 58 | $scope.error = { |
| 59 | modal_title: "", |
| 60 | title: "", |
| 61 | desc: "", |
| 62 | type: "warning" |
| 63 | }; |
| 64 | |
Gunnar Mills | ee6efd8 | 2018-03-21 15:31:56 -0500 | [diff] [blame] | 65 | $scope.activateImage = function(imageId, imageVersion, imageType){ |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 66 | $scope.activate_image_id = imageId; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 67 | $scope.activate_image_version = imageVersion; |
Gunnar Mills | ee6efd8 | 2018-03-21 15:31:56 -0500 | [diff] [blame] | 68 | $scope.activate_image_type = imageType; |
Gunnar Mills | e7f8397 | 2018-03-21 11:03:35 -0500 | [diff] [blame] | 69 | $scope.activate_confirm = true; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | $scope.displayError = function(data){ |
| 73 | $scope.error = data; |
| 74 | $scope.display_error = true; |
| 75 | } |
| 76 | |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 77 | function waitForActive(imageId){ |
| 78 | var deferred = $q.defer(); |
| 79 | var startTime = new Date(); |
| 80 | pollActivationTimer = $interval(function(){ |
| 81 | APIUtils.getActivation(imageId).then(function(state){ |
| 82 | //@TODO: display an error message if image "Failed" |
| 83 | if(((/\.Active$/).test(state.data)) || ((/\.Failed$/).test(state.data))){ |
| 84 | $interval.cancel(pollActivationTimer); |
| 85 | pollActivationTimer = undefined; |
| 86 | deferred.resolve(state); |
| 87 | } |
| 88 | }, function(error){ |
| 89 | $interval.cancel(pollActivationTimer); |
| 90 | pollActivationTimer = undefined; |
| 91 | console.log(error); |
| 92 | deferred.reject(error); |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 93 | }); |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 94 | var now = new Date(); |
| 95 | if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.ACTIVATION){ |
| 96 | $interval.cancel(pollActivationTimer); |
| 97 | pollActivationTimer = undefined; |
| 98 | console.log("Time out activating image, " + imageId); |
| 99 | deferred.reject("Time out. Image did not activate in allotted time."); |
| 100 | } |
| 101 | }, Constants.POLL_INTERVALS.ACTIVATION); |
| 102 | return deferred.promise; |
| 103 | } |
| 104 | |
| 105 | $scope.activateConfirmed = function(){ |
| 106 | APIUtils.activateImage($scope.activate_image_id).then(function(state){ |
| 107 | $scope.loadFirmwares(); |
| 108 | return state; |
| 109 | }, function(error){ |
| 110 | $scope.displayError({ |
| 111 | modal_title: 'Error during activation call', |
| 112 | title: 'Error during activation call', |
| 113 | desc: JSON.stringify(error.data), |
| 114 | type: 'Error' |
| 115 | }); |
| 116 | }).then(function(activationState){ |
| 117 | waitForActive($scope.activate_image_id).then(function(state){ |
| 118 | $scope.loadFirmwares(); |
| 119 | }, function(error){ |
| 120 | $scope.displayError({ |
| 121 | modal_title: 'Error during image activation', |
| 122 | title: 'Error during image activation', |
| 123 | desc: JSON.stringify(error.data), |
| 124 | type: 'Error' |
| 125 | }); |
Gunnar Mills | 6d9ef5a | 2018-03-26 15:34:31 -0500 | [diff] [blame] | 126 | }).then(function(state){ |
| 127 | if($scope.activate.reboot){ |
Gunnar Mills | 90f8e69 | 2018-04-03 14:33:52 -0500 | [diff] [blame] | 128 | // Despite the new image being active, issue, |
| 129 | // https://github.com/openbmc/openbmc/issues/2764, can cause a |
| 130 | // system to brick, if the system reboots before the service to set |
| 131 | // the U-Boot variables is complete. Wait 10 seconds before rebooting |
| 132 | // to ensure this service is complete. This issue is fixed in newer images, but |
| 133 | // the user may be updating from an older image that does not that have this fix. |
| 134 | // TODO: remove this timeout after sufficient time has passed. |
| 135 | $timeout(function() { |
| 136 | APIUtils.bmcReboot(function(response){}, function(error){ |
| 137 | $scope.displayError({ |
| 138 | modal_title: 'Error during BMC reboot', |
| 139 | title: 'Error during BMC reboot', |
| 140 | desc: JSON.stringify(error.data), |
| 141 | type: 'Error' |
| 142 | }); |
Gunnar Mills | 6d9ef5a | 2018-03-26 15:34:31 -0500 | [diff] [blame] | 143 | }); |
Gunnar Mills | 90f8e69 | 2018-04-03 14:33:52 -0500 | [diff] [blame] | 144 | }, 10000); |
Gunnar Mills | 6d9ef5a | 2018-03-26 15:34:31 -0500 | [diff] [blame] | 145 | } |
Gunnar Mills | 033025f | 2018-03-06 14:49:40 -0600 | [diff] [blame] | 146 | }); |
| 147 | }); |
| 148 | $scope.activate_confirm = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 151 | $scope.upload = function(){ |
Gunnar Mills | 87c3db6 | 2018-04-13 16:14:41 -0500 | [diff] [blame] | 152 | if($scope.file) { |
| 153 | $scope.uploading = true; |
| 154 | APIUtils.uploadImage($scope.file).then(function(response){ |
| 155 | $scope.uploading = false; |
| 156 | $scope.loadFirmwares(); |
| 157 | }, function(error){ |
| 158 | $scope.uploading = false; |
| 159 | $scope.displayError({ |
| 160 | modal_title: 'Error during image upload', |
| 161 | title: 'Error during image upload', |
| 162 | desc: 'Error uploading image', |
| 163 | type: 'Error uploading image, please check the image' |
| 164 | }); |
| 165 | }); |
| 166 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 167 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 168 | |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 169 | //TODO: openbmc/openbmc#1691 Add support to return |
| 170 | //the id of the newly created image, downloaded via |
| 171 | //tftp. Polling the number of software objects is a |
| 172 | //near term solution. |
| 173 | function waitForDownload(){ |
| 174 | var deferred = $q.defer(); |
| 175 | var startTime = new Date(); |
| 176 | pollDownloadTimer = $interval(function(){ |
| 177 | var now = new Date(); |
| 178 | if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.DOWNLOAD_IMAGE){ |
| 179 | $interval.cancel(pollDownloadTimer); |
| 180 | pollDownloadTimer = undefined; |
| 181 | deferred.reject(new Error(Constants.MESSAGES.POLL.DOWNLOAD_IMAGE_TIMEOUT)); |
| 182 | } |
| 183 | |
| 184 | APIUtils.getFirmwares().then(function(response){ |
| 185 | if(response.data.length === $scope.firmwares.length + 1){ |
| 186 | $interval.cancel(pollDownloadTimer); |
| 187 | pollDownloadTimer = undefined; |
| 188 | deferred.resolve(response.data); |
| 189 | } |
| 190 | }, function(error){ |
| 191 | $interval.cancel(pollDownloadTimer); |
| 192 | pollDownloadTimer = undefined; |
| 193 | deferred.reject(error); |
| 194 | }); |
| 195 | }, Constants.POLL_INTERVALS.DOWNLOAD_IMAGE); |
| 196 | |
| 197 | return deferred.promise; |
| 198 | } |
| 199 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 200 | $scope.download = function(){ |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 201 | $scope.download_success = false; |
Gunnar Mills | 6d7b4a8 | 2018-04-02 15:25:36 -0500 | [diff] [blame] | 202 | $scope.download_error_msg = ""; |
| 203 | if(!$scope.download_host || !$scope.download_filename){ |
Gunnar Mills | 36379b9 | 2018-04-02 16:53:53 -0500 | [diff] [blame] | 204 | $scope.download_error_msg = "Field is required!"; |
| 205 | return false; |
Gunnar Mills | 6d7b4a8 | 2018-04-02 15:25:36 -0500 | [diff] [blame] | 206 | } |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 207 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 208 | $scope.downloading = true; |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 209 | APIUtils.getFirmwares().then(function(response){ |
| 210 | $scope.firmwares = response.data; |
| 211 | }).then(function(){ |
| 212 | return APIUtils.downloadImage($scope.download_host, |
| 213 | $scope.download_filename).then(function(downloadStatus){ |
| 214 | return downloadStatus; |
| 215 | }); |
| 216 | }).then(function(downloadStatus){ |
| 217 | return waitForDownload(); |
| 218 | }).then(function(newFirmwareList){ |
| 219 | $scope.download_host = ""; |
| 220 | $scope.download_filename = ""; |
| 221 | $scope.downloading = false; |
| 222 | $scope.download_success = true; |
| 223 | $scope.loadFirmwares(); |
Gunnar Mills | 36379b9 | 2018-04-02 16:53:53 -0500 | [diff] [blame] | 224 | }, function(error){ |
CamVan Nguyen | 58301ec | 2018-04-20 21:33:01 -0500 | [diff] [blame^] | 225 | console.log(error); |
| 226 | $scope.displayError({ |
| 227 | modal_title: 'Error during downloading Image', |
| 228 | title: 'Error during downloading Image', |
| 229 | desc: error, |
| 230 | type: 'Error' |
| 231 | }); |
| 232 | $scope.downloading = false; |
| 233 | }); |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Gunnar Mills | 6473a41 | 2018-03-01 16:19:37 -0600 | [diff] [blame] | 236 | $scope.changePriority = function(imageId, imageVersion, from, to){ |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 237 | $scope.priority_image_id = imageId; |
Gunnar Mills | 6473a41 | 2018-03-01 16:19:37 -0600 | [diff] [blame] | 238 | $scope.priority_image_version = imageVersion; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 239 | $scope.priority_from = from; |
| 240 | $scope.priority_to = to; |
Gunnar Mills | f4d9bc4 | 2018-03-06 15:42:40 -0600 | [diff] [blame] | 241 | $scope.confirm_priority = true; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | $scope.confirmChangePriority = function(){ |
| 245 | $scope.loading = true; |
| 246 | APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){ |
| 247 | $scope.loading = false; |
| 248 | if(response.status == 'error'){ |
| 249 | $scope.displayError({ |
| 250 | modal_title: response.data.description, |
| 251 | title: response.data.description, |
| 252 | desc: response.data.exception, |
| 253 | type: 'Error' |
| 254 | }); |
| 255 | }else{ |
| 256 | $scope.loadFirmwares(); |
| 257 | } |
| 258 | }); |
| 259 | $scope.confirm_priority = false; |
| 260 | } |
Gunnar Mills | 607a120 | 2018-03-01 16:26:50 -0600 | [diff] [blame] | 261 | $scope.deleteImage = function(imageId, imageVersion){ |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 262 | $scope.delete_image_id = imageId; |
Gunnar Mills | 607a120 | 2018-03-01 16:26:50 -0600 | [diff] [blame] | 263 | $scope.delete_image_version = imageVersion; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 264 | $scope.confirm_delete = true; |
| 265 | } |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 266 | $scope.confirmDeleteImage = function(){ |
| 267 | $scope.loading = true; |
| 268 | APIUtils.deleteImage($scope.delete_image_id).then(function(response){ |
| 269 | $scope.loading = false; |
| 270 | if(response.status == 'error'){ |
| 271 | $scope.displayError({ |
| 272 | modal_title: response.data.description, |
| 273 | title: response.data.description, |
| 274 | desc: response.data.exception, |
| 275 | type: 'Error' |
| 276 | }); |
| 277 | }else{ |
| 278 | $scope.loadFirmwares(); |
| 279 | } |
| 280 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 281 | $scope.confirm_delete = false; |
| 282 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 283 | $scope.fileNameChanged = function(){ |
| 284 | $scope.file_empty = false; |
| 285 | } |
| 286 | |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 287 | $scope.filters = { |
| 288 | bmc: { |
| 289 | imageType: 'BMC' |
| 290 | }, |
| 291 | host: { |
| 292 | imageType: 'Host' |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | $scope.loadFirmwares = function(){ |
Michael Davis | df3bd12 | 2017-08-10 11:03:42 -0500 | [diff] [blame] | 297 | APIUtils.getFirmwares().then(function(result){ |
| 298 | $scope.firmwares = result.data; |
| 299 | $scope.bmcActiveVersion = result.bmcActiveVersion; |
| 300 | $scope.hostActiveVersion = result.hostActiveVersion; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 301 | }); |
| 302 | } |
| 303 | |
| 304 | $scope.loadFirmwares(); |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 305 | } |
| 306 | ] |
| 307 | ); |
| 308 | |
| 309 | })(angular); |