Skip to content

Commit 46e6a9c

Browse files
authored
Merge pull request #3 from speed47/webos3
v0.1.4
2 parents 08ca0b3 + 74e172e commit 46e6a9c

File tree

4 files changed

+160
-36
lines changed

4 files changed

+160
-36
lines changed

.github/workflows/package.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: package
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- master
9+
- main
10+
11+
jobs:
12+
package:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v5
16+
with:
17+
persist-credentials: false
18+
- name: apt-get install
19+
env:
20+
DEBIAN_FRONTEND: noninteractive
21+
run: sudo apt-get update && sudo apt-get dist-upgrade -y && sudo apt-get install -y npm
22+
- name: npm install
23+
run: npm install
24+
- name: npm build
25+
run: npm run build
26+
- name: npm package
27+
run: npm run package
28+
- name: get ipk artifact name
29+
id: ipk
30+
run: |
31+
GIT_COMMIT=$(git rev-parse --short HEAD)
32+
PREFIX=$(basename $(ls -1 *.ipk) .ipk)
33+
echo "zip=$PREFIX-$GIT_COMMIT" >> "$GITHUB_OUTPUT"
34+
- uses: actions/upload-artifact@v4
35+
with:
36+
name: ${{ steps.ipk.outputs.zip }}
37+
path: '*.ipk'
38+
retention-days: 30

frontend/index.js

