Add TOML file parsing

This commit is contained in:
David Boreham 2023-08-08 21:06:44 -06:00
parent 0c7dcc61be
commit c08622a2d1
6 changed files with 39 additions and 16 deletions

View File

@ -21,6 +21,7 @@ from enum import Enum
from pathlib import Path from pathlib import Path
import os import os
import sys import sys
import tomli
default_spec_file_content = """config: default_spec_file_content = """config:
node_moniker: my-node-name node_moniker: my-node-name
@ -38,6 +39,22 @@ class SetupPhase(Enum):
ILLEGAL = 3 ILLEGAL = 3
def _get_chain_id_from_config():
chain_id = None
with open("laconic-network-dir/config/client.toml", "rb") as f:
toml_dict = tomli.load(f)
chain_id = toml_dict["chain_id"]
return chain_id
def _get_node_moniker_from_config():
moniker = None
with open("laconic-network-dir/config/config.toml", "rb") as f:
toml_dict = tomli.load(f)
moniker = toml_dict["moniker"]
return moniker
def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCommand, extra_args): def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCommand, extra_args):
print(f"parameters: {parameters}") print(f"parameters: {parameters}")
@ -51,6 +68,10 @@ def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCo
if not parameters.chain_id: if not parameters.chain_id:
print("--chain-id is required") print("--chain-id is required")
sys.exit(1) sys.exit(1)
# node_moniker must be supplied
if not parameters.node_moniker:
print("Error: --node-moniker is required")
sys.exit(1)
phase = SetupPhase.INITIALIZE phase = SetupPhase.INITIALIZE
elif parameters.join_network: elif parameters.join_network:
if parameters.initialize_network or parameters.create_network: if parameters.initialize_network or parameters.create_network:
@ -69,6 +90,7 @@ def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCo
if os.path.exists(network_dir): if os.path.exists(network_dir):
print(f"Error: network directory {network_dir} already exists") print(f"Error: network directory {network_dir} already exists")
sys.exit(1) sys.exit(1)
os.mkdir(network_dir) os.mkdir(network_dir)
mounts = [ mounts = [
VolumeMapping(network_dir, "/root/.laconicd") VolumeMapping(network_dir, "/root/.laconicd")
@ -83,6 +105,8 @@ def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCo
if not os.path.exists(network_dir): if not os.path.exists(network_dir):
print(f"Error: network directory {network_dir} doesn't exist") print(f"Error: network directory {network_dir} doesn't exist")
sys.exit(1) sys.exit(1)
# Get the chain_id from the config file created in the INITIALIZE phase
chain_id = _get_chain_id_from_config()
mounts = [ mounts = [
VolumeMapping(network_dir, "/root/.laconicd") VolumeMapping(network_dir, "/root/.laconicd")
] ]
@ -98,7 +122,7 @@ def setup(command_context: DeployCommandContext, parameters: LaconicStackSetupCo
output3, status3 = run_container_command( output3, status3 = run_container_command(
command_context, command_context,
"laconicd", "laconicd",
f"laconicd gentx {parameters.key_name} 90000000000achk --chain-id {parameters.chain_id} --keyring-backend test", f"laconicd gentx {parameters.key_name} 90000000000achk --chain-id {chain_id} --keyring-backend test",
mounts) mounts)
print(f"Command output: {output3}") print(f"Command output: {output3}")

View File

@ -312,7 +312,7 @@ def _make_cluster_context(ctx, stack, include, exclude, cluster, env_file):
if ctx.verbose: if ctx.verbose:
print(f"files: {compose_files}") print(f"files: {compose_files}")
return ClusterContext(cluster, compose_files, pre_start_commands, post_start_commands, cluster_config, env_file) return ClusterContext(ctx, cluster, compose_files, pre_start_commands, post_start_commands, cluster_config, env_file)
def _convert_to_new_format(old_pod_array): def _convert_to_new_format(old_pod_array):

View File

@ -17,9 +17,11 @@ from typing import List
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from python_on_whales import DockerClient from python_on_whales import DockerClient
from app.command_types import CommandOptions
@dataclass @dataclass
class ClusterContext: class ClusterContext:
options: CommandOptions # TODO: this should be in its own object not stuffed in here
cluster: str cluster: str
compose_files: List[str] compose_files: List[str]
pre_start_commands: List[str] pre_start_commands: List[str]
@ -56,3 +58,8 @@ class LaconicStackSetupCommand:
join_network: bool join_network: bool
create_network: bool create_network: bool
network_dir: str network_dir: str
@dataclass
class LaconicStackCreateCommand:
network_dir: str

View File

@ -15,7 +15,6 @@
import os import os
from typing import List from typing import List
from dataclasses import dataclass
from app.deploy_types import DeployCommandContext, VolumeMapping from app.deploy_types import DeployCommandContext, VolumeMapping
from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir
@ -51,6 +50,8 @@ def run_container_command(ctx: DeployCommandContext, service: str, command: str,
docker = ctx.docker docker = ctx.docker
container_image = _container_image_from_service(ctx.stack, service) container_image = _container_image_from_service(ctx.stack, service)
docker_volumes = _volumes_to_docker(mounts) docker_volumes = _volumes_to_docker(mounts)
if ctx.cluster_context.options.debug:
print(f"Running this command in {service} container: {command}")
docker_output = docker.run(container_image, ["-c", command], entrypoint="sh", volumes=docker_volumes) docker_output = docker.run(container_image, ["-c", command], entrypoint="sh", volumes=docker_volumes)
# There doesn't seem to be a way to get an exit code from docker.run() # There doesn't seem to be a way to get an exit code from docker.run()
return (docker_output, 0) return (docker_output, 0)

14
cli.py
View File

@ -16,6 +16,7 @@
import click import click
from dataclasses import dataclass from dataclasses import dataclass
from app.command_types import CommandOptions
from app import setup_repositories from app import setup_repositories
from app import build_containers from app import build_containers
from app import build_npms from app import build_npms
@ -26,17 +27,6 @@ from app import deployment
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@dataclass
class Options:
stack: str
quiet: bool = False
verbose: bool = False
dry_run: bool = False
local_stack: bool = False
debug: bool = False
continue_on_error: bool = False
@click.group(context_settings=CONTEXT_SETTINGS) @click.group(context_settings=CONTEXT_SETTINGS)
@click.option('--stack', help="specify a stack to build/deploy") @click.option('--stack', help="specify a stack to build/deploy")
@click.option('--quiet', is_flag=True, default=False) @click.option('--quiet', is_flag=True, default=False)
@ -49,7 +39,7 @@ class Options:
@click.pass_context @click.pass_context
def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error): def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error):
"""Laconic Stack Orchestrator""" """Laconic Stack Orchestrator"""
ctx.obj = Options(stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error) ctx.obj = CommandOptions(stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error)
cli.add_command(setup_repositories.command, "setup-repositories") cli.add_command(setup_repositories.command, "setup-repositories")

View File

@ -6,3 +6,4 @@ click>=8.1.6
PyYAML>=6.0.1 PyYAML>=6.0.1
ruamel.yaml>=0.17.32 ruamel.yaml>=0.17.32
pydantic==1.10.9 pydantic==1.10.9
tomli==2.0.1