Skip to content

Commit cf64129

Browse files
refactor: extract & rename functions to simplify Tiltfile
1 parent 62a1df6 commit cf64129

File tree

3 files changed

+127
-108
lines changed

3 files changed

+127
-108
lines changed

Tiltfile

Lines changed: 4 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ localnet_config_defaults = {
5656
"enabled": True,
5757
},
5858

59-
# TODO_CONSIDERATION: Consider using git submodules for all repos where we
60-
# are dependent on a sibling directory.
59+
# NOTE: git submodule usage was explicityly avoided to reduce environment complexity.
6160

6261
# By default, we use the `helm_repo` function below to point to the remote repository
6362
# but can update it to the locally cloned repo for testing & development
@@ -398,115 +397,12 @@ if localnet_config["rest"]["enabled"]:
398397
k8s_resource("rest", labels=["data_nodes"], port_forwards=["10000"])
399398

400399
### Pocketdex Shannon Indexer
401-
pocketdex_root_path = localnet_config["indexer"]["repo_path"]
402-
pocketdex_tilt_path = os.path.join(pocketdex_root_path, "tiltfiles", "pocketdex.tilt")
403-
postgres_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-postgres.yaml")
404-
pgadmin_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-pgadmin.yaml")
405-
indexer_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-indexer.yaml")
406-
gql_engine_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-gql-engine.yaml")
407-
408-
409-
# pocketdex_repo_exists returns true if the sibling pocketdex repo exists.
410-
def pocketdex_repo_exists():
411-
return str(
412-
local("[ -d {} ] && echo 'true' || echo 'false'".format(pocketdex_root_path),
413-
echo_off=True)
414-
).strip()
415-
416-
417-
# pocketdex_disabled_resource creates a tilt resource that prints a message indicating
418-
# that the indexer is disabled and how to enable it.
419-
def pocketdex_disabled_resource(reason):
420-
local_resource("⚠️ Indexer Disabled",
421-
"echo '{}'".format(reason),
422-
labels=["Pocketdex"])
423-
424-
425-
# fetch_pocketdex_main fetches the main branch from the remote from which the repo was cloned.
426-
def fetch_pocketdex_main():
427-
local("git fetch {} main".format(main_branch_remote_name()),
428-
dir=pocketdex_root_path,
429-
echo_off=True)
430-
431-
432-
# main_branch_remote_name returns the name of the remote from which the repo was cloned.
433-
def main_branch_remote_name():
434-
return str(
435-
local("git rev-parse --abbrev-ref @{u} | cut -d '/' -f1",
436-
dir=pocketdex_root_path,
437-
echo_off=True),
438-
).strip()
439-
440-
441-
# pocketdex_changes returns a tuple of integers representing the number of local
442-
# and remote changes, respectively.
443-
def pocketdex_changes():
444-
all_changes = str(
445-
local("git rev-list --left-right --count HEAD...{}/main".format(main_branch_remote_name()),
446-
dir=pocketdex_root_path,
447-
echo_off=True)
448-
)
449-
450-
# Split the output into local and remote changes and convert to integers.
451-
num_local_changes, num_remote_changes = [int(x) for x in all_changes.split()]
452-
453-
return (num_local_changes, num_remote_changes)
454-
455-
456-
# pocketdex_main_is_outdated returns true if there's a diff between the local and remote main branches.
457-
def pocketdex_main_is_outdated():
458-
fetch_pocketdex_main()
459-
_, num_remote_changes = pocketdex_changes()
460-
return num_remote_changes != 0
461-
462-
463-
# pocketdex_outdated_resource creates a tilt resource that prints a message indicating
464-
# that the indexer is outdated and how many commits behind it is.
465-
def pocketdex_outdated_resource():
466-
_, num_remote_changes = pocketdex_changes()
467-
local_resource("🔄 Updates Available",
468-
"""
469-
echo 'Pocketdex main branch is outdated; {} commits behind. Please `git pull --ff-only` to update pocketdex.'
470-
""".format(num_remote_changes),
471-
labels=["Pocketdex"])
472-
473-
474-
def load_pocketdex():
475-
if pocketdex_main_is_outdated():
476-
pocketdex_outdated_resource()
477-
478-
pocketdex_tilt = load_dynamic(pocketdex_tilt_path)
479-
pocketdex_tilt["pocketdex"](pocketdex_root_path,
480-
genesis_file_name="localnet.json",
481-
postgres_values_path=postgres_values_path,
482-
pgadmin_values_path=pgadmin_values_path,
483-
indexer_values_path=indexer_values_path,
484-
gql_engine_values_path=gql_engine_values_path)
485-
400+
load("./tiltfiles/pocketdex.tilt", "check_and_load_pocketdex")
486401

