1
0
Fork 0

Added Docker awareness

Added more clear user hints for how to run the container for creating/deleting albums depending on whether the script is running bare python or in Docker
This commit is contained in:
Salvoxia 2024-08-09 18:14:40 +02:00
parent 1f0332f121
commit 6638166e05
2 changed files with 24 additions and 5 deletions

View file

@ -6,6 +6,6 @@ COPY immich_auto_album.py requirements.txt docker/immich_auto_album.sh docker/se
RUN pip install --no-cache-dir -r /script/requirements.txt \ RUN pip install --no-cache-dir -r /script/requirements.txt \
&& chmod +x /script/setup_cron.sh /script/immich_auto_album.sh \ && chmod +x /script/setup_cron.sh /script/immich_auto_album.sh \
&& rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/* && rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*
ENV IS_DOCKER=1
WORKDIR /script WORKDIR /script
CMD ["sh", "-c", "/script/setup_cron.sh && crond -f"] CMD ["sh", "-c", "/script/setup_cron.sh && crond -f"]

View file

@ -3,6 +3,7 @@ import requests
import argparse import argparse
import logging import logging
import sys import sys
import os
import datetime import datetime
from collections import defaultdict from collections import defaultdict
import urllib3 import urllib3
@ -23,6 +24,8 @@ SCRIPT_MODE_CREATE = "CREATE"
SCRIPT_MODE_CLEANUP = "CLEANUP" SCRIPT_MODE_CLEANUP = "CLEANUP"
# Delete ALL albums # Delete ALL albums
SCRIPT_MODE_DELETE_ALL = "DELETE_ALL" SCRIPT_MODE_DELETE_ALL = "DELETE_ALL"
# Environment variable to check if the script is running inside Docker
ENV_IS_DOCKER = "IS_DOCKER"
parser = argparse.ArgumentParser(description="Create Immich Albums from an external library path based on the top level folders", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser = argparse.ArgumentParser(description="Create Immich Albums from an external library path based on the top level folders", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
@ -45,6 +48,7 @@ args = vars(parser.parse_args())
logging.basicConfig(level=args["log_level"], stream=sys.stdout, format='time=%(asctime)s level=%(levelname)s msg=%(message)s') logging.basicConfig(level=args["log_level"], stream=sys.stdout, format='time=%(asctime)s level=%(levelname)s msg=%(message)s')
logging.Formatter.formatTime = (lambda self, record, datefmt=None: datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).astimezone().isoformat(sep="T",timespec="milliseconds")) logging.Formatter.formatTime = (lambda self, record, datefmt=None: datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).astimezone().isoformat(sep="T",timespec="milliseconds"))
root_paths = args["root_path"] root_paths = args["root_path"]
root_url = args["api_url"] root_url = args["api_url"]
api_key = args["api_key"] api_key = args["api_key"]
@ -64,6 +68,8 @@ delete_confirm = args["delete_confirm"]
if mode != SCRIPT_MODE_CREATE: if mode != SCRIPT_MODE_CREATE:
unattended = False unattended = False
is_docker = os.environ.get(ENV_IS_DOCKER, False)
logging.debug("root_path = %s", root_paths) logging.debug("root_path = %s", root_paths)
logging.debug("root_url = %s", root_url) logging.debug("root_url = %s", root_url)
logging.debug("api_key = %s", api_key) logging.debug("api_key = %s", api_key)
@ -77,6 +83,7 @@ logging.debug("insecure = %s", insecure)
logging.debug("ignore = %s", ignore_albums) logging.debug("ignore = %s", ignore_albums)
logging.debug("mode = %s", mode) logging.debug("mode = %s", mode)
logging.debug("delete_confirm = %s", delete_confirm) logging.debug("delete_confirm = %s", delete_confirm)
logging.debug("is_docker = %s", is_docker)
# Verify album levels # Verify album levels
if is_integer(album_levels) and album_levels == 0: if is_integer(album_levels) and album_levels == 0:
@ -357,8 +364,12 @@ if mode == SCRIPT_MODE_DELETE_ALL:
album_names = [] album_names = []
for album in albums: for album in albums:
album_names.append(album['albumName']) album_names.append(album['albumName'])
print("Would delete the following albums (ALL albums!). Call with --delete-confirm to actually delete albums!") print("Would delete the following albums (ALL albums!):")
print(album_names) print(album_names)
if is_docker:
print("Run the container with environment variable DELETE_CONFIRM set to 1 to actually delete these albums!")
else:
print("Call with --delete-confirm to actually delete albums!")
exit(0) exit(0)
cpt = 0 cpt = 0
for album in albums: for album in albums:
@ -412,8 +423,12 @@ logging.info("%d albums identified", len(album_to_assets))
logging.info("Album list: %s", list(album_to_assets.keys())) logging.info("Album list: %s", list(album_to_assets.keys()))
if not unattended and mode == SCRIPT_MODE_CREATE: if not unattended and mode == SCRIPT_MODE_CREATE:
print("Press enter to create these albums, Ctrl+C to abort") if is_docker:
input() print("Check that this is the list of albums you want to create. Run the container with environment variable UNATTENDED set to 1 to actually create these albums.")
exit(0)
else:
print("Press enter to create these albums, Ctrl+C to abort")
input()
album_to_id = {} album_to_id = {}
@ -427,8 +442,12 @@ logging.info("%d existing albums identified", len(albums))
if mode == SCRIPT_MODE_CLEANUP: if mode == SCRIPT_MODE_CLEANUP:
# Delete Confirm check # Delete Confirm check
if not delete_confirm: if not delete_confirm:
print("Would delete the following albums. Call with --delete-confirm to actually delete albums!") print("Would delete the following albums:")
print(list(album_to_id.keys())) print(list(album_to_id.keys()))
if is_docker:
print("Run the container with environment variable DELETE_CONFIRM set to 1 to actually delete these albums!")
else:
print(" Call with --delete-confirm to actually delete albums!")
exit(0) exit(0)
cpt = 0 cpt = 0