luci2: adapt views to changed luci2 framework
[project/luci2/ui.git] / luci2 / htdocs / luci2 / view / system.upgrade.js
1 L.ui.view.extend({
2         title: L.tr('Flash operations'),
3
4         handle_flash_upload: function() {
5                 var self = this;
6                 L.ui.upload(
7                         L.tr('Firmware upload'),
8                         L.tr('Select the sysupgrade image to flash and click "%s" to proceed.').format(L.tr('Ok')), {
9                                 filename: '/tmp/firmware.bin',
10                                 success: function(info) {
11                                         self.handle_flash_verify(info);
12                                 }
13                         }
14                 );
15         },
16
17         handle_flash_verify: function(info) {
18                 var self = this;
19                 L.system.testUpgrade().then(function(res) {
20                         if (res.code == 0)
21                         {
22                                 L.ui.dialog(
23                                         L.tr('Verify firmware'), [
24                                                 $('<p />').text(L.tr('The firmware image was uploaded completely. Please verify the checksum and file size below, then click "%s" to start the flash procedure.').format(L.tr('Ok'))),
25                                                 $('<ul />')
26                                                         .append($('<li />')
27                                                                 .append($('<strong />').text(L.tr('Checksum') + ': '))
28                                                                 .append(info.checksum))
29                                                         .append($('<li />')
30                                                                 .append($('<strong />').text(L.tr('Size') + ': '))
31                                                                 .append('%1024mB'.format(info.size))),
32                                                 $('<label />')
33                                                         .append($('<input />')
34                                                                 .attr('type', 'checkbox')
35                                                                 .prop('checked', true))
36                                                         .append(' ')
37                                                         .append(L.tr('Keep configuration when reflashing'))
38                                         ], {
39                                                 style: 'confirm',
40                                                 confirm: function() {
41                                                         //L.system.startUpgrade().then(function() {
42                                                         //      L.ui.reconnect();
43                                                         //});
44
45                                                         alert('Flash...');
46                                                 }
47                                         }
48                                 );
49                         }
50                         else
51                         {
52                                 L.ui.dialog(
53                                         L.tr('Invalid image'), [
54                                                 $('<p />').text(L.tr('Firmware image verification failed, the "sysupgrade" command responded with the message below:')),
55                                                 $('<pre />')
56                                                         .addClass('alert-message')
57                                                         .text(res.stdout || res.stderr),
58                                                 $('<p />').text(L.tr('Image verification failed with code %d.').format(res.code))
59                                         ], {
60                                                 style: 'close',
61                                                 close: function() {
62                                                         L.system.cleanUpgrade().then(function() {
63                                                                 L.ui.dialog(false);
64                                                         });
65                                                 }
66                                         }
67                                 );
68                         }
69                 });
70         },
71
72         handle_backup_upload: function() {
73                 var self = this;
74                 L.ui.upload(
75                         L.tr('Backup restore'),
76                         L.tr('Select the backup archive to restore and click "%s" to proceed.').format(L.tr('Ok')), {
77                                 filename: '/tmp/backup.tar.gz',
78                                 success: function(info) {
79                                         self.handle_backup_verify(info);
80                                 }
81                         }
82                 );
83         },
84
85         handle_backup_verify: function(info) {
86                 var self = this;
87                 L.ui.dialog(
88                         L.tr('Backup restore'), [
89                                 $('<p />').text(L.tr('The backup archive was uploaded completely. Please verify the checksum and file size below, then click "%s" to restore the archive.').format(L.tr('Ok'))),
90                                 $('<ul />')
91                                         .append($('<li />')
92                                                 .append($('<strong />').text(L.tr('Checksum') + ': '))
93                                                 .append(info.checksum))
94                                         .append($('<li />')
95                                                 .append($('<strong />').text(L.tr('Size') + ': '))
96                                                 .append('%1024mB'.format(info.size)))
97                         ], {
98                                 style: 'confirm',
99                                 confirm: function() {
100                                         self.handle_backup_restore();
101                                 }
102                         }
103                 );
104         },
105
106         handle_backup_restore: function() {
107                 var self = this;
108                 L.system.restoreBackup().then(function(res) {
109                         if (res.code == 0)
110                         {
111                                 L.ui.dialog(
112                                         L.tr('Backup restore'), [
113                                                 $('<p />').text(L.tr('The backup was successfully restored, it is advised to reboot the system now in order to apply all configuration changes.')),
114                                                 $('<input />')
115                                                         .addClass('cbi-button')
116                                                         .attr('type', 'button')
117                                                         .attr('value', L.tr('Reboot system'))
118                                                         .click(function() { alert('Reboot...'); })
119                                         ], {
120                                                 style: 'close',
121                                                 close: function() {
122                                                         L.system.cleanBackup().then(function() {
123                                                                 L.ui.dialog(false);
124                                                         });
125                                                 }
126                                         }
127                                 );
128                         }
129                         else
130                         {
131                                 L.ui.dialog(
132                                         L.tr('Backup restore'), [
133                                                 $('<p />').text(L.tr('Backup restoration failed, the "sysupgrade" command responded with the message below:')),
134                                                 $('<pre />')
135                                                         .addClass('alert-message')
136                                                         .text(res.stdout || res.stderr),
137                                                 $('<p />').text(L.tr('Backup restoration failed with code %d.').format(res.code))
138                                         ], {
139                                                 style: 'close',
140                                                 close: function() {
141                                                         L.system.cleanBackup().then(function() {
142                                                                 L.ui.dialog(false);
143                                                         });
144                                                 }
145                                         }
146                                 );
147                         }
148                 });
149         },
150
151         handle_backup_download: function() {
152                 var form = $('#btn_backup').parent();
153
154                 form.find('[name=sessionid]').val(L.globals.sid);
155                 form.submit();
156         },
157
158         handle_reset: function() {
159                 L.ui.dialog(L.tr('Really reset all changes?'), L.tr('This will reset the system to its initial configuration, all changes made since the initial flash will be lost!'), {
160                         style: 'confirm',
161                         confirm: function() {
162                                 //L.system.startReset().then(function() {
163                                 //      L.ui.reconnect();
164                                 //});
165
166                                 alert('Reset...');
167                         }
168                 });
169         },
170
171         execute: function() {
172                 var self = this;
173
174                 L.system.testReset().then(function(reset_avail) {
175                         if (!reset_avail) {
176                                 $('#btn_reset').prop('disabled', true);
177                         }
178
179                         if (!self.options.acls.backup) {
180                                 $('#btn_restore, #btn_save, textarea').prop('disabled', true);
181                         }
182                         else {
183                                 $('#btn_backup').click(function() { self.handle_backup_download(); });
184                                 $('#btn_restore').click(function() { self.handle_backup_upload(); });
185                         }
186
187                         if (!self.options.acls.upgrade) {
188                                 $('#btn_flash, #btn_reset').prop('disabled', true);
189                         }
190                         else {
191                                 $('#btn_flash').click(function() { self.handle_flash_upload(); });
192                                 $('#btn_reset').click(function() { self.handle_reset(); });
193                         }
194
195                         return L.system.getBackupConfig();
196                 }).then(function(config) {
197                         $('textarea')
198                                 .attr('rows', (config.match(/\n/g) || [ ]).length + 1)
199                                 .val(config);
200
201                         $('#btn_save')
202                                 .click(function() {
203                                         var data = ($('textarea').val() || '').replace(/\r/g, '').replace(/\n?$/, '\n');
204                                         L.ui.loading(true);
205                                         L.system.setBackupConfig(data).then(function() {
206                                                 $('textarea')
207                                                         .attr('rows', (data.match(/\n/g) || [ ]).length + 1)
208                                                         .val(data);
209
210                                                 L.ui.loading(false);
211                                         });
212                                 });
213
214                         $('#btn_list')
215                                 .click(function() {
216                                         L.ui.loading(true);
217                                         L.system.listBackup().then(function(list) {
218                                                 L.ui.loading(false);
219                                                 L.ui.dialog(
220                                                         L.tr('Backup file list'),
221                                                         $('<textarea />')
222                                                                 .css('width', '100%')
223                                                                 .attr('rows', list.length)
224                                                                 .prop('readonly', true)
225                                                                 .addClass('form-control')
226                                                                 .val(list.join('\n')),
227                                                         { style: 'close' }
228                                                 );
229                                         });
230                                 });
231                 }).then(function() {
232                         $('#tabs').show().tabs();
233                 });
234         }
235 });