From 6caaecb42b61ff12dbd4417088ae5fc63e55e48b Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 6 Apr 2022 15:08:34 +0200 Subject: [PATCH] pipe into main python script --- README.md | 30 +++++++++++++++++++++++++----- main.py | 28 ++++++++++++++++++---------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 94d067c..bfe3638 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,33 @@ ## Usage -usage: `./main.py []` +Usage: ` | ./main.py` -Example: +- Example: Pipe docker log from remote computer into `main.py` -```bash -(venv-310) [user@linux snoflake-stats]$ ./main.py snowflake-proxy root_at_my_server -``` + ```bash + (venv-310) [user@linux snoflake-stats]$ ssh root_at_my_server 'docker logs snowflake-proxy' 2>&1 | ./main.py + ``` + +- Example: Pipe logfile into `main.py` + + ```bash + (venv-310) [user@linux snoflake-stats]$ cat snowflake.log | ./main.py + ``` + +Usage: `./main.py []` + +- Example: Specify name of local docker container + + ```bash + (venv-310) [user@linux snoflake-stats]$ ./main.py snowflake-proxy + ``` + +- Example: Docker container name and ssh hostname + + ```bash + (venv-310) [user@linux snoflake-stats]$ ./main.py snowflake-proxy root_at_my_server + ``` ## Example output diff --git a/main.py b/main.py index 9eff0c6..1aa3a12 100755 --- a/main.py +++ b/main.py @@ -8,17 +8,11 @@ import exec def main(): - if len(sys.argv) < 2: - print(f'usage: {sys.argv[0]} []', file=sys.stderr) - exit(1) + if len(sys.argv) > 1: + log = get_docker_log() + else: + log = sys.stdin.read() - container_name = sys.argv[1] - - ssh_hostname = None - if len(sys.argv) > 2: - ssh_hostname = sys.argv[2] - - log: str = docker_logs(container_name, ssh_hostname) filtered: List[str] = [line for line in log.splitlines() if Throughput.PATTERN in line] # filtered = filtered_example() @@ -46,6 +40,20 @@ def filtered_example() -> List[str]: ] +def get_docker_log() -> str: + if len(sys.argv) < 2: + print(f'usage: {sys.argv[0]} []', file=sys.stderr) + exit(1) + + container_name = sys.argv[1] + + ssh_hostname = None + if len(sys.argv) > 2: + ssh_hostname = sys.argv[2] + + return docker_logs(container_name, ssh_hostname) + + def docker_logs(container_name: str, ssh_host: str = None) -> str: return exec.execute_and_capture(['docker', 'logs', container_name], ssh_host, 'stderr')