blob: 127a7c2c3443de50a8d9cb0d076e148c76014e1e [file] [log] [blame]
Michael Davis43366db2017-05-15 18:12:35 -05001/**
2 * Controller for firmware
3 *
4 * @module app/configuration
5 * @exports firmwareController
6 * @name firmwareController
7 * @version 0.1.0
8 */
9
10window.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 Islam1acb4122017-11-02 13:20:32 -050022 'Constants',
Gunnar Mills033025f2018-03-06 14:49:40 -060023 '$interval',
24 '$q',
25 function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q) {
Michael Davis43366db2017-05-15 18:12:35 -050026 $scope.dataService = dataService;
27
Michael Davis43366db2017-05-15 18:12:35 -050028 //Scroll to target anchor
29 $scope.gotoAnchor = function () {
30 $location.hash('upload');
31 $anchorScroll();
32 };
Iftekharul Islamc0161392017-06-14 15:46:15 -050033
34 $scope.firmwares = [];
35 $scope.bmcActiveVersion = "";
36 $scope.hostActiveVersion = "";
37 $scope.display_error = false;
Gunnar Millse7f83972018-03-21 11:03:35 -050038 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050039 $scope.delete_image_id = "";
Gunnar Mills607a1202018-03-01 16:26:50 -060040 $scope.delete_image_version = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -050041 $scope.activate_image_id = "";
Gunnar Millse7f83972018-03-21 11:03:35 -050042 $scope.activate_image_version = "";
Gunnar Millsee6efd82018-03-21 15:31:56 -050043 $scope.activate_image_type = "";
Iftekharul Islam1acb4122017-11-02 13:20:32 -050044 $scope.priority_image_id = "";
Gunnar Mills6473a412018-03-01 16:19:37 -060045 $scope.priority_image_version = "";
Iftekharul Islam1acb4122017-11-02 13:20:32 -050046 $scope.priority_from = -1;
47 $scope.priority_to = -1;
48 $scope.confirm_priority = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050049 $scope.file_empty = true;
50 $scope.uploading = false;
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -050051 $scope.activate = { reboot: true };
Iftekharul Islamc0161392017-06-14 15:46:15 -050052
Gunnar Mills033025f2018-03-06 14:49:40 -060053 var pollActivationTimer = undefined;
54
Iftekharul Islamc0161392017-06-14 15:46:15 -050055 $scope.error = {
56 modal_title: "",
57 title: "",
58 desc: "",
59 type: "warning"
60 };
61
Gunnar Millsee6efd82018-03-21 15:31:56 -050062 $scope.activateImage = function(imageId, imageVersion, imageType){
Iftekharul Islamc0161392017-06-14 15:46:15 -050063 $scope.activate_image_id = imageId;
Gunnar Millse7f83972018-03-21 11:03:35 -050064 $scope.activate_image_version = imageVersion;
Gunnar Millsee6efd82018-03-21 15:31:56 -050065 $scope.activate_image_type = imageType;
Gunnar Millse7f83972018-03-21 11:03:35 -050066 $scope.activate_confirm = true;
Iftekharul Islamc0161392017-06-14 15:46:15 -050067 }
68
69 $scope.displayError = function(data){
70 $scope.error = data;
71 $scope.display_error = true;
72 }
73
Gunnar Mills033025f2018-03-06 14:49:40 -060074 function waitForActive(imageId){
75 var deferred = $q.defer();
76 var startTime = new Date();
77 pollActivationTimer = $interval(function(){
78 APIUtils.getActivation(imageId).then(function(state){
79 //@TODO: display an error message if image "Failed"
80 if(((/\.Active$/).test(state.data)) || ((/\.Failed$/).test(state.data))){
81 $interval.cancel(pollActivationTimer);
82 pollActivationTimer = undefined;
83 deferred.resolve(state);
84 }
85 }, function(error){
86 $interval.cancel(pollActivationTimer);
87 pollActivationTimer = undefined;
88 console.log(error);
89 deferred.reject(error);
Iftekharul Islam2a489552017-11-02 13:23:08 -050090 });
Gunnar Mills033025f2018-03-06 14:49:40 -060091 var now = new Date();
92 if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.ACTIVATION){
93 $interval.cancel(pollActivationTimer);
94 pollActivationTimer = undefined;
95 console.log("Time out activating image, " + imageId);
96 deferred.reject("Time out. Image did not activate in allotted time.");
97 }
98 }, Constants.POLL_INTERVALS.ACTIVATION);
99 return deferred.promise;
100 }
101
102 $scope.activateConfirmed = function(){
103 APIUtils.activateImage($scope.activate_image_id).then(function(state){
104 $scope.loadFirmwares();
105 return state;
106 }, function(error){
107 $scope.displayError({
108 modal_title: 'Error during activation call',
109 title: 'Error during activation call',
110 desc: JSON.stringify(error.data),
111 type: 'Error'
112 });
113 }).then(function(activationState){
114 waitForActive($scope.activate_image_id).then(function(state){
115 $scope.loadFirmwares();
116 }, function(error){
117 $scope.displayError({
118 modal_title: 'Error during image activation',
119 title: 'Error during image activation',
120 desc: JSON.stringify(error.data),
121 type: 'Error'
122 });
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500123 }).then(function(state){
124 if($scope.activate.reboot){
125 APIUtils.bmcReboot(function(response){}, function(error){
126 $scope.displayError({
127 modal_title: 'Error during BMC reboot',
128 title: 'Error during BMC reboot',
129 desc: JSON.stringify(error.data),
130 type: 'Error'
131 });
132 });
133 }
Gunnar Mills033025f2018-03-06 14:49:40 -0600134 });
135 });
136 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500137 }
138
Iftekharul Islamc0161392017-06-14 15:46:15 -0500139 $scope.upload = function(){
Gunnar Millsdddb1492018-02-19 16:27:27 -0600140 if($scope.file) {
141 $scope.uploading = true;
142 APIUtils.uploadImage($scope.file).then(function(response){
143 $scope.uploading = false;
144 if(response.status == 'error'){
145 $scope.displayError({
146 modal_title: response.data.description,
147 title: response.data.description,
148 desc: response.data.exception,
149 type: 'Error'
150 });
151 }else{
152 $scope.loadFirmwares();
153 }
154 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500155 }
156 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500157
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500158 $scope.download = function(){
159 $scope.downloading = true;
160 APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
161 var data = response.data;
162 $scope.downloading = false;
163 var headers = response.headers();
164
165 var filename = headers['x-filename'];
166 var contentType = headers['content-type'];
167
168 if(!headers['x-filename']){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500169 filename = $scope.download_filename;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500170 }
171
172 var linkElement = document.createElement('a');
173 try {
174 var blob = new Blob([data], { type: contentType });
175 var url = window.URL.createObjectURL(blob);
176
177 linkElement.setAttribute('href', url);
178 linkElement.setAttribute("download", filename);
179
180 var clickEvent = new MouseEvent("click", {
181 "view": window,
182 "bubbles": true,
183 "cancelable": false
184 });
185 linkElement.dispatchEvent(clickEvent);
186 } catch (ex) {
187 console.log(ex);
188 }
189 });
190 }
191
Gunnar Mills6473a412018-03-01 16:19:37 -0600192 $scope.changePriority = function(imageId, imageVersion, from, to){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500193 $scope.priority_image_id = imageId;
Gunnar Mills6473a412018-03-01 16:19:37 -0600194 $scope.priority_image_version = imageVersion;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500195 $scope.priority_from = from;
196 $scope.priority_to = to;
Gunnar Millsf4d9bc42018-03-06 15:42:40 -0600197 $scope.confirm_priority = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500198 }
199
200 $scope.confirmChangePriority = function(){
201 $scope.loading = true;
202 APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){
203 $scope.loading = false;
204 if(response.status == 'error'){
205 $scope.displayError({
206 modal_title: response.data.description,
207 title: response.data.description,
208 desc: response.data.exception,
209 type: 'Error'
210 });
211 }else{
212 $scope.loadFirmwares();
213 }
214 });
215 $scope.confirm_priority = false;
216 }
Gunnar Mills607a1202018-03-01 16:26:50 -0600217 $scope.deleteImage = function(imageId, imageVersion){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500218 $scope.delete_image_id = imageId;
Gunnar Mills607a1202018-03-01 16:26:50 -0600219 $scope.delete_image_version = imageVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500220 $scope.confirm_delete = true;
221 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500222 $scope.confirmDeleteImage = function(){
223 $scope.loading = true;
224 APIUtils.deleteImage($scope.delete_image_id).then(function(response){
225 $scope.loading = false;
226 if(response.status == 'error'){
227 $scope.displayError({
228 modal_title: response.data.description,
229 title: response.data.description,
230 desc: response.data.exception,
231 type: 'Error'
232 });
233 }else{
234 $scope.loadFirmwares();
235 }
236 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500237 $scope.confirm_delete = false;
238 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500239 $scope.fileNameChanged = function(){
240 $scope.file_empty = false;
241 }
242
Iftekharul Islamc0161392017-06-14 15:46:15 -0500243 $scope.filters = {
244 bmc: {
245 imageType: 'BMC'
246 },
247 host: {
248 imageType: 'Host'
249 }
250 };
251
252 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500253 APIUtils.getFirmwares().then(function(result){
254 $scope.firmwares = result.data;
255 $scope.bmcActiveVersion = result.bmcActiveVersion;
256 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500257 });
258 }
259
260 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500261 }
262 ]
263 );
264
265})(angular);