pipe into main python script

This commit is contained in:
Daniel Langbein 2022-04-06 15:08:34 +02:00
parent 82bb71a35a
commit 6caaecb42b
2 changed files with 43 additions and 15 deletions

View File

@ -2,13 +2,33 @@
## Usage ## Usage
usage: `./main.py <docker-container-name> [<ssh-hostname>]` Usage: `<write-log-to-stdout> | ./main.py`
Example: - Example: Pipe docker log from remote computer into `main.py`
```bash ```bash
(venv-310) [user@linux snoflake-stats]$ ./main.py snowflake-proxy root_at_my_server (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 <docker-container-name> [<ssh-hostname>]`
- 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 ## Example output

28
main.py
View File

@ -8,17 +8,11 @@ import exec
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) > 1:
print(f'usage: {sys.argv[0]} <docker-container-name> [<ssh-hostname>]', file=sys.stderr) log = get_docker_log()
exit(1) 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() filtered: List[str] = [line for line in log.splitlines()
if Throughput.PATTERN in line] if Throughput.PATTERN in line]
# filtered = filtered_example() # 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]} <docker-container-name> [<ssh-hostname>]', 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: def docker_logs(container_name: str, ssh_host: str = None) -> str:
return exec.execute_and_capture(['docker', 'logs', container_name], ssh_host, 'stderr') return exec.execute_and_capture(['docker', 'logs', container_name], ssh_host, 'stderr')