blob: 50cff2810b619bee9cc1a94d452e35868fcf3cdc [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 Millsdddb1492018-02-19 16:27:27 -0600151 if($scope.file) {
152 $scope.uploading = true;
153 APIUtils.uploadImage($scope.file).then(function(response){
154 $scope.uploading = 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 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500166 }
167 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500168
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500169 $scope.download = function(){
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500170 $scope.download_error_msg = "";
171 if(!$scope.download_host || !$scope.download_filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500172 $scope.download_error_msg = "Field is required!";
173 return false;
Gunnar Mills6d7b4a82018-04-02 15:25:36 -0500174 }
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500175 $scope.downloading = true;
176 APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
Gunnar Mills36379b92018-04-02 16:53:53 -0500177 $scope.downloading = false;
178 // TODO: refresh firmware page to display new image
179 }, function(error){
180 $scope.downloading = false;
181 $scope.displayError({
182 modal_title: 'Error during downloading Image',
183 title: 'Error during downloading Image',
184 desc: JSON.stringify(error),
185 type: 'Error'
186 });
187 });
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500188 }
189
Gunnar Mills6473a412018-03-01 16:19:37 -0600190 $scope.changePriority = function(imageId, imageVersion, from, to){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500191 $scope.priority_image_id = imageId;
Gunnar Mills6473a412018-03-01 16:19:37 -0600192 $scope.priority_image_version = imageVersion;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500193 $scope.priority_from = from;
194 $scope.priority_to = to;
Gunnar Millsf4d9bc42018-03-06 15:42:40 -0600195 $scope.confirm_priority = true;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500196 }
197
198 $scope.confirmChangePriority = function(){
199 $scope.loading = true;
200 APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){
201 $scope.loading = false;
202 if(response.status == 'error'){
203 $scope.displayError({
204 modal_title: response.data.description,
205 title: response.data.description,
206 desc: response.data.exception,
207 type: 'Error'
208 });
209 }else{
210 $scope.loadFirmwares();
211 }
212 });
213 $scope.confirm_priority = false;
214 }
Gunnar Mills607a1202018-03-01 16:26:50 -0600215 $scope.deleteImage = function(imageId, imageVersion){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500216 $scope.delete_image_id = imageId;
Gunnar Mills607a1202018-03-01 16:26:50 -0600217 $scope.delete_image_version = imageVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500218 $scope.confirm_delete = true;
219 }
Iftekharul Islam2a489552017-11-02 13:23:08 -0500220 $scope.confirmDeleteImage = function(){
221 $scope.loading = true;
222 APIUtils.deleteImage($scope.delete_image_id).then(function(response){
223 $scope.loading = false;
224 if(response.status == 'error'){
225 $scope.displayError({
226 modal_title: response.data.description,
227 title: response.data.description,
228 desc: response.data.exception,
229 type: 'Error'
230 });
231 }else{
232 $scope.loadFirmwares();
233 }
234 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500235 $scope.confirm_delete = false;
236 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500237 $scope.fileNameChanged = function(){
238 $scope.file_empty = false;
239 }
240
Iftekharul Islamc0161392017-06-14 15:46:15 -0500241 $scope.filters = {
242 bmc: {
243 imageType: 'BMC'
244 },
245 host: {
246 imageType: 'Host'
247 }
248 };
249
250 $scope.loadFirmwares = function(){
Michael Davisdf3bd122017-08-10 11:03:42 -0500251 APIUtils.getFirmwares().then(function(result){
252 $scope.firmwares = result.data;
253 $scope.bmcActiveVersion = result.bmcActiveVersion;
254 $scope.hostActiveVersion = result.hostActiveVersion;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500255 });
256 }
257
258 $scope.loadFirmwares();
Michael Davis43366db2017-05-15 18:12:35 -0500259 }
260 ]
261 );
262
263})(angular);