diff options
author | Daniel Nilsson <daniel.nilsson94@outlook.com> | 2024-02-28 23:12:55 +0100 |
---|---|---|
committer | Paul Donald <itsascambutmailmeanyway@gmail.com> | 2024-03-19 17:22:44 +0100 |
commit | c608a15a9c53a19654f5a36ba2056a7a1eb561da (patch) | |
tree | f6aaa47d4dc22d6ac77d4322d9109b8c6bbdf6f9 /applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade | |
parent | 374af8b522796424e2b915a4113ed37d22ae653c (diff) |
luci-app-attendedsysupgrade: don't reassign data and firmware info
The reassignment of the variable that holds the package information once you get a response from the sysupgrade server caused the package list (which is only loaded once when entering the app) to become an array instead of an object (since that's how the response is structured), which gave the result of once cancelling the firmware upgrade, the package list is now an array, making the package list unusable.
This commit updates the variables that shouldn't be changed (data and firmware) to constants and all handle methods to take these values as parameters instead, allowing us to avoid the possible reassignment which will otherwise result in unexpected control flows.
Ref: #6284
Signed-off-by: Daniel Nilsson <daniel.nilsson94@outlook.com>
Diffstat (limited to 'applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade')
-rw-r--r-- | applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js | 160 |
1 files changed, 75 insertions, 85 deletions
diff --git a/applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js b/applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js index 06bc1bb068..36b7c852cb 100644 --- a/applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js +++ b/applications/luci-app-attendedsysupgrade/htdocs/luci-static/resources/view/attendedsysupgrade/overview.js @@ -67,26 +67,10 @@ return view.extend({ building_image: [80, _('Generating firmware image')], }, - data: { - url: '', - revision: '', - advanced_mode: 0, - rebuilder: [], - sha256_unsigned: '', - }, - - firmware: { - profile: '', - target: '', - version: '', - packages: [], - diff_packages: true, - filesystem: '', - }, + request_hash: '', + sha256_unsigned: '', - selectImage: function (images) { - let firmware = this.firmware; - let data = this.data; + selectImage: function (images, data, firmware) { var filesystemFilter = function(e) { return (e.filesystem == firmware.filesystem); } @@ -105,13 +89,13 @@ return view.extend({ return images.filter(filesystemFilter).filter(typeFilter)[0]; }, - handle200: function (response) { + handle200: function (response, content, data, firmware) { response = response.json(); - let image = this.selectImage(response.images); + let image = this.selectImage(response.images, data, firmware); if (image.name != undefined) { - this.data.sha256_unsigned = image.sha256_unsigned; - let sysupgrade_url = `${this.data.url}/store/${response.bin_dir}/${image.name}`; + this.sha256_unsigned = image.sha256_unsigned; + let sysupgrade_url = `${data.url}/store/${response.bin_dir}/${image.name}`; let keep = E('input', { type: 'checkbox' }); keep.checked = true; @@ -123,7 +107,7 @@ return view.extend({ image.sha256, ]; - if (this.data.advanced_mode == 1) { + if (data.advanced_mode == 1) { fields.push( _('Profile'), response.id, @@ -142,7 +126,7 @@ return view.extend({ '', E('a', { href: sysupgrade_url }, _('Download firmware image')) ); - if (this.data.rebuilder) { + if (data.rebuilder) { fields.push(_('Rebuilds'), E('div', { id: 'rebuilder_status' })); } @@ -185,15 +169,15 @@ return view.extend({ ]; ui.showModal(_('Successfully created firmware image'), modal_body); - if (this.data.rebuilder) { - this.handleRebuilder(); + if (data.rebuilder) { + this.handleRebuilder(content, data, firmware); } } }, handle202: function (response) { response = response.json(); - this.data.request_hash = response.request_hash; + this.request_hash = response.request_hash; if ('queue_position' in response) { ui.showModal(_('Queued...'), [ @@ -219,8 +203,14 @@ return view.extend({ } }, - handleError: function (response) { + handleError: function (response, data, firmware) { response = response.json(); + const request_data = { + ...data, + request_hash: this.request_hash, + sha256_unsigned: this.sha256_unsigned, + ...firmware + }; let body = [ E('p', {}, _('Server response: %s').format(response.detail)), E( @@ -229,7 +219,7 @@ return view.extend({ _('Please report the error message and request') ), E('p', {}, _('Request Data:')), - E('pre', {}, JSON.stringify({ ...this.data, ...this.firmware }, null, 4)), + E('pre', {}, JSON.stringify({ ...request_data }, null, 4)), ]; if (response.stdout) { @@ -251,23 +241,23 @@ return view.extend({ ui.showModal(_('Error building the firmware image'), body); }, - handleRequest: function (server, main) { + handleRequest: function (server, main, content, data, firmware) { let request_url = `${server}/api/v1/build`; let method = 'POST'; - let content = this.firmware; + let local_content = content; /** * 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 = {}; + if (this.request_hash && main == true) { + request_url += `/${this.request_hash}`; + local_content = {}; method = 'GET'; } request - .request(request_url, { method: method, content: content }) + .request(request_url, { method: method, content: local_content }) .then((response) => { switch (response.status) { case 202: @@ -285,13 +275,13 @@ return view.extend({ case 200: if (main == true) { poll.remove(this.pollFn); - this.handle200(response); + this.handle200(response, content, data, firmware); } 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) { + let image = this.selectImage(response.images, data, firmware); + if (image.sha256_unsigned == this.sha256_unsigned) { view.innerText = '✅ %s'.format(server); } else { view.innerHTML = `⚠️ ${server} (<a href="${server}/store/${ @@ -305,7 +295,7 @@ return view.extend({ case 500: // build failed if (main == true) { poll.remove(this.pollFn); - this.handleError(response); + this.handleError(response, data, firmware); break; } else { poll.remove(this.rebuilder_polls[server]); @@ -317,14 +307,17 @@ return view.extend({ }); }, - handleRebuilder: function () { + handleRebuilder: function (content, data, firmware) { this.rebuilder_polls = {}; - for (let rebuilder of this.data.rebuilder) { + for (let rebuilder of data.rebuilder) { this.rebuilder_polls[rebuilder] = L.bind( this.handleRequest, this, rebuilder, - false + false, + content, + data, + firmware ); poll.add(this.rebuilder_polls[rebuilder], 5); document.getElementById( @@ -401,9 +394,9 @@ return view.extend({ }); }, - handleCheck: function () { - let { url, revision } = this.data; - let { version, target } = this.firmware; + handleCheck: function (data, firmware) { + let { url, revision, advanced_mode, branch } = data; + let { version, target, profile, packages } = firmware; let candidates = []; let request_url = `${url}/api/overview`; if (version.endsWith('SNAPSHOT')) { @@ -457,24 +450,21 @@ return view.extend({ } // skip branch upgrades outside the advanced mode - if ( - this.data.branch != remote_branch && - this.data.advanced_mode == 0 - ) { + if (branch != remote_branch && advanced_mode == 0) { continue; } candidates.unshift([remote_version, null]); // don't offer branches older than the current - if (this.data.branch == remote_branch) { + if (branch == remote_branch) { break; } } } // allow to re-install running firmware in advanced mode - if (this.data.advanced_mode == 1) { + if (advanced_mode == 1) { candidates.unshift([version, revision]); } @@ -483,9 +473,9 @@ return view.extend({ let mapdata = { request: { - profile: this.firmware.profile, + profile, version: candidates[0][0], - packages: Object.keys(this.firmware.packages).sort(), + packages: Object.keys(packages).sort(), }, }; @@ -517,7 +507,7 @@ return view.extend({ } } - if (this.data.advanced_mode == 1) { + if (advanced_mode == 1) { o = s.option(form.Value, 'profile', _('Board Name / Profile')); o = s.option(form.DynamicList, 'packages', _('Packages')); } @@ -527,8 +517,8 @@ return view.extend({ E( 'p', _('Currently running: %s - %s').format( - this.firmware.version, - this.data.revision + version, + revision ) ), form_rendered, @@ -541,11 +531,14 @@ return view.extend({ 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; + const content = { + ...firmware, + packages: mapdata.request.packages, + version: mapdata.request.version, + profile: mapdata.request.profile + }; this.pollFn = L.bind(function () { - this.handleRequest(this.data.url, true); + this.handleRequest(url, true, content, data, firmware); }, this); poll.add(this.pollFn, 5); poll.start(); @@ -584,27 +577,24 @@ return view.extend({ }, 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' - ); + const data = { + url: uci.get_first('attendedsysupgrade', 'server', 'url'), + branch: get_branch(response[1].release.version), + revision: response[1].release.revision, + efi: response[2], + advanced_mode: uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0, + rebuilder: uci.get_first('attendedsysupgrade', 'server', 'rebuilder') + }; + + const firmware = { + client: 'luci/' + response[0].packages['luci-app-attendedsysupgrade'], + packages: response[0].packages, + profile: response[1].board_name, + target: response[1].release.target, + version: response[1].release.version, + diff_packages: true, + filesystem: response[1].rootfs_type + }; return E('p', [ E('h2', _('Attended Sysupgrade')), @@ -623,15 +613,15 @@ return view.extend({ E( 'p', _('Currently running: %s - %s').format( - this.firmware.version, - this.data.revision + firmware.version, + data.revision ) ), E( 'button', { class: 'btn cbi-button cbi-button-positive important', - click: ui.createHandlerFn(this, this.handleCheck), + click: ui.createHandlerFn(this, this.handleCheck, data, firmware), }, _('Search for firmware upgrade') ), |