Not super polished, but EVERYTHING WORKS!

This commit is contained in:
askiiart 2022-12-15 16:08:26 -06:00
parent 4474ba5e29
commit 6a61684a80
4 changed files with 41 additions and 34 deletions

View file

@ -1,7 +1,6 @@
from docker_wrapper import Docker
from pprint import pprint
# Goals: Loop: Select container (or exit), then menu to do stuff to container (start, stop, rm, list info, go back to main menu)
while True:
# Main Menu
print('Select the container to manage:')
@ -10,46 +9,43 @@ while True:
print(f' {i} - {containers[i]}')
print(' q - quit')
selection = input()
container_i = input()
print()
if selection == 'q':
if container_i == 'q':
exit(0)
while True:
# Container Menu
container = containers[int(selection)]
container = containers[int(container_i)]
print()
print(f'You selected {container}. What would you like to do with it?')
print(f' view - View container info - (docker {container} list)')
print(f' view - View container info - (docker ps -a)')
print(f' start - Starts the container (docker start {container})')
print(f' stop - Stops the container (docker stop {container}')
print(f' rm - Deletes the container (docker rm {container})')
print( ' menu - Back to the main menu')
selection = input()
print()
if selection == 'menu':
break
elif selection == 'view':
pprint(Docker.container_info(container)) # TODO: Make better, custom printer later
pprint(Docker.container_info(container)) # TODO: Make better, custom printer
elif selection == 'start':
print('Starting...')
status = Docker.start(container)
if status == 0:
print(f'{container} started successfully')
else:
print(f'{container} did NOT start successfully. Exit code: {status}')
elif selection == 'stop':
print('Stopping...')
status = Docker.stop(container)
if status == 0:
print(f'{container} stopped successfully')
else:
print(f'{container} was NOT stopped successfully. Exit code: {status}')
print('Done.')
elif selection == 'rm':
break_later = False
while selection != 'y' and selection != 'n' and selection != 'Y' and selection != 'N':
print(f'WARNING! This will DELETE {container}!')
print(f'Are you absolutely sure you want to delete {container}? (y/N)')
@ -57,11 +53,11 @@ while True:
if selection == 'y' or selection == 'Y':
print('Deleting...')
status = Docker.rm(container)
if status == 0:
print(f'{container} has been deleted')
print('Done')
break_later = True
elif selection == 'n' or selection == 'N':
print(f'Operation cancelled, returning to {container} menu...')
if break_later:
break
elif selection == 'menu':

View file

@ -24,18 +24,14 @@ for dir in os.listdir(compose_path):
containers = []
for dir in compose_dirs:
containers.append()
container = dir[:-1]
container = container[container.rfind('/')+1:]
containers.append(container)
# COMPOSE!
for i in range(len(compose_dirs)):
dir = compose_dirs[i]
container = containers[i]
status = Docker.stop(container) # Assigned to vars so I can see
if status != 0:
print(f'Error in stopping {container}, skipping...')
status = Docker.rm(container)
if status != 0:
print(f'Error in removing {container}, skipping...')
status = Docker.compose(dir)
if status != 0:
print(f'Error in composing {container}, skipping...')

View file

@ -1,2 +1,2 @@
compose-path=docker-data/
exclude-containers=pihole
exclude-containers=wg-easy,pihole

View file

@ -6,13 +6,15 @@ class NoContainersError(Exception):
pass
class Docker:
def ps(raw_info=getoutput('docker ps')):
@staticmethod
def running_containers_info():
"""
Gets info about all running Docker containers from docker ps
:return: Nested dict of containers info
"""
raw_info = getoutput('docker ps')
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 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'),
@ -32,7 +34,7 @@ class Docker:
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
for j in range(len(header_indices_keys)):
start_i = header_indices[header_indices_keys[j]]
if j+1 != len(header_indices):
end_i = header_indices[header_indices_keys[j+1]]
@ -43,10 +45,16 @@ class Docker:
return info
def containers(raw_info=getoutput('docker container list')):
@staticmethod
def containers():
"""
:return: A list of docker containers
:returns:
int: A list of docker containers
"""
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.'))
# Remove header
raw_info = raw_info[raw_info.find('\n')+1:]
info = {}
@ -58,11 +66,13 @@ class Docker:
return containers
def all_containers_info(raw_info=getoutput('docker container list')):
@staticmethod
def all_containers_info():
"""
Gets info about all the Docker containers
:return: Nested dict of containers info
"""
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.'))
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
@ -84,7 +94,7 @@ class Docker:
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
for j in range(len(header_indices_keys)):
start_i = header_indices[header_indices_keys[j]]
if j+1 != len(header_indices):
end_i = header_indices[header_indices_keys[j+1]]
@ -95,6 +105,7 @@ class Docker:
return info
@staticmethod
def container_info(container):
"""
Returns the info about a given container
@ -106,6 +117,7 @@ class Docker:
"""
return Docker.all_containers_info()[container]
@staticmethod
def compose(dir):
"""
Composes whatever is in dir
@ -121,6 +133,7 @@ class Docker:
os.chdir(cwd)
return status
@staticmethod
def start(container):
"""
Starts a container
@ -132,6 +145,7 @@ class Docker:
"""
return getstatusoutput(f'docker start {container}')[0]
@staticmethod
def stop(container):
"""
Stops a container
@ -143,6 +157,7 @@ class Docker:
"""
return getstatusoutput(f'docker stop {container}')[0]
@staticmethod
def rm(container):
"""
Deletes a container