-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
cmd_install.py
644 lines (501 loc) · 20.6 KB
/
cmd_install.py
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import os
import re
import shutil
import subprocess
import sys
from typing import Dict, Optional, Tuple
import click
import requests
from packaging.version import Version
from lightning_app.core.constants import LIGHTNING_APPS_PUBLIC_REGISTRY, LIGHTNING_COMPONENT_PUBLIC_REGISTRY
from lightning_app.utilities.app_helpers import Logger
logger = Logger(__name__)
@click.group(name="install")
def install() -> None:
"""Install Lightning AI selfresources."""
pass
@install.command("app")
@click.argument("name", type=str)
@click.option(
"--yes",
"-y",
is_flag=True,
help="disables prompt to ask permission to create env and run install cmds",
)
@click.option(
"--version",
"-v",
type=str,
help="Specify the version to install. By default it uses 'latest'",
default="latest",
show_default=True,
)
@click.option(
"--overwrite",
"-f",
is_flag=True,
default=False,
help="When set, overwrite the app directory without asking if it already exists.",
)
def install_app(name: str, yes: bool, version: str, overwrite: bool = False) -> None:
_install_app_command(name, yes, version, overwrite=overwrite)
@install.command("component")
@click.argument("name", type=str)
@click.option(
"--yes",
"-y",
is_flag=True,
help="disables prompt to ask permission to create env and run install cmds",
)
@click.option(
"--version",
"-v",
type=str,
help="Specify the version to install. By default it uses 'latest'",
default="latest",
show_default=True,
)
def install_component(name: str, yes: bool, version: str) -> None:
_install_component_command(name, yes, version)
def _install_app_command(name: str, yes: bool, version: str, overwrite: bool = False) -> None:
if "github.com" in name:
if version != "latest":
logger.warn(
"When installing from GitHub, only the 'latest' version is supported. "
f"The provided version ({version}) will be ignored."
)
return non_gallery_app(name, yes, overwrite=overwrite)
else:
return gallery_app(name, yes, version, overwrite=overwrite)
def _install_component_command(name: str, yes: bool, version: str, overwrite: bool = False) -> None:
if "github.com" in name:
if version != "latest":
logger.warn(
"When installing from GitHub, only the 'latest' version is supported. "
f"The provided version ({version}) will be ignored."
)
return non_gallery_component(name, yes)
else:
return gallery_component(name, yes, version)
def gallery_apps_and_components(
name: str, yes_arg: bool, version_arg: str, cwd: str = None, overwrite: bool = False
) -> Optional[str]:
try:
org, app_or_component = name.split("/")
except Exception:
return None
entry, kind = _resolve_entry(name, version_arg)
if kind == "app":
# give the user the chance to do a manual install
source_url, git_url, folder_name, git_sha = _show_install_app_prompt(
entry, app_or_component, org, yes_arg, resource_type="app"
)
# run installation if requested
_install_app_from_source(source_url, git_url, folder_name, cwd=cwd, overwrite=overwrite, git_sha=git_sha)
return os.path.join(os.getcwd(), *entry["appEntrypointFile"].split("/"))
elif kind == "component":
# give the user the chance to do a manual install
source_url, git_url, folder_name, git_sha = _show_install_app_prompt(
entry, app_or_component, org, yes_arg, resource_type="component"
)
if "@" in git_url:
git_url = git_url.split("git+")[1].split("@")[0]
# run installation if requested
_install_app_from_source(source_url, git_url, folder_name, cwd=cwd, overwrite=overwrite, git_sha=git_sha)
return os.path.join(os.getcwd(), *entry["entrypointFile"].split("/"))
return None
def gallery_component(name: str, yes_arg: bool, version_arg: str, cwd: str = None) -> str:
# make sure org/component-name name is correct
org, component = _validate_name(name, resource_type="component", example="lightning/LAI-slack-component")
# resolve registry (orgs can have a private registry through their environment variables)
registry_url = _resolve_component_registry()
# load the component resource
component_entry = _resolve_resource(registry_url, name=name, version_arg=version_arg, resource_type="component")
# give the user the chance to do a manual install
git_url = _show_install_component_prompt(component_entry, component, org, yes_arg)
# run installation if requested
_install_component_from_source(git_url)
return os.path.join(os.getcwd(), component_entry["entrypointFile"])
def non_gallery_component(gh_url: str, yes_arg: bool, cwd: str = None) -> None:
# give the user the chance to do a manual install
git_url = _show_non_gallery_install_component_prompt(gh_url, yes_arg)
# run installation if requested
_install_component_from_source(git_url)
def gallery_app(name: str, yes_arg: bool, version_arg: str, cwd: str = None, overwrite: bool = False) -> str:
# make sure org/app-name syntax is correct
org, app = _validate_name(name, resource_type="app", example="lightning/quick-start")
# resolve registry (orgs can have a private registry through their environment variables)
registry_url = _resolve_app_registry()
# load the app resource
app_entry = _resolve_resource(registry_url, name=name, version_arg=version_arg, resource_type="app")
# give the user the chance to do a manual install
source_url, git_url, folder_name, git_sha = _show_install_app_prompt(
app_entry, app, org, yes_arg, resource_type="app"
)
# run installation if requested
_install_app_from_source(source_url, git_url, folder_name, cwd=cwd, overwrite=overwrite, git_sha=git_sha)
return os.path.join(os.getcwd(), folder_name, app_entry["appEntrypointFile"])
def non_gallery_app(gh_url: str, yes_arg: bool, cwd: str = None, overwrite: bool = False) -> None:
# give the user the chance to do a manual install
repo_url, folder_name = _show_non_gallery_install_app_prompt(gh_url, yes_arg)
# run installation if requested
_install_app_from_source(repo_url, repo_url, folder_name, cwd=cwd, overwrite=overwrite)
def _show_install_component_prompt(entry: Dict[str, str], component: str, org: str, yes_arg: bool) -> str:
git_url = entry["gitUrl"]
# yes arg does not prompt the user for permission to install anything
# automatically creates env and sets up the project
if yes_arg:
return git_url
prompt = f"""
⚡ Installing Lightning component ⚡
component name : {component}
developer : {org}
Installation runs the following command for you:
pip install {git_url}
"""
logger.info(prompt)
try:
value = input("\nPress enter to continue: ")
value = value.strip().lower()
should_install = len(value) == 0 or value in {"y", "yes", 1}
if not should_install:
raise KeyboardInterrupt()
return git_url
except KeyboardInterrupt:
repo = entry["sourceUrl"]
m = f"""
⚡ Installation aborted! ⚡
Install the component yourself by visiting:
{repo}
"""
raise SystemExit(m)
def _show_non_gallery_install_component_prompt(gh_url: str, yes_arg: bool) -> str:
if ".git@" not in gh_url:
m = """
Error, your github url must be in the following format:
git+https://github.com/OrgName/repo-name.git@ALongCommitSHAString
Example:
git+https://github.com/Lightning-AI/LAI-slack-messenger.git@14f333456ffb6758bd19458e6fa0bf12cf5575e1
"""
raise SystemExit(m)
developer = gh_url.split("/")[3]
component_name = gh_url.split("/")[4].split(".git")[0]
repo_url = re.search(r"git\+(.*).git", gh_url).group(1) # type: ignore
# yes arg does not prompt the user for permission to install anything
# automatically creates env and sets up the project
if yes_arg:
return gh_url
prompt = f"""
⚡ Installing Lightning component ⚡
component name : {component_name}
developer : {developer}
⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡
WARNING: this is NOT an official Lightning Gallery component
Install at your own risk
⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡
Installation runs the following command for you:
pip install {gh_url}
"""
logger.info(prompt)
try:
value = input("\nPress enter to continue: ")
value = value.strip().lower()
should_install = len(value) == 0 or value in {"y", "yes", 1}
if not should_install:
raise KeyboardInterrupt()
return gh_url
except KeyboardInterrupt:
m = f"""
⚡ Installation aborted! ⚡
Install the component yourself by visiting:
{repo_url}
"""
raise SystemExit(m)
def _show_install_app_prompt(
entry: Dict[str, str], app: str, org: str, yes_arg: bool, resource_type: str
) -> Tuple[str, str, str, Optional[str]]:
source_url = entry["sourceUrl"] # This URL is used only to display the repo and extract folder name
full_git_url = entry["gitUrl"] # Used to clone the repo (can include tokens for private repos)
git_url_parts = full_git_url.split("#ref=")
git_url = git_url_parts[0]
git_sha = git_url_parts[1] if len(git_url_parts) == 2 else None
folder_name = source_url.split("/")[-1]
# yes arg does not prompt the user for permission to install anything
# automatically creates env and sets up the project
if yes_arg:
return source_url, git_url, folder_name, git_sha
prompt = f"""
⚡ Installing Lightning {resource_type} ⚡
{resource_type} name : {app}
developer: {org}
Installation creates and runs the following commands for you:
git clone {source_url}
cd {folder_name}
pip install -r requirements.txt
pip install -e .
"""
logger.info(prompt)
try:
value = input("\nPress enter to continue: ")
value = value.strip().lower()
should_install = len(value) == 0 or value in {"y", "yes", 1}
if not should_install:
raise KeyboardInterrupt()
return source_url, git_url, folder_name, git_sha
except KeyboardInterrupt:
repo = entry["sourceUrl"]
m = f"""
⚡ Installation aborted! ⚡
Install the {resource_type} yourself by visiting:
{repo}
"""
raise SystemExit(m)
def _show_non_gallery_install_app_prompt(gh_url: str, yes_arg: bool) -> Tuple[str, str]:
try:
if gh_url.endswith(".git"):
# folder_name when it's a GH url with .git
folder_name = gh_url.split("/")[-1]
folder_name = folder_name[:-4]
else:
# the last part of the url is the folder name otherwise
folder_name = gh_url.split("/")[-1]
org = re.search(r"github.com\/(.*)\/", gh_url).group(1) # type: ignore
except Exception as e: # noqa
m = """
Your github url is not supported. Here's the supported format:
https://github.com/YourOrgName/your-repo-name
Example:
https://github.com/Lightning-AI/lightning
"""
raise SystemExit("")
# yes arg does not prompt the user for permission to install anything
# automatically creates env and sets up the project
if yes_arg:
return gh_url, folder_name
prompt = f"""
⚡ Installing Lightning app ⚡
app source : {gh_url}
developer : {org}
⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡
WARNING: this is NOT an official Lightning Gallery app
Install at your own risk
⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡
Installation creates and runs the following commands for you:
git clone {gh_url}
cd {folder_name}
pip install -r requirements.txt
pip install -e .
"""
logger.info(prompt)
try:
value = input("\nPress enter to continue: ")
value = value.strip().lower()
should_install = len(value) == 0 or value in {"y", "yes", 1}
if not should_install:
raise KeyboardInterrupt()
return gh_url, folder_name
except KeyboardInterrupt:
m = f"""
⚡ Installation aborted! ⚡
Install the app yourself by visiting {gh_url}
"""
raise SystemExit(m)
def _validate_name(name: str, resource_type: str, example: str) -> Tuple[str, str]:
# ensure resource identifier is properly formatted
try:
org, resource = name.split("/")
except Exception as e: # noqa
m = f"""
{resource_type} name format must have organization/{resource_type}-name
Examples:
{example}
user/{resource_type}-name
You passed in: {name}
"""
raise SystemExit(m)
m = f"""
⚡ Installing Lightning {resource_type} ⚡
{resource_type} name: {resource}
developer: {org}
"""
return org, resource
def _resolve_entry(name, version_arg) -> Tuple[Optional[Dict], Optional[str]]:
entry = None
kind = None
# resolve registry (orgs can have a private registry through their environment variables)
registry_url = _resolve_app_registry()
# load the app resource
entry = _resolve_resource(registry_url, name=name, version_arg=version_arg, resource_type="app", raise_error=False)
if not entry:
registry_url = _resolve_component_registry()
# load the component resource
entry = _resolve_resource(
registry_url, name=name, version_arg=version_arg, resource_type="component", raise_error=False
)
kind = "component" if entry else None
else:
kind = "app"
return entry, kind
def _resolve_resource(
registry_url: str, name: str, version_arg: str, resource_type: str, raise_error: bool = True
) -> Dict[str, str]:
gallery_entries = []
try:
response = requests.get(registry_url)
data = response.json()
if resource_type == "app":
gallery_entries = [a for a in data["apps"] if a["canDownloadSourceCode"]]
elif resource_type == "component":
gallery_entries = data["components"]
except requests.ConnectionError:
m = f"""
Network connection error, could not load list of available Lightning {resource_type}s.
Try again when you have a network connection!
"""
sys.tracebacklimit = 0
raise SystemError(m)
entries = []
all_versions = []
for x in gallery_entries:
if name == x["name"]:
entries.append(x)
all_versions.append(x["version"])
if len(entries) == 0:
if raise_error:
raise SystemExit(f"{resource_type}: '{name}' is not available on ⚡ Lightning AI ⚡")
else:
return None
entry = None
if version_arg == "latest":
entry = max(entries, key=lambda app: Version(app["version"]))
else:
for e in entries:
if e["version"] == version_arg:
entry = e
break
if entry is None and raise_error:
if raise_error:
raise Exception(
f"{resource_type}: 'Version {version_arg} for {name}' is not available on ⚡ Lightning AI ⚡. "
f"Here is the list of all availables versions:{os.linesep}{os.linesep.join(all_versions)}"
)
else:
return None
return entry
def _install_with_env(repo_url: str, folder_name: str, cwd: str = None) -> None:
if not cwd:
cwd = os.getcwd()
# clone repo
logger.info(f"⚡ RUN: git clone {repo_url}")
subprocess.call(["git", "clone", repo_url])
# step into the repo folder
os.chdir(f"{folder_name}")
cwd = os.getcwd()
# create env
logger.info(f"⚡ CREATE: virtual env at {cwd}")
subprocess.call(["python", "-m", "venv", cwd])
# activate and install reqs
# TODO: remove shell=True... but need to run command in venv
logger.info("⚡ RUN: install requirements (pip install -r requirements.txt)")
subprocess.call("source bin/activate && pip install -r requirements.txt", shell=True)
# install project
# TODO: remove shell=True... but need to run command in venv
logger.info("⚡ RUN: setting up project (pip install -e .)")
subprocess.call("source bin/activate && pip install -e .", shell=True)
m = f"""
⚡ Installed! ⚡ to use your app
go into the folder: cd {folder_name}
activate the environment: source bin/activate
run the app: lightning run app [the_app_file.py]
"""
logger.info(m)
def _install_app_from_source(
source_url: str, git_url: str, folder_name: str, cwd: str = None, overwrite: bool = False, git_sha: str = None
) -> None:
"""Installing lighting app from the `git_url`
Args:
source_url:
source repo url without any tokens and params, this param is used only for displaying
git_url:
repo url that is used to clone, this can contain tokens
folder_name:
where to clone the repo ?
cwd:
Working director. If not specified, current working directory is used.
overwrite:
If true, overwrite the app directory without asking if it already exists
git_sha:
The git_sha for checking out the git repo of the app.
"""
if not cwd:
cwd = os.getcwd()
destination = os.path.join(cwd, folder_name)
if os.path.exists(destination):
if not overwrite:
raise SystemExit(
f"Folder {folder_name} exists, please delete it and try again, "
f"or force to overwrite the existing folder by passing `--overwrite`.",
)
shutil.rmtree(destination)
# clone repo
logger.info(f"⚡ RUN: git clone {source_url}")
try:
subprocess.check_output(["git", "clone", git_url], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if "Repository not found" in str(e.output):
m = f"""
Looks like the github url was not found or doesn't exist. Do you have a typo?
{source_url}
"""
raise SystemExit(m)
else:
raise Exception(e)
# step into the repo folder
os.chdir(f"{folder_name}")
cwd = os.getcwd()
try:
if git_sha:
subprocess.check_output(["git", "checkout", git_sha], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if "did not match any" in str(e.output):
raise SystemExit("Looks like the git SHA is not valid or doesn't exist in app repo.")
else:
raise Exception(e)
# activate and install reqs
# TODO: remove shell=True... but need to run command in venv
logger.info("⚡ RUN: install requirements (pip install -r requirements.txt)")
subprocess.call("pip install -r requirements.txt", shell=True)
# install project
# TODO: remove shell=True... but need to run command in venv
logger.info("⚡ RUN: setting up project (pip install -e .)")
subprocess.call("pip install -e .", shell=True)
m = f"""
⚡ Installed! ⚡ to use your app:
cd {folder_name}
lightning run app app.py
"""
logger.info(m)
def _install_component_from_source(git_url: str) -> None:
logger.info("⚡ RUN: pip install")
out = subprocess.check_output(["pip", "install", git_url])
possible_success_message = [x for x in str(out).split("\\n") if "Successfully installed" in x]
if len(possible_success_message) > 0:
uninstall_step = possible_success_message[0]
uninstall_step = re.sub("Successfully installed", "", uninstall_step).strip()
uninstall_step = re.sub("-0.0.0", "", uninstall_step).strip()
m = """
⚡ Installed! ⚡
to use your component:
from the_component import TheClass
make sure to add this entry to your Lightning APP requirements.txt file:
{git_url}
if you want to uninstall, run this command:
pip uninstall {uninstall_step}
"""
logger.info(m)
def _resolve_app_registry() -> str:
registry = os.environ.get("LIGHTNING_APP_REGISTRY", LIGHTNING_APPS_PUBLIC_REGISTRY)
return registry
def _resolve_component_registry() -> str:
registry = os.environ.get("LIGHTNING_COMPONENT_REGISTRY", LIGHTNING_COMPONENT_PUBLIC_REGISTRY)
return registry