-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
106 lines (96 loc) · 4.09 KB
/
Copy pathindex.ts
File metadata and controls
106 lines (96 loc) · 4.09 KB
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
#!/usr/bin/env node
// To run this example, create a .env file in this directory with:
// MESA_ORG=your-org
// MESA_API_KEY=your-mesa-key
// E2B_API_KEY=your-e2b-key
//
// Then run:
// npm start
import 'dotenv/config';
import { Sandbox } from 'e2b';
import { Mesa } from '@mesadev/sdk';
import tinyE2bRepl from './repl.ts';
const ORG =
process.env.MESA_ORG ??
(() => {
throw Error('$MESA_ORG not set.');
})();
const MESA_API_KEY =
process.env.MESA_API_KEY ??
(() => {
throw Error('$MESA_API_KEY not set.');
})();
if (!process.env.E2B_API_KEY) {
throw Error('$E2B_API_KEY not set.');
}
// Mint a short-lived access token OUTSIDE the sandbox, where your API key lives.
// Only this token is injected into the sandbox below — your long-lived API key
// never crosses the boundary. Signing is local (no network round-trip) and the
// token expires on its own, so a compromised sandbox leaks at most a
// soon-to-expire, narrowly-scoped credential.
const mesa = new Mesa({ apiKey: MESA_API_KEY, org: ORG });
const { token } = await mesa.tokens.create({
scopes: ['read', 'write'],
// Optionally restrict the token to specific repos (full `org/repo` names):
// repos: [`${ORG}/my-repo`],
ttl_seconds: 60 * 60, // 1 hour (max 24h). The mount lasts exactly this long.
});
console.log('Creating E2B sandbox...');
const sandbox = await Sandbox.create();
try {
// Set up Mesa within the E2B sandbox.
//
// We recommend installing Mesa as part of the container definition (e.g. Docker image),
// but here we install it directly to keep the example small.
// You can install Mesa as per the guide in https://docs.mesa.dev/content/virtual-filesystem/os-level.
//
// Mesa's installer will install all its dependencies through your system's package manager.
console.log('Installing Mesa...');
await sandbox.commands.run('curl -fsSL https://mesa.dev/install.sh | sh');
// It is critical that you enable the user_allow_other flag in your fuse configuration.
//
// This allows users outside of yourself to also access the mesa mount you mounted. Mesa requires this for
// operation. See https://www.man7.org/linux/man-pages/man8/mount.fuse3.8.html for more details.
console.log('Configuring FUSE...');
await sandbox.commands.run(
[
"sed -i 's/^#user_allow_other/user_allow_other/' /etc/fuse.conf",
// E2B exposes /dev/fuse as root-only by default.
'chmod 666 /dev/fuse',
].join(' && '),
{ user: 'root' }
);
// You can run mesa in daemon mode to kick it off in the background.
//
// The flags we are using here are:
// -d, --daemonize Spawns mesa in the background
// -y, --non-interactive Tells mesa to use the default values for all its configuration values. It will create a
// new config file for you.
//
// We pass two environment variables:
// MESA_ORG tells mesa which organization to add to config.toml.
// MESA_API_KEY provides the credential for this process. It accepts an
// API key OR an access token; here we pass the short-lived
// token we minted above, so the raw API key never enters the
// sandbox. See
// https://docs.mesa.dev/content/reference/mesa-cli-configuration.
//
// Mesa writes only the organization to config.toml on first boot; the token
// is read from the environment and is never persisted to disk.
console.log(`Mounting ${ORG}...`);
await sandbox.commands.run('mesa mount -d -y', {
envs: {
MESA_ORG: ORG,
MESA_API_KEY: token, // the short-lived token, NOT the raw API key
},
});
// You can now explore repos in your org. We've written a tiny REPL here you can use to explore the sandbox.
//
// The default configuration is created in ~/.config/mesa/config.toml
// and your files will be in ~/.local/share/mesa/mnt/<org>/<repo>
await tinyE2bRepl(sandbox, { cwd: `~/.local/share/mesa/mnt/${ORG}` });
} finally {
// No matter what happens, let's make sure we clean up the sandbox so we don't burn E2B tokens!
console.log('\nCleaning up sandbox...');
await sandbox.kill();
}