Major improvements in error handling, & small fix

This commit is contained in:
askiiart 2022-12-15 17:38:51 -06:00
parent 16632f933b
commit 1e05dd2cd6

View file

@ -2,9 +2,11 @@ from subprocess import getoutput, getstatusoutput
import pprint
import os
class NoContainersError(Exception):
pass
class Docker:
@staticmethod
def running_containers_info():
@ -14,12 +16,13 @@ class Docker:
"""
raw_info = getoutput('docker ps')
if '\n' not in raw_info:
raise(NoContainersError('A running Docker container is required to run this program. Please run a docker container and try again.'))
raise (NoContainersError(
'A running Docker container is required to run this program. Please run a docker container and try again.'))
# 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')}
'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:]
@ -45,7 +48,6 @@ class Docker:
return info
@staticmethod
def containers():
"""
@ -54,7 +56,8 @@ class Docker:
"""
raw_info = getoutput('docker container list')
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.'))
raise (NoContainersError(
'A Docker container is required to run this program. Please create a docker container and try again.'))
# Remove header
raw_info = raw_info[raw_info.find('\n')+1:]
info = {}
@ -74,12 +77,13 @@ class Docker:
"""
raw_info = getoutput('docker ps -a')
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.'))
raise (NoContainersError(
'A Docker container is required to run this program. Please create a docker container and try again.'))
# 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')}
'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:]
@ -129,7 +133,7 @@ class Docker:
"""
cwd = os.getcwd()
os.chdir(dir)
status = getstatusoutput('docker compose up -detach --build --remove-orphans')
status = getstatusoutput('docker compose up --detach --build --remove-orphans')
os.chdir(cwd)
return status
@ -169,6 +173,24 @@ class Docker:
"""
return getstatusoutput(f'docker rm {container}')[0]
@staticmethod
def containers_exist():
"""
Checks if any containers exist
:returns:
bool: True if containers exist, False otherwise
"""
return True if '\n' in getoutput('docker ps -a') else False
def aliens_exist():
"""
Checks if aliens exist
:returns:
bool: Up all night long; And there's something very wrong
"""
return True if 'blink-182' in 'your playlist' else False
if __name__ == '__main__':
try: