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
|
'use strict';
'require view';
'require form';
'require uci';
'require rpc';
'require ui';
'require poll';
'require request';
'require dom';
var callPackagelist = rpc.declare({
object: 'rpc-sys',
method: 'packagelist',
});
var callSystemBoard = rpc.declare({
object: 'system',
method: 'board'
});
var callUpgradeStart = rpc.declare({
object: 'rpc-sys',
method: 'upgrade_start',
params: ["keep"]
});
function install_sysupgrade(url, keep, sha256) {
displayStatus("notice spinning", E('p', _('Downloading firmware from server to browser')));
request.get(url, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
responseType: 'blob'
})
.then(response => {
var 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());
displayStatus("notice spinning", E('p', _('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) {
displayStatus("warning", [
E('b', _('Wrong checksum')),
E('p', _('Error during download of firmware. Please try again')),
E('div', {
'class': 'btn',
'click': ui.hideModal
}, _('Close'))
]);
} else {
displayStatus('warning spinning', E('p', _('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');
}
});
}
});
});
}
function request_sysupgrade(server_url, data) {
var res, req;
if (data.request_hash) {
req = request.get(server_url + "/api/build/" + data.request_hash)
} else {
req = request.post(server_url + "/api/build", {
profile: data.board_name,
version: data.version,
packages: data.packages,
diff_packages: true,
})
}
req.then(response => {
switch (response.status) {
case 200:
var res = response.json()
var image;
console.log(res)
for (image of res.images) {
if (image.type == "sysupgrade") {
break;
}
}
if (image.name != undefined) {
var sysupgrade_url = server_url + "/store/" + res.bin_dir + "/" + image.name;
var keep = E('input', {
type: 'checkbox'
})
keep.checked = true;
var fields = [
_('Version'), res.version_number + ' ' + res.version_code,
_('File'), E('a', {
'href': sysupgrade_url
}, image.name),
_('SHA256'), image.sha256,
_('Build Date'), res.build_at,
_('Target'), res.target,
];
var table = E('div', {
'class': 'table'
});
for (var i = 0; i < fields.length; i += 2) {
table.appendChild(E('div', {
'class': 'tr'
}, [
E('div', {
'class': 'td left',
'width': '33%'
}, [fields[i]]),
E('div', {
'class': 'td left'
}, [(fields[i + 1] != null) ? fields[i + 1] : '?'])
]));
}
var modal_body = [
table,
E('p', {}, 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('div', {
'class': 'btn cbi-button-action',
'click': function() {
install_sysupgrade(sysupgrade_url, keep.checked, image.sha256)
}
}, _('Install Sysupgrade'))
])
]
ui.showModal(_('Successfully created sysupgrade image'), modal_body);
}
break;
case 202:
res = response.json()
data.request_hash = res.request_hash;
switch (res.status) {
case "queued":
displayStatus("notice spinning", E('p', _('Request in build queue')));
break;
case "started":
displayStatus("notice spinning", E('p', _('Building the sysupgrade image')));
break;
}
setTimeout(function() {
request_sysupgrade(server_url, data);
}, 5000);
break;
case 400: // bad request
case 422: // bad package
case 500: // build failed
res = response.json()
var body = [
E('p', {}, _(res.message)),
E('p', {}, _("Please report the error message and request")),
E('b', {}, _("Request to server:")),
E('pre', {}, JSON.stringify(data, null, 4)),
]
if (res.stdout) {
body.push(E('b', {}, "STDOUT:"))
body.push(E('pre', {}, res.stdout))
}
if (res.stderr) {
body.push(E('b', {}, "STDERR:"))
body.push(E('pre', {}, res.stderr))
}
body = body.concat([
E('div', {
'class': 'right'
}, [
E('div', {
'class': 'btn',
'click': ui.hideModal
}, _('Close'))
])
]);
ui.showModal(_('Error building the sysupgrade'), body);
break;
}
});
}
function check_sysupgrade(server_url, current_version, board_name, packages) {
displayStatus("notice spinning", E('p', _('Searching for an available sysupgrade')));
var current_branch = current_version.split(".").slice(0, 2).join(".");
var candidates = [];
fetch(server_url + "/api/latest")
.then(response => response.json())
.then(response => {
if (current_version == "SNAPSHOT") {
candidates.push("SNAPSHOT");
} else {
for (let version of response["latest"]) {
var branch = version.split(".").slice(0, 2).join(".");
// already latest version installed
if (current_version == version) {
break;
}
// warn user that a new major release would be installed
//if (current_branch != branch) {
// branch["warn_branch_jump"] = true;
//}
candidates.unshift(version);
// don't offer branches older than the current
if (current_branch == branch) {
break;
}
}
}
if (candidates) {
var m, s, o;
var advanced_mode = uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0;
console.log(candidates);
var mapdata = {
request: {
board_name: board_name,
version: candidates[0],
packages: Object.keys(packages).sort(),
}
}
m = new form.JSONMap(mapdata, '');
s = m.section(form.NamedSection, 'request', 'example', '',
'Use defaults for the safest update');
o = s.option(form.ListValue, 'version', 'Select firmware version');
for (let candidate of candidates) {
o.value(candidate, candidate);
}
if (advanced_mode == 1) {
o = s.option(form.Value, 'board_name', 'Board Name / Profile');
o = s.option(form.DynamicList, 'packages', 'Packages');
}
m.render()
.then(function(form_rendered) {
ui.showModal(_('New upgrade available'), [
form_rendered,
E('div', {
'class': 'right'
}, [
E('div', {
'class': 'btn',
'click': ui.hideModal
}, _('Cancel')),
' ',
E('div', {
'class': 'btn cbi-button-action',
'click': function() {
m.save().then(foo => {
request_sysupgrade(
server_url, mapdata.request
)
});
}
}, _('Request Sysupgrade'))
])
]);
});
} else {
ui.showModal(_('No upgrade available'), [
E('p', {}, _("The device runs the latest firmware version")),
E('div', {
'class': 'right'
}, [
E('div', {
'class': 'btn',
'click': ui.hideModal
}, _('Close'))
])
]);
}
});
}
function displayStatus(type, content) {
if (type) {
var message = ui.showModal('', '');
message.classList.add('alert-message');
DOMTokenList.prototype.add.apply(message.classList, type.split(/\s+/));
if (content)
dom.content(message, content);
} else {
ui.hideModal();
}
}
return view.extend({
load: function() {
return Promise.all([
L.resolveDefault(callPackagelist(), {}),
L.resolveDefault(callSystemBoard(), {}),
uci.load('attendedsysupgrade')
]);
},
render: function(res) {
var packages = res[0].packages;
var current_version = res[1].release.version;
var board_name = res[1].board_name;
var auto_search = uci.get_first('attendedsysupgrade', 'client', 'auto_search') || 1;
var server_url = uci.get_first('attendedsysupgrade', 'server', 'url');
var view = [
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.'))
];
if (auto_search == 1) {
check_sysupgrade(server_url, current_version, board_name, packages)
}
view.push(E('p', {
'class': 'btn cbi-button-positive',
'click': function() {
check_sysupgrade(server_url, current_version, board_name, packages)
}
}, _('Search for sysupgrade')));
return view;
},
});
|