Skip to content

Commit

Permalink
Merge pull request #4 from Team-Kujira/oracle
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
starsquidnodes authored Mar 6, 2024
2 parents 9c5b883 + fae584c commit 3b4d35b
Showing 1 changed file with 143 additions and 51 deletions.
194 changes: 143 additions & 51 deletions pond
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import shutil
import sys
import os
import json
import requests
from urllib.parse import urlparse
import logging

VERSION = "v0.1.0"
TAG = "v0.2.0"


def parse_args():
parser = argparse.ArgumentParser()

parser.add_argument("--namespace", default="teamkujira")
parser.add_argument("--debug", action="store_true")

subparsers = parser.add_subparsers()
subparsers.required = True
Expand All @@ -27,8 +29,7 @@ def parse_args():
help="use podman")
init_parser.add_argument("--docker", action="store_true",
help="use docker")
# init_parser.add_argument("--kujira-version", default="v0.9.3-1")
# init_parser.add_argument("--feeder-version", default="mainnet")
init_parser.add_argument("--tag", default=TAG, help="tag")

start_parser = subparsers.add_parser("start", help="start components")
start_parser.add_argument("target", nargs="?")
Expand All @@ -37,44 +38,18 @@ def parse_args():
stop_parser.add_argument("target", nargs="?")

subparsers.add_parser("info", help="info components")

subparsers.add_parser("tags", help="print available tags")

return parser.parse_args()


def get_tags(namespace, components=["kujira", "feeder"]):
all_tags = {}
baseurl = f"https://hub.docker.com/v2/namespaces/{namespace}/repositories"

for component in components:
url = f"{baseurl}/{component}/tags"
response = requests.get(url)
if response.status_code != 200:
print(f"docker api returned http-{response.status_code}")
return

tags = []
data = response.json()
for result in data.get("results", []):
tag = result["name"]
if tag.endswith("-arm64") or tag.endswith("-x86_64"):
continue
tags.append(tag)

all_tags[component] = tags
# https://hub.docker.com/v2/namespaces/teamkujira/repositories/feeder/tags

print(json.dumps(all_tags, indent=2))


def start_node_cmd(cmd, namespace, name, version, home, ports):
port_args = " ".join([f"-p 127.0.0.1:{x}:{x}" for x in ports])
uid = os.getuid()