487402
# Check if sibling pocketdex repo exists.
488403
# If it does, load the pocketdex.tilt file from the sibling repo.
489-
# Otherwise check the `indexer.clone_if_not_present` flag in `localnet_config.yaml` and EITHER:
404+
# Otherwise, check the `indexer.clone_if_not_present` flag in `localnet_config.yaml` and EITHER:
490405
# 1. clone pocketdex to ../pocketdex
491406
# -- OR --
492407
# 2. Prints a message if true or false
493-
if localnet_config["indexer"]["enabled"]:
494-
if pocketdex_repo_exists() == "false":
495-
if localnet_config["indexer"]["clone_if_not_present"]:
496-
print("Cloning pocketdex repo")
497-
# TODO_IMPROVE: https://github.com/tilt-dev/tilt-extensions/tree/master/git_resource
498-
local("""
499-
git clone https://github.com/pokt-network/pocketdex --branch main {}
500-
""".format(pocketdex_root_path))
501-
load_pocketdex()
502-
else:
503-
pocketdex_disabled_resource("""
504-
Pocketdex repo not found at {}. Set `clone_if_not_present` to `true` in `localnet_config.yaml`.
505-
""".format(pocketdex_root_path))
506-
else:
507-
print("Using existing pocketdex repo")
508-
load_pocketdex()
509-
else:
510-
pocketdex_disabled_resource("""
511-
Pocketdex indexer disabled. Set `indexer.enabled` to `true` in `localnet_config.yaml` to enable it.
512-
""")
408+
check_and_load_pocketdex(localnet_config["indexer"])

tiltfiles/git.tilt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# dir_repo_exists returns true if the dir_path exists.
2+
def dir_exists(dir_path):
3+
return str(
4+
local("[ -d {} ] && echo 'true' || echo 'false'".format(dir_path),
5+
echo_off=True)
6+
).strip()
7+
8+
9+
# repo_remote_name returns the name of the remote from which the git repo was cloned.
10+
def repo_remote_name(repo_root):
11+
return str(
12+
local("git rev-parse --abbrev-ref @{u} | cut -d '/' -f1",
13+
dir=repo_root,
14+
echo_off=True),
15+
).strip()
16+
17+
18+
# fetch_repo_main fetches the main branch from the remote from which the git repo was cloned.
19+
def fetch_repo_main(repo_root):
20+
local("git fetch {} main".format(repo_remote_name(repo_root)),
21+
dir=repo_root,
22+
echo_off=True)
23+
24+
25+
# repo_changes returns a tuple of integers representing the number of local
26+
# and remote changes for the git repos main branch, respectively.
27+
def repo_changes(repo_root):
28+
all_changes = str(
29+
local("""
30+
git rev-list --left-right --count HEAD...{}/main
31+
""".format(repo_remote_name(repo_root)),
32+
dir=repo_root,
33+
echo_off=True)
34+
)
35+
36+
# Split the output into local and remote changes and convert to integers.
37+
num_local_changes, num_remote_changes = [int(x) for x in all_changes.split()]
38+
39+
return (num_local_changes, num_remote_changes)
40+
41+
42+
# repo_is_outdated returns true if there's a diff between the local and remote main branches.
43+
def repo_is_outdated(repo_root):
44+
fetch_repo_main(repo_root)
45+
_, num_remote_changes = repo_changes(repo_root)
46+
return num_remote_changes != 0
47+
48+
49+
def clone_repo(repo_url, local_path):
50+
print("Cloning pocketdex repo")
51+
# TODO_IMPROVE: https://github.com/tilt-dev/tilt-extensions/tree/master/git_resource
52+
local("git clone {} --branch main {}".format(repo_url, local_path))

