This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdatabrickslabs-jupyterlab
executable file
·386 lines (341 loc) · 9.36 KB
/
databrickslabs-jupyterlab
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python
import argparse
import os
import re
import sys
from enum import Enum
from version_parser import Version
import databrickslabs_jupyterlab
from databrickslabs_jupyterlab.remote import (
configure_ssh,
download_notebook,
get_cluster,
get_python_path,
install_libs,
is_reachable,
version_check,
)
from databrickslabs_jupyterlab.local import (
conda_version,
create_kernelspec,
get_db_config,
prepare_ssh_config,
print_error,
print_ok,
remove_kernelspecs,
show_profiles,
write_config,
)
from databrickslabs_jupyterlab.install import install
from databrickslabs_jupyterlab.utils import bye
class VCheck(Enum):
a = "all"
d = "diff"
s = "same"
def __str__(self):
return self.value
parser = argparse.ArgumentParser(description="Configure remote Databricks access with JupyterLab")
parser.add_argument("profile", nargs="?", type=str, default="", help="A databricks-cli profile")
# parser.add_argument(
# "-b",
# "--bootstrap",
# dest="bootstrap",
# action="store_true",
# help="Bootstrap the local databrickslabs-jupyterlab environment",
# )
parser.add_argument(
"-d", "--delete", dest="delete", action="store_true", help="Delete a jupyter kernelspec"
)
parser.add_argument(
"-m",
"--mirror",
dest="mirror",
action="store_true",
help="Mirror a a remote Databricks environment",
)
# parser.add_argument(
# "-c",
# "--clipboard",
# dest="clipboard",
# action="store_true",
# help="Copy the personal access token to the clipboard",
# )
parser.add_argument(
"-D", "--debug", dest="debug", action="store_true", help="Debug calls to REST API"
)
parser.add_argument(
"-e",
"--env",
dest="extra_env",
nargs="+",
help="Environment variables to add to the remote notebook env",
)
parser.add_argument(
"-i", "--id", dest="cluster_id", help="The cluster_id to avoid manual selection"
)
parser.add_argument(
"-k",
"--kernelspec",
dest="kernelspec",
action="store_true",
help="Create a kernel specification",
)
parser.add_argument("-l", "--lab", dest="lab", action="store_true", help="Safely start JupyterLab")
parser.add_argument("-n", "--notebook_url", dest="notebook_url", help="Download demo notebook")
parser.add_argument(
"-o", "--organisation", dest="organisation", help="The organisation for Azure Databricks"
)
parser.add_argument(
"-p",
"--profiles",
dest="profiles",
action="store_true",
help="Show all databricks cli profiles and check SSH key",
)
parser.add_argument(
"-r",
"--reconfigure",
dest="reconfigure",
action="store_true",
help="Reconfigure cluster with id cluster_id",
)
parser.add_argument(
"-s",
"--ssh-config",
dest="sshconfig",
action="store_true",
help="Configure SSH access for a cluster",
)
parser.add_argument(
"-v",
"--version",
dest="version",
action="store_true",
help="Check version of databrickslabs-jupyterlab",
)
parser.add_argument(
"-V",
"--versioncheck",
dest="versioncheck",
type=VCheck,
choices=list(VCheck),
help="Check version of local env with remote env",
)
parser.add_argument(
"-w",
"--whitelist",
dest="use_whitelist",
action="store_true",
help="Use a whitelist (include pkg) of packages to install instead of blacklist (exclude pkg)",
)
parser.add_argument(
"-W",
"--print-whitelist",
dest="whitelist",
action="store_true",
help="Print whitelist (include pkg) of packages to install",
)
parser.add_argument(
"-B",
"--print-blacklist",
dest="blacklist",
action="store_true",
help="Print blacklist (exclude pkg) of packages to install",
)
parser.add_argument(
"-N",
"--no-spark",
dest="no_spark",
action="store_true",
help="Do not create a Spark Session",
)
args = parser.parse_args()
if args.debug:
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
#
# Print Version and exit
#
if args.version:
import databrickslabs_jupyterlab._version
print(databrickslabs_jupyterlab._version.__version__) # pylint: disable=protected-access
bye()
#
# Print White list and exit
#
if args.whitelist:
import databrickslabs_jupyterlab.install
print(" => If package exists in remote cluster an this list, it will be installed:")
print(databrickslabs_jupyterlab.install.WHITELIST)
bye()
#
# Print Black list and exit
#
if args.blacklist:
import databrickslabs_jupyterlab.install
print(" => These packages will note be installed:")
print(databrickslabs_jupyterlab.install.BLACKLIST)
bye()
#
# Print White list and exit
#
if args.delete:
remove_kernelspecs()
bye()
#
# Bootstrap the initial environment and exit
#
# if args.bootstrap:
# update_local()
# bye()
#
# Show all configure profiles
#
if args.profiles:
show_profiles()
bye()
#
# Download Demo notebook
#
if args.notebook_url:
download_notebook(args.notebook_url)
bye()
profile = args.profile
#
# Check that at least conda 4.7.5 is installed. If not, exit
#
version = conda_version()
if Version("4.7.5") < Version(version):
print("Valid version of conda detected: %s" % version)
else:
print("Too old conda version:")
print("Please update conda to at least 4.7.5")
sys.exit(1)
#
# Ensure profile argument is given
#
if args.profile == "":
parser.print_help()
print_error(
"\ndatabrickslabs-jupyterlab: error: the following arguments are required: profile\n"
)
bye(1)
print("\n* Getting host and token from .databrickscfg")
host, token = get_db_config(profile)
if (
("azuredatabricks.net" in host)
and (args.kernelspec or args.reconfigure)
and not args.organisation
):
if host.startswith("https://adb"):
r = re.compile(r"https://adb-(\d+)\.\d+.azuredatabricks.net")
result = r.match(host)
args.organisation = result.group(1)
else:
print_error(
"\n Error: To configure an Azure Databricks cluster using the old CNAME,\n"
+ "(<region>.azuredatabricks.net) the organization id (-o id) is required"
)
bye(1)
#
# Configure SSH for cluster
#
if args.sshconfig:
if not args.cluster_id:
print_error(" => Error: Provide '-i cluster_id' with '-s' ")
bye(1)
configure_ssh(profile, args.cluster_id)
bye()
#
# Check the ssh key for the given profile and retrieve host and token from ~/.databrickscfg
#
if not os.path.exists(os.path.expanduser("~/.ssh/id_%s.pub" % profile)):
print_error("\n Error: ssh key for profile '%s' is missing:" % profile)
print(" Use 'databrickslabs-jupyterlab %s -s' to configure ssh access" % profile)
bye(1)
#
# Copy PAT to clipboard. Can be combined with every other flag
#
# if args.clipboard:
# import pyperclip
# pyperclip.copy(token)
# print_ok(" => Personal access token copied to clipboard")
#
# Select remote cluster if not given by either -i or being in the respective mirror environment
#
if args.kernelspec or args.reconfigure or args.lab or args.versioncheck or args.mirror:
print("\n* Select remote cluster\n")
cluster_id, endpoint, cluster_name, conda_env = get_cluster(profile, args.cluster_id)
if cluster_name is None:
bye(1)
print("\n* Configuring ssh config for remote cluster")
prepare_ssh_config(cluster_id, profile, endpoint)
print(" => Testing whether cluster can be reached")
if not is_reachable(endpoint):
print_error("Cannot connect to remote cluster. Please check:")
print_error(
"- whether the address is reachable and the port is open of %s in cloud security group"
% endpoint
)
print_error("- whether VPN is enabled if you need one to connect to the cluster")
bye(1)
print_ok(" => OK")
#
# Compare local and remote libray versions
#
if args.versioncheck:
version_check(cluster_id, host, token, args.versioncheck)
bye()
#
# Create a conda environment that mirrors the selected remote cluster and exit
#
if args.mirror:
install(profile, host, token, cluster_id, cluster_name, args.use_whitelist)
bye()
#
# Install databrickslabs_jupyterlab libraries on the driver
#
if args.kernelspec or args.reconfigure or args.lab:
print("\n* Installing driver libraries")
result = install_libs(cluster_id, host, token)
if result[0] == 0:
print_ok(" => OK")
else:
print_error(result[1])
bye(1)
#
# Start Jupyter lab in a safely way with all prechecks from above
#
if args.lab:
import jupyterlab.labapp
sys.argv = sys.argv[:1]
jupyterlab.labapp.main()
#
# Create a jupyter kernelspecification
#
if args.kernelspec:
print("\n* Creating remote kernel spec")
print("args.extra_env", args.extra_env)
if args.extra_env is None:
extra_env = ""
else:
extra_env = " ".join(args.extra_env)
python_path = get_python_path(cluster_id)
# Using local conda environment name avoids overwriting of kernelspecs from different
# environments
create_kernelspec(
profile,
args.organisation,
host,
cluster_id,
cluster_name,
os.environ["CONDA_DEFAULT_ENV"],
python_path,
args.no_spark,
extra_env,
)
print_ok(" => OK")
print("\n* Setting global config of jupyter lab (autorestart, timeout)")
write_config()
print_ok(" => OK")
print("")