docker-composer/composer.py

46 lines
1.3 KiB
Python
Raw Normal View History

2022-12-13 08:20:23 -06:00
import os
2022-12-13 19:43:37 -06:00
from subprocess import getoutput
2022-12-13 08:20:23 -06:00
2022-12-13 19:43:37 -06:00
debug = True
2022-12-13 08:20:23 -06:00
2022-12-13 19:43:37 -06:00
# Read config file and make variables
config = open('docker-composer.conf', 'rt')
2022-12-13 08:20:23 -06:00
working_dir = os.getcwd()
compose_path = config.readline()
compose_path = compose_path[compose_path.find('=')+1:].strip()
if compose_path[-1:] != '/':
compose_path += '/'
2022-12-14 13:56:42 -06:00
if compose_path[1] != '/':
compose_path = f'{working_dir}/{compose_path}'
2022-12-13 08:20:23 -06:00
exclude_containers = config.readline()
2022-12-13 19:43:37 -06:00
exclude_containers = [container.strip() for container \
in exclude_containers[exclude_containers.find('=')+1:].split(',')]
2022-12-13 08:20:23 -06:00
compose_dirs = []
for dir in os.listdir(compose_path):
2022-12-13 19:43:37 -06:00
if os.path.isdir(compose_path + dir) and dir not in exclude_containers:
compose_dirs.append(compose_path + dir + '/')
# Print debug info
if debug:
print('Working directory: ' + working_dir)
print('Compose path: ' + compose_path)
print('Exclude containers: ' + str(exclude_containers))
print('Compose directories: ' + str(compose_dirs))
2022-12-13 08:20:23 -06:00
# COMPOSE!
for dir in compose_dirs:
2022-12-14 10:49:26 -06:00
container_name = dir[:-1][dir[:-1].rfind('/')+1:]
2022-12-13 19:43:37 -06:00
if debug:
print('Compose dir: ' + dir)
print('Container name: ' + container_name)
getoutput(f'docker stop {container_name}')
getoutput(f'docker rm {container_name}')
os.chdir(dir)
output = getoutput('docker compose up -d')
if debug:
print(output)