blob: 85edcb84e588b7914bba5ee7bf08841b6b1cde65 [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 = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -050053
Gunnar Mills033025f2018-03-06 14:49:40 -060054 var pollActivationTimer = undefined;
55
Iftekharul Islamc0161392017-06-14 15:46:15 -050056 $scope.error = {
57 modal_title: "",
58 title: "",
59 desc: "",
60 type: "warning"
61 };
62
Gunnar Millsee6efd82018-03-21 15:31:56 -050063 $scope.activateImage = function(imageId, imageVersion, imageType){
Iftekharul Islamc0161392017-06-14 15:46:15 -050064 $scope.activate_image_id = imageId;
Gunnar Millse7f83972018-03-21 11:03:35 -050065 $scope.activate_image_version = imageVersion;
Gunnar Millsee6efd82018-03-21 15:31:56 -050066 $scope.activate_image_type = imageType;
Gunnar Millse7f83972018-03-21 11:03:35 -050067 $scope.activate_confirm = true;
Iftekharul Islamc0161392017-06-14 15:46:15 -050068 }
69
70 $scope.displayError = function(data){
71 $scope.error = data;
72 $scope.display_error = true;
73 }
74
Gunnar Mills033025f2018-03-06 14:49:40 -060075 function waitForActive(imageId){
76 var deferred = $q.defer();
77 var startTime = new Date();
78 pollActivationTimer = $interval(function(){
79 APIUtils.getActivation(imageId).then(function(state){
80 //@TODO: display an error message if image "Failed"
81 if(((/\.Active$/).test(state.data)) || ((/\.Failed$/).test(state.data))){
82 $interval.cancel(pollActivationTimer);
83 pollActivationTimer = undefined;
84 deferred.resolve(state);
85 }
86 }, function(error){
87 $interval.cancel(pollActivationTimer);
88 pollActivationTimer = undefined;
89 console.log(error);
90 deferred.reject(error);
Iftekharul Islam2a489552017-11-02 13:23:08 -050091 });
Gunnar Mills033025f2018-03-06 14:49:40 -060092 var now = new Date();
93 if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.ACTIVATION){
94 $interval.cancel(pollActivationTimer);
95 pollActivationTimer = undefined;
96 console.log("Time out activating image, " + imageId);
97 deferred.reject("Time out. Image did not activate in allotted time.");
98 }
99 }, Constants.POLL_INTERVALS.ACTIVATION);
100 return deferred.promise;
101 }
102
103 $scope.activateConfirmed = function(){
104 APIUtils.activateImage($scope.activate_image_id).then(function(state){
105 $scope.loadFirmwares();
106 return state;
107 }, function(error){
108 $scope.displayError({
109 modal_title: 'Error during activation call',
110 title: 'Error during activation call',
111 desc: JSON.stringify(error.data),
112 type: 'Error'
113 });
114 }).then(function(activationState){
115 waitForActive($scope.activate_image_id).then(function(state){
116 $scope.loadFirmwares();
117 }, function(error){
118 $scope.displayError({
119 modal_title: 'Error during image activation',
120 title: 'Error during image activation',
121 desc: JSON.stringify(error.data),
122 type: 'Error'
123 });
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500124 }).then(function(state){
125 if($scope.activate.reboot){
Gunnar Mills90f8e692018-04-03 14:33:52 -0500126 // Despite the new image being active, issue,
127 // https://github.com/openbmc/openbmc/issues/2764, can cause a
128 // system to brick, if the system reboots before the service to set
129 // the U-Boot variables is complete. Wait 10 seconds before rebooting
130 // to ensure this service is complete. This issue is fixed in newer images, but
131 // the user may be updating from an older image that does not that have this fix.
132 // TODO: remove this timeout after sufficient time has passed.
133 $timeout(function() {
134 APIUtils.bmcReboot(function(response){}, function(error){
135 $scope.displayError({
136 modal_title: 'Error during BMC reboot',
137 title: 'Error during BMC reboot',
138 desc: JSON.stringify(error.data),
139 type: 'Error'
140 });
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500141 });
Gunnar Mills90f8e692018-04-03 14:33:52 -0500142 }, 10000);
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500143 }
Gunnar Mills033025f2018-03-06 14:49:40 -0600144 });
145 });
146 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500147 }
148
Iftekharul Islamc0161392017-06-14 15:46:15 -0500149 $scope.upload = function(){
Gunnar Mills87c3db62018-04-13 16:14:41 -0500150 if($scope.file) {
151 $scope.uploading = true;
152 APIUtils.uploadImage($scope.file).then(function(response){
153 $scope.uploading = false;
154 $scope.loadFirmwares();
155 }, function(error){
156 $scope.uploading = false;
157 $scope.displayError({
158 modal_title: 'Error during image upload',
159 title: 'Error during image upload',
160 desc: 'Error uploading image',
161 type: 'Error uploading image, please check the image'
162 });
163 });
164 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500165 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500166
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500167 $scope.download = function(){
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500168 $scope.download_error_msg = "";
169 if(!$scope.download_host || !$scope.download_filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500170 $scope.download_error_msg = "Field is required!";
171 return false;
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500172 }
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500173 $scope.downloading = true;
174 APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
Gunnar Mills36379b92018-04-02 16:53:53 -0500175 $scope.downloading = false;
176 // TODO: refresh firmware page to display new image
177 }, function(error){
178 $scope.downloading = false;
179 $scope.displayError({
180 modal_title: 'Error during downloading Image',
181 title: 'Error during downloading Image',
182 desc: JSON.stringify(error),
183 type: 'Error'
184 });
185 });
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500186 }
187
Gunnar Mills6473a412018-03-01 16:19:37 -0600188 $scope.changePriority = function(imageId, imageVersion, from, to){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500189 $scope.priority_image_id = imageId;
Gunnar Mills6473a412018-03-01 16:19:37 -0600190 $scope.priority_image_version = imageVersion;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500191 $scope.priority_from = from;
192 $scope.priority_to = to;
Gunnar Millsf4d9bc42018-03-06 15:42:40 -0600193 $scope.confirm_priority = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500194 }
195
196 $scope.confirmChangePriority = function(){
197 $scope.loading = true;
198 APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){
199 $scope.loading = false;
200 if(response.status == 'error'){
201 $scope.displayError({
202 modal_title: response.data.description,
203 title: response.data.description,
204 desc: response.data.exception,
205 type: 'Error'
206 });
207 }else{
208 $scope.loadFirmwares();
209 }
210 });
211 $scope.confirm_priority = false;
212 }
Gunnar Mills607a1202018-03-01 16:26:50 -0600213 $scope.deleteImage = function(imageId, imageVersion){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500214 $scope.delete_image_id = imageId;
Gunnar Mills607a1202018-03-01 16:26:50 -0600215 $scope.delete_image_version = imageVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500216 $scope.confirm_delete = true;
217 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500218 $scope.confirmDeleteImage = function(){
219 $scope.loading = true;
220 APIUtils.deleteImage($scope.delete_image_id).then(function(response){
221 $scope.loading = false;
222 if(response.status == 'error'){
223 $scope.displayError({
224 modal_title: response.data.description,
225 title: response.data.description,
226 desc: response.data.exception,
227 type: 'Error'
228 });
229 }else{
230 $scope.loadFirmwares();
231 }
232 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500233 $scope.confirm_delete = false;
234 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500235 $scope.fileNameChanged = function(){
236 $scope.file_empty = false;
237 }
238
Iftekharul Islamc0161392017-06-14 15:46:15 -0500239 $scope.filters = {
240 bmc: {
241 imageType: 'BMC'
242 },
243 host: {
244 imageType: 'Host'
245 }
246 };
247
248 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500249 APIUtils.getFirmwares().then(function(result){
250 $scope.firmwares = result.data;
251 $scope.bmcActiveVersion = result.bmcActiveVersion;
252 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500253 });
254 }
255
256 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500257 }
258 ]
259 );
260
261})(angular);