FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
computer/script/computerd-fuse-flush.mjs at example-javascript · earthlings-dev/computer · GitHub
earthlings-dev
/
computer
Public
forked from
cloudflare/computer
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
computer
/
script
/
computerd-fuse-flush.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
211 lines (189 loc) · 7.1 KB
Breadcrumbs
computer
/
script
/
computerd-fuse-flush.mjs
Copy path
File metadata and controls
executable file
·
211 lines (189 loc) · 7.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env node
// End-to-end verification that computerd's FUSE driver spills its
// in-memory write buffer into the backing VFS so capnweb-side
// pullOnce sees the bytes. Reproduces the production container
// failure (FUSE-write \u2192 RPC-read returns 0 bytes) locally against
// a freshly-built computerd binary.
//
// Boots one computerd container with FUSE mounted on /workspace, writes
// a file inside the container via `docker exec` (kernel \u2192 FUSE
// driver \u2192 in-memory buffer \u2192 release/flush spill), then dials
// the container's WebSocket from the host, calls pullOnce, and
// reads the file back through the SQLiteWorkspaceProvider on a
// fresh receiver DB.
//
// Knobs:
// COMPUTERD_BINARY path to computerd-linux-x64 (default artifacts/computerd/...)
// KEEP=1 leave the container running on failure for poking
import
{
execFile
}
from
"node:child_process"
;
import
{
existsSync
}
from
"node:fs"
;
import
{
dirname
,
resolve
}
from
"node:path"
;
import
{
fileURLToPath
}
from
"node:url"
;
import
{
promisify
}
from
"node:util"
;
const
execFileP
=
promisify
(
execFile
)
;
const
__dirname
=
dirname
(
fileURLToPath
(
import
.
meta
.
url
)
)
;
const
REPO_ROOT
=
resolve
(
__dirname
,
".."
)
;
const
BINARY
=
process
.
env
.
COMPUTERD_BINARY
??
resolve
(
REPO_ROOT
,
"artifacts/computerd/computerd-linux-x64"
)
;
const
KEEP
=
process
.
env
.
KEEP
===
"1"
;
if
(
!
existsSync
(
BINARY
)
)
{
console
.
error
(
`computerd binary not found at
${
BINARY
}
`
)
;
console
.
error
(
"run `npm run build:bin --workspace @cloudflare/computerd` first"
)
;
process
.
exit
(
2
)
;
}
const
ADD_HOST_GATEWAY
=
process
.
platform
===
"linux"
;
async
function
bootContainer
(
)
{
const
args
=
[
"run"
,
"--rm"
,
"-d"
,
"--platform"
,
"linux/amd64"
,
"--privileged"
,
"--device"
,
"/dev/fuse"
,
"--cap-add"
,
"SYS_ADMIN"
,
"--cap-add"
,
"MKNOD"
,
"--security-opt"
,
"apparmor=unconfined"
,
"--security-opt"
,
"seccomp=unconfined"
,
]
;
if
(
ADD_HOST_GATEWAY
)
{
args
.
push
(
"--add-host"
,
"host.docker.internal:host-gateway"
)
;
}
args
.
push
(
"-v"
,
`
${
BINARY
}
:/usr/local/bin/computerd:ro`
,
"-p"
,
"0:8080"
,
"-e"
,
"PORT=8080"
,
"-e"
,
"MOUNT_POINT=/workspace"
,
"debian:stable-slim"
,
"bash"
,
"-c"
,
// libfuse2 + a long-lived shell so we can `docker exec` into the
// same mount namespace where computerd mounted FUSE. computerd backgrounds
// and we tail /tmp/computerd.log for debugging.
"apt-get update >/dev/null 2>&1 && "
+
"apt-get install -y --no-install-recommends fuse3 libfuse2t64 >/dev/null 2>&1 && "
+
"mkdir -p /workspace && "
+
"/usr/local/bin/computerd >/tmp/computerd.log 2>&1 & "
+
"COMPUTERD_PID=$!; "
+
"trap 'kill $COMPUTERD_PID 2>/dev/null' EXIT; "
+
"wait $COMPUTERD_PID"
,
)
;
const
{
stdout
}
=
await
execFileP
(
"docker"
,
args
)
;
const
cid
=
stdout
.
trim
(
)
;
const
{
stdout
:
portOut
}
=
await
execFileP
(
"docker"
,
[
"port"
,
cid
,
"8080/tcp"
]
)
;
const
port
=
Number
(
portOut
.
split
(
"\n"
)
[
0
]
.
split
(
":"
)
.
pop
(
)
)
;
const
url
=
`http://127.0.0.1:
${
port
}
`
;
const
deadline
=
Date
.
now
(
)
+
60_000
;
while
(
Date
.
now
(
)
<
deadline
)
{
try
{
const
r
=
await
fetch
(
`
${
url
}
/health`
)
;
if
(
r
.
ok
)
return
{
cid
,
url
,
port
}
;
}
catch
{
/* not ready */
}
await
new
Promise
(
(
r
)
=>
setTimeout
(
r
,
250
)
)
;
}
throw
new
Error
(
`container
${
cid
}
did not become healthy within 60s`
)
;
}
async
function
dockerExec
(
cid
,
...
cmd
)
{
const
{
stdout
,
stderr
}
=
await
execFileP
(
"docker"
,
[
"exec"
,
cid
,
...
cmd
]
)
;
return
{
stdout
,
stderr
}
;
}
async
function
killContainer
(
cid
)
{
if
(
KEEP
)
{
console
.
error
(
`KEEP=1 set; leaving container
${
cid
}
running`
)
;
return
;
}
try
{
await
execFileP
(
"docker"
,
[
"kill"
,
cid
]
)
;
}
catch
{
/* already dead */
}
}
async
function
main
(
)
{
process
.
stderr
.
write
(
"booting computerd container ...\n"
)
;
const
container
=
await
bootContainer
(
)
;
process
.
stderr
.
write
(
`
${
container
.
url
}
(
${
container
.
cid
.
slice
(
0
,
12
)
}
)\n`
)
;
let
failed
=
false
;
try
{
// 1. Confirm FUSE actually mounted; the rest of the test is
// meaningless against a fallback to the container's root fs.
const
{
stdout
:
mount
}
=
await
dockerExec
(
container
.
cid
,
"mount"
)
;
if
(
!
/
f
u
s
e
o
n
\/
w
o
r
k
s
p
a
c
e
/
.
test
(
mount
)
)
{
throw
new
Error
(
`FUSE not mounted in container:\n
${
mount
}
`
)
;
}
process
.
stderr
.
write
(
" FUSE mounted on /workspace\n"
)
;
// 2. Write through FUSE. echo > triggers FUSE create, write,
// flush (on close), release \u2014 every spill point landed in
// the 68407fc fix.
const
payload
=
`from-fuse
${
Date
.
now
(
)
}
\n`
;
await
dockerExec
(
container
.
cid
,
"bash"
,
"-c"
,
`printf '%s' '
${
payload
}
' > /workspace/x.txt`
)
;
process
.
stderr
.
write
(
` wrote
${
payload
.
trim
(
)
}
to /workspace/x.txt via FUSE\n`
)
;
// 3. Pull from the container's WebSocket on the host. Mirrors
// what the host DO does after exec returns.
const
{
createWorkspaceClient
}
=
await
import
(
`
${
REPO_ROOT
}
/node_modules/@cloudflare/computer-rpc/dist/client.js`
)
;
const
{
Database
,
SQLiteWorkspaceProvider
,
initializeSchema
}
=
await
import
(
`
${
REPO_ROOT
}
/node_modules/@cloudflare/dofs/dist/index.js`
)
;
const
{
SQLiteTestStorage
}
=
await
import
(
`
${
REPO_ROOT
}
/node_modules/@cloudflare/dofs/dist/testing.js`
)
;
const
{
pullOnce
}
=
await
import
(
`
${
REPO_ROOT
}
/node_modules/@cloudflare/computer-rpc/dist/sync-driver.js`
)
;
const
wsUrl
=
`
${
container
.
url
.
replace
(
"http://"
,
"ws://"
)
}
/ws`
;
const
client
=
createWorkspaceClient
(
{
url
:
wsUrl
}
)
;
const
recvStorage
=
new
SQLiteTestStorage
(
)
;
const
recvDb
=
new
Database
(
recvStorage
)
;
initializeSchema
(
recvDb
,
(
)
=>
Date
.
now
(
)
)
;
try
{
const
applied
=
await
pullOnce
(
recvDb
,
client
.
sync
)
;
process
.
stderr
.
write
(
` pullOnce applied
${
applied
}
entries\n`
)
;
if
(
applied
===
0
)
{
throw
new
Error
(
"pullOnce applied 0 entries; expected at least 1 for /x.txt"
)
;
}
// 4. Read the file back through the receiver-side provider.
// The bytes have to match what we wrote through FUSE.
const
provider
=
new
SQLiteWorkspaceProvider
(
recvDb
,
{
now
:
(
)
=>
Date
.
now
(
)
}
)
;
const
back
=
provider
.
readFileSync
(
"/x.txt"
,
"utf8"
)
;
if
(
back
!==
payload
)
{
throw
new
Error
(
`byte mismatch:\n wrote:
${
JSON
.
stringify
(
payload
)
}
\n read:
${
JSON
.
stringify
(
back
)
}
`
,
)
;
}
process
.
stderr
.
write
(
` pull-back matches:
${
JSON
.
stringify
(
back
.
trim
(
)
)
}
\n`
)
;
}
finally
{
await
client
.
close
(
)
;
recvStorage
.
close
(
)
;
}
process
.
stderr
.
write
(
"\nPASS: FUSE write reached the host via pullOnce\n"
)
;
}
catch
(
err
)
{
failed
=
true
;
process
.
stderr
.
write
(
`\nFAIL:
${
err
.
message
}
\n`
)
;
// Dump computerd's logs to help diagnose.
try
{
const
{
stdout
}
=
await
dockerExec
(
container
.
cid
,
"cat"
,
"/tmp/computerd.log"
)
;
process
.
stderr
.
write
(
`\n--- computerd logs ---\n
${
stdout
}
\n----------------\n`
)
;
}
catch
{
/* container might already be dead */
}
}
finally
{
await
killContainer
(
container
.
cid
)
;
}
process
.
exit
(
failed
?
1
:
0
)
;
}
main
(
)
.
catch
(
(
err
)
=>
{
console
.
error
(
err
)
;
process
.
exit
(
1
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL