-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathtemplate.py
More file actions
136 lines (124 loc) · 5.12 KB
/
template.py
File metadata and controls
136 lines (124 loc) · 5.12 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
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
from e2b import Template, wait_for_url
def make_template(
kernels: list[str] = ["python", "r", "javascript", "deno", "bash", "java"],
set_user_workdir: bool = False,
):
kernels = ["python", "javascript"] + kernels
# Start with base template
template = (
Template()
.from_image("python:3.12")
.set_user("root")
.set_workdir("/")
.set_envs(
{
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"JUPYTER_CONFIG_PATH": ".jupyter",
"IPYTHON_CONFIG_PATH": ".ipython",
"SERVER_PATH": ".server",
"JAVA_VERSION": "11",
"JAVA_HOME": "/usr/lib/jvm/java-${JAVA_VERSION}-openjdk-amd64",
"IJAVA_VERSION": "1.3.0",
"DENO_INSTALL": "/opt/deno",
"DENO_VERSION": "v2.4.0",
"R_VERSION": "4.5.*",
}
)
.apt_install(
[
"build-essential",
"curl",
"git",
"util-linux",
"jq",
"sudo",
"fonts-noto-cjk",
"ca-certificates",
]
)
.run_cmd("curl -fsSL https://deb.nodesource.com/setup_20.x | bash -")
.apt_install("nodejs")
.copy("requirements.txt", "requirements.txt")
.pip_install("--no-cache-dir -r requirements.txt")
)
if "python" in kernels:
template = template.run_cmd("ipython kernel install --name 'python3' --user")
# Install R Kernel if requested
if "r" in kernels:
template = (
template.run_cmd(
[
"sudo gpg --keyserver keyserver.ubuntu.com --recv-key 95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7",
"sudo gpg --armor --export 95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7 | sudo tee /etc/apt/trusted.gpg.d/cran_debian_key.asc",
'echo "deb https://cloud.r-project.org/bin/linux/debian trixie-cran40/" | sudo tee /etc/apt/sources.list.d/cran.list',
]
)
.apt_install("r-base=${R_VERSION} r-base-dev")
.run_cmd(
[
"R -e \"install.packages('IRkernel', repos='https://cloud.r-project.org')\"",
"R -e \"IRkernel::installspec(user = FALSE, name = 'r', displayname = 'R')\"",
]
)
)
# Install JavaScript Kernel if requested
if "javascript" in kernels:
template = template.npm_install(
"--unsafe-perm git+https://github.com/e2b-dev/ijavascript.git",
g=True,
).run_cmd("ijsinstall --install=global")
# Install Deno Kernel if requested
if "deno" in kernels:
template = template.run_cmd(
[
"curl -fsSL https://deno.land/install.sh | sh -s ${DENO_VERSION}",
"PATH=$DENO_INSTALL/bin:$PATH",
"deno jupyter --unstable --install",
]
).copy("deno.json", ".local/share/jupyter/kernels/deno/kernel.json")
# Install Bash Kernel if requested
if "bash" in kernels:
template = template.pip_install("bash_kernel").run_cmd(
"python -m bash_kernel.install"
)
# Install Java and Java Kernel if requested
if "java" in kernels:
template = template.run_cmd(
[
"mkdir -p /usr/lib/jvm",
"curl -fsSL https://download.java.net/java/ga/jdk${JAVA_VERSION}/openjdk-${JAVA_VERSION}_linux-x64_bin.tar.gz | tar -xz -C /usr/lib/jvm",
"update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-${JAVA_VERSION}/bin/java 1",
"update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-${JAVA_VERSION}/bin/javac 1",
"wget https://github.com/SpencerPark/IJava/releases/download/v${IJAVA_VERSION}/ijava-${IJAVA_VERSION}.zip",
"unzip ijava-${IJAVA_VERSION}.zip",
"python install.py --sys-prefix",
]
)
# Common setup steps (always run)
template = (
template
# Create server virtual environment
.copy("server", ".server")
.run_cmd("python -m venv .server/.venv")
# Copy and install server requirements
.run_cmd(
".server/.venv/bin/pip install --no-cache-dir -r .server/requirements.txt"
)
)
if set_user_workdir:
template = template.set_user("user").set_workdir("/home/user")
# Copy configuration files
template = (
template.copy("matplotlibrc", ".config/matplotlib/.matplotlibrc")
.copy("start-up.sh", ".jupyter/start-up.sh")
.run_cmd("chmod +x .jupyter/start-up.sh")
.copy("jupyter_server_config.py", ".jupyter/")
.make_dir(".ipython/profile_default/startup")
.copy("ipython_kernel_config.py", ".ipython/profile_default/")
.copy("startup_scripts", ".ipython/profile_default/startup")
)
return template.set_start_cmd(
".jupyter/start-up.sh", wait_for_url("http://localhost:49999/health")
)