-
Notifications
You must be signed in to change notification settings - Fork 5
/
audius-cli
executable file
·1168 lines (992 loc) · 35.1 KB
/
audius-cli
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
#!/usr/bin/env python3
import json
import logging
import os
import pathlib
import random
import shutil
import subprocess
import sys
import time
import urllib.error
import urllib.request
import click
import crontab
import dotenv
import psutil
from web3 import Web3
from web3.providers import BaseProvider, HTTPProvider
GB = 1024**3
RECOMMENDED_CPU_COUNT = 8
RECOMMENDED_MEMORY = 16 * GB
RECOMMENDED_STORAGE = {
"creator-node": 2048 * GB,
"discovery-provider": 256 * GB,
"identity-service": 256 * GB,
}
SERVICE_PORTS = {
"discovery-provider": "5000",
"discovery-provider-notifications": "6000",
"creator-node": "4000",
"identity-service": "7000",
}
SERVICES = (
"creator-node",
"discovery-provider",
"identity-service",
)
# if type == discovery-provider and is not in this list, run core only
DISCPROV_WHITELIST = (
".audius.co",
".creatorseed.com",
".monophonic.digital",
".figment.io",
".tikilabs.com",
)
REGISTERED_PLUGINS = "REGISTERED_PLUGINS"
DB_URL = "audius_db_url"
# Used for updating chainspec
EXTRA_VANITY = "0x22466c6578692069732061207468696e6722202d204166726900000000000000"
EXTRA_SEAL = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
service_type = click.Choice(SERVICES)
network_type = click.Choice(["prod", "stage", "dev"])
container_type = click.Choice(["backend", "cache", "db"])
def run(cmd, **kwargs):
print(subprocess.list2cmdline(cmd))
return subprocess.run(cmd, **kwargs)
def get_network(ctx, service):
"""Returns the network name for the given service."""
return dotenv.dotenv_values(ctx.obj["manifests_path"] / service / ".env").get(
"NETWORK", "prod"
)
def get_override_path(ctx, service):
"""Returns path to override.env for the given service."""
return pathlib.Path(
dotenv.dotenv_values(ctx.obj["manifests_path"] / service / ".env").get(
"OVERRIDE_PATH", ctx.obj["manifests_path"] / service / "override.env"
)
)
def lock(ctx, group):
lockfile = pathlib.Path(f"~/.local/share/audius-cli/{group}.lock").expanduser()
lockfile.parent.mkdir(parents=True, exist_ok=True)
lockfile.touch(exist_ok=True)
pid, retries = lockfile.read_text(), 0
while pid.isdigit() and int(pid) != os.getpid() and psutil.pid_exists(int(pid)):
if retries == 600: # 10 mins
click.secho("Giving up on lock")
sys.exit(1)
if retries % 15 == 0:
click.secho(f"Waiting for lock (held by pid {pid})", color="gray")
time.sleep(1)
pid = lockfile.read_text()
retries += 1
lockfile.write_text(str(os.getpid()))
def set_automatic_env(ctx):
with crontab.CronTab(user=True) as cron:
auto_upgrade = (
"true" if any(cron.find_comment("audius-cli auto-upgrade")) else "false"
)
git_hash = (
run(
["git", "rev-parse", "HEAD"],
capture_output=True,
cwd=ctx.obj["manifests_path"],
)
.stdout.decode()
.rstrip()
)
# give application awareness of host level auto upgrade cron job
ctx.invoke(
set_config,
service="creator-node",
key="autoUpgradeEnabled",
value=auto_upgrade,
)
ctx.invoke(
set_config,
service="discovery-provider",
key="audius_auto_upgrade_enabled",
value=auto_upgrade,
)
ctx.invoke(
set_config,
service="discovery-provider",
key="AUDIUS_DOCKER_COMPOSE_GIT_SHA",
value=git_hash,
)
ctx.invoke(
set_config,
service="creator-node",
key="AUDIUS_DOCKER_COMPOSE_GIT_SHA",
value=git_hash,
)
def prune():
disk_usage_percent = psutil.disk_usage("/").percent
if disk_usage_percent > 90:
run(
["docker", "system", "prune", "--all", "--force"],
)
else:
run(
["docker", "system", "prune", "--all", "--force", "--filter", "until=48h"],
)
def clear_clique_db():
try:
print("Resetting chain")
run(
[
"docker",
"rm",
"--force",
"chain",
],
)
clique_db_path = "/var/k8s/discovery-provider-chain/db/clique/"
run(["sudo", "rm", "-rf", clique_db_path], timeout=60)
except Exception as e:
print(f"Error in removing clique db: {e}")
@click.group()
@click.pass_context
def cli(ctx):
"""A tool for managing audius services"""
ctx.ensure_object(dict)
ctx.obj["manifests_path"] = pathlib.Path(
os.getenv("MANIFESTS_PATH", os.path.dirname(os.path.realpath(__file__)))
)
logging.basicConfig(
filename=ctx.obj["manifests_path"] / "audius-cli.log",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s",
)
@cli.command()
@click.argument("service", type=service_type)
@click.pass_context
def check_config(ctx, service):
"""Check the config for a service"""
env = ctx.obj["manifests_path"] / service / f"{get_network(ctx, service)}.env"
override_env = get_override_path(ctx, service)
env_data = dotenv.dotenv_values(env)
override_env_data = dotenv.dotenv_values(override_env)
unset = False
for key, value in env_data.items():
if override_env_data.get(key, value) == "":
unset = True
click.secho(f"{key} is not set", fg="red")
if unset:
sys.exit(1)
else:
click.secho("All keys are set", fg="green")
@cli.command()
@click.argument("service", type=service_type)
@click.pass_context
def health_check(ctx, service):
"""Check the health of a service"""
path = ctx.obj["manifests_path"] / service
container = "backend"
if service == "discovery-provider-notifications":
path = ctx.obj["manifests_path"] / "discovery-provider"
container = "notifications"
elif service == "creator-node":
container = "audiusd"
proc = run(
[
"docker",
"compose",
"--project-directory",
path,
"ps",
"-q",
container,
],
capture_output=True,
)
if proc.returncode:
click.secho("Service is not running", fg="yellow")
sys.exit(1)
try:
response = json.load(
urllib.request.urlopen(
f"http://localhost:{SERVICE_PORTS[service]}/health_check"
)
)
click.secho("Response:", bold=True)
click.echo(json.dumps(response, indent=2, sort_keys=True))
partial = False
if service == "creator-node":
healthy = response["data"]["healthy"]
elif service == "discovery-provider":
healthy = "block_difference" in response["data"]
block_diff = response["data"]["block_difference"]
max_block_diff = response["data"]["maximum_healthy_block_difference"]
partial = block_diff > max_block_diff
if block_diff > max_block_diff:
click.secho(
f"Block difference ({block_diff}) is greater than maximum healthy block difference ({max_block_diff})",
fg="yellow",
)
elif service == "identity-service":
healthy = response["healthy"]
elif service == "discovery-provider-notifications":
healthy = response["healthy"]
if healthy and partial:
click.secho("Service is partially healthy", fg="yellow")
sys.exit(2)
elif healthy:
click.secho("Service is healthy", fg="green")
sys.exit(0)
else:
click.secho("Service is not healthy", fg="red")
sys.exit(1)
except (
ConnectionError,
ConnectionRefusedError,
urllib.error.HTTPError,
urllib.error.URLError,
json.JSONDecodeError,
):
click.secho("Service is not healthy", fg="red")
sys.exit(1)
@cli.command()
@click.argument("service", type=click.Choice(('creator-node', 'discovery-provider')))
@click.pass_context
def min_version(ctx, service):
"""Check the minimum required version for a service"""
env = ctx.obj["manifests_path"] / service / f"{get_network(ctx, service)}.env"
override_env = get_override_path(ctx, service)
env_data = dotenv.dotenv_values(env)
override_env_data = dotenv.dotenv_values(override_env)
if service == "creator-node":
service = "content-node"
elif service == "discovery-provider":
service = "discovery-node"
rpc_env_var = "audius_web3_eth_provider_url" if service == "discovery-node" else "ethProviderUrl"
rpc_url = override_env_data.get(rpc_env_var, env_data.get(rpc_env_var))
if not rpc_url:
click.secho(f"{rpc_env_var} env var is not set", fg="red")
sys.exit(1)
eth_registry_address_env_var = "audius_eth_contracts_registry" if service == "discovery-node" else "ethRegistryAddress"
eth_registry_address_str = override_env_data.get(eth_registry_address_env_var, env_data.get(eth_registry_address_env_var))
if not eth_registry_address_str:
click.secho(f"{eth_registry_address_env_var} env var is not set", fg="red")
sys.exit(1)
eth_registry_address = Web3.to_checksum_address(eth_registry_address_str)
eth_web3 = get_eth_web3(rpc_url)
eth_abi_values = load_eth_abi_values(os.path.join(ctx.obj["manifests_path"], "eth-contracts", "ABIs"))
eth_registry_instance = eth_web3.eth.contract(
address=eth_registry_address, abi=eth_abi_values["Registry"]["abi"]
)
contract_address = eth_registry_instance.functions.getContract(
"ServiceTypeManagerProxy".encode("utf-8")
).call()
contract_instance = eth_web3.eth.contract(
address=contract_address, abi=eth_abi_values["ServiceTypeManagerProxy"]["abi"]
)
min_version = contract_instance.functions.getCurrentVersion(
service.encode("utf-8")
).call()
click.secho(f"Minimum required version for {service} is {min_version.decode('utf-8')}", fg="green")
sys.exit(0)
def load_eth_abi_values(abi_dir):
json_files = os.listdir(abi_dir)
loaded_abi_values = {}
for contract_json_file in json_files:
fullPath = os.path.join(abi_dir, contract_json_file)
with open(fullPath) as f:
data = json.load(f)
loaded_abi_values[data["contractName"]] = data
return loaded_abi_values
def get_eth_web3(rpc_url):
provider = MultiProvider(rpc_url)
for p in provider.providers:
p.middlewares.clear()
# Remove the default JSON-RPC retry middleware
# as it correctly cannot handle eth_getLogs block range
# throttle down.
# See https://web3py.readthedocs.io/en/stable/examples.html
eth_web3 = Web3(provider)
eth_web3.strict_bytes_type_checking = False
return eth_web3
class MultiProvider(BaseProvider):
"""
Implements a custom web3 provider
ref: https://web3py.readthedocs.io/en/stable/internals.html#writing-your-own-provider
"""
def __init__(self, providers):
self.providers = [HTTPProvider(provider) for provider in providers.split(",")]
def make_request(self, method, params):
for provider in random.sample(self.providers, k=len(self.providers)):
try:
return provider.make_request(method, params)
except Exception:
continue
raise Exception("All requests failed")
def isConnected(self):
return any(provider.isConnected() for provider in self.providers)
def __str__(self):
return f"MultiProvider({self.providers})"
@cli.command()
@click.option("-y", "--yes", is_flag=True)
@click.option("--seed", is_flag=True, help="Seed the discovery-provider database")
@click.option(
"--auto-seed",
is_flag=True,
help="Seed the discovery-provider database if it's empty (e.g. for new nodes)",
)
@click.option("--chain", is_flag=True)
@click.argument("service", type=service_type)
@click.pass_context
def launch(ctx, service, seed, auto_seed, chain, yes):
"""Launch the service"""
set_automatic_env(ctx)
lock(ctx, "docker")
log_file = ctx.obj["manifests_path"] / "auto-upgrade.log"
try:
# clear the previous auto-upgrade logs
open(log_file, "w").close()
except Exception as e:
print(f"Error while clearing the log file: {e}")
try:
ctx.invoke(check_config, service=service)
except SystemExit:
pass
total_memory = psutil.virtual_memory().total
cpu_count = psutil.cpu_count()
total_storage = shutil.disk_usage("/var/k8s").total
click.echo(f"CPUs:\t{cpu_count}\t(required: {RECOMMENDED_CPU_COUNT})")
click.echo(
f"Memory:\t{total_memory // GB}GB\t(required: {RECOMMENDED_MEMORY // GB}GB)"
)
click.echo(
f"Storage:\t{total_storage // GB}GB\t(required: {RECOMMENDED_STORAGE[service] // GB}GB)"
)
if (
cpu_count < RECOMMENDED_CPU_COUNT
or total_memory < RECOMMENDED_MEMORY
or total_storage < RECOMMENDED_STORAGE[service]
):
click.secho("System does not meet requirements", fg="red")
else:
click.secho("System meets requirements", fg="green")
if not yes:
click.confirm(click.style("Do you want to continue?", bold=True), abort=True)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"pull",
],
check=True,
)
if service == "discovery-provider" and (seed or auto_seed):
# make sure services that use postgres are not running
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / "discovery-provider",
"down",
"backend",
"indexer",
"trpc",
"comms",
"notifications",
"es-indexer",
],
)
click.secho("Seeding the discovery provider", fg="yellow")
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / "discovery-provider",
"run",
"seed",
"bash",
"/usr/share/seed.sh",
get_network(ctx, "discovery-provider"),
"true" if auto_seed else "false",
],
)
plugins = get_registered_plugins(ctx, service)
if service == "discovery-provider":
ctx.invoke(launch_chain)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
*plugins,
"up",
"--remove-orphans",
"-d",
],
check=True,
)
prune()
@cli.command()
@click.argument("service", type=service_type)
@click.argument("containers", type=container_type, nargs=-1)
@click.pass_context
def logs(ctx, service, containers):
"""Get logs for a service/container"""
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"logs",
"-f",
*containers,
],
)
@cli.command()
@click.argument("service", type=service_type, required=False)
@click.argument("containers", type=container_type, nargs=-1)
@click.pass_context
def restart(ctx, service, containers):
"""Restart a service/container"""
set_automatic_env(ctx)
lock(ctx, "docker")
services = [service]
if service is None:
services = SERVICES
for service in services:
server = "backend"
if service == "creator-node":
server = "audiusd"
proc = run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"ps",
"-q",
server,
],
capture_output=True,
)
plugins = get_registered_plugins(ctx, service)
if proc.stdout:
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
*plugins,
"up",
"--build",
"--force-recreate",
"--remove-orphans",
"-d",
*containers,
],
)
prune()
@cli.command()
@click.argument("service", type=service_type, required=False)
@click.argument("containers", type=container_type, nargs=-1)
@click.pass_context
def down(ctx, service, containers):
"""Stops a service/container"""
lock(ctx, "docker")
services = [service]
if service is None:
services = SERVICES
for service in services:
if not containers and service == "discovery-provider":
# ensure removal of the chain container
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"--profile",
"chain",
"down",
],
)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"down",
*containers,
],
)
@cli.command()
@click.option("--unset", is_flag=True)
@click.option("--required", is_flag=True)
@click.argument("service", type=service_type)
@click.argument("key", required=False)
@click.argument("value", required=False)
@click.pass_context
def set_config(ctx, service, unset, required, key, value):
"""Set a config value"""
if required and (key or value):
click.secho("--required cannot be used when key or value is set", fg="red")
sys.exit(1)
env = ctx.obj["manifests_path"] / service / f"{get_network(ctx, service)}.env"
env_data = dotenv.dotenv_values(env)
override_env = get_override_path(ctx, service)
override_env_data = dotenv.dotenv_values(override_env)
if required:
logging.info("audius-cli set-config required")
for key, value in env_data.items():
if not unset and value == "":
value = click.prompt(
click.style(key, bold=True),
override_env_data.get(key, env_data.get(key)),
)
logging.info(f"> audius-cli set-config key={key!r} value={value!r}")
dotenv.set_key(override_env, key, value)
if unset and value == "":
logging.info(f"> audius-cli set-config unset key={key!r}")
dotenv.unset_key(override_env, key)
else:
if key is None:
key = click.prompt(click.style("Key", bold=True))
if not unset and value is None:
value = click.prompt(click.style("Value", bold=True))
if unset:
logging.info(f"audius-cli set-config unset key={key!r}")
dotenv.unset_key(override_env, key)
else:
logging.info(f"audius-cli set-config key={key!r} value={value!r}")
dotenv.set_key(override_env, key, value)
@cli.command()
@click.argument("service", type=service_type)
@click.pass_context
def get_config(ctx, service):
server = "backend"
if service == "creator-node":
server = "audiusd"
proc = run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"ps",
"-q",
server,
],
capture_output=True,
)
# get from inside container if running otherwise fallback override.env
if proc.stdout:
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
"exec",
server,
"env",
]
)
else:
env = ctx.obj["manifests_path"] / service / f"{get_network(ctx, service)}.env"
env_data = dotenv.dotenv_values(env)
override_env = get_override_path(ctx, service)
override_env_data = dotenv.dotenv_values(override_env)
current_env_data = {**env_data, **override_env_data}
for key, value in current_env_data.items():
print(f"{key}={value}")
@cli.command()
@click.option("--unset", is_flag=True)
@click.option("-y", "--yes", is_flag=True)
@click.argument("tag", required=False)
@click.pass_context
def set_tag(ctx, unset, yes, tag):
"""Set the commit tag"""
if not unset and tag is None:
tag = click.prompt(click.style("Tag", bold=True))
if not unset:
try:
for commit in json.load(
urllib.request.urlopen(
f"https://api.github.com/repos/AudiusProject/audius-protocol/commits?sha={tag}&per_page=10"
)
):
short_message = commit["commit"]["message"].split("\n")[0]
click.echo(
f"{click.style(commit['sha'][:8], bold=True, fg='yellow')}: "
f"[{click.style(commit['commit']['author']['name'], bold=True, fg='blue')}] "
f"{short_message}"
)
except (ConnectionError, urllib.error.HTTPError, json.JSONDecodeError):
click.secho("Failed to get commit messages", fg="red")
logging.info(f"audius-cli set-tag tag={tag!r}")
for service in SERVICES:
key = "TAG"
env_file = ctx.obj["manifests_path"] / service / ".env"
if unset:
logging.info(f"> audius-cli set-tag unset service={service!r}")
dotenv.unset_key(env_file, key)
else:
logging.info(f"> audius-cli set-tag service={service!r} tag={tag!r}")
dotenv.set_key(env_file, key, tag)
@cli.command()
@click.option("--unset", is_flag=True)
@click.argument("service", required=True)
@click.argument("path", type=click.Path(), required=False)
@click.pass_context
def set_override_path(ctx, unset, service, path):
"""Specify alternate location for override.env"""
if not unset:
if not service:
service = click.prompt(click.style("Service", bold=True))
if not path:
path = click.prompt(click.style("Path", bold=True))
env_file = ctx.obj["manifests_path"] / service / ".env"
if unset:
logging.info(
f"audius-cli set-override-path unset service={service!r} path={path!r}"
)
dotenv.unset_key(env_file, "OVERRIDE_PATH")
else:
logging.info(f"audius-cli set-override-path service={service!r} path={path!r}")
dotenv.set_key(env_file, "OVERRIDE_PATH", path)
@cli.command()
@click.option("--unset", is_flag=True)
@click.argument("network", type=network_type, required=False)
@click.pass_context
def set_network(ctx, unset, network):
"""Set the deployment network"""
if not unset and network is None:
network = click.prompt(click.style("Network", bold=True))
logging.info(f"audius-cli set-network network={network!r}")
for service in SERVICES:
env_file = ctx.obj["manifests_path"] / service / ".env"
if unset:
logging.info(f"> audius-cli set-network unset service={service!r}")
dotenv.unset_key(env_file, "NETWORK")
else:
logging.info(
f"> audius-cli set-network service={service!r} network={network!r}"
)
dotenv.set_key(env_file, "NETWORK", network)
@cli.command()
@click.argument("branch", required=False)
@click.pass_context
def pull_reset(ctx, branch):
"""Pull latest updates from remote and hard reset"""
try:
subprocess.run(["git", "fetch"], check=True, cwd=ctx.obj["manifests_path"])
if branch:
subprocess.run(
["git", "checkout", branch], check=True, cwd=ctx.obj["manifests_path"]
)
# Get the name of the current branch
current_branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=ctx.obj["manifests_path"],
text=True,
).strip()
# Run git reset with the current branch
subprocess.run(
["git", "reset", "--hard", f"origin/{current_branch}"],
check=True,
cwd=ctx.obj["manifests_path"],
)
except subprocess.CalledProcessError as e:
click.secho(f"Could not pull and hard reset: {e}", fg="red")
sys.exit(1)
@cli.command()
@click.argument("branch", required=False)
@click.pass_context
def upgrade(ctx, branch):
"""Pulls from latest source and re-launches all running services"""
log_file = ctx.obj["manifests_path"] / "auto-upgrade.log"
try:
# clear the previous auto-upgrade logs
open(log_file, "w").close()
except Exception as e:
print(f"Error while clearing the log file: {e}")
try:
ctx.forward(pull_reset)
set_automatic_env(ctx)
lock(ctx, "docker")
identity_override_env = get_override_path(ctx, "identity-service")
creator_override_env = get_override_path(ctx, "creator-node")
disc_override_env = get_override_path(ctx, "discovery-provider")
# determine service type based on override.env location and env vars in it
service = ""
if creator_override_env.exists() and dotenv.get_key(creator_override_env, "creatorNodeEndpoint"):
service = "creator-node"
elif identity_override_env.exists() and dotenv.get_key(identity_override_env, "ethOwnerWallet"):
service = "identity-service"
elif disc_override_env.exists() and dotenv.get_key(disc_override_env, "audius_delegate_owner_wallet"):
service = "discovery-provider"
else:
click.secho("Unable to determine service type. Ensure your override.env is configured", fg="yellow")
sys.exit(1)
if service == "discovery-provider" and not is_in_discprov_whitelist(ctx):
# Stop and remove all containers except vector and audiusd
containers = run(
["docker", "ps", "--format", "{{.Names}}"],
capture_output=True,
text=True
).stdout.strip().split('\n')
containers = [c for c in containers if c and c not in ('vector', 'audiusd')]
if containers: # Only run if containers were found
run(["docker", "rm", "-f"] + containers)
plugins = get_registered_plugins(ctx, service)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
*plugins,
"pull",
],
)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
*plugins,
"up",
"--remove-orphans",
"-d",
],
)
run(
[
"docker",
"compose",
"--project-directory",
ctx.obj["manifests_path"] / service,
*plugins,
"up",
"--force-recreate",
"vector",
"--remove-orphans",
"-d",
],
)
if service == "discovery-provider":
ctx.invoke(launch_chain)
chain_curl_res = run(
[
"docker",
"exec",
"chain",
"curl",
"-s",
"--max-time",
"10",
"http://ipv4.icanhazip.com"
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
).stdout.strip()
chain_created = run(
[
"docker",
"inspect",
"-f",
"\'{{ .Created }}\'",
"chain"
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
).stdout.strip()
print("IP from chain: " + chain_curl_res)
print("chain created: " + chain_created)
if chain_curl_res in ["209.161.4.44", "209.161.4.38", "3.235.40.101", "108.160.129.21"] and not chain_created.startswith("2024-02"):
# resync problem containers once
clear_clique_db()
ctx.invoke(launch_chain)
finally:
lock(ctx, "docker")
prune()
@cli.command()
@click.option("--remove", is_flag=True)
# random so nodes in the network stagger upgrades
@click.argument("cron-expression", default=f"{random.randint(0, 59)} * * * *")
@click.pass_context
def auto_upgrade(ctx, remove, cron_expression):
"""Setup auto upgrade with a cron job"""
with crontab.CronTab(user=True) as cron:
for job in cron.find_comment("audius-cli auto-upgrade"):
print(
f'Removed auto-upgrade cron. If you want to re-enable, run: audius-cli auto-upgrade "{job.slices}"'
)
cron.remove(job)
if not remove:
log_file = ctx.obj["manifests_path"] / "auto-upgrade.log"
try:
open(log_file, "x").close()
except FileExistsError:
pass
except:
logging.warn(f"Unable to create `{log_file}` file")
job = cron.new(
(
f"date >> {log_file};"
f"/usr/local/bin/audius-cli upgrade >> {log_file} 2>&1;"
),
"audius-cli auto-upgrade",
)
job.setall(cron_expression)
@cli.command()
@click.pass_context