Skip to content

Commit

Permalink
add cli ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 committed May 21, 2024
1 parent 59ff657 commit f810a37
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 38 deletions.
103 changes: 94 additions & 9 deletions xrpld_netgen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
enable_node_amendment,
)
from xrpld_netgen.utils.misc import (
run_file,
remove_directory,
bcolors,
check_deps,
run_start,
run_stop,
)

basedir = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -58,6 +61,16 @@


def main():
print("")
print(" _ __ ____ ____ __ ____ _ __ __ ______ ")
print(" | |/ // __ \/ __ \/ / / __ \ / | / /__ / /_/ ____/__ ____ ")
print(" | // /_/ / /_/ / / / / / / / |/ / _ \/ __/ / __/ _ \/ __ \ ")
print(" / |/ _, _/ ____/ /___/ /_/ / / /| / __/ /_/ /_/ / __/ / / / ")
print("/_/|_/_/ |_/_/ /_____/_____/ /_/ |_/\___/\__/\____/\___/_/ /_/ ")
print("")

check_deps([f"{basedir}/deploykit/prerequisites.sh"])

parser = argparse.ArgumentParser(
description="A python cli to build xrpld networks and standalone ledgers."
)
Expand Down Expand Up @@ -312,12 +325,23 @@ def main():
NETWORK_TYPE = args.network_type
NETWORK_ID = args.network_id

print(
f"{bcolors.BLUE}Starting Local Network with the following parameters:{bcolors.END}"
)
print(f" - Log Level: {LOG_LEVEL}")
print(f" - Public Key: {PUBLIC_KEY}")
print(f" - Import Key: {IMPORT_KEY}")
print(f" - Protocol: {PROTOCOL}")
print(f" - Network Type: {NETWORK_TYPE}")
print(f" - Network ID: {NETWORK_ID}")

start_local(
LOG_LEVEL, PUBLIC_KEY, IMPORT_KEY, PROTOCOL, NETWORK_TYPE, NETWORK_ID
)

if args.command == "stop:local":
run_file("./stop.sh")
print(f"{bcolors.BLUE}Stopping Local Network{bcolors.END}")
run_stop(["./stop.sh"])

# CREATE NETWORK
if args.command == "create:network":
Expand All @@ -344,6 +368,19 @@ def main():
if not QUORUM:
QUORUM = NUM_VALIDATORS - 1

print(
f"{bcolors.BLUE}Creating Network with the following parameters:{bcolors.END}"
)
print(f" - Log Level: {LOG_LEVEL}")
print(f" - Protocol: {PROTOCOL}")
print(f" - Number of Validators: {NUM_VALIDATORS}")
print(f" - Number of Peers: {NUM_PEERS}")
print(f" - Network ID: {NETWORK_ID}")
print(f" - Build Server: {BUILD_SERVER}")
print(f" - Build Version: {BUILD_VERSION}")
print(f" - Genesis: {GENESIS}")
print(f" - Quorum: {QUORUM}")

