1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
'use strict';
'require view';
'require form';
'require uci';
'require rpc';
'require ui';
'require poll';
'require request';
'require dom';
'require fs';
let callPackagelist = rpc.declare({
object: 'rpc-sys',
method: 'packagelist',
});
let callSystemBoard = rpc.declare({
object: 'system',
method: 'board',
});
let callUpgradeStart = rpc.declare({
object: 'rpc-sys',
method: 'upgrade_start',
params: ['keep'],
});
/**
* Returns the branch of a given version. This helps to offer upgrades
* for point releases (aka within the branch).
*
* Logic:
* SNAPSHOT -> SNAPSHOT
* 21.02-SNAPSHOT -> 21.02
* 21.02.0-rc1 -> 21.02
* 19.07.8 -> 19.07
*
* @param {string} version
* Input version from which to determine the branch
* @returns {string}
* The determined branch
*/
function get_branch(version) {
return version.replace('-SNAPSHOT', '').split('.').slice(0, 2).join('.');
}
/**
* The OpenWrt revision string contains both a hash as well as the number
* commits since the OpenWrt/LEDE reboot. It helps to determine if a
* snapshot is newer than another.
*
* @param {string} revision
* Revision string of a OpenWrt device
* @returns {integer}
* The number of commits since OpenWrt/LEDE reboot
*/
function get_revision_count(revision) {
return parseInt(revision.substring(1).split('-')[0]);
}
return view.extend({
steps: {
init: [10, _('Received build request')],
download_imagebuilder: [20, _('Downloading ImageBuilder archive')],
unpack_imagebuilder: [40, _('Setting Up ImageBuilder')],
calculate_packages_hash: [60, _('Validate package selection')],
building_image: [80, _('Generating firmware image')],
},
data: {
url: '',
revision: '',
advanced_mode: 0,
rebuilder: [],
sha256_unsigned: '',
},
firmware: {
profile: '',
target: '',
version: '',
packages: [],
diff_packages: true,
filesystem: '',
},
selectImage: function (images) {
let firmware = this.firmware;
let data = this.data;
var filesystemFilter = function(e) {
return (e.filesystem == firmware.filesystem);
}
var typeFilter = function(e) {
if (firmware.target.indexOf("x86") != -1) {
// x86 images can be combined-efi (EFI) or combined (BIOS)
if (data.efi) {
return (e.type == 'combined-efi');
} else {
return (e.type == 'combined');
}
} else {
return (e.type == 'sysupgrade' || e.type == 'combined');
}
}
return images.filter(filesystemFilter).filter(typeFilter)[0];
},
handle200: function (response) {
response = response.json();
let image = this.selectImage(response.images);
if (image.name != undefined) {
this.data.sha256_unsigned = image.sha256_unsigned;
let sysupgrade_url = `${this.data.url}/store/${response.bin_dir}/${image.name}`;
let keep = E('input', { type: 'checkbox' });
keep.checked = true;
let fields = [
_('Version'),
`${response.version_number} ${response.version_code}`,
_('SHA256'),
image.sha256,
];
if (this.data.advanced_mode == 1) {
fields.push(
_('Profile'),
response.id,
_('Target'),
response.target,
_('Build Date'),
response.build_at,
_('Filename'),
image.name,
_('Filesystem'),
image.filesystem
);
}
fields.push(
'',
E('a', { href: sysupgrade_url }, _('Download firmware image'))
);
if (this.data.rebuilder) {
fields.push(_('Rebuilds'), E('div', { id: 'rebuilder_status' }));
}
let table = E('div', { class: 'table' });
for (let i = 0; i < fields.length; i += 2) {
table.appendChild(
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '33%' }, [fields[i]]),
E('td', { class: 'td left' }, [fields[i + 1]]),
])
);
}
let modal_body = [
table,
E(
'p',
{ class: 'mt-2' },
E('label', { class: 'btn' }, [
keep,
' ',
_('Keep settings and retain the current configuration'),
])
),
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
' ',
E(
'button',
{
class: 'btn cbi-button cbi-button-positive important',
click: ui.createHandlerFn(this, function () {
this.handleInstall(sysupgrade_url, keep.checked, image.sha256);
}),
},
_('Install firmware image')
),
]),
];
ui.showModal(_('Successfully created firmware image'), modal_body);
if (this.data.rebuilder) {
this.handleRebuilder();
}
}
},
handle202: function (response) {
response = response.json();
this.data.request_hash = response.request_hash;
if ('queue_position' in response) {
ui.showModal(_('Queued...'), [
E(
'p',
{ class: 'spinning' },
_('Request in build queue position %s').format(
response.queue_position
)
),
]);
} else {
ui.showModal(_('Building Firmware...'), [
E(
'p',
{ class: 'spinning' },
_('Progress: %s%% %s').format(
this.steps[response.imagebuilder_status][0],
this.steps[response.imagebuilder_status][1]
)
),
]);
}
},
handleError: function (response) {
response = response.json();
let body = [
E('p', {}, _('Server response: %s').format(response.detail)),
E(
'a',
{ href: 'https://github.com/openwrt/asu/issues' },
_('Please report the error message and request')
),
E('p', {}, _('Request Data:')),
E('pre', {}, JSON.stringify({ ...this.data, ...this.firmware }, null, 4)),
];
if (response.stdout) {
body.push(E('b', {}, 'STDOUT:'));
body.push(E('pre', {}, response.stdout));
}
if (response.stderr) {
body.push(E('b', {}, 'STDERR:'));
body.push(E('pre', {}, response.stderr));
}
body = body.concat([
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
]),
]);
ui.showModal(_('Error building the firmware image'), body);
},
handleRequest: function (server, main) {
let request_url = `${server}/api/v1/build`;
let method = 'POST';
let content = this.firmware;
/**
* If `request_hash` is available use a GET request instead of
* sending the entire object.
*/
if (this.data.request_hash && main == true) {
request_url += `/${this.data.request_hash}`;
content = {};
method = 'GET';
}
request
.request(request_url, { method: method, content: content })
.then((response) => {
switch (response.status) {
case 202:
if (main) {
this.handle202(response);
} else {
response = response.json();
let view = document.getElementById(server);
view.innerText = `⏳ (${
this.steps[response.imagebuilder_status][0]
}%) ${server}`;
}
break;
case 200:
if (main == true) {
poll.remove(this.pollFn);
this.handle200(response);
} else {
poll.remove(this.rebuilder_polls[server]);
response = response.json();
let view = document.getElementById(server);
let image = this.selectImage(response.images);
if (image.sha256_unsigned == this.data.sha256_unsigned) {
view.innerText = '✅ %s'.format(server);
} else {
view.innerHTML = `⚠️ ${server} (<a href="${server}/store/${
response.bin_dir
}/${image.name}">${_('Download')}</a>)`;
}
}
break;
case 400: // bad request
case 422: // bad package
case 500: // build failed
if (main == true) {
poll.remove(this.pollFn);
this.handleError(response);
break;
} else {
poll.remove(this.rebuilder_polls[server]);
document.getElementById(server).innerText = '🚫 %s'.format(
server
);
}
}
});
},
handleRebuilder: function () {
this.rebuilder_polls = {};
for (let rebuilder of this.data.rebuilder) {
this.rebuilder_polls[rebuilder] = L.bind(
this.handleRequest,
this,
rebuilder,
false
);
poll.add(this.rebuilder_polls[rebuilder], 5);
document.getElementById(
'rebuilder_status'
).innerHTML += `<p id="${rebuilder}">⏳ ${rebuilder}</p>`;
}
poll.start();
},
handleInstall: function (url, keep, sha256) {
ui.showModal(_('Downloading...'), [
E(
'p',
{ class: 'spinning' },
_('Downloading firmware from server to browser')
),
]);
request
.get(url, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
responseType: 'blob',
})
.then((response) => {
let form_data = new FormData();
form_data.append('sessionid', rpc.getSessionID());
form_data.append('filename', '/tmp/firmware.bin');
form_data.append('filemode', 600);
form_data.append('filedata', response.blob());
ui.showModal(_('Uploading...'), [
E(
'p',
{ class: 'spinning' },
_('Uploading firmware from browser to device')
),
]);
request
.get(`${L.env.cgi_base}/cgi-upload`, {
method: 'PUT',
content: form_data,
})
.then((response) => response.json())
.then((response) => {
if (response.sha256sum != sha256) {
ui.showModal(_('Wrong checksum'), [
E(
'p',
_('Error during download of firmware. Please try again')
),
E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
]);
} else {
ui.showModal(_('Installing...'), [
E(
'p',
{ class: 'spinning' },
_('Installing the sysupgrade. Do not unpower device!')
),
]);
L.resolveDefault(callUpgradeStart(keep), {}).then((response) => {
if (keep) {
ui.awaitReconnect(window.location.host);
} else {
ui.awaitReconnect('192.168.1.1', 'openwrt.lan');
}
});
}
});
});
},
handleCheck: function () {
let { url, revision } = this.data;
let { version, target } = this.firmware;
let candidates = [];
let request_url = `${url}/api/overview`;
if (version.endsWith('SNAPSHOT')) {
request_url = `${url}/api/v1/revision/${version}/${target}`;
}
ui.showModal(_('Searching...'), [
E(
'p',
{ class: 'spinning' },
_('Searching for an available sysupgrade of %s - %s').format(
version,
revision
)
),
]);
L.resolveDefault(request.get(request_url)).then((response) => {
if (!response.ok) {
ui.showModal(_('Error connecting to upgrade server'), [
E(
'p',
{},
_('Could not reach API at "%s". Please try again later.').format(
response.url
)
),
E('pre', {}, response.responseText),
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
]),
]);
return;
}
if (version.endsWith('SNAPSHOT')) {
const remote_revision = response.json().revision;
if (
get_revision_count(revision) < get_revision_count(remote_revision)
) {
candidates.push([version, remote_revision]);
}
} else {
const latest = response.json().latest;
for (let remote_version of latest) {
let remote_branch = get_branch(remote_version);
// already latest version installed
if (version == remote_version) {
break;
}
// skip branch upgrades outside the advanced mode
if (
this.data.branch != remote_branch &&
this.data.advanced_mode == 0
) {
continue;
}
candidates.unshift([remote_version, null]);
// don't offer branches older than the current
if (this.data.branch == remote_branch) {
break;
}
}
}
// allow to re-install running firmware in advanced mode
if (this.data.advanced_mode == 1) {
candidates.unshift([version, revision]);
}
if (candidates.length) {
let s, o;
let mapdata = {
request: {
profile: this.firmware.profile,
version: candidates[0][0],
packages: Object.keys(this.firmware.packages).sort(),
},
};
let map = new form.JSONMap(mapdata, '');
s = map.section(
form.NamedSection,
'request',
'',
'',
'Use defaults for the safest update'
);
o = s.option(form.ListValue, 'version', 'Select firmware version');
for (let candidate of candidates) {
if (candidate[0] == version && candidate[1] == revision) {
o.value(
candidate[0],
_('[installed] %s').format(
candidate[1]
? `${candidate[0]} - ${candidate[1]}`
: candidate[0]
)
);
} else {
o.value(
candidate[0],
candidate[1] ? `${candidate[0]} - ${candidate[1]}` : candidate[0]
);
}
}
if (this.data.advanced_mode == 1) {
o = s.option(form.Value, 'profile', _('Board Name / Profile'));
o = s.option(form.DynamicList, 'packages', _('Packages'));
}
L.resolveDefault(map.render()).then((form_rendered) => {
ui.showModal(_('New firmware upgrade available'), [
E(
'p',
_('Currently running: %s - %s').format(
this.firmware.version,
this.data.revision
)
),
form_rendered,
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
' ',
E(
'button',
{
class: 'btn cbi-button cbi-button-positive important',
click: ui.createHandlerFn(this, function () {
map.save().then(() => {
this.firmware.packages = mapdata.request.packages;
this.firmware.version = mapdata.request.version;
this.firmware.profile = mapdata.request.profile;
this.pollFn = L.bind(function () {
this.handleRequest(this.data.url, true);
}, this);
poll.add(this.pollFn, 5);
poll.start();
});
}),
},
_('Request firmware image')
),
]),
]);
});
} else {
ui.showModal(_('No upgrade available'), [
E(
'p',
_('The device runs the latest firmware version %s - %s').format(
version,
revision
)
),
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
]),
]);
}
});
},
load: function () {
return Promise.all([
L.resolveDefault(callPackagelist(), {}),
L.resolveDefault(callSystemBoard(), {}),
L.resolveDefault(fs.stat('/sys/firmware/efi'), null),
uci.load('attendedsysupgrade'),
]);
},
render: function (response) {
this.firmware.client =
'luci/' + response[0].packages['luci-app-attendedsysupgrade'];
this.firmware.packages = response[0].packages;
this.firmware.profile = response[1].board_name;
this.firmware.target = response[1].release.target;
this.firmware.version = response[1].release.version;
this.data.branch = get_branch(response[1].release.version);
this.firmware.filesystem = response[1].rootfs_type;
this.data.revision = response[1].release.revision;
this.data.efi = response[2];
this.data.url = uci.get_first('attendedsysupgrade', 'server', 'url');
this.data.advanced_mode =
uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0;
this.data.rebuilder = uci.get_first(
'attendedsysupgrade',
'server',
'rebuilder'
);
return E('p', [
E('h2', _('Attended Sysupgrade')),
E(
'p',
_(
'The attended sysupgrade service allows to easily upgrade vanilla and custom firmware images.'
)
),
E(
'p',
_(
'This is done by building a new firmware on demand via an online service.'
)
),
E(
'p',
_('Currently running: %s - %s').format(
this.firmware.version,
this.data.revision
)
),
E(
'button',
{
class: 'btn cbi-button cbi-button-positive important',
click: ui.createHandlerFn(this, this.handleCheck),
},
_('Search for firmware upgrade')
),
]);
},
handleSaveApply: null,
handleSave: null,
handleReset: null,
});
|