12 let callPackagelist = rpc.declare({
14 method: 'packagelist',
17 let callSystemBoard = rpc.declare({
22 let callUpgradeStart = rpc.declare({
24 method: 'upgrade_start',
29 * Returns the branch of a given version. This helps to offer upgrades
30 * for point releases (aka within the branch).
33 * SNAPSHOT -> SNAPSHOT
34 * 21.02-SNAPSHOT -> 21.02
35 * 21.02.0-rc1 -> 21.02
38 * @param {string} version
39 * Input version from which to determine the branch
41 * The determined branch
43 function get_branch(version) {
44 return version.replace('-SNAPSHOT', '').split('.').slice(0, 2).join('.');
48 * The OpenWrt revision string contains both a hash as well as the number
49 * commits since the OpenWrt/LEDE reboot. It helps to determine if a
50 * snapshot is newer than another.
52 * @param {string} revision
53 * Revision string of a OpenWrt device
55 * The number of commits since OpenWrt/LEDE reboot
57 function get_revision_count(revision) {
58 return parseInt(revision.substring(1).split('-')[0]);
63 init: [10, _('Received build request')],
64 download_imagebuilder: [20, _('Downloading ImageBuilder archive')],
65 unpack_imagebuilder: [40, _('Setting Up ImageBuilder')],
66 calculate_packages_hash: [60, _('Validate package selection')],
67 building_image: [80, _('Generating firmware image')],
73 selectImage: function (images, data, firmware) {
74 var filesystemFilter = function(e) {
75 return (e.filesystem == firmware.filesystem);
77 var typeFilter = function(e) {
78 if (firmware.target.indexOf("x86") != -1) {
79 // x86 images can be combined-efi (EFI) or combined (BIOS)
81 return (e.type == 'combined-efi');
83 return (e.type == 'combined');
86 return (e.type == 'sysupgrade' || e.type == 'combined');
89 return images.filter(filesystemFilter).filter(typeFilter)[0];
92 handle200: function (response, content, data, firmware) {
93 response = response.json();
94 let image = this.selectImage(response.images, data, firmware);
96 if (image.name != undefined) {
97 this.sha256_unsigned = image.sha256_unsigned;
98 let sysupgrade_url = `${data.url}/store/${response.bin_dir}/${image.name}`;
100 let keep = E('input', { type: 'checkbox' });
105 `${response.version_number} ${response.version_code}`,
110 if (data.advanced_mode == 1) {
127 E('a', { href: sysupgrade_url }, _('Download firmware image'))
129 if (data.rebuilder) {
130 fields.push(_('Rebuilds'), E('div', { id: 'rebuilder_status' }));
133 let table = E('div', { class: 'table' });
135 for (let i = 0; i < fields.length; i += 2) {
137 E('tr', { class: 'tr' }, [
138 E('td', { class: 'td left', width: '33%' }, [fields[i]]),
139 E('td', { class: 'td left' }, [fields[i + 1]]),
149 E('label', { class: 'btn' }, [
152 _('Keep settings and retain the current configuration'),
155 E('div', { class: 'right' }, [
156 E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
161 class: 'btn cbi-button cbi-button-positive important',
162 click: ui.createHandlerFn(this, function () {
163 this.handleInstall(sysupgrade_url, keep.checked, image.sha256);
166 _('Install firmware image')
171 ui.showModal(_('Successfully created firmware image'), modal_body);
172 if (data.rebuilder) {
173 this.handleRebuilder(content, data, firmware);
178 handle202: function (response) {
179 response = response.json();
180 this.request_hash = response.request_hash;
182 if ('queue_position' in response) {
183 ui.showModal(_('Queued...'), [
186 { class: 'spinning' },
187 _('Request in build queue position %s').format(
188 response.queue_position
193 ui.showModal(_('Building Firmware...'), [
196 { class: 'spinning' },
197 _('Progress: %s%% %s').format(
198 this.steps[response.imagebuilder_status][0],
199 this.steps[response.imagebuilder_status][1]
206 handleError: function (response, data, firmware) {
207 response = response.json();
208 const request_data = {
210 request_hash: this.request_hash,
211 sha256_unsigned: this.sha256_unsigned,
215 E('p', {}, _('Server response: %s').format(response.detail)),
218 { href: 'https://github.com/openwrt/asu/issues' },
219 _('Please report the error message and request')
221 E('p', {}, _('Request Data:')),
222 E('pre', {}, JSON.stringify({ ...request_data }, null, 4)),
225 if (response.stdout) {
226 body.push(E('b', {}, 'STDOUT:'));
227 body.push(E('pre', {}, response.stdout));
230 if (response.stderr) {
231 body.push(E('b', {}, 'STDERR:'));
232 body.push(E('pre', {}, response.stderr));
236 E('div', { class: 'right' }, [
237 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
241 ui.showModal(_('Error building the firmware image'), body);
244 handleRequest: function (server, main, content, data, firmware) {
245 let request_url = `${server}/api/v1/build`;
247 let local_content = content;
250 * If `request_hash` is available use a GET request instead of
251 * sending the entire object.
253 if (this.request_hash && main == true) {
254 request_url += `/${this.request_hash}`;
260 .request(request_url, { method: method, content: local_content })
261 .then((response) => {
262 switch (response.status) {
265 this.handle202(response);
267 response = response.json();
269 let view = document.getElementById(server);
270 view.innerText = `⏳ (${
271 this.steps[response.imagebuilder_status][0]
277 poll.remove(this.pollFn);
278 this.handle200(response, content, data, firmware);
280 poll.remove(this.rebuilder_polls[server]);
281 response = response.json();
282 let view = document.getElementById(server);
283 let image = this.selectImage(response.images, data, firmware);
284 if (image.sha256_unsigned == this.sha256_unsigned) {
285 view.innerText = '✅ %s'.format(server);
287 view.innerHTML = `⚠️ ${server} (<a href="${server}/store/${
289 }/${image.name}">${_('Download')}</a>)`;
293 case 400: // bad request
294 case 422: // bad package
295 case 500: // build failed
297 poll.remove(this.pollFn);
298 this.handleError(response, data, firmware);
301 poll.remove(this.rebuilder_polls[server]);
302 document.getElementById(server).innerText = '🚫 %s'.format(
310 handleRebuilder: function (content, data, firmware) {
311 this.rebuilder_polls = {};
312 for (let rebuilder of data.rebuilder) {
313 this.rebuilder_polls[rebuilder] = L.bind(
322 poll.add(this.rebuilder_polls[rebuilder], 5);
323 document.getElementById(
325 ).innerHTML += `<p id="${rebuilder}">⏳ ${rebuilder}</p>`;
330 handleInstall: function (url, keep, sha256) {
331 ui.showModal(_('Downloading...'), [
334 { class: 'spinning' },
335 _('Downloading firmware from server to browser')
342 'Content-Type': 'application/x-www-form-urlencoded',
344 responseType: 'blob',
346 .then((response) => {
347 let form_data = new FormData();
348 form_data.append('sessionid', rpc.getSessionID());
349 form_data.append('filename', '/tmp/firmware.bin');
350 form_data.append('filemode', 600);
351 form_data.append('filedata', response.blob());
353 ui.showModal(_('Uploading...'), [
356 { class: 'spinning' },
357 _('Uploading firmware from browser to device')
362 .get(`${L.env.cgi_base}/cgi-upload`, {
366 .then((response) => response.json())
367 .then((response) => {
368 if (response.sha256sum != sha256) {
369 ui.showModal(_('Wrong checksum'), [
372 _('Error during download of firmware. Please try again')
374 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
377 ui.showModal(_('Installing...'), [
380 { class: 'spinning' },
381 _('Installing the sysupgrade. Do not unpower device!')
385 L.resolveDefault(callUpgradeStart(keep), {}).then((response) => {
387 ui.awaitReconnect(window.location.host);
389 ui.awaitReconnect('192.168.1.1', 'openwrt.lan');
397 handleCheck: function (data, firmware) {
398 this.request_hash = '';
399 let { url, revision, advanced_mode, branch } = data;
400 let { version, target, profile, packages } = firmware;
402 let request_url = `${url}/api/overview`;
403 if (version.endsWith('SNAPSHOT')) {
404 request_url = `${url}/api/v1/revision/${version}/${target}`;
407 ui.showModal(_('Searching...'), [
410 { class: 'spinning' },
411 _('Searching for an available sysupgrade of %s - %s').format(
418 L.resolveDefault(request.get(request_url)).then((response) => {
420 ui.showModal(_('Error connecting to upgrade server'), [
424 _('Could not reach API at "%s". Please try again later.').format(
428 E('pre', {}, response.responseText),
429 E('div', { class: 'right' }, [
430 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
435 if (version.endsWith('SNAPSHOT')) {
436 const remote_revision = response.json().revision;
438 get_revision_count(revision) < get_revision_count(remote_revision)
440 candidates.push([version, remote_revision]);
443 const latest = response.json().latest;
445 for (let remote_version of latest) {
446 let remote_branch = get_branch(remote_version);
448 // already latest version installed
449 if (version == remote_version) {
453 // skip branch upgrades outside the advanced mode
454 if (branch != remote_branch && advanced_mode == 0) {
458 candidates.unshift([remote_version, null]);
460 // don't offer branches older than the current
461 if (branch == remote_branch) {
467 // allow to re-install running firmware in advanced mode
468 if (advanced_mode == 1) {
469 candidates.unshift([version, revision]);
472 if (candidates.length) {
478 version: candidates[0][0],
479 version_code: revision,
480 packages: Object.keys(packages).sort(),
484 let map = new form.JSONMap(mapdata, '');
491 'Use defaults for the safest update'
493 o = s.option(form.ListValue, 'version', 'Select firmware version');
494 for (let candidate of candidates) {
495 if (candidate[0] == version && candidate[1] == revision) {
498 _('[installed] %s').format(
500 ? `${candidate[0]} - ${candidate[1]}`
507 candidate[1] ? `${candidate[0]} - ${candidate[1]}` : candidate[0]
512 if (advanced_mode == 1) {
513 o = s.option(form.Value, 'profile', _('Board Name / Profile'));
514 o = s.option(form.DynamicList, 'packages', _('Packages'));
517 L.resolveDefault(map.render()).then((form_rendered) => {
518 ui.showModal(_('New firmware upgrade available'), [
521 _('Currently running: %s - %s').format(
527 E('div', { class: 'right' }, [
528 E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
533 class: 'btn cbi-button cbi-button-positive important',
534 click: ui.createHandlerFn(this, function () {
535 map.save().then(() => {
538 packages: mapdata.request.packages,
539 version: mapdata.request.version,
540 version_code: mapdata.request.version_code,
541 profile: mapdata.request.profile
543 this.pollFn = L.bind(function () {
544 this.handleRequest(url, true, content, data, firmware);
546 poll.add(this.pollFn, 5);
551 _('Request firmware image')
557 ui.showModal(_('No upgrade available'), [
560 _('The device runs the latest firmware version %s - %s').format(
565 E('div', { class: 'right' }, [
566 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
573 load: async function () {
574 const promises = await Promise.all([
575 L.resolveDefault(callPackagelist(), {}),
576 L.resolveDefault(callSystemBoard(), {}),
577 L.resolveDefault(fs.stat('/sys/firmware/efi'), null),
578 uci.load('attendedsysupgrade'),
581 url: uci.get_first('attendedsysupgrade', 'server', 'url'),
582 branch: get_branch(promises[1].release.version),
583 revision: promises[1].release.revision,
585 advanced_mode: uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0,
586 rebuilder: uci.get_first('attendedsysupgrade', 'server', 'rebuilder')
589 client: 'luci/' + promises[0].packages['luci-app-attendedsysupgrade'],
590 packages: promises[0].packages,
591 profile: promises[1].board_name,
592 target: promises[1].release.target,
593 version: promises[1].release.version,
595 filesystem: promises[1].rootfs_type
597 return [data, firmware];
600 render: function (response) {
601 const data = response[0];
602 const firmware = response[1];
605 E('h2', _('Attended Sysupgrade')),
609 'The attended sysupgrade service allows to easily upgrade vanilla and custom firmware images.'
615 'This is done by building a new firmware on demand via an online service.'
620 _('Currently running: %s - %s').format(
628 class: 'btn cbi-button cbi-button-positive important',
629 click: ui.createHandlerFn(this, this.handleCheck, data, firmware),
631 _('Search for firmware upgrade')
635 handleSaveApply: null,