-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathtemplate.py
More file actions
152 lines (139 loc) · 5.88 KB
/
template.py
File metadata and controls
152 lines (139 loc) · 5.88 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from e2b import Template, wait_for_url
def make_template(
kernels: list[str] = ["python", "r", "javascript", "deno", "bash", "java"],
is_docker: bool = False,
):
enabled_kernels = set(["python", "javascript"] + kernels)
# Start with base template
template = (
Template()
.from_image("python:3.12")
.set_user("root")
.set_workdir("/root")
.set_envs(
{
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"JUPYTER_CONFIG_PATH": "/root/.jupyter",
"IPYTHON_CONFIG_PATH": "/root/.ipython",
"SERVER_PATH": "/root/.server",
"JAVA_VERSION": "11",
"JAVA_HOME": "/usr/lib/jvm/jdk-${JAVA_VERSION}",
"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",
"supervisor",
]
)
.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 enabled_kernels:
template = template.run_cmd("ipython kernel install --name 'python3' --user")
# Install R Kernel if requested
if "r" in enabled_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 enabled_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 enabled_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 enabled_kernels:
template = template.pip_install("bash_kernel").run_cmd(
"python -m bash_kernel.install"
)
# Install Java and Java Kernel if requested
if "java" in enabled_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"
)
)
# Copy configuration files
template = (
template.copy("matplotlibrc", ".config/matplotlib/.matplotlibrc")
.copy("start-up.sh", ".jupyter/start-up.sh")
.copy("start-code-interpreter.sh", ".jupyter/start-code-interpreter.sh")
.copy("start-jupyter.sh", ".jupyter/start-jupyter.sh")
.run_cmd(
"chmod +x .jupyter/start-code-interpreter.sh .jupyter/start-up.sh .jupyter/start-jupyter.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")
# Install supervisord config
.copy("supervisord.conf", "/etc/supervisord.conf")
)
if is_docker:
# create user user and /home/user
template = template.run_cmd("useradd -m user")
template = template.run_cmd("mkdir -p /home/user")
template = template.run_cmd("chown -R user:user /home/user")
# add to sudoers
template = template.run_cmd(
"echo 'user ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
)
template = template.set_user("user").set_workdir("/home/user")
return template.set_start_cmd(
"sudo /root/.jupyter/start-up.sh", wait_for_url("http://localhost:49999/health")
)