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', |
| 23 | function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants) { |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 24 | $scope.dataService = dataService; |
| 25 | |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 26 | //Scroll to target anchor |
| 27 | $scope.gotoAnchor = function () { |
| 28 | $location.hash('upload'); |
| 29 | $anchorScroll(); |
| 30 | }; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 31 | |
| 32 | $scope.firmwares = []; |
| 33 | $scope.bmcActiveVersion = ""; |
| 34 | $scope.hostActiveVersion = ""; |
| 35 | $scope.display_error = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 36 | $scope.reboot_confirm = false; |
| 37 | $scope.preserve_settings_confirm = false; |
| 38 | $scope.delete_image_id = ""; |
| 39 | $scope.activate_image_id = ""; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 40 | $scope.priority_image_id = ""; |
| 41 | $scope.priority_from = -1; |
| 42 | $scope.priority_to = -1; |
| 43 | $scope.confirm_priority = false; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 44 | $scope.file_empty = true; |
| 45 | $scope.uploading = false; |
| 46 | |
| 47 | $scope.error = { |
| 48 | modal_title: "", |
| 49 | title: "", |
| 50 | desc: "", |
| 51 | type: "warning" |
| 52 | }; |
| 53 | |
| 54 | $scope.activateImage = function(imageId){ |
| 55 | $scope.activate_image_id = imageId; |
| 56 | $scope.preserve_settings_confirm = true; |
| 57 | } |
| 58 | |
| 59 | $scope.displayError = function(data){ |
| 60 | $scope.error = data; |
| 61 | $scope.display_error = true; |
| 62 | } |
| 63 | |
| 64 | $scope.preserveSettingsConfirmed = function(){ |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 65 | $scope.uploading = true; |
| 66 | APIUtils.activateImage($scope.activate_image_id).then(function(response){ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame^] | 67 | $scope.uploading = false; |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 68 | if(response.status == 'error'){ |
| 69 | $scope.displayError({ |
| 70 | modal_title: response.data.description, |
| 71 | title: response.data.description, |
| 72 | desc: response.data.exception, |
| 73 | type: 'Error' |
| 74 | }); |
| 75 | }else{ |
| 76 | $scope.loadFirmwares(); |
| 77 | } |
| 78 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 79 | $scope.preserve_settings_confirm = false; |
| 80 | } |
| 81 | |
| 82 | $scope.confirmWarmReboot = function(){ |
| 83 | $scope.reboot_confirm = false; |
| 84 | } |
| 85 | |
| 86 | $scope.upload = function(){ |
Gunnar Mills | dddb149 | 2018-02-19 16:27:27 -0600 | [diff] [blame] | 87 | if($scope.file) { |
| 88 | $scope.uploading = true; |
| 89 | APIUtils.uploadImage($scope.file).then(function(response){ |
| 90 | $scope.uploading = false; |
| 91 | if(response.status == 'error'){ |
| 92 | $scope.displayError({ |
| 93 | modal_title: response.data.description, |
| 94 | title: response.data.description, |
| 95 | desc: response.data.exception, |
| 96 | type: 'Error' |
| 97 | }); |
| 98 | }else{ |
| 99 | $scope.loadFirmwares(); |
| 100 | } |
| 101 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 102 | } |
| 103 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 104 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 105 | $scope.download = function(){ |
| 106 | $scope.downloading = true; |
| 107 | APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){ |
| 108 | var data = response.data; |
| 109 | $scope.downloading = false; |
| 110 | var headers = response.headers(); |
| 111 | |
| 112 | var filename = headers['x-filename']; |
| 113 | var contentType = headers['content-type']; |
| 114 | |
| 115 | if(!headers['x-filename']){ |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 116 | filename = $scope.download_filename; |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | var linkElement = document.createElement('a'); |
| 120 | try { |
| 121 | var blob = new Blob([data], { type: contentType }); |
| 122 | var url = window.URL.createObjectURL(blob); |
| 123 | |
| 124 | linkElement.setAttribute('href', url); |
| 125 | linkElement.setAttribute("download", filename); |
| 126 | |
| 127 | var clickEvent = new MouseEvent("click", { |
| 128 | "view": window, |
| 129 | "bubbles": true, |
| 130 | "cancelable": false |
| 131 | }); |
| 132 | linkElement.dispatchEvent(clickEvent); |
| 133 | } catch (ex) { |
| 134 | console.log(ex); |
| 135 | } |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | $scope.changePriority = function(imageId, from, to){ |
| 140 | $scope.priority_image_id = imageId; |
| 141 | $scope.priority_from = from; |
| 142 | $scope.priority_to = to; |
| 143 | |
| 144 | if((from + to) == 1){ |
| 145 | $scope.confirm_priority = true; |
| 146 | }else{ |
| 147 | $scope.confirmChangePriority(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $scope.confirmChangePriority = function(){ |
| 152 | $scope.loading = true; |
| 153 | APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){ |
| 154 | $scope.loading = false; |
| 155 | if(response.status == 'error'){ |
| 156 | $scope.displayError({ |
| 157 | modal_title: response.data.description, |
| 158 | title: response.data.description, |
| 159 | desc: response.data.exception, |
| 160 | type: 'Error' |
| 161 | }); |
| 162 | }else{ |
| 163 | $scope.loadFirmwares(); |
| 164 | } |
| 165 | }); |
| 166 | $scope.confirm_priority = false; |
| 167 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 168 | $scope.deleteImage = function(imageId){ |
| 169 | $scope.delete_image_id = imageId; |
| 170 | $scope.confirm_delete = true; |
| 171 | } |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 172 | $scope.confirmDeleteImage = function(){ |
| 173 | $scope.loading = true; |
| 174 | APIUtils.deleteImage($scope.delete_image_id).then(function(response){ |
| 175 | $scope.loading = false; |
| 176 | if(response.status == 'error'){ |
| 177 | $scope.displayError({ |
| 178 | modal_title: response.data.description, |
| 179 | title: response.data.description, |
| 180 | desc: response.data.exception, |
| 181 | type: 'Error' |
| 182 | }); |
| 183 | }else{ |
| 184 | $scope.loadFirmwares(); |
| 185 | } |
| 186 | }); |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 187 | $scope.confirm_delete = false; |
| 188 | } |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 189 | $scope.fileNameChanged = function(){ |
| 190 | $scope.file_empty = false; |
| 191 | } |
| 192 | |
| 193 | $scope.uploading = false; |
| 194 | $scope.filters = { |
| 195 | bmc: { |
| 196 | imageType: 'BMC' |
| 197 | }, |
| 198 | host: { |
| 199 | imageType: 'Host' |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | $scope.loadFirmwares = function(){ |
Michael Davis | df3bd12 | 2017-08-10 11:03:42 -0500 | [diff] [blame] | 204 | APIUtils.getFirmwares().then(function(result){ |
| 205 | $scope.firmwares = result.data; |
| 206 | $scope.bmcActiveVersion = result.bmcActiveVersion; |
| 207 | $scope.hostActiveVersion = result.hostActiveVersion; |
Iftekharul Islam | c016139 | 2017-06-14 15:46:15 -0500 | [diff] [blame] | 208 | }); |
| 209 | } |
| 210 | |
| 211 | $scope.loadFirmwares(); |
Michael Davis | 43366db | 2017-05-15 18:12:35 -0500 | [diff] [blame] | 212 | } |
| 213 | ] |
| 214 | ); |
| 215 | |
| 216 | })(angular); |