Not super polished, but EVERYTHING WORKS!
This commit is contained in:
parent
4474ba5e29
commit
6a61684a80
4 changed files with 41 additions and 34 deletions
|
@ -1,7 +1,6 @@
|
||||||
from docker_wrapper import Docker
|
from docker_wrapper import Docker
|
||||||
from pprint import pprint
|
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:
|
while True:
|
||||||
# Main Menu
|
# Main Menu
|
||||||
print('Select the container to manage:')
|
print('Select the container to manage:')
|
||||||
|
@ -10,46 +9,43 @@ while True:
|
||||||
print(f' {i} - {containers[i]}')
|
print(f' {i} - {containers[i]}')
|
||||||
print(' q - quit')
|
print(' q - quit')
|
||||||
|
|
||||||
selection = input()
|
container_i = input()
|
||||||
print()
|
print()
|
||||||
|
|
||||||
if selection == 'q':
|
if container_i == 'q':
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Container Menu
|
# Container Menu
|
||||||
container = containers[int(selection)]
|
container = containers[int(container_i)]
|
||||||
print()
|
print()
|
||||||
print(f'You selected {container}. What would you like to do with it?')
|
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' start - Starts the container (docker start {container})')
|
||||||
print(f' stop - Stops the container (docker stop {container}')
|
print(f' stop - Stops the container (docker stop {container}')
|
||||||
print(f' rm - Deletes the container (docker rm {container})')
|
print(f' rm - Deletes the container (docker rm {container})')
|
||||||
print( ' menu - Back to the main menu')
|
print( ' menu - Back to the main menu')
|
||||||
|
|
||||||
selection = input()
|
selection = input()
|
||||||
|
print()
|
||||||
|
|
||||||
if selection == 'menu':
|
if selection == 'menu':
|
||||||
break
|
break
|
||||||
|
|
||||||
elif selection == 'view':
|
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':
|
elif selection == 'start':
|
||||||
|
print('Starting...')
|
||||||
status = Docker.start(container)
|
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':
|
elif selection == 'stop':
|
||||||
|
print('Stopping...')
|
||||||
status = Docker.stop(container)
|
status = Docker.stop(container)
|
||||||
if status == 0:
|
print('Done.')
|
||||||
print(f'{container} stopped successfully')
|
|
||||||
else:
|
|
||||||
print(f'{container} was NOT stopped successfully. Exit code: {status}')
|
|
||||||
|
|
||||||
elif selection == 'rm':
|
elif selection == 'rm':
|
||||||
|
break_later = False
|
||||||
while selection != 'y' and selection != 'n' and selection != 'Y' and selection != 'N':
|
while selection != 'y' and selection != 'n' and selection != 'Y' and selection != 'N':
|
||||||
print(f'WARNING! This will DELETE {container}!')
|
print(f'WARNING! This will DELETE {container}!')
|
||||||
print(f'Are you absolutely sure you want to delete {container}? (y/N)')
|
print(f'Are you absolutely sure you want to delete {container}? (y/N)')
|
||||||
|
@ -57,12 +53,12 @@ while True:
|
||||||
if selection == 'y' or selection == 'Y':
|
if selection == 'y' or selection == 'Y':
|
||||||
print('Deleting...')
|
print('Deleting...')
|
||||||
status = Docker.rm(container)
|
status = Docker.rm(container)
|
||||||
if status == 0:
|
|
||||||
print(f'{container} has been deleted')
|
|
||||||
print('Done')
|
print('Done')
|
||||||
|
break_later = True
|
||||||
elif selection == 'n' or selection == 'N':
|
elif selection == 'n' or selection == 'N':
|
||||||
print(f'Operation cancelled, returning to {container} menu...')
|
print(f'Operation cancelled, returning to {container} menu...')
|
||||||
break
|
if break_later:
|
||||||
|
break
|
||||||
|
|
||||||
elif selection == 'menu':
|
elif selection == 'menu':
|
||||||
print('Returning to main menu...')
|
print('Returning to main menu...')
|
||||||
|
|
10
composer.py
10
composer.py
|
@ -24,18 +24,14 @@ for dir in os.listdir(compose_path):
|
||||||
|
|
||||||
containers = []
|
containers = []
|
||||||
for dir in compose_dirs:
|
for dir in compose_dirs:
|
||||||
containers.append()
|
container = dir[:-1]
|
||||||
|
container = container[container.rfind('/')+1:]
|
||||||
|
containers.append(container)
|
||||||
|
|
||||||
# COMPOSE!
|
# COMPOSE!
|
||||||
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
|
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)
|
status = Docker.rm(container)
|
||||||
if status != 0:
|
|
||||||
print(f'Error in removing {container}, skipping...')
|
|
||||||
status = Docker.compose(dir)
|
status = Docker.compose(dir)
|
||||||
if status != 0:
|
|
||||||
print(f'Error in composing {container}, skipping...')
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
compose-path=docker-data/
|
compose-path=docker-data/
|
||||||
exclude-containers=pihole
|
exclude-containers=wg-easy,pihole
|
|
@ -6,13 +6,15 @@ class NoContainersError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Docker:
|
class Docker:
|
||||||
def ps(raw_info=getoutput('docker ps')):
|
@staticmethod
|
||||||
|
def running_containers_info():
|
||||||
"""
|
"""
|
||||||
Gets info about all running Docker containers from docker ps
|
Gets info about all running Docker containers from docker ps
|
||||||
:return: Nested dict of containers info
|
:return: Nested dict of containers info
|
||||||
"""
|
"""
|
||||||
|
raw_info = getoutput('docker ps')
|
||||||
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 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'),
|
||||||
|
@ -32,7 +34,7 @@ class Docker:
|
||||||
for i in range(len(containers)):
|
for i in range(len(containers)):
|
||||||
info[containers[i]] = {}
|
info[containers[i]] = {}
|
||||||
header_indices_keys = list(header_indices)
|
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]]
|
start_i = header_indices[header_indices_keys[j]]
|
||||||
if j+1 != len(header_indices):
|
if j+1 != len(header_indices):
|
||||||
end_i = header_indices[header_indices_keys[j+1]]
|
end_i = header_indices[header_indices_keys[j+1]]
|
||||||
|
@ -43,10 +45,16 @@ class Docker:
|
||||||
|
|
||||||
return info
|
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
|
# Remove header
|
||||||
raw_info = raw_info[raw_info.find('\n')+1:]
|
raw_info = raw_info[raw_info.find('\n')+1:]
|
||||||
info = {}
|
info = {}
|
||||||
|
@ -58,11 +66,13 @@ class Docker:
|
||||||
|
|
||||||
return containers
|
return containers
|
||||||
|
|
||||||
def all_containers_info(raw_info=getoutput('docker container list')):
|
@staticmethod
|
||||||
|
def all_containers_info():
|
||||||
"""
|
"""
|
||||||
Gets info about all the Docker containers
|
Gets info about all the Docker containers
|
||||||
:return: Nested dict of containers info
|
:return: Nested dict of containers info
|
||||||
"""
|
"""
|
||||||
|
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)
|
||||||
|
@ -84,7 +94,7 @@ class Docker:
|
||||||
for i in range(len(containers)):
|
for i in range(len(containers)):
|
||||||
info[containers[i]] = {}
|
info[containers[i]] = {}
|
||||||
header_indices_keys = list(header_indices)
|
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]]
|
start_i = header_indices[header_indices_keys[j]]
|
||||||
if j+1 != len(header_indices):
|
if j+1 != len(header_indices):
|
||||||
end_i = header_indices[header_indices_keys[j+1]]
|
end_i = header_indices[header_indices_keys[j+1]]
|
||||||
|
@ -95,6 +105,7 @@ class Docker:
|
||||||
|
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def container_info(container):
|
def container_info(container):
|
||||||
"""
|
"""
|
||||||
Returns the info about a given container
|
Returns the info about a given container
|
||||||
|
@ -106,6 +117,7 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
return Docker.all_containers_info()[container]
|
return Docker.all_containers_info()[container]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def compose(dir):
|
def compose(dir):
|
||||||
"""
|
"""
|
||||||
Composes whatever is in dir
|
Composes whatever is in dir
|
||||||
|
@ -121,6 +133,7 @@ class Docker:
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def start(container):
|
def start(container):
|
||||||
"""
|
"""
|
||||||
Starts a container
|
Starts a container
|
||||||
|
@ -132,6 +145,7 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
return getstatusoutput(f'docker start {container}')[0]
|
return getstatusoutput(f'docker start {container}')[0]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def stop(container):
|
def stop(container):
|
||||||
"""
|
"""
|
||||||
Stops a container
|
Stops a container
|
||||||
|
@ -143,6 +157,7 @@ class Docker:
|
||||||
"""
|
"""
|
||||||
return getstatusoutput(f'docker stop {container}')[0]
|
return getstatusoutput(f'docker stop {container}')[0]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def rm(container):
|
def rm(container):
|
||||||
"""
|
"""
|
||||||
Deletes a container
|
Deletes a container
|
||||||
|
|
Loading…
Reference in a new issue