-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.py
1452 lines (1225 loc) · 41.3 KB
/
cli.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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2016-2022 Faculty Science Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command line interface."""
import contextlib
import operator
import os
import os.path
import shutil
import stat
import subprocess
import sys
import tempfile
import textwrap
import time
import uuid
from distutils.version import StrictVersion
import click
import faculty
import faculty.config
import requests
import faculty.clients.base
import faculty.datasets
from faculty.clients.server import (
DedicatedServerResources,
ServerStatus,
SharedServerResources,
)
from faculty.clients.serveragent import ServerAgentClient
from faculty.session import get_session
from tabulate import tabulate
import faculty_cli.parse
import faculty_cli.shell
import faculty_cli.update
import faculty_cli.version
SSH_OPTIONS = [
"-o",
"IdentitiesOnly=yes",
"-o",
"StrictHostKeyChecking=no",
"-o",
"BatchMode=yes",
]
class AmbiguousNameError(Exception):
"""Exception when name matches multiple resources."""
pass
class NameNotFoundError(Exception):
"""Exception when a resource name is not found."""
pass
def _print_and_exit(msg, code):
"""Print error message and exit with given code."""
click.echo(msg, err=True)
sys.exit(code)
def _get_pypi_versions():
"""List releases available from PyPI."""
response = requests.get(
"https://pypi.org/pypi/faculty-cli/json", timeout=1
)
versions = response.json()["releases"].keys()
return [StrictVersion(v) for v in versions]
def _populate_creds_file():
"""Prompt user for client ID and secret and save them."""
while True:
domain = click.prompt(
"Domain", default=faculty.config.DEFAULT_DOMAIN, err=True
)
client_id = click.prompt("Client ID", err=True).strip()
client_secret = click.prompt("Client secret", err=True).strip()
profile = faculty.config.Profile(
domain=domain,
protocol=faculty.config.DEFAULT_PROTOCOL,
client_id=client_id,
client_secret=client_secret,
)
session = faculty.session.Session(
profile, faculty.session.accesstoken.AccessTokenMemoryCache()
)
try:
session.access_token()
except Exception:
click.echo("Invalid credentials. Please try again.", err=True)
else:
break
credentials = textwrap.dedent(
"""\
[{profile}]
domain = {domain}
client_id = {client_id}
client_secret = {client_secret}
""".format(
profile=faculty.config.DEFAULT_PROFILE,
client_id=client_id,
client_secret=client_secret,
domain=domain,
)
)
credentials_file = faculty.config.resolve_credentials_path()
try:
os.makedirs(os.path.dirname(credentials_file))
except OSError:
pass
with open(credentials_file, "w") as creds_file:
creds_file.write(credentials)
os.chmod(
credentials_file,
stat.S_IRUSR | stat.S_IWUSR & ~stat.S_IRGRP & ~stat.S_IROTH,
)
def _check_creds_file_perms():
"""Check the permissions of the credentials file are correct."""
credentials_file = faculty.config.resolve_credentials_path()
if oct(os.stat(credentials_file).st_mode & 0o777)[-2:] != "00":
msg = textwrap.dedent(
"""\
Permissions for {0} are too open.
Your credentials file must not be accessible to other users on this
computer.
Run 'chmod 0600 {0}' to fix this.""".format(
credentials_file
)
)
_print_and_exit(msg, 66)
def _ensure_creds_file_present():
"""Ensure the user's credentials file is present."""
credentials_file = faculty.config.resolve_credentials_path()
try:
open(credentials_file)
except IOError:
msg = textwrap.dedent(
"""\
It looks like this is the first time you've used the Faculty CLI on
this computer, so you must enter your Faculty credentials. They'll be
saved so you don't have to enter them again.
"""
)
click.echo(msg, err=True)
_populate_creds_file()
def _check_credentials():
"""Check if credentials are present in environment or config file."""
try:
faculty.config.resolve_profile()
except faculty.config.CredentialsError:
_ensure_creds_file_present()
_check_creds_file_perms()
def _get_authenticated_user_id():
client = faculty.client("account")
return client.authenticated_user_id()
def _list_projects():
"""List all projects accessible by user."""
client = faculty.client("project")
user_id = _get_authenticated_user_id()
projects = client.list_accessible_by_user(user_id)
return projects
def _match_project(project):
"""Find a project by matching its name."""
projects = _list_projects()
matching_projects = [p for p in projects if p.name == project]
if len(matching_projects) == 1:
project = matching_projects[0]
else:
if not matching_projects:
msg = 'no project of name "{}" found'.format(project)
raise NameNotFoundError(msg)
else:
msg = (
'more than one project of name "{}", please select by '
"project ID instead"
).format(project)
raise AmbiguousNameError(msg)
return project.id
def _resolve_project(project):
"""Resolve a project name or ID to a project ID."""
try:
project_id = uuid.UUID(project)
except ValueError:
project_id = _match_project(project)
return project_id
def _get_servers(project_id, name=None, status=None):
"""List servers in the given project."""
client = faculty.client("server")
servers = client.list(project_id, name)
if status is not None:
servers = [s for s in servers if s.status == status]
return servers
def _list_user_servers(user_id, status=None):
"""List all servers owned by user."""
client = faculty.client("server")
servers = client.list_for_user(user_id)
if status is not None:
servers = [s for s in servers if s.status == status]
return servers
def _server_by_name(project_id, server_name, status=None):
"""Resolve a project ID and server name to a server ID."""
servers = _get_servers(project_id, server_name, status)
if len(servers) == 1:
return servers[0]
else:
adjective = "available" if status is None else status.value
if not servers:
msg = 'no {} server of name "{}" in this project'.format(
adjective, server_name
)
raise NameNotFoundError(msg)
else:
msg = (
'more than one {} server of name "{}", please select by '
"server ID instead"
).format(adjective, server_name)
raise AmbiguousNameError(msg)
def _any_server(project_id, status=None):
"""Get any server from project."""
servers = _get_servers(project_id, status=status)
if not servers:
adjective = "available" if status is None else status.value
_print_and_exit("No {} server in project.".format(adjective), 78)
return servers[0].id
def _resolve_server(project, server=None, ensure_running=True):
"""Resolve project and server names to project and server IDs."""
project_id = _resolve_project(project)
status = ServerStatus.RUNNING if ensure_running else None
try:
server_id = uuid.UUID(server)
except ValueError:
server_id = _server_by_name(project_id, server, status).id
except TypeError:
server_id = _any_server(project_id, status)
return project_id, server_id
def _server_spec(server):
"""Return formatted strings for machine type, cpu and memory of server."""
if isinstance(server.resources, SharedServerResources):
machine_type = "-"
cpus = "{:.3g}".format(server.resources.milli_cpus / 1000)
memory_gb = "{:.3g}GB".format(server.resources.memory_mb / 1000)
else:
machine_type = server.resources.node_type
cpus = "-"
memory_gb = "-"
return machine_type, cpus, memory_gb
def _get_ssh_details(project, server):
project_id, server_id = _resolve_server(project, server)
client = faculty.client("server")
return client.get_ssh_details(project_id, server_id)
def _job_by_name(project_id, job_name):
"""Resolve a project ID and job name to a job ID."""
client = faculty.client("job")
jobs = client.list(project_id)
matching_jobs = [job for job in jobs if job.metadata.name == job_name]
if len(matching_jobs) == 1:
return matching_jobs[0]
else:
if not matching_jobs:
msg = 'no job of name "{}" in this project'.format(job_name)
raise NameNotFoundError(msg)
else:
msg = (
'more than one job of name "{}", please select by job ID '
"instead"
).format(job_name)
raise AmbiguousNameError(msg)
def _resolve_job(project, job):
"""Resolve project and job names to project and job IDs."""
project_id = _resolve_project(project)
try:
job_id = uuid.UUID(job)
except ValueError:
job_id = _job_by_name(project_id, job).id
return project_id, job_id
def _environment_by_name(project_id, environment_name):
client = faculty.client("environment")
environments = client.list(project_id)
matching_environments = [
e for e in environments if e.name == environment_name
]
if len(matching_environments) == 1:
return matching_environments[0]
else:
if not matching_environments:
msg = 'no available environment of name "{}"'.format(
environment_name
)
raise NameNotFoundError(msg)
else:
msg = (
'more than one environment of name "{}", please select by '
"environment ID instead"
).format(environment_name)
raise AmbiguousNameError(msg)
def _resolve_environment(project_id, environment):
"""Resolve environment to environment IDs."""
try:
environment_id = uuid.UUID(environment)
except ValueError:
environment_id = _environment_by_name(project_id, environment).id
return environment_id
@contextlib.contextmanager
def _save_key_to_file(key):
tmpdir = tempfile.mkdtemp()
filename = os.path.join(tmpdir, "key.pem")
with open(filename, "w") as keyfile:
keyfile.write(key)
os.chmod(filename, stat.S_IRUSR & ~stat.S_IRGRP & ~stat.S_IROTH)
yield filename
shutil.rmtree(tmpdir)
PERMISSION_DENIED_MESSAGE = """
Permission was denied when attempting to connect to your Faculty server. A
bug in earlier versions of OpenSSH (including the version distributed with
macOS 10.10) may be the cause - please try updating your operating system or
SSH version and try again.
""".replace(
"\n", " "
).strip()
def _run_ssh_cmd(argv):
"""Run a command and print a message when a string is matched."""
process = subprocess.Popen(argv, stderr=subprocess.PIPE)
line = process.stderr.readline()
while line:
click.echo(line, nl=False, err=True)
if (
b"Permission denied" in line
and b"rsync: send_files failed to open" not in line
):
click.echo(PERMISSION_DENIED_MESSAGE, err=True)
line = process.stderr.readline()
return process.wait()
def _format_datetime(timestamp):
if timestamp is None:
return "-"
else:
return timestamp.strftime("%Y-%m-%d %H:%M")
class FacultyCLIGroup(click.Group):
def __call__(self, *args, **kwargs):
try:
super(FacultyCLIGroup, self).__call__(*args, **kwargs)
except AmbiguousNameError as err:
_print_and_exit(err, 64)
except NameNotFoundError as err:
_print_and_exit(err, 64)
@click.group(cls=FacultyCLIGroup)
@click.version_option(
version=faculty_cli.version.__version__, prog_name="faculty-cli"
)
def cli():
"""Command line interface to Faculty."""
try:
faculty_cli.update.check_for_new_release()
except Exception: # pylint: disable=broad-except
pass
@cli.command()
def version():
"""Print the faculty_cli version number."""
click.echo(faculty_cli.version.__version__)
@cli.command()
def login():
"""Write Faculty credentials to file."""
credentials_file = faculty.config.resolve_credentials_path()
if os.path.exists(credentials_file):
if not click.confirm("Overwrite existing credentials file?"):
return
_populate_creds_file()
@cli.group()
def project():
"""Manipulate Faculty projects."""
pass
@project.command(name="list")
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Print extra information about projects.",
)
def list_projects(verbose):
"""List accessible Faculty projects."""
_check_credentials()
projects = _list_projects()
if verbose:
if not projects:
click.echo("No projects.")
else:
click.echo(
tabulate(
[(p.name, p.id) for p in projects],
("Project Name", "ID"),
tablefmt="plain",
)
)
else:
for project in projects:
click.echo(project.name)
@project.command(name="new")
@click.argument("name")
def new_project(name):
"""Create new project."""
client = faculty.client("project")
user_id = _get_authenticated_user_id()
try:
returned_project = client.create(user_id, name)
except faculty.clients.base.BadRequest as err:
_print_and_exit(err.error, 64)
click.echo(
"Created project {} with ID {}".format(
returned_project.name, returned_project.id
)
)
@cli.group()
def server():
"""Manipulate Faculty servers."""
pass
@server.command(name="list")
@click.argument("project", required=False, metavar="PROJECT")
@click.option(
"-a",
"--all",
is_flag=True,
help="Show all servers, not just running ones.",
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Print extra information about servers.",
)
def list_servers(project, all, verbose):
"""List your Faculty servers.
If you do not specify a project, all servers will be listed."""
status_filter = None if all else ServerStatus.RUNNING
if not project:
projects = {project.id: project.name for project in _list_projects()}
user_id = _get_authenticated_user_id()
servers = [
(projects[server.project_id], server)
for server in _list_user_servers(user_id, status=status_filter)
]
else:
project_id = _resolve_project(project)
servers = [
("", server)
for server in _get_servers(project_id, status=status_filter)
]
status_filter = None if all else ServerStatus.RUNNING
headers = (
"Project Name",
"Server Name",
"Type",
"Machine Type",
"CPUs",
"RAM",
"Status",
"Server ID",
"Started",
)
found_servers = []
for project_name, server in servers:
machine_type, cpus, memory_gb = _server_spec(server)
found_servers.append(
(
project_name,
server.name,
server.type,
machine_type,
cpus,
memory_gb,
server.status.value,
server.id,
server.created_at.strftime("%Y-%m-%d %H:%M"),
)
)
if not found_servers and verbose:
click.echo("No servers.")
elif project and verbose:
servers = [server[1:] for server in found_servers]
click.echo(tabulate(servers, headers[1:], tablefmt="plain"))
elif project or not verbose:
for server in found_servers:
click.echo(server[1])
elif not project and verbose:
click.echo(tabulate(found_servers, headers, tablefmt="plain"))
@server.command(name="open")
@click.argument("project")
@click.option("--server", is_flag=False, help="Name or ID of server to use.")
def open_(project, server):
"""Open a Faculty server in your browser."""
project_id, server_id = _resolve_server(project, server)
client = faculty.client("server")
server = client.get(project_id, server_id)
https_services = [
service for service in server.services if service.name == "https"
]
if not https_services:
_print_and_exit(
"Server {} is not running an application that "
"can be opened in a web browser".format(server.name),
1,
)
[https_service] = https_services
url = "{}://{}".format(https_service.scheme, https_service.host)
click.echo("Opening {}".format(url))
click.launch(url)
@server.command()
@click.argument("project")
@click.option(
"--cores",
type=float,
default=1,
show_default=True,
help="Number of CPU cores",
)
@click.option(
"--memory",
type=float,
default=4,
show_default=True,
help="Server memory in GB",
)
@click.option(
"--type",
"type_",
is_flag=False,
default="jupyter",
show_default=True,
help="Server type",
)
@click.option(
"--machine-type",
"machine_type",
default=None,
show_default=False,
help="Machine type for a dedicated instance, e.g. m5.xlarge. "
"If set, the memory and CPU arguments are ignored.",
)
@click.option(
"--version",
"version",
is_flag=False,
help="Server image version [advanced]",
)
@click.option("--name", is_flag=False, help="Name to assign to the server")
@click.option(
"--environment",
"environments",
multiple=True,
help="Environments to apply to the server",
)
@click.option(
"--wait",
is_flag=True,
help="Wait until the server is running before exiting.",
)
def new(
project,
cores,
memory,
type_,
machine_type,
version,
name,
environments,
wait,
):
"""Create a new Faculty server."""
# pylint: disable=too-many-arguments
project_id = _resolve_project(project)
environment_ids = [
_resolve_environment(project_id, env) for env in environments
]
if machine_type is None or machine_type == "custom":
resources = SharedServerResources(
milli_cpus=int(cores * 1000), memory_mb=int(memory * 1000)
)
elif machine_type is not None and machine_type != "custom":
resources = DedicatedServerResources(node_type=machine_type)
client = faculty.client("server")
try:
server_id = client.create(
project_id, type_, resources, name, version, environment_ids
)
except faculty.clients.base.BadRequest as err:
_print_and_exit(err.error, 64)
server = client.get(project_id, server_id)
click.echo("Creating server {} in project {}".format(server.name, project))
if wait:
while True:
server_ids = {
server_.id
for server_ in _get_servers(
project_id, status=ServerStatus.RUNNING
)
}
if server.id in server_ids:
break
time.sleep(1)
@server.command()
@click.argument("project")
@click.argument("server")
def terminate(project, server):
"""Terminate a Faculty server."""
_, server_id = _resolve_server(project, server, ensure_running=False)
client = faculty.client("server")
client.delete(server_id)
@server.command(name="instance-types")
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Print extra information about instance types.",
)
def instance_types(verbose):
"""List the types of servers available on dedicated infrastructure."""
client = faculty.client("cluster")
types = client.list_single_tenanted_node_types(
interactive_instances_configured=True
)
types = sorted(types, key=operator.attrgetter("cost_usd_per_hour"))
if verbose:
if not types:
click.echo("No servers on dedicated infrastructure available.")
else:
headers = (
"Machine Type",
"CPUs",
"RAM",
"GPUs",
"GPU Name",
"Cost",
)
rows = [
(
type_.name,
"{:.3g}".format(type_.milli_cpus / 1000),
"{:.3g} GB".format(type_.memory_mb / 1000),
type_.num_gpus or "-",
type_.gpu_name or "-",
"$ {:.3f} / hour".format(type_.cost_usd_per_hour),
)
for type_ in types
]
click.echo(tabulate(rows, headers, tablefmt="plain"))
else:
for type_ in types:
click.echo(type_.name)
@server.command()
@click.argument("project")
@click.argument("server")
def ssh_details(project, server):
"""Echo the username, hostname and SSH port for a Faculty server.
After running this command, SSH into the server using:
$ ssh <username>@<hostname> -p <port>
For this command to work, you will first need to add your public SSH key
to `~/.ssh/authorized_keys` on the Faculty Platform.
"""
details = _get_ssh_details(project, server)
click.echo(
tabulate(
[(details.hostname, details.port, details.username)],
("Hostname", "Port", "Username"),
tablefmt="plain",
)
)
@cli.command(context_settings={"ignore_unknown_options": True})
@click.argument("project")
@click.argument("server")
@click.argument("ssh_opts", nargs=-1, type=click.UNPROCESSED)
def shell(project, server, ssh_opts):
"""Open a shell on a Faculty server.
Any additional arguments given are passed on to SSH. This allows you to set
up, for example, port forwarding:
$ faculty shell <project> <server> -L 9000:localhost:8888
"""
details = _get_ssh_details(project, server)
with _save_key_to_file(details.key) as filename:
cmd = (
["ssh"]
+ SSH_OPTIONS
+ [
"-p",
str(details.port),
"-i",
filename,
"{}@{}".format(details.username, details.hostname),
]
)
cmd += list(ssh_opts)
_run_ssh_cmd(cmd)
@cli.group()
def environment():
"""Manipulate Faculty server environments."""
_check_credentials()
@environment.command(name="list")
@click.argument("project")
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Print extra information about environments.",
)
def list_environments(project, verbose):
"""List your environments."""
client = faculty.client("environment")
project_id = _resolve_project(project)
environments = client.list(project_id)
if verbose:
if not environments:
click.echo("No environments.")
else:
click.echo(
tabulate(
[(e.name, e.id) for e in environments],
("Environment Name", "ID"),
tablefmt="plain",
)
)
else:
for environment in environments:
click.echo(environment.name)
@environment.command()
@click.argument("project")
@click.argument("server")
@click.argument("environment")
def apply(project, server, environment):
"""Apply an environment to the server."""
project_id, server_id = _resolve_server(project, server)
environment_id = _resolve_environment(project_id, environment)
client = faculty.client("server")
client.apply_environment(server_id, environment_id)
click.echo(
"Applying environment {} to server {} for project {}".format(
environment, server, project
)
)
def _format_command(command):
formatted_parts = []
for part in command:
if len(part.split()) > 1:
formatted_parts.append(repr(part))
else:
formatted_parts.append(part)
return " ".join(formatted_parts)
def _get_service(server, name):
for service in server.services:
if service.name == name:
return service
raise RuntimeError("cube has no service called {}".format(name))
def _get_hound_url(server):
service = _get_service(server, "hound")
return "{}://{}:{}".format(service.scheme, service.host, service.port)
@environment.command()
@click.argument("project")
@click.argument("server")
def status(project, server):
"""Get the execution status for an environment."""
project_id, server_id = _resolve_server(project, server)
server_client = faculty.client("server")
server = server_client.get(project_id, server_id)
hound_url = _get_hound_url(server)
session = get_session()
client = ServerAgentClient(hound_url, session)
execution = client.latest_environment_execution()
if execution is None:
msg = "No environment has yet been applied to this server."
_print_and_exit(msg, 64)
click.echo("Latest environment execution:")
click.echo(" Status: {}".format(execution.status.value))
for i, environment in enumerate(execution.environments):
click.echo("")
click.echo("Environment {}".format(i))
for j, step in enumerate(environment.steps):
click.echo("")
click.echo("Step {}:".format(j))
click.echo(" Status: {}".format(step.status.value))
click.echo(" Command: {}".format(_format_command(step.command)))
@environment.command()
@click.argument("project")
@click.argument("server")
@click.option(
"--step",
"-s",
"step_number",
type=int,
help="Display only the logs for this step",
)
def logs(project, server, step_number):
"""Stream the logs for a server environment application."""
project_id, server_id = _resolve_server(project, server)
server_client = faculty.client("server")
server = server_client.get(project_id, server_id)
hound_url = _get_hound_url(server)
session = get_session()
client = ServerAgentClient(hound_url, session)
execution = client.latest_environment_execution()
if execution is None:
msg = "No environment has yet been applied to this server."
_print_and_exit(msg, 64)
steps = [
step
for environment_execution in execution.environments
for step in environment_execution.steps
]
if step_number is not None:
try:
steps = [steps[step_number]]
except IndexError:
_print_and_exit("step {} out of range".format(step_number), 64)
for step in steps:
for line in client.stream_environment_execution_step_logs(
execution.id, step.id
):
click.echo(line.content)
@cli.group()
def job():
"""Manipulate Faculty jobs."""
pass
@job.command(name="list")
@click.argument("project")
@click.option(
"-v", "--verbose", is_flag=True, help="Print extra information about jobs."
)
def list_jobs(project, verbose):
"""List the jobs in a project."""
project_id = _resolve_project(project)
client = faculty.client("job")
jobs = client.list(project_id)
if verbose:
if not jobs:
click.echo("No jobs.")
else:
rows = [
(job.metadata.name, job.id, job.metadata.description)
for job in jobs
]
click.echo(
tabulate(rows, ("Name", "ID", "Description"), tablefmt="plain")
)
else:
for job in jobs:
click.echo(job.metadata.name)
@job.command(name="list-runs")
@click.argument("project")
@click.argument("job")
@click.option(