blob: d209a5a8463e15d720137ca4244c3c609a4a3cfd [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',
Gunnar Mills90f8e692018-04-03 14:33:52 -050025 '$timeout',
26 function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants, $interval, $q, $timeout) {
Michael Davis43366db2017-05-15 18:12:35 -050027 $scope.dataService = dataService;
28
Michael Davis43366db2017-05-15 18:12:35 -050029 //Scroll to target anchor
30 $scope.gotoAnchor = function () {
31 $location.hash('upload');
32 $anchorScroll();
33 };
Iftekharul Islamc0161392017-06-14 15:46:15 -050034
35 $scope.firmwares = [];
36 $scope.bmcActiveVersion = "";
37 $scope.hostActiveVersion = "";
38 $scope.display_error = false;
Gunnar Millse7f83972018-03-21 11:03:35 -050039 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050040 $scope.delete_image_id = "";
Gunnar Mills607a1202018-03-01 16:26:50 -060041 $scope.delete_image_version = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -050042 $scope.activate_image_id = "";
Gunnar Millse7f83972018-03-21 11:03:35 -050043 $scope.activate_image_version = "";
Gunnar Millsee6efd82018-03-21 15:31:56 -050044 $scope.activate_image_type = "";
Iftekharul Islam1acb4122017-11-02 13:20:32 -050045 $scope.priority_image_id = "";
Gunnar Mills6473a412018-03-01 16:19:37 -060046 $scope.priority_image_version = "";
Iftekharul Islam1acb4122017-11-02 13:20:32 -050047 $scope.priority_from = -1;
48 $scope.priority_to = -1;
49 $scope.confirm_priority = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -050050 $scope.file_empty = true;
51 $scope.uploading = false;
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -050052 $scope.activate = { reboot: true };
Gunnar Mills6d7b4a82018-04-02 15:25:36 -050053 $scope.download_error_msg = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -050054
Gunnar Mills033025f2018-03-06 14:49:40 -060055 var pollActivationTimer = undefined;
56
Iftekharul Islamc0161392017-06-14 15:46:15 -050057 $scope.error = {
58 modal_title: "",
59 title: "",
60 desc: "",
61 type: "warning"
62 };
63
Gunnar Millsee6efd82018-03-21 15:31:56 -050064 $scope.activateImage = function(imageId, imageVersion, imageType){
Iftekharul Islamc0161392017-06-14 15:46:15 -050065 $scope.activate_image_id = imageId;
Gunnar Millse7f83972018-03-21 11:03:35 -050066 $scope.activate_image_version = imageVersion;
Gunnar Millsee6efd82018-03-21 15:31:56 -050067 $scope.activate_image_type = imageType;
Gunnar Millse7f83972018-03-21 11:03:35 -050068 $scope.activate_confirm = true;
Iftekharul Islamc0161392017-06-14 15:46:15 -050069 }
70
71 $scope.displayError = function(data){
72 $scope.error = data;
73 $scope.display_error = true;
74 }
75
Gunnar Mills033025f2018-03-06 14:49:40 -060076 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 Islam2a489552017-11-02 13:23:08 -050092 });
Gunnar Mills033025f2018-03-06 14:49:40 -060093 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 Mills6d9ef5a2018-03-26 15:34:31 -0500125 }).then(function(state){
126 if($scope.activate.reboot){
Gunnar Mills90f8e692018-04-03 14:33:52 -0500127 // 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 Mills6d9ef5a2018-03-26 15:34:31 -0500142 });
Gunnar Mills90f8e692018-04-03 14:33:52 -0500143 }, 10000);
Gunnar Mills6d9ef5a2018-03-26 15:34:31 -0500144 }
Gunnar Mills033025f2018-03-06 14:49:40 -0600145 });
146 });
147 $scope.activate_confirm = false;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500148 }
149
Iftekharul Islamc0161392017-06-14 15:46:15 -0500150 $scope.upload = function(){
Gunnar Mills87c3db62018-04-13 16:14:41 -0500151 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 Islamc0161392017-06-14 15:46:15 -0500166 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500167
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500168 $scope.download = function(){
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500169 $scope.download_error_msg = "";
170 if(!$scope.download_host || !$scope.download_filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500171 $scope.download_error_msg = "Field is required!";
172 return false;
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500173 }
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500174 $scope.downloading = true;
175 APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
Gunnar Mills36379b92018-04-02 16:53:53 -0500176 $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 Islam1acb4122017-11-02 13:20:32 -0500187 }
188
Gunnar Mills6473a412018-03-01 16:19:37 -0600189 $scope.changePriority = function(imageId, imageVersion, from, to){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500190 $scope.priority_image_id = imageId;
Gunnar Mills6473a412018-03-01 16:19:37 -0600191 $scope.priority_image_version = imageVersion;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500192 $scope.priority_from = from;
193 $scope.priority_to = to;
Gunnar Millsf4d9bc42018-03-06 15:42:40 -0600194 $scope.confirm_priority = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500195 }
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 Mills607a1202018-03-01 16:26:50 -0600214 $scope.deleteImage = function(imageId, imageVersion){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500215 $scope.delete_image_id = imageId;
Gunnar Mills607a1202018-03-01 16:26:50 -0600216 $scope.delete_image_version = imageVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500217 $scope.confirm_delete = true;
218 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500219 $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 Islamc0161392017-06-14 15:46:15 -0500234 $scope.confirm_delete = false;
235 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500236 $scope.fileNameChanged = function(){
237 $scope.file_empty = false;
238 }
239
Iftekharul Islamc0161392017-06-14 15:46:15 -0500240 $scope.filters = {
241 bmc: {
242 imageType: 'BMC'
243 },
244 host: {
245 imageType: 'Host'
246 }
247 };
248
249 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500250 APIUtils.getFirmwares().then(function(result){
251 $scope.firmwares = result.data;
252 $scope.bmcActiveVersion = result.bmcActiveVersion;
253 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500254 });
255 }
256
257 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500258 }
259 ]
260 );
261
262})(angular);