From 86ffad86c7ce424435f02fad2064a1e98bda0d83 Mon Sep 17 00:00:00 2001 From: Daniel Roythorne Date: Thu, 7 Apr 2022 13:29:54 +0100 Subject: [PATCH] Added port subcommand. --- podman_compose.py | 54 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 5e4b52c..4a78f12 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -12,6 +12,7 @@ import sys import os import getpass import argparse +import itertools import subprocess import time import re @@ -913,9 +914,6 @@ def container_to_args(compose, cnt, detached=True): if is_str(entrypoint): entrypoint = shlex.split(entrypoint) podman_args.extend(["--entrypoint", json.dumps(entrypoint)]) - platform = cnt.get("platform", None) - if platform is not None: - podman_args.extend(["--platform", platform]) # WIP: healthchecks are still work in progress healthcheck = cnt.get("healthcheck", None) or {} @@ -2296,11 +2294,37 @@ def compose_logs(compose, args): @cmd_run(podman_compose, "config", "displays the compose file") def compose_config(compose, args): if args.services: - for service in compose.services: - print(service) + for service in compose.services: print(service) return print(compose.merged_yaml) +@cmd_run( + podman_compose, "port", "Prints the public port for a port binding." +) +def compose_port(compose, args): + #TODO - deal with pod index + compose.assert_services(args.service) + containers = compose.container_names_by_service[args.service] + container_ports = list(itertools.chain(*(compose.container_by_name[c]["ports"] for c in containers))) + + def _published_target(port_string): + published, target = port_string.split(':')[-2:] + return int(published), int(target) + + select_udp = (args.protocol == "udp") + published, target = None, None + for p in container_ports: + is_udp = (p[-4:] == "/udp") + + if select_udp and is_udp: + published, target = _published_target(p[-4:]) + if not select_udp and not is_udp: + published, target = _published_target(p) + + if target == args.private_port: + print(published) + return + ################### # command arguments parsing @@ -2682,9 +2706,27 @@ def compose_build_parse(parser): @cmd_parse(podman_compose, "config") def compose_config_parse(parser): parser.add_argument( - "--services", help="Print the service names, one per line.", action="store_true" + "--services", + help="Print the service names, one per line.", + action="store_true" ) +@cmd_parse(podman_compose, "port") +def compose_port_parse(parser): + parser.add_argument( + "--index", + type=int, + default=1, + help="index of the container if there are multiple instances of a service", + ) + parser.add_argument( + "--protocol", + choices=["tcp", "udp"], + default="tcp", + help="tcp or udp", + ) + parser.add_argument("service", metavar="service", nargs=None, help="service name") + parser.add_argument("private_port", metavar="private_port", nargs=None, type=int, help="private port") def main(): podman_compose.run()