commands = {
"docker": [
f"docker rm {name}".split(),
f"docker rm -f {name}".split(),
f"docker run -e USER={uid} -d {port_args} --network pond --name {name} --network-alias {name} -v {home}/{name}:/kujira {namespace}/kujira:{version} kujirad --home /kujira start".split()
],
"podman": [
Expand All @@ -85,18 +60,16 @@ def start_node_cmd(cmd, namespace, name, version, home, ports):
return commands.get(cmd)


def start_feeder_cmd(cmd, namespace, name, version, home, mnemonic):
def start_feeder_cmd(cmd, namespace, name, version, home, port):
uid = os.getuid()

commands = {
"docker": [
f"docker rm {name}".split(),
f"docker run -e USER={uid} -d --network pond --network-alias {name} --name {name} -v {home}/{name}:/feeder -e".split(
) + [f"PRICE_FEEDER_MNEMONIC={ mnemonic }", f"{namespace}/feeder:{version}"]
f"docker run -e USER={uid} -d -p 127.0.0.1:{port}:{port} --network pond --network-alias {name} --name {name} -v {home}/{name}:/feeder {namespace}/feeder:{version} price-feeder config.toml".split()
],
"podman": [
f"podman run --name {name} -d --pod pond -v {home}/{name}:/feeder -e".split(
) + [f"PRICE_FEEDER_MNEMONIC={ mnemonic }", f"{namespace}/feeder:{version}"]
f"podman run --name {name} -d --pod pond -v {home}/{name}:/feeder {namespace}/feeder:{version} price-feeder config.toml".split()
]
}

Expand Down Expand Up @@ -134,8 +107,7 @@ def init(home, args):
cmd = "podman"

if not cmd:
print("neither docker nor podman found")
sys.exit(1)
error("neither docker nor podman found")

if cmd == "docker":
uid = os.getuid()
Expand All @@ -162,7 +134,7 @@ def init(home, args):

cmd += [
"-v", f"{home}:/tmp/pond",
f"docker.io/{args.namespace}/prepare:{VERSION}",
f"docker.io/{args.namespace}/prepare:{args.tag}",
"prepare.py", "--nodes", f"{args.nodes}", "--clear"
] + extra_args

Expand All @@ -179,7 +151,7 @@ def info(home):
def start(args, config, home):
cmd = config.get("command")
if not cmd:
print("command not set")
error("command not set")

ns = args.namespace

Expand All @@ -188,24 +160,30 @@ def start(args, config, home):
ports = []
for chain_id, validators in config["validators"].items():
for validator in validators:
_ports = [
validator["api_url"].split(":")[-1],
validator["rpc_url"].split(":")[-1]
node_ports = [
urlparse(validator["api_url"]).port,
urlparse(validator["rpc_url"]).port
]

ports += _ports
# backwards compatibility
if "app_url" in validator.keys():
node_ports.append(urlparse(validator["app_url"]).port)

ports += node_ports

name = validator["moniker"].lower()
version = config["version"]["kujira"]
commands = start_node_cmd(
cmd, ns, name, version, home, _ports
cmd, ns, name, version, home, node_ports
) + commands

if chain_id == "pond-1":
name = name.replace("kujira", "feeder")
version = config["version"]["feeder"]
node_port = urlparse(validator["feeder_url"]).port
ports.append(node_port)
commands += start_feeder_cmd(
cmd, ns, name, version, home, validator["mnemonic"]
cmd, ns, name, version, home, node_port
)

version = config["version"]["relayer"]
Expand All @@ -225,13 +203,39 @@ def start(args, config, home):
"docker network create pond".split()
] + commands

if not config.get("codes"):
commands.append(["sleep", "5"])

extra = []
if cmd == "docker":
uid = os.getuid()
command = ["docker", "run", "-e",
f"USER={uid}", "--network", "pond"]
host = "kujira1-1"
else:
command = ["podman", "run", "--pod", "pond"]
host = "127.0.0.1"

version = config["version"]["prepare"]

command += [
"-v", f"{home}:/tmp/pond",
f"docker.io/{args.namespace}/prepare:{version}",
"/tmp/contracts/deploy.py", "/tmp/contracts/plans",
"--home", "/tmp/pond/kujira1-1",
"--node", f"http://{host}:10157",
"--pond-json", "/tmp/pond/pond.json"
]

commands.append(command)

run(commands)


def stop(config):
cmd = config.get("command")
if not cmd:
print("command not set")
error("command not set")

commands = [
[cmd, "kill", "relayer"]
Expand All @@ -254,32 +258,120 @@ def stop(config):

def run(commands):
for cmd in commands:
subprocess.run(cmd)
debug(" ".join(cmd))
# subprocess.call(cmd)
subprocess.call(cmd, stdout=subprocess.DEVNULL)


def main():
args = parse_args()

log_level = logging.INFO
if args.debug:
log_level = logging.DEBUG

logging.basicConfig(
level=log_level,
format="%(levelname)s %(message)s"
)

logging.addLevelName(logging.DEBUG, "DBG")
logging.addLevelName(logging.INFO, "INF")
logging.addLevelName(logging.WARNING, "WRN")
logging.addLevelName(logging.ERROR, "ERR")

home = os.path.expanduser("~") + "/.pond"
config = None

if args.command == "init":
init(home, args)
else:
if not os.path.isdir(home):
print(f"{home} not found, you need to init pond at first")
sys.exit(1)
error(f"{home} not found, you need to init pond at first")

config = json.load(open(f"{home}/pond.json", "r"))

if args.command == "start":
if is_running(config):
info("pond already running")
return

start(args, config, home)
elif args.command == "stop":
if not is_running(config):
return
stop(config)
elif args.command == "info":
info(home)
elif args.command == "tags":
get_tags(args.namespace)
for i in TAGS:
print(i)


def is_running(config):
cmd = config.get("command")
if not cmd:
error("command not set")

command = [
cmd, "ps", "--format", "json"
]

result = subprocess.check_output(command)
if not result:
return False

result = result.decode('utf8').replace("'", '"')

if cmd == "docker":
result = result.replace("\n", ",")
result = f"[{result[:-1]}]"
else:
result = result[:-1]

data = json.loads(result)

for i in data:
if cmd == "docker":
if i.get("Networks") == "pond":
return True
else:
if i.get("PodName") == "pond":
return True

return False


def error(message=None, **kwargs):
log(message, logging.ERROR, kwargs)
sys.exit(1)


def warning(message=None, **kwargs):
log(message, logging.WARNING, kwargs)


def debug(message=None, **kwargs):
log(message, logging.DEBUG, kwargs)


def info(message=None, **kwargs):
log(message, logging.INFO, kwargs)


def log(message, level, kwargs):
tags = []
for k, v in kwargs.items():
tags.append(f"{k}={v}")

tags = ", ".join(tags)

if message:
message = f"{message} {tags}"
else:
message = tags

logging.log(level, message)


if __name__ == "__main__":
Expand Down

0 comments on commit 3b4d35b

Please sign in to comment.