From 9c2ee9284183ae311205b696fd3c2eebbc8dc72e Mon Sep 17 00:00:00 2001 From: Joey Hafner Date: Thu, 1 Dec 2022 18:41:51 -0800 Subject: [PATCH 1/2] Remove unnecessary Dockerfile commands --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8d9b090..a716b0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,5 @@ RUN echo "\n"\ >> /usr/local/apache2/conf/httpd.conf WORKDIR /usr/local/apache2/htdocs/ -RUN mkdir download RUN chown -R $PUID:$PGID /usr/local/apache2/htdocs -ENV IMG false CMD ["/bin/bash","/init.sh"] \ No newline at end of file From fa66dd34864879125ebbd42930b5cde57d68489b Mon Sep 17 00:00:00 2001 From: Joey Hafner Date: Thu, 1 Dec 2022 18:42:26 -0800 Subject: [PATCH 2/2] Refactor script, update docs, update compose --- README.md | 65 +++++++++++++---- docker-compose.yml | 33 ++++----- init.sh | 171 +++++++++++++++------------------------------ 3 files changed, 124 insertions(+), 145 deletions(-) diff --git a/README.md b/README.md index 397319c..bbb6571 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -This is a simple image for hosting your own 5eTools instance. It is based on the Apache `httpd` image and uses a heavily-modified version of the auto-updater script from the [5eTools wiki](https://wiki.5e.tools/index.php/5eTools_Install_Guide). This image is built from [this GitHub repository](https://github.com/Jafner/5etools-docker). +This is a simple image for hosting your own 5eTools instance. It is based on the Apache `httpd` image and uses components of the auto-updater script from the [5eTools wiki](https://wiki.5e.tools/index.php/5eTools_Install_Guide). This image is built from [this GitHub repository](https://github.com/Jafner/5etools-docker). # Usage +Below we talk about how to install and configure the container. ## Default Configuration You can quick-start this image by running: @@ -11,25 +12,63 @@ curl -o docker-compose.yml https://raw.githubusercontent.com/Jafner/5etools-dock docker-compose up -d && docker logs -f 5etools-docker ``` -Then give the container a few minutes to come online and it will be accessible at `localhost:8080`. +Then give the container a few minutes to come online (it takes a while to pull the Github repository) and it will be accessible at `localhost:8080`. When you stop the container, it will automatically delete itself. The downloaded files will remain in the `~/5etools-docker/htdocs` directory, so you can always start the container back up by running `docker-compose up -d`. -## Configuring the Setup -The image uses a handful of environment variables to figure out how you want it to run. -By default, I assume you want to automatically download the latest files from the temporary github mirror. You can configure exactly how you want the script to run with environment variables within the docker-compose file. +## Volume Mapping +By default, I assume you want to keep downloaded files, even if the container dies. And you want the downloaded files to be located at `~/5etools-docker/htdocs`. -### IMG (defaults to true) -When downloading from the `get.5e.tools` structure, grab both the base site files and the image files for the bestiary, items, adventures, etc.. This increases time and bandwidth needed to bring the server up. +If you want the files to be located somewhere else on your system, change the left side of the volume mapping. For example, if I wanted to keep my files at `~/data/docker/5etools`, the volume mapping would be: -### AUTOUPDATE (defaults to true) -Setting this to false bypasses all downloading logic and falls back to the local files if available, or exits if there is no local version. +``` + volumes: + - ~/data/docker/5etools:/usr/local/apache2/htdocs +``` -### DL_TYPE (defaults to github) -This can be set to "get", "github", or "mega". It is used to decide which logic to use to download the source files. +Alternatively, you can have Docker or Compose manage your volume. (This makes adding homebrew practically impossible.) -### DL_LINK (defaults to temporary mirror) -This can be set to the URL of the source files you want to use. For a github repository, use the HTTPS link ending with `.git`. For mega, use the full link to the file. For get, use the base domain (e.g. `https://get.5e.tools`), rather than a specific file. +Use a Compose-managed volume with: +``` +... + volumes: + - 5etools-docker:/usr/local/apache2/htdocs +... +volumes: + 5etools-docker: +``` +Or have the Docker engine manage the volume (as opposed to Compose). First, create the volume with `docker volume create 5etools-docker`, then add the following to your `docker-compose.yml`: +``` +... + volumes: + - 5etools-docker:/usr/local/apache2/htdocs +... +volumes: + 5etools-docker: + external: true +``` + +## Environment Variables +The image uses environment variables to figure out how you want it to run. +By default, I assume you want to automatically download the latest files from the Github mirror. Use the environment variables in the `docker-compose.yml` file to configure things. + +### SOURCE (defaults to GITHUB) +Required unless OFFLINE_MODE=TRUE. +Expects one of "GITHUB", "GET5ETOOLS", or "GET5ETOOLS-NOIMG". Where: + > "GITHUB" pulls from https://github.com/5etools-mirror-1/5etools-mirror-1 + > "GET5ETOOLS" pulls from https://get.5e.tools + > "GET5ETOOLS-NOIMG" pulls from https://get.5e.tools without image files. + +The get.5e.tools source has been down (redirecting to 5e.tools) during development. This method is not tested. + +**Note: As of December 2022, get.5e.tools has been down for several months**. The URL redirects to the main 5etools page, but does not provide packaged archives of the site like it used to. I will update this if or when the original get.5e.tools returns. + +### OFFLINE_MODE +Optional. Expects "TRUE" to enable. +Setting this to true tells the server to run from the local files if available, or exits if there is no local version. + +### PUID and PGID +During the image build process, we set the owner of the `htdocs` directory to `1000:1000` by default. If you need a different UID and GID to own the files, you can build the image from the source Dockerfile and pass the PUID and PGID variables as desired. ## Integrating a reverse proxy Supporting integration of a reverse proxy is beyond the scope of this guide. diff --git a/docker-compose.yml b/docker-compose.yml index 4eb1df7..3ca58c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,25 +2,22 @@ version: "3" services: 5etools-docker: container_name: 5etools-docker - image: jafner/5etools-docker:latest + image: jafner/5etools-docker volumes: - - ~/5etools-docker/htdocs:/usr/local/apache2/htdocs + - 5etools-docker:/usr/local/apache2/htdocs ports: - 8080:80/tcp environment: - - IMG= - # defaults to "false" - # expects "true" or "false" - - AUTOUPDATE= - # defaults to "true" - # expects "true" or "false" - - DL_TYPE= - # defaults to "github" - # expects "get", "github", or "mega" - # where "get" refers to the old `get.5e.tools` structure, - # "github" refers to the root of a specific github repository, - # and "mega" refers to a mega.nz download link - - DL_LINK= - # defaults to https://github.com/5etools-mirror-1/5etools-mirror-1.github.io.git - # expects a URL with the correct content for the DL_TYPE - # if you're using the github type, make sure the url ends with .git + - SOURCE=GITHUB + # Required unless OFFLINE_MODE=TRUE + # Expects one of "GITHUB", "GET5ETOOLS", or "GET5ETOOLS-NOIMG". Where: + # GITHUB pulls from https://github.com/5etools-mirror-1/5etools-mirror-1 + # GET5ETOOLS pulls from https://get.5e.tools + # GET5ETOOLS-NOIMG pulls from https://get.5e.tools without image files. + # The get.5e.tools source has been down (redirecting to 5e.tools) during development. + # This method is not tested. + #- OFFLINE_MODE=TRUE + # Optional. Expects "TRUE" or "FALSE". + +volumes: + 5etools-docker: \ No newline at end of file diff --git a/init.sh b/init.sh index f8e25d3..e633ed2 100644 --- a/init.sh +++ b/init.sh @@ -1,57 +1,63 @@ #!/bin/bash # based (loosely) on: https://wiki.5e.tools/index.php/5eTools_Install_Guide -cd /usr/local/apache2/htdocs +# Ensure clean, non-root ownership of the htdocs directory. +# Delete index.html if it's the stock apache file. Otherwise it impedes the git clone. +chown -R $PUID:$PGID /usr/local/apache2/htdocs +if grep -Fq '

It works!

' "/usr/local/apache2/htdocs/index.html"; then + rm /usr/local/apache2/htdocs/index.html +fi -# this variable can be passed into the environment via docker run or docker-compose -# since this variable is required, I declare it explicitly here -# expects "get", "github", or "mega" -# where "get" refers to the old `get.5e.tools` structure, -# "github" refers to the root of a specific github repository, -# and "mega" refers to a mega.nz download link -# defaults to "github" -DL_TYPE=${DL_TYPE:-github} - -# this variable can be passed into the environment via docker run or docker-compose -# since this variable is required, I declare it explicitly here -# expects a URL with the correct content for the DL_TYPE -# defaults to the temporary github mirror -DL_LINK=${DL_LINK:-https://github.com/5etools-mirror-1/5etools-mirror-1.github.io.git} - -# this variable can be passed into the environment via docker run or docker-compose -# since this variable is required, I declare it explicitly here -# expects "true" or "false" -# defaults to "true" -AUTOUPDATE=${AUTOUPDATE:-true} - -# this variable can be passed into the environment via -# expects "true" or "false" -# defaults to "false" -IMG=${IMG:-false} - - -if [ $AUTOUPDATE = false ]; then - # if the user doesn't want to update from a source, - # check for local version - # if local version is found, print version and start server - # if no local version is found, print error message and exit 1 - echo "Auto update disabled. Checking for local version..." +# If the user doesn't want to update from a source, +# check for local version. +# If local version is found, print version and start server. +# If no local version is found, print error message and exit. +if [ "$OFFLINE_MODE" = "TRUE" ]; then + echo "Offline mode is enabled. Will try to launch from local files. Checking for local version..." if [ -f /usr/local/apache2/htdocs/package.json ]; then VERSION=$(jq -r .version package.json) # Get version from package.json echo " === Starting version $VERSION" + httpd-foreground else echo " === No local version detected. Exiting." exit 1 fi -else - # if the user does want to update from a source, - # check if url provided via the $DL_LINK env variable is connectable - echo "Auto update enabled. Checking for remote version..." - echo " === Checking connection to $DL_LINK..." - SITE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" $DL_LINK) - if [ $SITE_STATUS = 200 ] || [ $SITE_STATUS = 301 ]; then # if the source URL is reachable - if [ $DL_TYPE = "get" ]; then # the get.5e.tools structure - echo " === Using get structure to download from $DL_LINK" +fi + +# The SOURCE variable must be set if OFFLINE_MODE is not TRUE +if [ -z "${SOURCE}" ]; then + echo "SOURCE variable not set. Expects one of \"GITHUB\", \"GET5ETOOLS\", or \"GET5ETOOLS-NOIMG\". Exiting." + exit 1 +fi + +# Move to the working directory for working with files. +cd /usr/local/apache2/htdocs + +echo " === Checking directory permissions for /usr/local/apache2/htdocs" +ls -ld /usr/local/apache2/htdocs + +SOURCE=${SOURCE} +case $SOURCE in + "GITHUB") + DL_LINK=https://github.com/5etools-mirror-1/5etools-mirror-1.github.io.git + echo " === Using GitHub structure to update from $DL_LINK" + echo " === Warning: images will be downloaded automatically, which will take longer" + if [ ! -d "./.git" ]; then # if no git repository already exists + echo " === No existing git repository, creating one" + git config --global user.email "autodeploy@jafner.tools" + git config --global user.name "AutoDeploy" + git config --global pull.rebase false # Squelch nag message + git clone --depth=1 $DL_LINK . + fi + echo " === Pulling from GitHub... (This might take a while)" + git pull --depth=1 origin master #2> /dev/null + VERSION=$(jq -r .version package.json) # Get version from package.json + echo " === Starting version $VERSION" + httpd-foreground + ;; + "GET5ETOOLS*") + DL_LINK=https://get.5e.tools + echo " === Using get structure to download from $DL_LINK" echo " === WARNING: This part of the script has not yet been tested. Please open an issue on the github if you have trouble." # get remote version number # takes three steps of wizardry. I did not write this, but it works so I don't touch it. @@ -66,7 +72,7 @@ else cd ./download/ curl --progress-bar -k -O -J $DL_LINK/src/ -C - - if [ "$IMG" = "true" ]; then # download images + if [ "$SOURCE" != *"NOIMG"* ]; then # download images echo " === Downloading images... " curl --progress-bar -k -O -J $DL_LINK/img/ -C - fi @@ -76,7 +82,7 @@ else echo " === Extracting site..." 7z x ./download/$FILENAME -o./ -y - if [ "$IMG" = "true" ]; then # extract images + if [ "$SOURCE" != *"NOIMG"* ]; then # extract images echo " === Extracting images..." 7z x ./download/$FILENAME_IMG -o./img -y mv ./img/tmp/5et/img/* ./img @@ -99,73 +105,10 @@ else VERSION=$(jq -r .version package.json) # Get version from package.json echo " === Starting version $VERSION" httpd-foreground - elif [ $DL_TYPE = "github" ]; then # the github structure - echo " === Using GitHub structure to update from $DL_LINK" - echo " === Warning: images will be downloaded automatically, which will take longer" - if [ ! -d "./.git" ]; then # if no git repository already exists - git config --global user.email "autodeploy@jafner.tools" - git config --global user.name "AutoDeploy" - git init > /dev/null 2>&1 - git add . > /dev/null - git commit -m "Init" > /dev/null - git remote add upstream $DL_LINK - fi - echo " === Pulling from GitHub... (This might take a while)" - git pull --depth=1 upstream master 2> /dev/null - VERSION=$(jq -r .version package.json) # Get version from package.json - echo " === Starting version $VERSION" - httpd-foreground - elif [ $DL_TYPE = "mega" ]; then # the mega structure - echo " === Using mega structure to download from $DL_LINK" - echo " === Warning: This method will overwrite the current local version because it cannot check the remote version" - echo " === Warning: This method ignores the IMG environment variable." - # downloading files - mkdir -p ./download - megadl --path ./download/ --no-progress --print-names $DL_LINK > filename # downloads the file to ./download/ and redirects the filename to a file called filename - FILENAME=$(cat filename) - rm filename + ;; + *) + echo "SOURCE variable set incorrectly. Exiting..." + exit + ;; - # extracting files - echo " === Extracting site..." - 7z x ./download/$FILENAME -o./ -y - - # configuring the index.html and 5etools.html files - echo " === Configuring..." # honestly I don't know enough HTML/CSS/JS to tell exactly what this part of the script does :L - find . -name \*.html -exec sed -i 's/"width=device-width, initial-scale=1"/"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/' {} \; - sed -i 's//\n/' index.html - sed -i 's/navigator.serviceWorker.register("\/sw.js/navigator.serviceWorker.register("sw.js/' index.html - sed -i 's/navigator.serviceWorker.register("\/sw.js/navigator.serviceWorker.register("sw.js/' 5etools.html - - # cleaning up downloads - echo " === Cleaning up downloads" - find ./download/ -type f ! -name "*.${VER}.zip" -exec rm {} + # delete the downloaded zip files - - # starting the server - VERSION=$(jq -r .version package.json) # Get version from package.json - echo " === Starting version $VERSION" - httpd-foreground - else # if the DL_TYPE env var is not recognized - echo " === Could not determine download structure." - if [ -f /usr/local/apache2/htdocs/package.json ]; then - VERSION=$(jq -r .version package.json) # Get version from package.json - echo " === Falling back to local version: $VERSION" - httpd-foreground - else - echo " === No local version found! You must be able to access $DL_LINK to grab the 5eTools files." - echo " === Hint: Make sure you have the correct DL_TYPE environment variable set." - exit 1 - fi - fi - else # if the download source is not accessible - echo " === Could not connect to $DL_LINK" - if [ -f /usr/local/apache2/htdocs/package.json ]; then - VERSION=$(jq -r .version package.json) # Get version from package.json - echo " === Falling back to local version: $VERSION" - httpd-foreground - else - echo " === No local version found! You must be able to access $DL_LINK to grab the 5eTools files." - echo " === Hint: Make sure you have the correct DL_TYPE environment variable set." - exit 1 - fi - fi -fi +esac