blob: 780bb193cbcc76e66e25f8291725728a1562cff3 [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
Michael Davis43366db2017-05-15 18:12:35 -05007 */
8
9window.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 Islam1acb4122017-11-02 13:20:32 -050021 'Constants',
Gunnar Mills033025f2018-03-06 14:49:40 -060022 '$interval',
23 '$q',
Gunnar Mills90f8e692018-04-03 14:33:52 -050024 '$timeout',
25 function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q, $timeout) {
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 };
Gunnar Mills6d7b4a82018-04-02 15:25:36 -050052 $scope.download_error_msg = "";
CamVan Nguyen58301ec2018-04-20 21:33:01 -050053 $scope.download_success = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050054
Gunnar Mills033025f2018-03-06 14:49:40 -060055 var pollActivationTimer = undefined;
CamVan Nguyen58301ec2018-04-20 21:33:01 -050056 var pollDownloadTimer = undefined;
Gunnar Mills033025f2018-03-06 14:49:40 -060057
Iftekharul Islamc0161392017-06-14 15:46:15 -050058 $scope.error = {
59 modal_title: "",
60 title: "",
61 desc: "",
62 type: "warning"
63 };
64
Gunnar Millsee6efd82018-03-21 15:31:56 -050065 $scope.activateImage = function(imageId, imageVersion, imageType){
Iftekharul Islamc0161392017-06-14 15:46:15 -050066 $scope.activate_image_id = imageId;
Gunnar Millse7f83972018-03-21 11:03:35 -050067 $scope.activate_image_version = imageVersion;
Gunnar Millsee6efd82018-03-21 15:31:56 -050068 $scope.activate_image_type = imageType;
Gunnar Millse7f83972018-03-21 11:03:35 -050069 $scope.activate_confirm = true;
Iftekharul Islamc0161392017-06-14 15:46:15 -050070 }
71
72 $scope.displayError = function(data){
73 $scope.error = data;
74 $scope.display_error = true;
75 }
76
Gunnar Mills033025f2018-03-06 14:49:40 -060077 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 Islam2a489552017-11-02 13:23:08 -050093 });
Gunnar Mills033025f2018-03-06 14:49:40 -060094 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 Mills6d9ef5a2018-03-26 15:34:31 -0500126 }).then(function(state){
127 if($scope.activate.reboot){
Gunnar Mills90f8e692018-04-03 14:33:52 -0500128 // 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 Mills6d9ef5a2018-03-26 15:34:31 -0500143 });
Gunnar Mills90f8e692018-04-03 14:33:52 -0500144 }, 10000);
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500145 }
Gunnar Mills033025f2018-03-06 14:49:40 -0600146 });
147 });
148 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500149 }
150
Iftekharul Islamc0161392017-06-14 15:46:15 -0500151 $scope.upload = function(){
Gunnar Mills87c3db62018-04-13 16:14:41 -0500152 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 Islamc0161392017-06-14 15:46:15 -0500167 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500168
CamVan Nguyen58301ec2018-04-20 21:33:01 -0500169 //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 Islam1acb4122017-11-02 13:20:32 -0500200 $scope.download = function(){
CamVan Nguyen58301ec2018-04-20 21:33:01 -0500201 $scope.download_success = false;
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500202 $scope.download_error_msg = "";
203 if(!$scope.download_host || !$scope.download_filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500204 $scope.download_error_msg = "Field is required!";
205 return false;
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500206 }
CamVan Nguyen58301ec2018-04-20 21:33:01 -0500207
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500208 $scope.downloading = true;
CamVan Nguyen58301ec2018-04-20 21:33:01 -0500209 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 Mills36379b92018-04-02 16:53:53 -0500224 }, function(error){
CamVan Nguyen58301ec2018-04-20 21:33:01 -0500225 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 Islam1acb4122017-11-02 13:20:32 -0500234 }
235
Gunnar Mills6473a412018-03-01 16:19:37 -0600236 $scope.changePriority = function(imageId, imageVersion, from, to){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500237 $scope.priority_image_id = imageId;
Gunnar Mills6473a412018-03-01 16:19:37 -0600238 $scope.priority_image_version = imageVersion;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500239 $scope.priority_from = from;
240 $scope.priority_to = to;
Gunnar Millsf4d9bc42018-03-06 15:42:40 -0600241 $scope.confirm_priority = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500242 }
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 Mills607a1202018-03-01 16:26:50 -0600261 $scope.deleteImage = function(imageId, imageVersion){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500262 $scope.delete_image_id = imageId;
Gunnar Mills607a1202018-03-01 16:26:50 -0600263 $scope.delete_image_version = imageVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500264 $scope.confirm_delete = true;
265 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500266 $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 Islamc0161392017-06-14 15:46:15 -0500281 $scope.confirm_delete = false;
282 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500283 $scope.fileNameChanged = function(){
284 $scope.file_empty = false;
285 }
286
Iftekharul Islamc0161392017-06-14 15:46:15 -0500287 $scope.filters = {
288 bmc: {
289 imageType: 'BMC'
290 },
291 host: {
292 imageType: 'Host'
293 }
294 };
295
296 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500297 APIUtils.getFirmwares().then(function(result){
298 $scope.firmwares = result.data;
299 $scope.bmcActiveVersion = result.bmcActiveVersion;
300 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500301 });
302 }
303
304 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500305 }
306 ]
307 );
308
309})(angular);