#!/usr/bin/python3 from pathlib import Path from subprocess import call from datetime import datetime from typing import List KEEP_DAYS = 3 def main(): build_images() update_containers() prune() def build_images(): images = read_file(Path('images.conf')) today = datetime.now().strftime('%Y%m%d') for image in images: path = Path(f'../images/{image}') call(['docker', 'build', '--no-cache', f'--tag={image}-panthera:{today}', '.'], cwd=path) def update_containers(): containers = read_file(Path('container.conf')) for container in containers: path = Path(f'../container/{container}') call(['docker-compose', 'pull'], cwd=path) call(['docker-compose', 'up', '-d'], cwd=path) def read_file(path: Path) -> List[str]: content = path.read_text().strip() elements = content.split('\n') elements = [item for item in elements if not item.startswith('#')] return elements def prune(): hours = int(KEEP_DAYS * 24) call(['docker', 'system', 'prune', '--all', '--force', '--filter', f'until={hours}h']) if __name__ == "__main__": main()