Compare commits

...

5 Commits

View File

@@ -222,15 +222,37 @@ def change_containers_status(containers, status):
print(f"No containers to {status}.") print(f"No containers to {status}.")
def is_image_whitelisted(container, images): def is_image_whitelisted(container, images):
"""
Return True if the container's image matches any of the whitelist patterns.
Also prints out the image name and the match result.
"""
# fetch the image (e.g. "nextcloud:23-fpm-alpine")
info = get_image_info(container)[0] info = get_image_info(container)[0]
return any(img in info for img in images)
# check against each pattern
whitelisted = any(pattern in info for pattern in images)
# log the result
print(f"Container {container!r} → image {info!r} → whitelisted? {whitelisted}", flush=True)
return whitelisted
def is_container_stop_required(containers): def is_container_stop_required(containers):
"""Check if any of the containers are using images that are not whitelisted.""" """
return any( Check if any of the containers are using images that are not whitelisted.
not is_image_whitelisted(c, IMAGES_NO_STOP_REQUIRED) If so, print them out and return True; otherwise return False.
for c in containers """
) # Find all containers whose image isnt on the whitelist
not_whitelisted = [
c for c in containers
if not is_image_whitelisted(c, IMAGES_NO_STOP_REQUIRED)
]
if not_whitelisted:
print(f"Containers requiring stop because they are not whitelisted: {', '.join(not_whitelisted)}")
return True
return False
def create_volume_directory(volume_name): def create_volume_directory(volume_name):
"""Create necessary directories for backup.""" """Create necessary directories for backup."""
@@ -335,13 +357,13 @@ def main():
help='List of image names for which no backup should be performed (optional)' help='List of image names for which no backup should be performed (optional)'
) )
args = parser.parse_args() args = parser.parse_args()
DATABASE_CONTAINERS = args.DATABASE_CONTAINERS DATABASE_CONTAINERS = args.database_containers
IMAGES_NO_STOP_REQUIRED = args.images_no_stop_required IMAGES_NO_STOP_REQUIRED = args.images_no_stop_required
if args.images_no_backup_required is not None: if args.images_no_backup_required is not None:
global IMAGES_NO_BACKUP_REQUIRED global IMAGES_NO_BACKUP_REQUIRED
IMAGES_NO_BACKUP_REQUIRED = args.images_no_backup_required IMAGES_NO_BACKUP_REQUIRED = args.images_no_backup_required
print('Start volume backups...') print('💾 Start volume backups...', flush=True)
volume_names = execute_shell_command("docker volume ls --format '{{.Name}}'") volume_names = execute_shell_command("docker volume ls --format '{{.Name}}'")
for volume_name in volume_names: for volume_name in volume_names:
print(f'Start backup routine for volume: {volume_name}') print(f'Start backup routine for volume: {volume_name}')