Major improvements in error handling, & small fix
This commit is contained in:
parent
16632f933b
commit
1e05dd2cd6
1 changed files with 38 additions and 16 deletions
|
@ -2,9 +2,11 @@ from subprocess import getoutput, getstatusoutput
|
||||||
import pprint
|
import pprint
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class NoContainersError(Exception):
|
class NoContainersError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Docker:
|
class Docker:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def running_containers_info():
|
def running_containers_info():
|
||||||
|
@ -14,12 +16,13 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
raw_info = getoutput('docker ps')
|
raw_info = getoutput('docker ps')
|
||||||
if '\n' not in raw_info:
|
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: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
|
||||||
header = raw_info[:raw_info.find('\n')+1]
|
header = raw_info[:raw_info.find('\n')+1]
|
||||||
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),
|
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),
|
||||||
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'),
|
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'),
|
||||||
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
|
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
|
||||||
|
|
||||||
# Remove header (example above)
|
# Remove header (example above)
|
||||||
raw_info = raw_info[raw_info.find('\n')+1:]
|
raw_info = raw_info[raw_info.find('\n')+1:]
|
||||||
|
@ -44,8 +47,7 @@ class Docker:
|
||||||
raw_info.split('\n')[i][start_i:end_i].strip()
|
raw_info.split('\n')[i][start_i:end_i].strip()
|
||||||
|
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def containers():
|
def containers():
|
||||||
"""
|
"""
|
||||||
|
@ -54,7 +56,8 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
raw_info = getoutput('docker container list')
|
raw_info = getoutput('docker container list')
|
||||||
if '\n' not in raw_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.'))
|
raise (NoContainersError(
|
||||||
|
'A Docker container is required to run this program. Please create a docker container and try again.'))
|
||||||
# Remove header
|
# Remove header
|
||||||
raw_info = raw_info[raw_info.find('\n')+1:]
|
raw_info = raw_info[raw_info.find('\n')+1:]
|
||||||
info = {}
|
info = {}
|
||||||
|
@ -63,9 +66,9 @@ class Docker:
|
||||||
containers = []
|
containers = []
|
||||||
for line in raw_info.split('\n'):
|
for line in raw_info.split('\n'):
|
||||||
containers.append(line.strip()[line.strip().rfind(' ')+1:])
|
containers.append(line.strip()[line.strip().rfind(' ')+1:])
|
||||||
|
|
||||||
return containers
|
return containers
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def all_containers_info():
|
def all_containers_info():
|
||||||
"""
|
"""
|
||||||
|
@ -74,12 +77,13 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
raw_info = getoutput('docker ps -a')
|
raw_info = getoutput('docker ps -a')
|
||||||
if '\n' not in raw_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.'))
|
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: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
|
||||||
header = raw_info[:raw_info.find('\n')+1]
|
header = raw_info[:raw_info.find('\n')+1]
|
||||||
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),
|
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),
|
||||||
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'),
|
'COMMAND': header.find('COMMAND'), 'CREATED': header.find('CREATED'), 'STATUS': header.find('STATUS'),
|
||||||
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
|
'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
|
||||||
|
|
||||||
# Remove header (example above)
|
# Remove header (example above)
|
||||||
raw_info = raw_info[raw_info.find('\n')+1:]
|
raw_info = raw_info[raw_info.find('\n')+1:]
|
||||||
|
@ -116,7 +120,7 @@ class Docker:
|
||||||
dict: The info about the container
|
dict: The info about the container
|
||||||
"""
|
"""
|
||||||
return Docker.all_containers_info()[container]
|
return Docker.all_containers_info()[container]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compose(dir):
|
def compose(dir):
|
||||||
"""
|
"""
|
||||||
|
@ -129,10 +133,10 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
os.chdir(dir)
|
os.chdir(dir)
|
||||||
status = getstatusoutput('docker compose up -detach --build --remove-orphans')
|
status = getstatusoutput('docker compose up --detach --build --remove-orphans')
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
return status
|
return status
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def start(container):
|
def start(container):
|
||||||
"""
|
"""
|
||||||
|
@ -144,7 +148,7 @@ class Docker:
|
||||||
int: The exit code of docker start
|
int: The exit code of docker start
|
||||||
"""
|
"""
|
||||||
return getstatusoutput(f'docker start {container}')[0]
|
return getstatusoutput(f'docker start {container}')[0]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def stop(container):
|
def stop(container):
|
||||||
"""
|
"""
|
||||||
|
@ -156,7 +160,7 @@ class Docker:
|
||||||
int: The exit code of docker stop
|
int: The exit code of docker stop
|
||||||
"""
|
"""
|
||||||
return getstatusoutput(f'docker stop {container}')[0]
|
return getstatusoutput(f'docker stop {container}')[0]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rm(container):
|
def rm(container):
|
||||||
"""
|
"""
|
||||||
|
@ -169,6 +173,24 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
return getstatusoutput(f'docker rm {container}')[0]
|
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__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue