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