From 0ecf09e35bac51c486363b516470e1bc198d14ff Mon Sep 17 00:00:00 2001 From: --global <--global> Date: Thu, 15 Dec 2022 09:20:24 -0600 Subject: [PATCH] Fix major stuff/moved stuff to docker_parser.py --- README.md | 4 +++- basic_management.py | 39 +-------------------------------------- docker_parser.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 561c915..f75d0fd 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,6 @@ Notes: ## Status - `composer.py` and `re_compose` are finished -- Haven't started on `basic_management.py` +- Redoing stuff as a new class `docker_parser.py` + - ps() is done + - container_info() still a WIP diff --git a/basic_management.py b/basic_management.py index b687fea..efcf6d6 100644 --- a/basic_management.py +++ b/basic_management.py @@ -3,44 +3,7 @@ from subprocess import getoutput # Goals: Loop: Select container (or exit), then menu to do stuff to container (start, stop, rm, list info, go back to main menu) -def docker_info(): - """ - Gets info about all the Docker containers - :return: Nested dict of containers info - """ - raw_info = getoutput('docker ps') - # Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces) - header = raw_info[:raw_info.find('\n')+1] - header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'), \ - 'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'), \ - 'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')} - - # Remove header (example above) - raw_info = raw_info[raw_info.find('\n')+1:] - info = {} - - # Find container names - containers = [] - for line in raw_info.split('\n'): - containers.append(line.strip()[line.strip().rfind(' ')+1:]) - - # Fill in info - for i in range(len(containers)): - info[containers[i]] = {} - header_indices_keys = list(header_indices) - for j in range(len(header_indices_keys)): # TODO: Fix - start_i = header_indices[header_indices_keys[j]] - if j+1 != len(header_indices): - end_i = header_indices[header_indices_keys[j+1]] - else: - end_i = len(header) - info[containers[i]][header_indices[header_indices_keys[j]]] = \ - raw_info.split('\n')[i][start_i:end_i].strip() - - return info - - if '\n' not in getoutput('docker ps'): print('No containers running') exit() -docker_info() +ps() diff --git a/docker_parser.py b/docker_parser.py index b50c0dd..9eebc1b 100644 --- a/docker_parser.py +++ b/docker_parser.py @@ -5,6 +5,44 @@ class NoContainersError(Exception): pass class DockerParser: + def ps(raw_info='docker ps'): + """ + Gets info about all running Docker containers from docker ps + :return: Nested dict of containers info + """ + if '\n' not in raw_info: + raise(NoContainersError('A Docker container is required to run this program. Please create a docker container and try again.')) + raw_info = getoutput('docker ps') + # Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces) + header = raw_info[:raw_info.find('\n')+1] + header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'), + 'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'), + 'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')} + + # Remove header (example above) + raw_info = raw_info[raw_info.find('\n')+1:] + info = {} + + # Find container names + containers = [] + for line in raw_info.split('\n'): + containers.append(line.strip()[line.strip().rfind(' ')+1:]) + + # Fill in info + for i in range(len(containers)): + info[containers[i]] = {} + header_indices_keys = list(header_indices) + for j in range(len(header_indices_keys)): # TODO: Fix + start_i = header_indices[header_indices_keys[j]] + if j+1 != len(header_indices): + end_i = header_indices[header_indices_keys[j+1]] + else: + end_i = len(header) + info[containers[i]][header_indices[header_indices_keys[j]]] = \ + raw_info.split('\n')[i][start_i:end_i].strip() + + return info + def containers_info(raw_info=getoutput('docker container list')): """ Gets info about all the Docker containers