Lines changed: 119 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function retry(attempt, cb) {
2323
return await cb();
2424
} catch (err) {
2525
if (attempt--) {
26-
log (`An error occured, ${attempt} tries left...`);
26+
log (`... failed (${err.message}), ${attempt} tries left`);
2727
continue;
2828
}
2929
throw err;
@@ -35,50 +35,136 @@ function log(s) {
3535
document.querySelector('pre').innerText += `[${new Date()}] ${s}\n`;
3636
}
3737

38+
function logres(res) {
39+
if (res.returnValue) {
40+
if (res.message) {
41+
log(`... success (${res.message})`);
42+
}
43+
else {
44+
log('... success');
45+
}
46+
}
47+
else {
48+
log(`... failed (${res.message})`);
49+
}
50+
}
51+
3852
(async () => {
53+
54+
/* We'll do a certain number of luna calls.
55+
On startup, sometimes these calls fail (bus overload?),
56+
so we'll retry them 3 times if we get an error.
57+
Each call usually take between 0 and 10 seconds. */
58+
3959
try {
40-
log('Registering input app...');
41-
await lunaCall('luna://com.webos.service.eim/addDevice', {
42-
appId: 'org.webosbrew.autostart',
43-
pigImage: '',
44-
mvpdIcon: '',
60+
/* Be polite and launch the actual previous input app, if any.
61+
This'll give back control to the user while we finish our setup
62+
in the background, as the whole process can be slow due to luna calls
63+
and the number of other processes competing for CPU on startup. */
64+
await retry(3, async () => {
65+
log("Checking last input app...");
66+
const lastinput = await lunaCall('luna://org.webosbrew.hbchannel.service/exec', {
67+
command: 'cat /var/lib/eim/lastinput',
68+
});
69+
if (lastinput.stdoutString) {
70+
const lastinputPayload = JSON.parse(lastinput.stdoutString);
71+
if (lastinputPayload.appId) {
72+
log(`Last input app: ${lastinputPayload.appId}`);
73+
if (lastinputPayload.appId !== 'org.webosbrew.autostart') {
74+
log("Relaunching this app...");
75+
const res = await lunaCall('luna://com.webos.service.applicationManager/launch', {
76+
id: lastinputPayload.appId,
77+
});
78+
logres(res);
79+
}
80+
}
81+
}
82+
else {
83+
log("No app to relaunch.");
84+
}
4585
});
86+
} catch (err) {
87+
log(`Couldn't start last input app but carrying on:\n${err.stack}`);
88+
}
4689

90+
try {
91+
/* Launch autostart soon in the process because even if we fail later down,
92+
at least everything will have started (including SSH). */
4793
await retry(3, async () => {
48-
log('Setting up eim overlay...');
49-
const res = await lunaCall('luna://org.webosbrew.hbchannel.service/exec', {
50-
command: 'if [[ ! -d /var/lib/webosbrew/eim ]]; then cp -r /var/lib/eim /var/lib/webosbrew/eim; fi ; if ! findmnt /var/lib/eim; then mount --bind /var/lib/webosbrew/eim /var/lib/eim ; fi',
94+
log("Launching autostart...");
95+
const res = await lunaCall('luna://org.webosbrew.hbchannel.service/autostart', {});
96+
logres(res);
97+
});
98+
} catch (err) {
99+
log(`Couldn't run autostart, but carrying on:\n${err.stack}`);
100+
}
101+
102+
/* Now, all that we want is ensuring that we get started on next boot,
103+
all the logic below is here to ensure this is the case. */
104+
105+
try {
106+
/* First, unregister (always succeeds, even if we weren't registered),
107+
this is needed because we'll get started only on the boot following
108+
a successful addDevice, and this only succeeds if we're not already in the list. */
109+
await retry(3, async () => {
110+
log('Unregistering ourselves as input app...');
111+
const res = await lunaCall('luna://com.webos.service.eim/deleteDevice', {
112+
appId: 'org.webosbrew.autostart',
51113
});
52-
log(`Result: ${res.stdoutString} ${res.stderrString}`);
114+
logres(res);
53115
});
116+
} catch (err) {
117+
log(`Couldn't unregister, but carrying on:\n${err.stack}`);
118+
}
54119

55-
log("Launching autostart...");
56-
const res2 = await lunaCall('luna://org.webosbrew.hbchannel.service/autostart', {});
57-
log(`Result: ${res2.message}`);
120+
try {
121+
// Register again to ensure we're started on next boot.
122+
await retry(3, async () => {
123+
log('Registering ourselves as input app...');
124+
const res = await lunaCall('luna://com.webos.service.eim/addDevice', {
125+
appId: 'org.webosbrew.autostart',
126+
pigImage: '',
127+
mvpdIcon: '',
128+
type: 'MVPD_IP', // can be either MVPD_IP or MVPD_RF, required for webOS 3.4 at least
129+
label: 'Autostart', // required for webOS 3.4 at least
130+
description: 'webosbrew autostart', // required for webOS 3.4 at least
131+
});
132+
logres(res);
133+
});
134+
} catch (err) {
135+
log(`Couldn't register, but carrying on:\n${err.stack}`);
136+
}
58137

59-
log("Removing input app...");
60-
await lunaCall('luna://com.webos.service.eim/deleteDevice', {
61-
appId: 'org.webosbrew.autostart',
138+
try {
139+
/* Now, setup an eim overlay so that any changes done later don't erase our own app
140+
if /var/lib/webosbrew/eim already exists, keep it that way, changes done in previous
141+
sessions will live here, so just bind mount it, otherwise create it
142+
from the /var/lib/eim contents. */
143+
await retry(3, async () => {
144+
log('Setting up eim overlay...');
145+
const res = await lunaCall('luna://org.webosbrew.hbchannel.service/exec', {
146+
command: 'if [[ ! -d /var/lib/webosbrew/eim ]]; then cp -r /var/lib/eim /var/lib/webosbrew/eim && echo cp ok || echo cp failed; fi ; if ! findmnt /var/lib/eim; then mount --bind /var/lib/webosbrew/eim /var/lib/eim && echo mount overlay ok || echo mount overlay failed; fi',
147+
});
148+
log(`Result: ${res.stdoutString} ${res.stderrString}`);
62149
});
150+
} catch (err) {
151+
log(`Couldn't setup overlay, stopping here:\n${err.stack}`);
152+
return;
153+
}
63154

64-
log("Checking last input app...");
65-
const lastinput = await lunaCall('luna://org.webosbrew.hbchannel.service/exec', {
66-
command: 'cat /var/lib/eim/lastinput',
155+
try {
156+
/* This just removes ourselves from the overlay, but we still live in the real /var/lib/eim,
157+
which guarantees that we'll survive on the next reboot. */
158+
await retry(3, async () => {
159+
log('Unregistering ourselves as input app from the overlay...');
160+
const res = await lunaCall('luna://com.webos.service.eim/deleteDevice', {
161+
appId: 'org.webosbrew.autostart',
162+
});
163+
logres(res);
67164
});
68-
if (lastinput.stdoutString) {
69-
const lastinputPayload = JSON.parse(lastinput.stdoutString);
70-
if (lastinputPayload.appId) {
71-
log(`Last input app: ${lastinputPayload.appId}`);
72-
if (lastinputPayload.appId !== 'org.webosbrew.autostart') {
73-
log("Relaunching...");
74-
await lunaCall('luna://com.webos.service.applicationManager/launch', {
75-
id: lastinputPayload.appId,
76-
});
77-
}
78-
}
79-
}
80-
log("Done.");
81165
} catch (err) {
82-
log(`An error occured: ${err.stack}`);
166+
log(`Couldn't unregister, but carrying on:\n${err.stack}`);
83167
}
168+
169+
log("Done.");
84170
})();

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "org.webosbrew.autostart",
3-
"version": "0.1.2",
3+
"version": "0.1.4",
44
"description": "Autostart test application",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)