Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Docker support #89

Closed
wants to merge 5 commits into from
Closed

WIP: Docker support #89

wants to merge 5 commits into from

Conversation

Taxel
Copy link
Owner

@Taxel Taxel commented Dec 8, 2020

As per note of @glensc in #88 this is now a pull request.

The following changes have been made:

  • I rebased the branch on my changes in Update to use guids from python-plexapi to fix Plexmovieagent breakage #87, if we wait for the official python-plexapi package to get updated this will have to be redone
  • I changed the file structure: With configuration, cache, logs, and source code in one directory the project felt unorganized, so I should have probably done this before even making the project open source.
  • The docker image is based on @twolaw 's Dockerfile. The main differences are, that I am using a multistep build (unnecessarily I might add, this does not really make the resulting image much smaller, but it was fun to play around with) and that the script does not run once by default but on a cronjob, so the container can be configured and then left running forever. One can, however, pass the environment variable "RUN_ONCE" with a non-empty value and the script will only be run once, after which the container shuts down. Changing the cron schedule is also done via an environment var: CRON_SCHEDULE
  • Running this in Docker also means that via volume mounts and multiple containers, one can use different configs for multiple users and/or Plex servers, solving Allow custom envfile path to be used #70 indirectly (only in Docker, of course. Not when running this with Python. Since I extracted all the filepaths to config.py this should be trivial to implement, though)

What might be the most controversial change, is that I am rerouting the log outputs to stdout if the terminal is non-interactive. I use it to detect if the script is running in a container since Docker containers should output to stdout/stderr and let Docker take care of the logging. Do you agree the script should no longer write to lastRun.log if it is running in a container and is the sys.isatty(0) check okay or should I add an argument like python3 main.py --log-to-stdout so this is easier to control?

Since the file structure changes alone constitute some manual work when upgrading from an older version of PlexTraktSync, I also propose we create a release 0.9.0 before merging the pull request for this once the feature is complete and then release 1.0.0 along with the docker image (I'd like to try Github Packages as the image registry and create a Github action that automatically builds, tags and pushes each new release in the future)

cp /var/config.json /var/config/config.json
fi

if [[ ! -z "${RUN_ONCE}" ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[ is bashism, but /bin/sh is the interpreter.

(cd ./src && python3 ./main.py)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need a subshell, there's nothing else after python command, so no need to subshell cd'ing

in fact. you should use exec to release memory from shell process:

Suggested change
(cd ./src && python3 ./main.py)
cd ./src && exec python3 ./main.py

and perhaps just use set -e instead of &&

Suggested change
(cd ./src && python3 ./main.py)
see -e
cd ./src
exec python3 ./main.py

SCHEDULE="${CRON_SCHEDULE}"
fi
# install cronjob
(crontab -l ; echo "$SCHEDULE python3 /var/src/main.py") | sort - | uniq - | crontab -
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you did cd in plex_track_sync.sh, but not here. is the cd needed or not?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| sort - | uniq - can be replaced with | sort -u

but check it first, you may have crippled sort in your image, i.e one from (older) busybox

# install cronjob
(crontab -l ; echo "$SCHEDULE python3 /var/src/main.py") | sort - | uniq - | crontab -
# run crond so the container does not terminate
crond -l 2 -f
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this crond is last command in the script, prepend with exec, so that:

  1. memory is released from shell
  2. signals get routed to main process, not shell (which typically ignores them)

@simonc56
Copy link
Collaborator

simonc56 commented Dec 17, 2020

I ran docker-compose up -d and the image built fine (on arm: 75MB, mine was 58MB).
But the container is created without /var/config/ folder so the entry.sh can"t go further than :

config.json does not exist. copying it.
cp: can't create '/var/config/config.json': No such file or directory
Running the script every 2 hours (CRON_SCHEDULE='0 */2 * * *'). Set environment var 'CRON_SCHEDULE' to a valid cron expression to configure this
If you want to run the script only once, specify the environment var 'RUN_ONCE=1'
Traceback (most recent call last):
  File "/var/src/main.py", line 3, in <module>
    from config import CONFIG, CONFIG_DIR, LOG_DIR, CACHE_DIR
  File "/var/src/config.py", line 11, in <module>
    with open(config_file, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '/var/src/../config/config.json'

Is the volume map for config/ missing in docker-compose.yml ?
Did I do something wrong ?

edit: after adding ./config:/var/config it's working good. I understand docker-compose.yml is in WIP status.

@SJ50
Copy link

SJ50 commented Dec 17, 2020

@twolaw you need to mount “src” and “config” folder.

./src:/var/src
./config:/var/config

@glensc glensc changed the base branch from master to 0.5.x April 17, 2021 12:30
@simonc56 simonc56 closed this Jun 15, 2021
@simonc56 simonc56 deleted the branch 0.5.x June 15, 2021 17:57
Repository owner locked as resolved and limited conversation to collaborators Jan 11, 2022
@glensc glensc deleted the docker branch December 12, 2022 23:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants