forked from vast-ai/vast-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vast.py
executable file
·906 lines (793 loc) · 34.1 KB
/
vast.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
#!/usr/bin/env python3
from __future__ import unicode_literals, print_function
import re
import json
import sys
import argparse
import os
import requests
import getpass
try:
from urllib import quote_plus # Python 2.X
except ImportError:
from urllib.parse import quote_plus # Python 3+
try:
JSONDecodeError = json.JSONDecodeError
except AttributeError:
JSONDecodeError = ValueError
try:
input = raw_input
except NameError:
pass
server_url_default = "https://vast.ai/api/v0"
api_key_file_base = "~/.vast_api_key"
api_key_file = os.path.expanduser(api_key_file_base)
api_key_guard = object()
class argument(object):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
class hidden_aliases(object):
# just a bit of a hack
def __init__(self, l):
self.l = l
def __iter__(self):
return iter(self.l)
def __bool__(self):
return False
def __nonzero__(self):
return False
def append(self,x):
self.l.append(x)
class apwrap(object):
def __init__(self, *args, **kwargs):
kwargs["formatter_class"] = argparse.RawDescriptionHelpFormatter
self.parser = argparse.ArgumentParser(*args, **kwargs)
self.parser.set_defaults(func=self.fail_with_help)
self.subparsers_ = None
self.subparser_objs = []
self.added_help_cmd = False
self.post_setup = []
self.verbs = set()
self.objs = set()
def fail_with_help(self, *a, **kw):
self.parser.print_help(sys.stderr)
raise SystemExit
def add_argument(self, *a, **kw):
if not kw.get("parent_only"):
for x in self.subparser_objs:
try:
x.add_argument(*a, **kw)
except argparse.ArgumentError:
# duplicate - or maybe other things, hopefully not
pass
return self.parser.add_argument(*a, **kw)
def subparsers(self, *a, **kw):
if self.subparsers_ is None:
kw["metavar"] = "command"
kw["help"] = "command to run. one of:"
self.subparsers_ = self.parser.add_subparsers(*a, **kw)
return self.subparsers_
def get_name(self, verb, obj):
if obj:
self.verbs.add(verb)
self.objs.add(obj)
name = verb + ' ' + obj
else:
self.objs.add(verb)
name = verb
return name
def command(self, *arguments, aliases=(), help=None, **kwargs):
help_ = help
if not self.added_help_cmd:
self.added_help_cmd = True
@self.command(argument("subcommand", default=None, nargs="?"), help="print this help message")
def help(*a, **kw):
self.fail_with_help()
def inner(func):
dashed_name = func.__name__.replace("_", "-")
verb, _, obj = dashed_name.partition("--")
name = self.get_name(verb, obj)
aliases_transformed = [] if aliases else hidden_aliases([])
for x in aliases:
verb, _, obj = x.partition(" ")
aliases_transformed.append(self.get_name(verb, obj))
kwargs["formatter_class"] =argparse.RawDescriptionHelpFormatter
sp = self.subparsers().add_parser(name, aliases=aliases_transformed, help=help_, **kwargs)
self.subparser_objs.append(sp)
for arg in arguments:
sp.add_argument(*arg.args, **arg.kwargs)
sp.set_defaults(func=func)
return func
if len(arguments) == 1 and type(arguments[0]) != argument:
func = arguments[0]
arguments = []
return inner(func)
return inner
def parse_args(self, argv=None, *a, **kw):
if argv is None:
argv = sys.argv[1:]
argv_ = []
for x in argv:
if argv_ and argv_[-1] in self.verbs:
argv_[-1] += " " + x
else:
argv_.append(x)
args = self.parser.parse_args(argv_, *a, **kw)
for func in self.post_setup:
func(args)
return args
parser = apwrap()
def apiurl(args, subpath, query_args=None):
if query_args is None:
query_args = {}
if args.api_key is not None:
query_args["api_key"] = args.api_key
if query_args:
#a_list = [<expression> for <l-expression> in <expression>]
'''
vector result;
for (l_expression: expression) {
result.push_back(expression);
}
'''
#an_iterator = (<expression> for <l-expression> in <expression>)
return args.url + subpath + "?" + "&".join(
"{x}={y}".format(x=x, y=quote_plus(y if isinstance(y, str) else json.dumps(y))) for x, y in query_args.items())
else:
return args.url + subpath
def deindent(message):
"""
deindent a quoted string
"""
message = re.sub(r" *$", "", message, flags=re.MULTILINE)
indents = [len(x) for x in re.findall("^ *(?=[^ ])", message, re.MULTILINE) if len(x)]
a = min(indents)
message = re.sub(r"^ {," + str(a) + "}", "", message, flags=re.MULTILINE)
return message.strip()
displayable_fields = (
("id", "ID", "{}", None, True),
("cuda_max_good", "CUDA", "{:0.1f}", None, True),
("num_gpus", "Num", "{}x", None, False),
("gpu_name", "Model", "{}", None, True),
("pcie_bw", "PCIE_BW", "{:0.1f}", None, True),
("cpu_cores_effective", "vCPUs", "{:0.1f}", None, True),
("cpu_ram", "RAM", "{:0.1f}", lambda x: x/1000, False),
("disk_space", "Storage", "{:.0f}", None, True),
("dph_total", "$/hr", "{:0.4f}", None, True),
("dlperf", "DLPerf", "{:0.1f}", None, True),
("dlperf_per_dphtotal", "DLP/$", "{:0.1f}", None, True),
("inet_up", "Net_up", "{:0.1f}", None, True),
("inet_down", "Net_down", "{:0.1f}", None, True),
("reliability2", "R", "{:0.1f}", lambda x: x * 100, True),
("duration", "Max_Days", "{:0.1f}", lambda x: x/(24.0*60.0*60.0), True),
("machine_id", "machine_id", "{}", None, True),
)
instance_fields = (
("id", "ID", "{}", None, True),
("machine_id", "Machine", "{}", None, True),
("actual_status", "Status", "{}", None, True),
("num_gpus", "Num", "{}x", None, False),
("gpu_name", "Model", "{}", None, True),
("gpu_util", "Util. %", "{:0.1f}", None, True),
("cpu_cores_effective", "vCPUs", "{:0.1f}", None, True),
("cpu_ram", "RAM", "{:0.1f}", lambda x: x/1000, False),
("disk_space", "Storage", "{:.0f}", None, True),
("ssh_host", "SSH Addr", "{}", None, True),
("ssh_port", "SSH Port", "{}", None, True),
("dph_total", "$/hr", "{:0.4f}", None, True),
("image_uuid", "Image", "{}", None, True),
#("dlperf", "DLPerf", "{:0.1f}", None, True),
#("dlperf_per_dphtotal", "DLP/$", "{:0.1f}", None, True),
("inet_up", "Net up", "{:0.1f}", None, True),
("inet_down", "Net down", "{:0.1f}", None, True),
("reliability2", "R", "{:0.1f}", lambda x: x * 100, True),
("label", "Label", "{}", None, True),
#("duration", "Max Days", "{:0.1f}", lambda x: x/(24.0*60.0*60.0), True),
)
def parse_query(query_str, res=None):
if res is None: res = {}
if type(query_str) == list:
query_str = " ".join(query_str)
query_str = query_str.strip()
opts = re.findall("([a-zA-Z0-9_]+)( *[=><!]+| +(?:[lg]te?|nin|neq|eq|not ?eq|not ?in|in) )?( *)(\[[^\]]+\]|[^ ]+)?( *)", query_str)
#res = {}
op_names = {
">=": "gte",
">": "gt",
"gt": "gt",
"gte": "gte",
"<=": "lte",
"<": "lt",
"lt": "lt",
"lte": "lte",
"!=": "neq",
"==": "eq",
"=": "eq",
"eq": "eq",
"neq": "neq",
"noteq": "neq",
"not eq": "neq",
"notin": "notin",
"not in": "notin",
"nin": "notin",
"in": "in",
};
field_alias = {
"cuda_vers": "cuda_max_good",
"display_active": "gpu_display_active",
"reliability": "reliability2",
"dlperf_usd": "dlperf_per_dphtotal",
"dph": "dph_total",
"flops_usd": "flops_per_dphtotal",
};
field_multiplier = {
"cpu_ram" : 1000,
"duration" : 1.0 / (24.0*60.0*60.0),
}
fields = {
"compute_cap",
"cpu_cores",
"cpu_cores_effective",
"cpu_ram",
"cuda_max_good",
"driver_version",
"disk_bw",
"disk_space",
"dlperf",
"dlperf_per_dphtotal"
"dph_total",
"duration",
"external",
"flops_per_dphtotal",
"gpu_display_active",
#"gpu_ram_free_min",
"gpu_mem_bw",
"gpu_name",
"gpu_ram",
"has_avx",
"host_id",
"id",
"inet_down",
"inet_down_cost",
"inet_up",
"inet_up_cost",
"min_bid",
"mobo_name",
"num_gpus",
"pci_gen",
"pcie_bw",
"reliability2",
"rentable",
"rented",
"storage_cost",
"total_flops",
"verified"
};
joined = "".join("".join(x) for x in opts)
if joined != query_str:
raise ValueError("Unconsumed text. Did you forget to quote your query? " + repr(joined) + " != " + repr(query_str))
for field, op, _, value, _ in opts:
value = value.strip(",[]")
v = res.setdefault(field, {})
op = op.strip()
op_name = op_names.get(op)
if field in field_alias:
field = field_alias[field];
if not field in fields:
print("Warning: Unrecognized field: {}, see list of recognized fields.".format(field), file=sys.stderr);
if not op_name:
raise ValueError("Unknown operator. Did you forget to quote your query? " + repr(op).strip("u"))
if op_name in ["in", "notin"]:
value = [x.strip() for x in value.split(",") if x.strip()]
if not value:
raise ValueError("Value cannot be blank. Did you forget to quote your query? " + repr((field, op, value)))
if not field:
raise ValueError("Field cannot be blank. Did you forget to quote your query? " + repr((field, op, value)))
if value in ["?", "*", "any"]:
if op_name != "eq":
raise ValueError("Wildcard only makes sense with equals.")
if field in v:
del v[field]
if field in res:
del res[field]
continue
if field in field_multiplier:
value = str(float(value) * field_multiplier[field]);
v[op_name] = value
res[field] = v;
return res
def display_table(rows, fields):
header = [name for _, name, _, _, _ in fields]
out_rows = [header]
lengths = [len(x) for x in header]
for instance in rows:
row = []
out_rows.append(row)
for key, name, fmt, conv, _ in fields:
conv = conv or (lambda x: x)
val = instance.get(key, None)
if val is None:
s = "-"
else:
val = conv(val)
s = fmt.format(val)
s = s.replace(' ', '_')
idx = len(row)
lengths[idx] = max(len(s), lengths[idx])
row.append(s)
for row in out_rows:
out = []
for l, s, f in zip(lengths, row, fields):
_, _, _, _, ljust = f
if ljust:
s = s.ljust(l)
else:
s = s.rjust(l)
out.append(s)
print(" ".join(out))
@parser.command(
argument("-t", "--type", default="on-demand", help="whether to show `interruptible` or `on-demand` offers. default: on-demand"),
argument("-i", "--interruptible", dest="type", const="interruptible", action="store_const", help="Alias for --type=interruptible"),
argument("-d", "--on-demand", dest="type", const="on-demand", action="store_const", help="Alias for --type=on-demand"),
argument("-n", "--no-default", action="store_true", help="Disable default query"),
argument("--disable-bundling", action="store_true", help="Show identical offers. This request is more heavily rate limited."),
argument("--storage", type=float, default=5.0, help="amount of storage to use for pricing, in GiB. default=5.0GiB"),
argument("-o", "--order", type=str, help="comma-separated list of fields to sort on. postfix field with - to sort desc. ex: -o 'num_gpus,total_flops-'. default='score-'", default='score-'),
argument("query", help="Query to search for. default: 'external=false rentable=true verified=true', pass -n to ignore default", nargs="*", default=None),
usage="vast search offers [--help] [--api-key API_KEY] [--raw] <query>",
epilog=deindent("""
Query syntax:
query = comparison comparison...
comparison = field op value
field = <name of a field>
op = one of: <, <=, ==, !=, >=, >, in, notin
value = <bool, int, float, etc> | 'any'
note: to pass '>' and '<' on the command line, make sure to use quotes
Examples:
./vast search offers 'compute_cap > 610 total_flops < 5'
./vast search offers 'reliability > 0.99 num_gpus>=4' -o 'num_gpus-'
./vast search offers 'rentable = any'
Available fields:
Name Type Description
compute_cap: int cuda compute capability*100 (ie: 650 for 6.5, 700 for 7.0)
cpu_cores: int # virtual cpus
cpu_cores_effective: float # virtual cpus you get
cpu_ram: float system RAM in gigabytes
cuda_vers: float cuda version
disk_bw: float disk read bandwidth, in MB/s
disk_space: float disk storage space, in GB
dlperf: float DL-perf score (see FAQ for explanation)
dlperf_usd: float DL-perf/$
dph: float $/hour rental cost
duration: float max rental duration in days
external: bool show external offers
flops_usd: float TFLOPs/$
gpu_mem_bw: float GPU memory bandwidth in GB/s
gpu_ram: float GPU RAM in GB
gpu_frac: float Ratio of GPUs in the offer to gpus in the system
has_avx: bool CPU supports AVX instruction set.
id: int instance unique ID
inet_down: float internet download speed in Mb/s
inet_down_cost: float internet download bandwidth cost in $/GB
inet_up: float internet upload speed in Mb/s
inet_up_cost: float internet upload bandwidth cost in $/GB
min_bid: float current minimum bid price in $/hr for interruptible
num_gpus: int # of GPUs
pci_gen: float PCIE generation
pcie_bw: float PCIE bandwidth (CPU to GPU)
reliability: float machine reliability score (see FAQ for explanation)
rentable: bool is the instance currently rentable
rented: bool is the instance currently rented
storage_cost: float storage cost in $/GB/month
total_flops: float total TFLOPs from all GPUs
verified: bool is the machine verified
"""),
aliases=hidden_aliases(["search instances"]),
)
def search__offers(args):
field_alias = {
"cuda_vers" : "cuda_max_good",
"reliability" : "reliability2",
"dlperf_usd" : "dlperf_per_dphtotal",
"dph" : "dph_total",
"flops_usd" : "flops_per_dphtotal",
};
try:
if args.no_default:
query = {}
else:
query = { "verified":{"eq":True}, "external":{"eq":False}, "rentable":{"eq":True} }
if args.query is not None:
query = parse_query(args.query, query)
#print("query length: {}".format(len(query)));
#for k,q in query.items():
#print("{} {}".format(k, q));
order = []
for name in args.order.split(","):
name = name.strip()
if not name: continue
direction = "asc"
if name.strip("-") != name:
direction = "desc"
field = name.strip("-");
if field in field_alias:
field = field_alias[field];
order.append([field, direction])
query["order"] = order
query["type"] = args.type
if args.disable_bundling:
query["disable_bundling"] = True
except ValueError as e:
print("Error: ", e)
return 1
url = apiurl(args, "/bundles", {"q":query});
#url = apiurl(args, "/bundles") + "?q=" + quote_plus(json.dumps(query));
r = requests.get(url);
r.raise_for_status()
rows = r.json()["offers"]
if args.raw:
print(json.dumps(rows, indent=1, sort_keys=True))
else:
#print(url);
#print("{N} instances types: ".format(N=len(rows)) );
display_table(rows, displayable_fields)
@parser.command(
usage="vast show instances [--api-key API_KEY] [--raw]",
)
def show__instances(args):
req_url = apiurl(args, "/instances", {"owner": "me"});
r = requests.get(req_url);
r.raise_for_status()
rows = r.json()["instances"]
if args.raw:
print(json.dumps(rows, indent=1, sort_keys=True))
else:
display_table(rows, instance_fields)
#print("{N} instances: ".format(N=len(rows)) );
#print("%-10s%-10s%-12s%-5s%-14s%-7s%-7s%-8s%-10s%-14s%-10s%-8s%-12s" % ("Instance", "Machine", "Status", "#", "GPUs", "util%", "vCPUs", "RAM", "Storage", "SSH Addr", "SSH Port", "$/hr", "Image"));
#for instance in rows:
# gpu_util = 0;
# if (instance["gpu_util"] is not None):
# gpu_util = int(instance["gpu_util"]);
# cost = str(float(instance["dph_total"]));
# print("%-10s%-10s%-12s%-2s x %-16s%-6i%-5i%3iGB %5iGB %-16s%-9s%-8s%-12s" % (instance["id"], instance["machine_id"], instance["actual_status"], 1*instance["num_gpus"], instance["gpu_name"], gpu_util, int(instance["cpu_cores"]), int(instance["cpu_ram"])/1000, int(instance["disk_space"]), instance["ssh_host"], instance["ssh_port"], cost[0:5], instance["image_uuid"]));
# #print("{id}: {json}".format(id=instance["id"], json=json.dumps(instance, indent=4, sort_keys=True)))
@parser.command(
argument("-q", "--quiet", action="store_true", help="only display numeric ids"),
usage = "vast show machines [OPTIONS]",
)
def show__machines(args):
req_url = apiurl(args, "/machines", {"owner": "me"});
r = requests.get(req_url);
r.raise_for_status()
rows = r.json()["machines"]
if args.raw:
print(json.dumps(rows, indent=1, sort_keys=True))
else:
for machine in rows:
if args.quiet:
print("{id}".format(id=machine["id"]))
else:
print("{N} machines: ".format(N=len(rows)) );
print("{id}: {json}".format(id=machine["id"], json=json.dumps(machine, indent=4, sort_keys=True)))
@parser.command(
argument("id", help="id of machine to list", type=int),
argument("-g", "--price_gpu", help="per gpu rental price in $/hour (price for active instances)", type=float),
argument("-s", "--price_disk", help="storage price in $/GB/month (price for inactive instances), default: $0.15/GB/month", type=float),
argument("-u", "--price_inetu", help="price for internet upload bandwidth in $/GB", type=float),
argument("-d", "--price_inetd", help="price for internet download bandwidth in $/GB", type=float),
argument("-m", "--min_chunk", help="minimum amount of gpus", type=int),
argument("-e", "--end_date", help="unix timestamp of the available until date (optional)", type=int),
usage = "vast list machine id [--price_gpu PRICE_GPU] [--price_inetu PRICE_INETU] [--price_inetd PRICE_INETD] [--api-key API_KEY]",
)
def list__machine(args):
#req_url = args.url + "/machines/create_asks/?user_id=" + str(args.user);
req_url = apiurl(args, "/machines/create_asks/");
#print("PUT " + req_url);
r = requests.put(req_url, json = {'machine':args.id, 'price_gpu':args.price_gpu, 'price_disk':args.price_disk, 'price_inetu':args.price_inetu, 'price_inetd':args.price_inetd, 'min_chunk':args.min_chunk, 'end_date':args.end_date } );
if (r.status_code == 200) :
#print(r.text);
rj = r.json();
if (rj["success"]) :
price_gpu_ = str(args.price_gpu) if args.price_gpu is not None else "def";
price_inetu_ = str(args.price_inetu);
price_inetd_ = str(args.price_inetd);
min_chunk_ = str(args.min_chunk);
end_date_ = str(args.end_date);
print("offers created for machine {args.id}, @ ${price_gpu_}/gpu/day, ${price_inetu_}/GB up, ${price_inetd_}/GB down, {min_chunk_}/min gpus, till {end_date_}".format(**locals()));
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
@parser.command(
argument("id", help="id of machine to list", type=int),
usage = "vast unlist machine <id>",
)
def unlist__machine(args):
req_url = apiurl(args, "/machines/{machine_id}/asks/".format(machine_id = args.id));
#req_url = args.url + "/machines/{machine_id}/asks/".format(machine_id = args.id);
#print(req_url);
r = requests.delete(req_url);
if (r.status_code == 200) :
#print(r.text);
rj = r.json();
if (rj["success"]) :
print("all offers for machine {machine_id} removed, machine delisted.".format(machine_id = args.id));
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
@parser.command(
argument("id", help="id of machine to remove default instance from", type=int),
)
def remove__defjob(args):
req_url = apiurl(args, "/machines/{machine_id}/defjob/".format(machine_id = args.id));
#print(req_url);
r = requests.delete(req_url);
if (r.status_code == 200) :
#print(r.text);
rj = r.json();
if (rj["success"]) :
print("default instance for machine {machine_id} removed.".format(machine_id = args.id));
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
def set_ask(args):
print("set asks!\n");
@parser.command(
argument("id", help="id of instance to start/restart", type=int),
usage = "vast start instance <id> [--raw]",
)
def start__instance(args):
url = apiurl(args, "/instances/{id}/".format(id=args.id))
r = requests.put(url, json={
"state": "running"
})
r.raise_for_status()
if (r.status_code == 200) :
rj = r.json();
if (rj["success"]) :
print("starting instance {args.id}.".format(**(locals())) );
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
@parser.command(
argument("id", help="id of instance to stop", type=int),
usage = "vast stop instance [--raw] <id>",
)
def stop__instance(args):
url = apiurl(args, "/instances/{id}/".format(id=args.id))
r = requests.put(url, json={
"state": "stopped"
})
r.raise_for_status()
if (r.status_code == 200) :
rj = r.json();
if (rj["success"]) :
print("stopping instance {args.id}.".format(**(locals())) );
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
@parser.command(
argument("id", help="id of instance to label", type=int),
argument("label", help="label to set", type=str),
usage = "vast label instance <id> <label>",
)
def label__instance(args):
url = apiurl(args, "/instances/{id}/".format(id=args.id))
r = requests.put(url, json={
"label": args.label
})
r.raise_for_status()
rj = r.json();
if rj["success"]:
print("label for {args.id} set to {args.label}.".format(**(locals())) );
else :
print(rj["msg"]);
@parser.command(
argument("id", help="id of instance to delete", type=int),
usage="vast destroy instance id [-h] [--api-key API_KEY] [--raw]"
)
def destroy__instance(args):
url = apiurl(args, "/instances/{id}/".format(id=args.id))
r = requests.delete(url, json={})
r.raise_for_status()
if (r.status_code == 200) :
rj = r.json();
if (rj["success"]) :
print("destroying instance {args.id}.".format(**(locals())) );
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
@parser.command(
argument("id", help="id of machine to launch default instance on", type=int),
argument("--price_gpu", help="per gpu rental price in $/hour", type=float),
argument("--price_inetu", help="price for internet upload bandwidth in $/GB", type=float),
argument("--price_inetd", help="price for internet download bandwidth in $/GB", type=float),
argument("--image", help="docker container image to launch", type=str),
argument("--args", nargs=argparse.REMAINDER, help="list of arguments passed to container launch"),
usage="vast set defjob id [--api-key API_KEY] [--price_gpu PRICE_GPU] [--price_inetu PRICE_INETU] [--price_inetd PRICE_INETD] [--image IMAGE] [--args ...]"
)
def set__defjob(args):
#req_url = args.url + "/machines/create_asks/?user_id=" + str(args.user);
req_url = apiurl(args, "/machines/create_bids/");
#print("PUT " + req_url);
r = requests.put(req_url, json =
{'machine':args.id, 'price_gpu':args.price_gpu, 'price_inetu':args.price_inetu, 'price_inetd':args.price_inetd,
'image':args.image, 'args':args.args } );
if (r.status_code == 200) :
#print(r.text);
rj = r.json();
if (rj["success"]) :
print("bids created for machine {args.id}, @ ${args.price_gpu}/gpu/day, ${args.price_inetu}/GB up, ${args.price_inetd}/GB down".format(**locals()));
else :
print(rj["msg"]);
else :
print(r.text);
print("failed with error {r.status_code}".format(**locals()));
#def delete_bid(args):
# print("delete bids!\n");
#
#def set_bid(args):
# print("set bids!\n");
#
#
#def accept_bid(args):
# print("accept bid!\n");
@parser.command(
argument("id", help="id of instance type to launch", type=int),
argument("--price", help="per machine bid price in $/hour", type=float),
argument("--disk", help="size of local disk partition in GB", type=float, default=10),
argument("--image", help="docker container image to launch", type=str),
argument("--label", help="label to set on the instance", type=str),
argument("--onstart", help="filename to use as onstart script", type=str),
argument("--onstart-cmd", help="contents of onstart script as single argument", type=str),
argument("--jupyter", help="Launch as a jupyter instance instead of an ssh instance.", action="store_true"),
argument("--jupyter-dir", help="For runtype 'jupyter', directory in instance to use to launch jupyter. Defaults to image's working directory.", type=str),
argument("--jupyter-lab", help="For runtype 'jupyter', directory in instance to use to launch jupyter. Defaults to image's working directory.", action="store_true"),
argument("--lang-utf8", help="Workaround for images with locale problems: install and generate locales before instance launch, and set locale to C.UTF-8.", action="store_true"),
argument("--python-utf8", help="Workaround for images with locale problems: set python's locale to C.UTF-8.", action="store_true"),
argument("--extra", help=argparse.SUPPRESS),
argument("--args", nargs=argparse.REMAINDER, help="DEPRECATED: list of arguments passed to container launch. Onstart is recommended for this purpose."),
argument("--create-from", help="Existing instance id to use as basis for new instance. Instance configuration should usually be identical, as only the difference from the base image is copied.", type=str),
argument("--force", help="Skip sanity checks when creating from an existing instance", action="store_true"),
usage = "vast create instance id [OPTIONS] [--args ...]",
)
def create__instance(args):
if args.onstart:
with open(args.onstart, "r") as reader:
args.onstart_cmd = reader.read()
runtype = 'ssh'
if args.args:
runtype = 'args'
if args.jupyter_dir or args.jupyter_lab:
args.jupyter = True
if args.jupyter and runtype == 'args':
print("Error: Can't use --jupyter and --args together. Try --onstart or --onstart-cmd instead of --args.", file=sys.stderr)
return 1
if args.jupyter:
runtype = 'jupyter'
url = apiurl(args, "/asks/{id}/".format(id=args.id))
r = requests.put(url, json={
"client_id": "me",
"image": args.image,
"args": args.args,
"price": args.price,
"disk": args.disk,
"label": args.label,
"extra": args.extra,
"onstart": args.onstart_cmd,
"runtype": runtype,
"python_utf8": args.python_utf8,
"lang_utf8": args.lang_utf8,
"use_jupyter_lab": args.jupyter_lab,
"jupyter_dir": args.jupyter_dir,
"create_from": args.create_from,
"force": args.force
})
r.raise_for_status()
if args.raw:
print(json.dumps(r.json(), indent=1))
else:
print("Started. {}".format(r.json()))
@parser.command(
argument("id", help="id of instance type to launch", type=int),
argument("--price", help="per machine bid price in $/hour", type=float),
usage = "vast change bid id [--price PRICE]",
epilog = deindent("""
Change the current bid price of instance id to PRICE.
If PRICE is not specified, then a winning bid price is used as the default.
"""),
)
def change__bid(args):
url = apiurl(args, "/instances/bid_price/{id}/".format(id=args.id))
r = requests.put(url, json={
"client_id": "me",
"price" : args.price,
})
r.raise_for_status()
print("Per gpu bid price changed".format(r.json()))
@parser.command(
argument("id", help="id of machine to set min bid price for", type=int),
argument("--price", help="per gpu min bid price in $/hour", type=float),
usage = "vast set min_bid id [--price PRICE]",
epilog = deindent("""
Change the current min bid price of machine id to PRICE.
"""),
)
def set__min_bid(args):
url = apiurl(args, "/machines/{id}/minbid/".format(id=args.id))
r = requests.put(url, json={
"client_id": "me",
"price" : args.price,
})
r.raise_for_status()
print("Per gpu min bid price changed".format(r.json()))
@parser.command(
argument("new_api_key", help="Api key to set as currently logged in user"),
usage = "vast set api-key APIKEY",
)
def set__api_key(args):
with open(api_key_file, "w") as writer:
writer.write(args.new_api_key)
print("Your api key has been saved in {}".format(api_key_file_base))
#def _load_sshkey(arg):
# if arg is not None and os.path.exists(arg):
# with open(arg, "r") as reader:
# return reader.read()
# return arg
login_deprecated_message = """
login via the command line is no longer supported.
go to https://vast.ai/console/cli in a web browser to get your api key, then run:
vast set api-key YOUR_API_KEY_HERE
"""
@parser.command(
argument("ignored", nargs="*"),
usage = login_deprecated_message
)
def create__account(args):
print(login_deprecated_message)
@parser.command(
argument("ignored", nargs="*"),
usage = login_deprecated_message,
)
def login(args):
print(login_deprecated_message)
def main():
parser.add_argument("--url", help="server REST api url", default=server_url_default)
parser.add_argument("--raw", action="store_true", help="output machine-readable json");
parser.add_argument("--api-key", help="api key. defaults to using the one stored in {}".format(api_key_file_base), type=str, required=False, default=api_key_guard)
args = parser.parse_args()
if args.api_key is api_key_guard:
if os.path.exists(api_key_file):
with open(api_key_file, "r") as reader:
args.api_key = reader.read().strip()
else:
args.api_key = None
try:
sys.exit(args.func(args) or 0)
except requests.exceptions.HTTPError as e :
try:
errmsg = e.response.json().get("msg");
except JSONDecodeError:
if e.response.status_code == 401:
errmsg = "Please log in or sign up"
else:
errmsg = "(no detail message supplied)"
print("failed with error {e.response.status_code}: {errmsg}".format(**locals()));
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, BrokenPipeError):
pass