tiltfiles/pocketdex.tilt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
load("./git.tilt",
2+
"dir_exists",
3+
"clone_repo",
4+
"fetch_repo_main",
5+
"repo_remote_name",
6+
"repo_changes",
7+
"repo_is_outdated")
8+
9+
10+
# pocketdex_disabled_resource creates a tilt resource that prints a message indicating
11+
# that the indexer is disabled and how to enable it.
12+
def pocketdex_disabled_resource(reason):
13+
local_resource("⚠️ Indexer Disabled",
14+
"echo '{}'".format(reason),
15+
labels=["Pocketdex"])
16+
17+
18+
# pocketdex_outdated_resource creates a tilt resource that prints a message indicating
19+
# that the indexer is outdated and how many commits behind it is.
20+
def pocketdex_outdated_resource(pocketdex_root_path):
21+
_, num_remote_changes = repo_changes(pocketdex_root_path)
22+
local_resource("🔄 Updates Available",
23+
"""
24+
echo 'Pocketdex main branch is outdated; {} commits behind. Please `git pull --ff-only` to update pocketdex.'
25+
""".format(num_remote_changes),
26+
labels=["Pocketdex"])
27+
28+
29+
def load_pocketdex(pocketdex_root_path):
30+
if repo_is_outdated(pocketdex_root_path):
31+
pocketdex_outdated_resource(pocketdex_root_path)
32+
33+
pocketdex_tilt_path = os.path.join(pocketdex_root_path, "tiltfiles", "pocketdex.tilt")
34+
pocketdex_tilt = load_dynamic(pocketdex_tilt_path)
35+
36+
postgres_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-postgres.yaml")
37+
pgadmin_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-pgadmin.yaml")
38+
indexer_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-indexer.yaml")
39+
gql_engine_values_path = os.path.join(".", "localnet", "kubernetes", "values-pocketdex-gql-engine.yaml")
40+
pocketdex_tilt["pocketdex"](pocketdex_root_path,
41+
genesis_file_name="localnet.json",
42+
postgres_values_path=postgres_values_path,
43+
pgadmin_values_path=pgadmin_values_path,
44+
indexer_values_path=indexer_values_path,
45+
gql_engine_values_path=gql_engine_values_path)
46+
47+
48+
# Check if sibling pocketdex repo exists.
49+
# If it does, load the pocketdex.tilt file from the sibling repo.
50+
# Otherwise, check the `indexer.clone_if_not_present` flag in `localnet_config.yaml` and EITHER:
51+
# 1. clone pocketdex to ../pocketdex
52+
# -- OR --
53+
# 2. Prints a message if true or false
54+
def check_and_load_pocketdex(indexer_config):
55+
if indexer_config["enabled"]:
56+
pocketdex_root_path = indexer_config["repo_path"]
57+
if dir_exists(pocketdex_root_path) == "false":
58+
if indexer_config["clone_if_not_present"]:
59+
clone_repo("https://github.com/pokt-network/pocketdex", pocketdex_root_path)
60+
load_pocketdex(pocketdex_root_path)
61+
else:
62+
pocketdex_disabled_resource("""
63+
Pocketdex repo not found at {}. Set `clone_if_not_present` to `true` in `localnet_config.yaml`.
64+
""".format(pocketdex_root_path))
65+
else:
66+
print("Using existing pocketdex repo")
67+
load_pocketdex(pocketdex_root_path)
68+
else:
69+
pocketdex_disabled_resource("""
70+
Pocketdex indexer disabled. Set `indexer.enabled` to `true` in `localnet_config.yaml` to enable it.
71+
""")

0 commit comments

Comments
 (0)