create_network(
LOG_LEVEL,
import_vl_key,
Expand All @@ -363,26 +400,49 @@ def main():
NODE_VERSION = args.node_version
BUILD_SERVER = args.build_server
BUILD_VERSION = args.build_version
print(
f"{bcolors.BLUE}Updating Node Version with the following parameters:{bcolors.END}"
)
print(f" - Network Name: {NAME}")
print(f" - Node ID: {NODE_ID}")
print(f" - Node Type: {NODE_VERSION}")
print(f" - Build Server: {BUILD_SERVER}")
print(f" - Build Version: {BUILD_VERSION}")
update_node_binary(NAME, NODE_ID, NODE_VERSION, BUILD_SERVER, BUILD_VERSION)

if args.command == "enable:amendment":
NAME = args.name
AMENDMENT_NAME = args.amendment_name
NODE_ID = args.node_id
NODE_VERSION = args.node_version
print(
f"{bcolors.BLUE}Enabling Amendment with the following parameters:{bcolors.END}"
)
print(f" - Network Name: {NAME}")
print(f" - Amendment Name: {AMENDMENT_NAME}")
print(f" - Node ID: {NODE_ID}")
print(f" - Node Type: {NODE_VERSION}")
enable_node_amendment(NAME, AMENDMENT_NAME, NODE_ID, NODE_VERSION)

# MANAGE NETWORK/STANDALONE
if args.command == "start":
NAME = args.name
run_file(f"{basedir}/{NAME}/start.sh")
print(f"{bcolors.BLUE}Starting Network: {NAME}{bcolors.END}")
run_start(
[f"{basedir}/{NAME}/start.sh"],
PROTOCOL,
BUILD_VERSION,
"network",
)

if args.command == "stop":
NAME = args.name
run_file(f"{basedir}/{NAME}/stop.sh")
print(f"{bcolors.BLUE}Stopping Network: {NAME}{bcolors.END}")
run_stop(f"{basedir}/{NAME}/stop.sh")

if args.command == "remove":
NAME = args.name
print(f"{bcolors.BLUE}Removing Network: {NAME}{bcolors.END}")
remove_directory(f"{basedir}/{NAME}")

# UP STANDALONE
Expand Down Expand Up @@ -413,6 +473,20 @@ def main():
BUILD_SERVER: str = "rippleci"
BUILD_TYPE: str = "image"

print(
f"{bcolors.BLUE}Setting Up Standalone Network with the following parameters:{bcolors.END}"
)
print(f" - Log Level: {LOG_LEVEL}")
print(f" - Build Type: {BUILD_TYPE}")
print(f" - Public Key: {PUBLIC_KEY}")
print(f" - Import Key: {IMPORT_KEY}")
print(f" - Protocol: {PROTOCOL}")
print(f" - Network Type: {NETWORK_TYPE}")
print(f" - Network ID: {NETWORK_ID}")
print(f" - Build Server: {BUILD_SERVER}")
print(f" - Build Version: {BUILD_VERSION}")
print(f" - IPFS Server: {IPFS_SERVER}")

if BUILD_TYPE == "image":
create_standalone_image(
LOG_LEVEL,
Expand All @@ -438,14 +512,23 @@ def main():
IPFS_SERVER,
)

run_file(f"{basedir}/{PROTOCOL}-{BUILD_VERSION}/start.sh")
print(f"Run with: xrpld-netgen start --name {PROTOCOL}-{BUILD_VERSION}")
run_start(
[f"{basedir}/{PROTOCOL}-{BUILD_VERSION}/start.sh"],
PROTOCOL,
BUILD_VERSION,
"standalone",
)

# DOWN STANDALONE
if args.command == "down:standalone":
NAME = args.name
print(
f"{bcolors.BLUE}Taking Down Standalone Network with the following parameters:{bcolors.END}"
)

if NAME:
run_file(f"{basedir}/{NAME}/stop.sh")
print(f" - Network Name: {NAME}")
run_stop([f"{basedir}/{NAME}/stop.sh"])
remove_directory(f"{basedir}/{NAME}")
return

Expand All @@ -458,8 +541,10 @@ def main():
if PROTOCOL == "xrpl" and not BUILD_VERSION:
BUILD_VERSION: str = XRPL_RELEASE

run_file(f"{basedir}/{PROTOCOL}-{BUILD_VERSION}/stop.sh")
# remove_directory(f"{basedir}/{PROTOCOL}-{BUILD_VERSION}")
print(f" - Network Name: {NAME}")
print(f" - Protocol: {PROTOCOL}")
print(f" - Build Version: {BUILD_VERSION}")
run_stop([f"{basedir}/{PROTOCOL}-{BUILD_VERSION}/stop.sh"])


if __name__ == "__main__":
Expand Down
14 changes: 14 additions & 0 deletions xrpld_netgen/deploykit/prerequisites.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

errors=0

which_docker=$(which docker)
[[ "$which_docker" = "" ]] && echo -e "❌ docker not installed" && errors=1 || echo "✅ docker installed"

has_internet=$(curl --connect-timeout 2 --silent https://build.xahau.tech/ | grep Index| wc -l| xargs)
[[ "$has_internet" -lt 2 ]] && echo -e "❌ cannot reach xahau build server" && errors=1 || echo "✅ can reach xahau build server"

can_run_docker=$(docker run --platform linux/amd64 --rm ubuntur uname -a 2>&1|grep x86_64|wc -l|xargs)
[[ "$can_run_docker" -gt 0 ]] && echo -e "❌ can run linux/amd64 in docker" && errors=1 || echo "✅ can run linux/amd64 in docker"

exit $errors
44 changes: 38 additions & 6 deletions xrpld_netgen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
get_commit_hash_from_server_version,
download_file_at_commit,
)
from xrpld_netgen.utils.misc import (
generate_ports,
save_local_config,
run_file,
)
from xrpld_netgen.utils.misc import generate_ports, save_local_config, bcolors
from xrpl_helpers.common.utils import write_file, read_json
from xrpl_helpers.rippled.utils import (
update_amendments,
Expand Down Expand Up @@ -132,6 +128,7 @@ def create_standalone_folder(
)
os.makedirs(f"{basedir}/{protocol}-{name}/config", exist_ok=True)
save_local_config(cfg_path, configs[0].data, configs[1].data)
print(f"✅ {bcolors.CYAN}Creating config")

lines: List[str] = get_feature_lines_from_content(feature_content)
features_json: Dict[str, Any] = parse_rippled_amendments(lines)
Expand All @@ -140,6 +137,8 @@ def create_standalone_folder(
f"{basedir}/{protocol}-{name}/genesis.json",
json.dumps(genesis_json, indent=4, sort_keys=True),
)
print(f"✅ {bcolors.CYAN}Updating features")

dockerfile: str = create_dockerfile(
binary,
name,
Expand All @@ -160,6 +159,7 @@ def create_standalone_folder(
f"{basedir}/deploykit/{protocol}.entrypoint",
f"{basedir}/{protocol}-{name}/entrypoint",
)
print(f"✅ {bcolors.CYAN}Building docker container...")
pwd_str: str = "${PWD}"
services[f"{protocol}"] = {
"build": {
Expand Down Expand Up @@ -318,6 +318,7 @@ def create_binary_folder(
)
os.makedirs(f"{basedir}/{protocol}-{name}/config", exist_ok=True)
save_local_config(cfg_path, configs[0].data, configs[1].data)
print(f"✅ {bcolors.CYAN}Creating config")

lines: List[str] = get_feature_lines_from_content(feature_content)
features_json: Dict[str, Any] = parse_rippled_amendments(lines)
Expand All @@ -326,6 +327,10 @@ def create_binary_folder(
f"{basedir}/{protocol}-{name}/genesis.json",
json.dumps(genesis_json, indent=4, sort_keys=True),
)
print(f"✅ {bcolors.CYAN}Updating features")
# for k, v in features_json.items():
# print(f"{bcolors.GREEN}feature: {bcolors.BLUE}{k}")

dockerfile: str = create_dockerfile(
binary,
name,
Expand All @@ -346,6 +351,7 @@ def create_binary_folder(
f"{basedir}/deploykit/{protocol}.entrypoint",
f"{basedir}/{protocol}-{name}/entrypoint",
)
print(f"✅ {bcolors.CYAN}Building docker container...")
pwd_str: str = "${PWD}"
services[f"{protocol}"] = {
"build": {
Expand Down Expand Up @@ -502,6 +508,8 @@ def create_local_folder(
)
os.makedirs(cfg_path, exist_ok=True)
save_local_config(cfg_path, configs[0].data, configs[1].data)
print(f"✅ {bcolors.CYAN}Creating config")

content: str = get_feature_lines_from_path(
"../src/ripple/protocol/impl/Feature.cpp"
)
Expand All @@ -511,6 +519,7 @@ def create_local_folder(
f"{cfg_path}/genesis.json",
json.dumps(genesis_json, indent=4, sort_keys=True),
)
print(f"✅ {bcolors.CYAN}Updating features")


def start_local(
Expand Down Expand Up @@ -564,4 +573,27 @@ def start_local(
stop_sh_content: str = update_stop_sh(protocol, name, 0, 0, False, True)
write_file("stop.sh", stop_sh_content)
os.chmod("stop.sh", 0o755)
run_file("./start.sh")
import sys
import subprocess

try:
result = subprocess.run(
["./start.sh"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if result.returncode == 0:
print(
f"{bcolors.CYAN}{protocol.capitalize()} local running at: {bcolors.PURPLE}6006 {bcolors.END}"
)
print(f"{bcolors.CYAN}Explorer running / starting container{bcolors.END}")
print(f"Listening at: {bcolors.PURPLE}http://localhost:4000{bcolors.END}")
else:
print(f"{bcolors.RED}ERROR{bcolors.END}", file=sys.stderr)
sys.exit(1)
except subprocess.CalledProcessError:
print(
f"{bcolors.RED}❌ Cannot connect to the Docker daemon at docker.sock. Is the docker daemon running?{bcolors.END}"
)
sys.exit(1)
Loading

0 comments on commit f810a37

Please sign in to comment.