From 0a80e3d8703bf0201aaadb09bab69a7afaa0a6b9 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 4 Sep 2023 11:54:21 -0600 Subject: [PATCH 1/3] Implement --map-ports-to-host feature --- .../docker-compose-fixturenet-laconicd.yml | 2 +- .../docker-compose-mainnet-laconicd.yml | 2 +- .../stacks/mainnet-laconic/deploy/commands.py | 4 -- app/data/stacks/test/deploy/commands.py | 4 -- app/deployment_create.py | 46 +++++++++++++++++-- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/app/data/compose/docker-compose-fixturenet-laconicd.yml b/app/data/compose/docker-compose-fixturenet-laconicd.yml index 5037687c..641229d4 100644 --- a/app/data/compose/docker-compose-fixturenet-laconicd.yml +++ b/app/data/compose/docker-compose-fixturenet-laconicd.yml @@ -15,7 +15,7 @@ services: - "6060" - "26657" - "26656" - - "9473:9473" + - "9473" - "8545" - "8546" - "9090" diff --git a/app/data/compose/docker-compose-mainnet-laconicd.yml b/app/data/compose/docker-compose-mainnet-laconicd.yml index d42c6a7c..ff2f3376 100644 --- a/app/data/compose/docker-compose-mainnet-laconicd.yml +++ b/app/data/compose/docker-compose-mainnet-laconicd.yml @@ -17,7 +17,7 @@ services: - "6060" - "26657" - "26656" - - "9473:9473" + - "9473" - "8545" - "8546" - "9090" diff --git a/app/data/stacks/mainnet-laconic/deploy/commands.py b/app/data/stacks/mainnet-laconic/deploy/commands.py index 3503b821..31c48662 100644 --- a/app/data/stacks/mainnet-laconic/deploy/commands.py +++ b/app/data/stacks/mainnet-laconic/deploy/commands.py @@ -32,9 +32,6 @@ default_spec_file_content = """config: chain_id: my-chain-id """ -init_help_text = """Add helpful text here on setting config variables. -""" - class SetupPhase(Enum): INITIALIZE = 1 @@ -275,7 +272,6 @@ def create(command_context: DeployCommandContext, extra_args): def init(command_context: DeployCommandContext): - print(init_help_text) yaml = get_yaml() return yaml.load(default_spec_file_content) diff --git a/app/data/stacks/test/deploy/commands.py b/app/data/stacks/test/deploy/commands.py index fc9c4502..3e90e1ef 100644 --- a/app/data/stacks/test/deploy/commands.py +++ b/app/data/stacks/test/deploy/commands.py @@ -23,9 +23,6 @@ default_spec_file_content = """config: config_variable: test-value """ -init_help_text = """Add helpful text here on setting config variables. -""" - # Output a known string to a know file in the bind mounted directory ./container-output-dir # for test purposes -- test checks that the file was written. @@ -40,7 +37,6 @@ def setup(command_context: DeployCommandContext, parameters, extra_args): def init(command_context: DeployCommandContext): - print(init_help_text) yaml = get_yaml() return yaml.load(default_spec_file_content) diff --git a/app/deployment_create.py b/app/deployment_create.py index 1338bc6e..63b3421b 100644 --- a/app/deployment_create.py +++ b/app/deployment_create.py @@ -17,6 +17,7 @@ import click from importlib import util import os from pathlib import Path +import random from shutil import copyfile, copytree import sys from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml @@ -166,10 +167,48 @@ def _find_extra_config_dirs(parsed_pod_file, pod): return config_dirs +def _get_mapped_ports(stack: str, map_recipe: str): + port_map_recipes = ["any-variable-random", "localhost-same", "any-same", "localhost-fixed-random", "any-fixed-random"] + ports = _get_ports(stack) + if ports: + # Implement any requested mapping recipe + if map_recipe: + if map_recipe in port_map_recipes: + for service in ports.keys(): + ports_array = ports[service] + for x in range(0, len(ports_array)): + orig_port = ports_array[x] + random_port = random.randint(20000,50000) # Beware: we're relying on luck to not collide + if map_recipe == "any-variable-random": + # This is the default so take no action + pass + elif map_recipe == "localhost-same": + # Replace instances of "- XX" with "- 127.0.0.1:XX" + ports_array[x] = f"127.0.0.1:{orig_port}:{orig_port}" + elif map_recipe == "any-same": + # Replace instances of "- XX" with "- 0.0.0.0:XX" + ports_array[x] = f"0.0.0.0:{orig_port}:{orig_port}" + elif map_recipe == "localhost-fixed-random": + # Replace instances of "- XX" with "- 127.0.0.1::XX" + ports_array[x] = f"127.0.0.1:{random_port}:{orig_port}" + elif map_recipe == "any-fixed-random": + # Replace instances of "- XX" with "- 0.0.0.0::XX" + ports_array[x] = f"0.0.0.0:{random_port}:{orig_port}" + else: + print("Error: bad map_recipe") + else: + print(f"Error: --map-ports-to-host must specify one of: {port_map_recipes}") + sys.exit(1) + return ports + + + @click.command() @click.option("--output", required=True, help="Write yaml spec file here") +@click.option("--map-ports-to-host", required=False, + help="Map ports to the host as one of: any-variable-random (default), localhost-same, any-same, localhost-fixed-random, any-fixed-random") @click.pass_context -def init(ctx, output): +def init(ctx, output, map_ports_to_host): yaml = get_yaml() stack = global_options(ctx).stack verbose = global_options(ctx).verbose @@ -180,9 +219,8 @@ def init(ctx, output): if verbose: print(f"Creating spec file for stack: {stack}") - ports = _get_ports(stack) - if ports: - spec_file_content["ports"] = ports + ports = _get_mapped_ports(stack, map_ports_to_host) + spec_file_content["ports"] = ports named_volumes = _get_named_volumes(stack) if named_volumes: -- 2.45.2 From 20e976a6f6175f14734793bc25c6227175615baa Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 4 Sep 2023 12:08:09 -0600 Subject: [PATCH 2/3] Update port mapping documentation --- app/data/stacks/mainnet-eth/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/data/stacks/mainnet-eth/README.md b/app/data/stacks/mainnet-eth/README.md index 23c8d325..42787d20 100644 --- a/app/data/stacks/mainnet-eth/README.md +++ b/app/data/stacks/mainnet-eth/README.md @@ -73,7 +73,7 @@ After deleting the volumes, any subsequent re-start will begin chain sync from c ## Ports It is usually necessary to expose certain container ports on one or more the host's addresses to allow incoming connections. -Any ports defined in the Docker compose file are exposed by default with random port assignments, but the values can be +Any ports defined in the Docker compose file are exposed by default with random port assignments, bound to "any" interface (IP address 0.0.0.0), but the port mappings can be customized by editing the "spec" file generated by `laconic-so deploy init`. In this example, ports `8545` and `5052` have been assigned to a specific addresses/port combination on the host, while @@ -92,7 +92,15 @@ volumes: mainnet_eth_geth_1_data: ./data/mainnet_eth_geth_1_data mainnet_eth_lighthouse_1_data: ./data/mainnet_eth_lighthouse_1_data ``` - +In addition, a stack-wide port mapping "recipe" can be applied at the time the +`laconic-so deploy init` command is run, by supplying the desired recipe with the `--map-ports-to-host` option. The following recipies are supported: +| Recipe | Host Port Mapping | +|--------|-------------------| +| any-variable-random | Bind to 0.0.0.0 using a random port assigned at start time (default) | +| localhost-same | Bind to 127.0.0.1 using the same port number as exposed by the containers | +| any-same | Bind to 0.0.0.0 using the same port number as exposed by the containers | +| localhost-fixed-random | Bind to 127.0.0.1 using a random port number selected at the time the command is run (not checked for already in use)| +| any-fixed-random | Bind to 0.0.0.0 using a random port number selected at the time the command is run (not checked for already in use) | ## Data volumes Container data volumes are bind-mounted to specified paths in the host filesystem. The default setup (generated by `laconic-so deploy init`) places the volumes in the `./data` subdirectory of the deployment directory: -- 2.45.2 From 9dfffd0d9935b5023149154ba612b841e022d510 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 4 Sep 2023 12:10:09 -0600 Subject: [PATCH 3/3] Typo --- app/data/stacks/mainnet-eth/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/data/stacks/mainnet-eth/README.md b/app/data/stacks/mainnet-eth/README.md index 42787d20..79a3e788 100644 --- a/app/data/stacks/mainnet-eth/README.md +++ b/app/data/stacks/mainnet-eth/README.md @@ -93,7 +93,7 @@ volumes: mainnet_eth_lighthouse_1_data: ./data/mainnet_eth_lighthouse_1_data ``` In addition, a stack-wide port mapping "recipe" can be applied at the time the -`laconic-so deploy init` command is run, by supplying the desired recipe with the `--map-ports-to-host` option. The following recipies are supported: +`laconic-so deploy init` command is run, by supplying the desired recipe with the `--map-ports-to-host` option. The following recipes are supported: | Recipe | Host Port Mapping | |--------|-------------------| | any-variable-random | Bind to 0.0.0.0 using a random port assigned at start time (default) | -- 2.45.2