-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2b.rs
More file actions
93 lines (80 loc) · 3.06 KB
/
Copy pathe2b.rs
File metadata and controls
93 lines (80 loc) · 3.06 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
//! ```text
//! export E2B_API_KEY=...
//! cargo run --features e2b --example e2b -- <template-id> [--keep]
//! ```
//!
//! `--keep` leaves the sandbox running. Build the template once with:
//!
//! ```text
//! python3 images/context.py images/desktop /tmp/e2b-ctx --for e2b
//! e2b template create holm-desktop -p /tmp/e2b-ctx -d Dockerfile \
//! -c "/usr/local/bin/holm-desktop" --ready-cmd "true" \
//! --cpu-count 2 --memory-mb 2048
//! ```
use holm::sandboxes::e2b::{self, cloud::Cloud};
use holm::{Auth, Button, Computer, Point, X11Profile};
use std::sync::Arc;
use std::time::Duration;
#[tokio::main]
async fn main() -> holm::Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
let keep = args.iter().any(|arg| arg == "--keep");
let Some(template) = args.iter().find(|arg| !arg.starts_with("--")).cloned() else {
eprintln!("usage: e2b <template-id> [--keep]");
std::process::exit(2);
};
let (machine, profile) = e2b::pair(Arc::new(Cloud::from_env()?), Arc::new(X11Profile));
println!("starting a sandbox …");
let computer = Computer::builder()
.machine(Arc::new(
machine
.public_viewer(true)
.expiring_after(Duration::from_secs(15 * 60)),
))
.profile(profile)
.image(&template)
.auth(Auth::Token)
.keep_on_drop(keep)
.launch()
.await?;
println!(" runtime {}", computer.provider());
match computer.viewer_url() {
Some(url) => println!(" watch it {url}"),
None => println!(" no viewer: this sandbox is secure"),
}
computer.open_url("https://example.com").await?;
tokio::time::sleep(Duration::from_secs(4)).await;
let frame = computer.screenshot().await?;
std::fs::write("e2b.png", &frame).ok();
println!(" screenshot: {} bytes -> e2b.png", frame.len());
match computer.browser() {
Some(devtools) => {
let started = std::time::Instant::now();
let pages = devtools.pages().await?;
let>
.iter()
.find(|page| page.url.contains("example.com"))
.or(pages.first())
.ok_or_else(|| holm::Error::denied("the browser has no page"))?;
let mut page = devtools.attach(target).await?;
let snapshot = page.snapshot(None, Some(20)).await?;
println!(
" devtools: {:?} with {} controls in {:?}",
snapshot.title,
snapshot.total,
started.elapsed()
);
}
None => println!(" devtools: none reaches this box"),
}
computer.click(Point::new(640, 400), Button::Left).await?;
println!(" cursor: {:?}", computer.cursor().await?);
let geometry = computer.primary().geometry().await?;
println!(" geometry: {geometry:?}");
if keep {
println!("\n left running as {}", computer.name());
println!(" it goes away on its own when its deadline runs out");
return Ok(());
}
computer.shutdown().await
}