Merge pull request #1 from askiiart/dev

Prepare for stable release
This commit is contained in:
askiiart 2022-12-15 17:46:55 -06:00 committed by GitHub
commit 6847661022
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 79 deletions

View file

@ -1,69 +1,107 @@
from docker_wrapper import Docker from docker_wrapper import Docker, NoContainersError
from pprint import pprint from subprocess import getoutput
while True:
# Main Menu
print('Select the container to manage:')
containers = Docker.containers()
for i in range(len(containers)):
print(f' {i} - {containers[i]}')
print(' q - quit')
container_i = input() def container_to_str(container):
print() """
Returns info about a Docker container as a string
:parameters:f
container: The name of the container (str)
if container_i == 'q': :returns:
exit(0) str: The container info as a string, formatted for printing
"""
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
info = Docker.container_info(container)
info_str = f'{container}\n'
info_str += f' CONTAINER ID: {info["CONTAINER ID"]}\n'
info_str += f' IMAGE: {info["IMAGE"]}\n'
info_str += f' COMMAND: {info["COMMAND"]}\n'
info_str += f' CREATED: {info["CREATED"]}\n'
info_str += f' STATUS: {info["STATUS"]}\n'
info_str += f' PORTS: {info["PORTS"]}\n'
info_str += f' NAMES: {info["NAMES"]}'
return info_str
try:
while True: while True:
# Container Menu if not Docker.containers_exist():
container = containers[int(container_i)] raise(NoContainersError('No containers exist! Please create a container and try again.'))
print()
print(f'You selected {container}. What would you like to do with it?')
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() # Main Menu
print('Select the container to manage:')
containers = Docker.containers()
for i in range(len(containers)):
print(f' {i} - {containers[i]}')
print(' q - Quit')
container_i = input()
print() print()
if selection == 'menu': if container_i == 'q':
break exit(0)
elif selection == 'view': while True:
pprint(Docker.container_info(container)) # TODO: Make better, custom printer if not Docker.containers_exist():
raise(NoContainersError('No containers exist! Please create a container and try again.'))
# Container Menu
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 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')
elif selection == 'start': selection = input()
print('Starting...') print()
status = Docker.start(container)
elif selection == 'stop': if selection == 'menu':
print('Stopping...')
status = Docker.stop(container)
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)')
selection = input()
if selection == 'y' or selection == 'Y':
print('Deleting...')
status = Docker.rm(container)
print('Done')
break_later = True
elif selection == 'n' or selection == 'N':
print(f'Operation cancelled, returning to {container} menu...')
if break_later:
break break
elif selection == 'menu': elif selection == 'view':
print('Returning to main menu...') print(container_to_str(container))
break
else: elif selection == 'start':
print('Selection invalid. Please try again.') print('Starting...')
print() Docker.start(container)
elif selection == 'stop':
print('Stopping...')
Docker.stop(container)
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)')
selection = input()
if selection == 'y' or selection == 'Y':
print('Stopping...')
Docker.stop(container)
print('Deleting...')
Docker.rm(container)
print('Done')
break_later = True
elif selection == 'n' or selection == 'N':
print(f'Operation cancelled, returning to {container} menu...')
if break_later:
print()
break
elif selection == 'menu':
print('Returning to main menu...')
break
else:
print('Selection invalid. Please try again.')
print()
except NoContainersError as e:
print('Error:', e)
print('No containers found. Exiting...')
exit()

View file

@ -1,5 +1,4 @@
import os import os
from subprocess import getoutput
from docker_wrapper import Docker from docker_wrapper import Docker
# Read config file and make variables # Read config file and make variables
@ -32,6 +31,4 @@ for dir in compose_dirs:
for i in range(len(compose_dirs)): for i in range(len(compose_dirs)):
dir = compose_dirs[i] dir = compose_dirs[i]
container = containers[i] container = containers[i]
status = Docker.stop(container) # Assigned to vars so I can see Docker.compose(dir)
status = Docker.rm(container)
status = Docker.compose(dir)

View file

@ -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:]
@ -45,7 +48,6 @@ class Docker:
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 = {}
@ -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:]
@ -129,7 +133,7 @@ class Docker:
""" """
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(dir) os.chdir(dir)
status = getstatusoutput('docker compose up -d') status = getstatusoutput('docker compose up --detach --build --remove-orphans')
os.chdir(cwd) os.chdir(cwd)
return status return status
@ -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:

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
os
subprocess