Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
137 lines (124 loc) · 4.26 KB
/
Copy pathcode.py
File metadata and controls
137 lines (124 loc) · 4.26 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
import os
import subprocess
import uuid
import nest_asyncio
import uvicorn
from pyngrok import ngrok
try:
from google.colab import drive
colab_env = True
except ImportError:
colab_env = False
EXTENSIONS = [
"ms-python.python",
"ms-toolsai.jupyter",
"mechatroner.rainbow-csv",
"vscode-icons-team.vscode-icons",
"mhutchie.git-graph",
"ms-toolsai.jupyter",
"ms-toolsai.jupyter-renderers",
"ms-python.vscode-pylance",
"ms-python.python",
"mutantdino.resourcemonitor",
"ccimage.jsonviewer",
"funkyremi.vscode-google-translate",
"vladeeg.vscode-theme-vdark"
]
CODESERVER_VERSION = "3.10.2"
CODESERVER_PACKAGE = "openvscode-server-insiders-v1.63.0-linux-x64"
CODESERVER_URL = "https://github.com/gitpod-io/openvscode-server/releases/download/openvscode-server-insiders-v1.63.0/"
CODESERVER_LINK = CODESERVER_URL + CODESERVER_PACKAGE + ".tar.gz"
class ColabCode:
def __init__(
self,
port=10000,
proto='http',
password=None,
authtoken=None,
mount_drive=False,
code=True,
lab=False,
extentions = []
):
self.port = port
self.proto = proto
self.password = password
self.authtoken = authtoken
self._mount = mount_drive
self._code = code
self._lab = lab
self.extentions = extentions
if self._lab:
self._start_server()
self._run_lab()
if self._code:
self._install_code()
self._install_extensions()
self._start_server()
self._run_code()
@staticmethod
def _install_code():
subprocess.run(["wget", "https://code-server.dev/install.sh"], stdout=subprocess.PIPE)
subprocess.run(
["sh", "install.sh", "--version", f"{CODESERVER_VERSION}"],
stdout=subprocess.PIPE,
)
def _install_extensions(self):
for ext in EXTENSIONS:
subprocess.run(["code-server", "--install-extension", f"{ext}"])
for ext in self.extentions:
subprocess.run(["code-server", "--install-extension", f"{ext}"])
def _start_server(self):
if self.authtoken:
ngrok.set_auth_token(self.authtoken)
active_tunnels = ngrok.get_tunnels()
for tunnel in active_tunnels:
public_url = tunnel.public_url
ngrok.disconnect(public_url)
url = ngrok.connect(addr=self.port, proto=self.proto, bind_tls=True).public_url
if self._code:
print(f"Code Server can be accessed on: {url}")
else:
print(f"Public URL: {url}")
def _run_lab(self):
token = str(uuid.uuid1())
print(f"Jupyter lab token: {token}")
base_cmd = "jupyter-lab --ip='localhost' --allow-root --ServerApp.allow_remote_access=True --no-browser"
os.system(f"fuser -n tcp -k {self.port}")
if self._mount and colab_env:
drive.mount("/content/drive")
if self.password:
lab_cmd = f" --ServerApp.token='{token}' --ServerApp.password='{self.password}' --port {self.port}"
else:
lab_cmd = f" --ServerApp.token='{token}' --ServerApp.password='' --port {self.port}"
lab_cmd = base_cmd + lab_cmd
with subprocess.Popen(
[lab_cmd],
shell=True,
stdout=subprocess.PIPE,
bufsize=1,
universal_newlines=True,
) as proc:
for line in proc.stdout:
print(line, end="")
def _run_code(self):
os.system(f"fuser -n tcp -k {self.port}")
if self._mount and colab_env:
drive.mount("/content/drive")
if self.password:
code_cmd = f"PASSWORD={self.password} code-server --port {self.port} --disable-telemetry"
else:
code_cmd = f"code-server --port {self.port} --auth none --disable-telemetry"
with subprocess.Popen(
[code_cmd],
shell=True,
stdout=subprocess.PIPE,
bufsize=1,
universal_newlines=True,
) as proc:
for line in proc.stdout:
print(line, end="")
def run_app(self, app, workers=1):
self._start_server()
nest_asyncio.apply()
uvicorn.run(app, host="127.0.0.1", port=self.port, workers=workers)