luci2: move most RPC proxy function declarations into the views using them to reduce...
[project/luci2/ui.git] / luci2 / htdocs / luci2 / view / system.upgrade.js
1 L.ui.view.extend({
2         title: L.tr('Flash operations'),
3
4         testUpgrade: L.rpc.declare({
5                 object: 'luci2.system',
6                 method: 'upgrade_test',
7                 expect: { '': { } }
8         }),
9
10         startUpgrade: L.rpc.declare({
11                 object: 'luci2.system',
12                 method: 'upgrade_start',
13                 params: [ 'keep' ]
14         }),
15
16         cleanUpgrade: L.rpc.declare({
17                 object: 'luci2.system',
18                 method: 'upgrade_clean'
19         }),
20
21         restoreBackup: L.rpc.declare({
22                 object: 'luci2.system',
23                 method: 'backup_restore'
24         }),
25
26         cleanBackup: L.rpc.declare({
27                 object: 'luci2.system',
28                 method: 'backup_clean'
29         }),
30
31         getBackupConfig: L.rpc.declare({
32                 object: 'luci2.system',
33                 method: 'backup_config_get',
34                 expect: { config: '' }
35         }),
36
37         setBackupConfig: L.rpc.declare({
38                 object: 'luci2.system',
39                 method: 'backup_config_set',
40                 params: [ 'data' ]
41         }),
42
43         listBackup: L.rpc.declare({
44                 object: 'luci2.system',
45                 method: 'backup_list',
46                 expect: { files: [ ] }
47         }),
48
49         testReset: L.rpc.declare({
50                 object: 'luci2.system',
51                 method: 'reset_test',
52                 expect: { supported: false }
53         }),
54
55         startReset: L.rpc.declare({
56                 object: 'luci2.system',
57                 method: 'reset_start'
58         }),
59
60         handleFlashUpload: function() {
61                 var self = this;
62                 L.ui.upload(
63                         L.tr('Firmware upload'),
64                         L.tr('Select the sysupgrade image to flash and click "%s" to proceed.').format(L.tr('Ok')), {
65                                 filename: '/tmp/firmware.bin',
66                                 success: function(info) {
67                                         self.handleFlashVerify(info);
68                                 }
69                         }
70                 );
71         },
72
73         handleFlashVerify: function(info) {
74                 var self = this;
75                 self.testUpgrade().then(function(res) {
76                         if (res.code == 0)
77                         {
78                                 L.ui.dialog(
79                                         L.tr('Verify firmware'), [
80                                                 $('<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'))),
81                                                 $('<ul />')
82                                                         .append($('<li />')
83                                                                 .append($('<strong />').text(L.tr('Checksum') + ': '))
84                                                                 .append(info.checksum))
85                                                         .append($('<li />')
86                                                                 .append($('<strong />').text(L.tr('Size') + ': '))
87                                                                 .append('%1024mB'.format(info.size))),
88                                                 $('<label />')
89                                                         .append($('<input />')
90                                                                 .attr('type', 'checkbox')
91                                                                 .prop('checked', true))
92                                                         .append(' ')
93                                                         .append(L.tr('Keep configuration when reflashing'))
94                                         ], {
95                                                 style: 'confirm',
96                                                 confirm: function() {
97                                                         //self.startUpgrade().then(function() {
98                                                         //      L.ui.reconnect();
99                                                         //});
100
101                                                         alert('Flash...');
102                                                 }
103                                         }
104                                 );
105                         }
106                         else
107                         {
108                                 L.ui.dialog(
109                                         L.tr('Invalid image'), [
110                                                 $('<p />').text(L.tr('Firmware image verification failed, the "sysupgrade" command responded with the message below:')),
111                                                 $('<pre />')
112                                                         .addClass('alert-message')
113                                                         .text(res.stdout || res.stderr),
114                                                 $('<p />').text(L.tr('Image verification failed with code %d.').format(res.code))
115                                         ], {
116                                                 style: 'close',
117                                                 close: function() {
118                                                         self.cleanUpgrade().then(function() {
119                                                                 L.ui.dialog(false);
120                                                         });
121                                                 }
122                                         }
123                                 );
124                         }
125                 });
126         },
127
128         handleBackupUpload: function() {
129                 var self = this;
130                 L.ui.upload(
131                         L.tr('Backup restore'),
132                         L.tr('Select the backup archive to restore and click "%s" to proceed.').format(L.tr('Ok')), {
133                                 filename: '/tmp/backup.tar.gz',
134                                 success: function(info) {
135                                         self.handleBackupVerify(info);
136                                 }
137                         }
138                 );
139         },
140
141         handleBackupVerify: function(info) {
142                 var self = this;
143                 L.ui.dialog(
144                         L.tr('Backup restore'), [
145                                 $('<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'))),
146                                 $('<ul />')
147                                         .append($('<li />')
148                                                 .append($('<strong />').text(L.tr('Checksum') + ': '))
149                                                 .append(info.checksum))
150                                         .append($('<li />')
151                                                 .append($('<strong />').text(L.tr('Size') + ': '))
152                                                 .append('%1024mB'.format(info.size)))
153                         ], {
154                                 style: 'confirm',
155                                 confirm: function() {
156                                         self.handleBackupRestore();
157                                 }
158                         }
159                 );
160         },
161
162         handleBackupRestore: function() {
163                 var self = this;
164                 self.restoreBackup().then(function(res) {
165                         if (res.code == 0)
166                         {
167                                 L.ui.dialog(
168                                         L.tr('Backup restore'), [
169                                                 $('<p />').text(L.tr('The backup was successfully restored, it is advised to reboot the system now in order to apply all configuration changes.')),
170                                                 $('<input />')
171                                                         .addClass('cbi-button')
172                                                         .attr('type', 'button')
173                                                         .attr('value', L.tr('Reboot system'))
174                                                         .click(function() { alert('Reboot...'); })
175                                         ], {
176                                                 style: 'close',
177                                                 close: function() {
178                                                         self.cleanBackup().then(function() {
179                                                                 L.ui.dialog(false);
180                                                         });
181                                                 }
182                                         }
183                                 );
184                         }
185                         else
186                         {
187                                 L.ui.dialog(
188                                         L.tr('Backup restore'), [
189                                                 $('<p />').text(L.tr('Backup restoration failed, the "sysupgrade" command responded with the message below:')),
190                                                 $('<pre />')
191                                                         .addClass('alert-message')
192                                                         .text(res.stdout || res.stderr),
193                                                 $('<p />').text(L.tr('Backup restoration failed with code %d.').format(res.code))
194                                         ], {
195                                                 style: 'close',
196                                                 close: function() {
197                                                         self.cleanBackup().then(function() {
198                                                                 L.ui.dialog(false);
199                                                         });
200                                                 }
201                                         }
202                                 );
203                         }
204                 });
205         },
206
207         handleBackupDownload: function() {
208                 var form = $('#btn_backup').parent();
209
210                 form.find('[name=sessionid]').val(L.globals.sid);
211                 form.submit();
212         },
213
214         handleReset: function() {
215                 var self = this;
216                 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!'), {
217                         style: 'confirm',
218                         confirm: function() {
219                                 //self.startReset().then(function() {
220                                 //      L.ui.reconnect();
221                                 //});
222
223                                 alert('Reset...');
224                         }
225                 });
226         },
227
228         execute: function() {
229                 var self = this;
230
231                 self.testReset().then(function(reset_avail) {
232                         if (!reset_avail) {
233                                 $('#btn_reset').prop('disabled', true);
234                         }
235
236                         if (!self.options.acls.backup) {
237                                 $('#btn_restore, #btn_save, textarea').prop('disabled', true);
238                         }
239                         else {
240                                 $('#btn_backup').click(function() { self.handleBackupDownload(); });
241                                 $('#btn_restore').click(function() { self.handleBackupUpload(); });
242                         }
243
244                         if (!self.options.acls.upgrade) {
245                                 $('#btn_flash, #btn_reset').prop('disabled', true);
246                         }
247                         else {
248                                 $('#btn_flash').click(function() { self.handleFlashUpload(); });
249                                 $('#btn_reset').click(function() { self.handleReset(); });
250                         }
251
252                         return self.getBackupConfig();
253                 }).then(function(config) {
254                         $('textarea')
255                                 .attr('rows', (config.match(/\n/g) || [ ]).length + 1)
256                                 .val(config);
257
258                         $('#btn_save')
259                                 .click(function() {
260                                         var data = ($('textarea').val() || '').replace(/\r/g, '').replace(/\n?$/, '\n');
261                                         L.ui.loading(true);
262                                         self.setBackupConfig(data).then(function() {
263                                                 $('textarea')
264                                                         .attr('rows', (data.match(/\n/g) || [ ]).length + 1)
265                                                         .val(data);
266
267                                                 L.ui.loading(false);
268                                         });
269                                 });
270
271                         $('#btn_list')
272                                 .click(function() {
273                                         L.ui.loading(true);
274                                         self.listBackup().then(function(list) {
275                                                 L.ui.loading(false);
276                                                 L.ui.dialog(
277                                                         L.tr('Backup file list'),
278                                                         $('<textarea />')
279                                                                 .css('width', '100%')
280                                                                 .attr('rows', list.length)
281                                                                 .prop('readonly', true)
282                                                                 .addClass('form-control')
283                                                                 .val(list.join('\n')),
284                                                         { style: 'close' }
285                                                 );
286                                         });
287                                 });
288                 });
289         }
290 });