Skip to content

Commit

Permalink
Add devicename as a secret
Browse files Browse the repository at this point in the history
  • Loading branch information
antlafarge committed Jul 15, 2024
1 parent 8cceebc commit b6fd2c4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 92 deletions.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,34 @@ You can send feedback and discuss the project in the [github discussions](https:
services:
jdownloader:
image: antlafarge/jdownloader:<b>&#60;TAG&#62;</b>
container_name: <b>&#60;CONTAINER-NAME&#62;</b> # optional
restart: <b>&#60;RESTART&#62;</b> # optional
user: <b>&#60;UID&#62;:&#60;GID&#62;</b> # optional
container_name: <b>&#60;CONTAINER-NAME&#62;</b> # Optional
restart: <b>&#60;RESTART&#62;</b> # Optional
user: <b>&#60;UID&#62;:&#60;GID&#62;</b> # Optional
volumes:
- "<b>&#60;DOWNLOADS-PATH&#62;</b>:/jdownloader/downloads/"
- "<b>&#60;CONFIG-PATH&#62;</b>:/jdownloader/cfg/" # optional
- "<b>&#60;LOGS-PATH&#62;</b>:/jdownloader/logs/" # optional
- "<b>&#60;CONFIG-PATH&#62;</b>:/jdownloader/cfg/" # Optional
- "<b>&#60;LOGS-PATH&#62;</b>:/jdownloader/logs/" # Optional
environment:
- "JD_EMAIL=<b>&#60;JD-EMAIL&#62;</b>" # optional (better to use secrets)
- "JD_PASSWORD=<b>&#60;JD-PASSWORD&#62;</b>" # optional (better to use secrets)
- "JD_DEVICENAME=<b>&#60;JD-DEVICENAME&#62;</b>" # optional
- "UMASK=<b>&#60;UMASK&#62;</b>" # optional
- "JAVA_OPTIONS=<b>&#60;JAVA-OPTIONS&#62;</b>" # optional
- "LOG_FILE=<b>&#60;LOG-FILE&#62;</b>" # optional
- "JD_EMAIL=<b>&#60;JD-EMAIL&#62;</b>" # Optional (better to use secrets)
- "JD_PASSWORD=<b>&#60;JD-PASSWORD&#62;</b>" # Optional (better to use secrets)
- "JD_DEVICENAME=<b>&#60;JD-DEVICENAME&#62;</b>" # Optional
- "UMASK=<b>&#60;UMASK&#62;</b>" # Optional
- "JAVA_OPTIONS=<b>&#60;JAVA-OPTIONS&#62;</b>" # Optional
- "LOG_FILE=<b>&#60;LOG-FILE&#62;</b>" # Optional
secrets:
- JD_EMAIL
- JD_PASSWORD
- JD_DEVICENAME # Optional
ports:
- "<b>&#60;PORT&#62;</b>:3129" # optional
- "<b>&#60;PORT&#62;</b>:3129" # Optional

secrets:
JD_EMAIL:
file: "<b>&#60;JD-EMAIL-FILE&#62;</b>" # Put your email in this file
file: "<b>&#60;JD-EMAIL-FILE&#62;</b>" # Put your myJD email in this file
JD_PASSWORD:
file: "<b>&#60;JD-PASSWORD-FILE&#62;</b>" # Put your password in this file
file: "<b>&#60;JD-PASSWORD-FILE&#62;</b>" # Put your myJD password in this file
JD_DEVICENAME: # Optional
file: "<b>&#60;JD-DEVICENAME-FILE&#62;</b>" # Put your myJD device name in this file
</pre>

<details>
Expand All @@ -71,18 +74,19 @@ services:
volumes:
- "<b>/hdd/JDownloader/downloads/</b>:/jdownloader/downloads/"
- "<b>/hdd/JDownloader/cfg/</b>:/jdownloader/cfg/"
environment:
- "JD_DEVICENAME=<b>JDownloader</b>"
secrets:
- JD_EMAIL
- JD_PASSWORD
- JD_DEVICENAME
ports:
- "<b>3129</b>:3129"
secrets:
JD_EMAIL:
file: "<b>/hdd/JDownloader/secrets/JD_EMAIL.txt</b>"
JD_PASSWORD:
file: "<b>/hdd/JDownloader/secrets/JD_PASSWORD.txt</b>"
JD_DEVICENAME:
file: "<b>/hdd/JDownloader/secrets/JD_DEVICENAME.txt</b>"
</pre>
</details>

Expand All @@ -100,6 +104,7 @@ Name | Type | Description | Optional | Default
**`<LOGS-PATH>`** | [Volume](https://docs.docker.com/reference/cli/docker/container/run/#volume) | Directory where the JDownloader log files will be stored on your host machine.<br>*If you use the `user` parameter, check the permissions of the directories you mount as volumes.* | Not recommended | In container
**`<JD-EMAIL-FILE>`** | [Secret](https://docs.docker.com/compose/use-secrets/) | The path to the docker secret file where your [myJDownloader](https://my.jdownloader.org) e-mail is saved. | **REQUIRED** |
**`<JD-PASSWORD-FILE>`** | [Secret](https://docs.docker.com/compose/use-secrets/) | The path to the docker secret file where your [myJDownloader](https://my.jdownloader.org) password is saved. | **REQUIRED** |
**`<JD-DEVICENAME-FILE>`** | [Secret](https://docs.docker.com/compose/use-secrets/) | The path to the docker secret file where your [myJDownloader](https://my.jdownloader.org) device name is saved. | Optional | (hostname)
**`<JAVA-OPTIONS>`** | [Env](https://docs.docker.com/reference/cli/docker/container/run/#env) | Java options.<br>*Use `-Xms128m -Xmx1g` to change initial and max Java heap size memory.* | Optional | Empty
**`<UMASK>`** | [Env](https://docs.docker.com/reference/cli/docker/container/run/#env) | Change the *umask*. | Optional | No change
**`<JD-DEVICENAME>`** | [Env](https://docs.docker.com/reference/cli/docker/container/run/#env) | Device name in your [myJDownloader web interface](https://my.jdownloader.org). | Optional | Hostname
Expand Down Expand Up @@ -165,6 +170,7 @@ docker run -d \
- Do the same for the secrets directory (`/path/to/jdownloader/secrets/`)
- And create the secret file for your e-mail : `echo 'my@email.fr' > /path/to/jdownloader/secrets/JD_EMAIL.txt`
- And create the secret file for your password : `echo 'MyGreatPassword' > /path/to/jdownloader/secrets/JD_PASSWORD.txt`
- And create the secret file for your device name : `echo 'JD-DOCKER' > /path/to/jdownloader/secrets/JD_DEVICENAME.txt`
- Run the container by choosing the [docker run](https://github.com/antlafarge/jdownloader#docker-run) or [docker compose](https://github.com/antlafarge/jdownloader#docker-compose) method and customize the parameters by using your [myJDownloader](https://my.jdownloader.org) credentials.
- You can check the container logs : `docker logs --follow --tail 100 jdownloader` (`CTRL + C` to quit)
- Wait some minutes for JDownloader to update and be available in your [myJDownloader web interface](https://my.jdownloader.org).
Expand Down
93 changes: 51 additions & 42 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,75 @@ set +H

source functions.sh

handleSignal()
{
handleSignal_signalCode=$1
log "Kill signal $handleSignal_signalCode received"
if [ -n "$pid" ]; then # If java process found
stop=true
killProcess $pid
else
log "________________________________________ CONTAINER KILLED __________________________________________"
exit $((128 + $handleSignal_signalCode))
fi
}
trap "handleSignal 1" SIGHUP
trap "handleSignal 2" SIGINT
trap "handleSignal 15" SIGTERM

log "________________________________________ CONTAINER STARTED _________________________________________"
group "[CONTAINER STARTED]"

# Detect OS (ubuntu or alpine)

OS=$(cat /etc/os-release | grep "ID=" | sed -En "s/^ID=(.+)$/\1/p")
OS_prettyName=$(cat /etc/os-release | grep "PRETTY_NAME=" | sed -En "s/^PRETTY_NAME=\"(.+)\"$/\1/p")
log "OS = \"$OS_prettyName\""

# JAVA version
# JAVA

if [ "$OS" = "alpine" ]; then
JAVA_VERSION="$(apk -vv info | grep "openjdk.*jre" | cut -d" " -f1)"
else
if [ "$OS" = "ubuntu" ]; then
JAVA_VERSION="$(dpkg -l | grep "openjdk.*jre" | cut -d" " -f3,4)"
else
JAVA_VERSION="$(apk -vv info | grep "openjdk.*jre" | cut -d" " -f1)"
fi

log "JAVA version = \"$JAVA_VERSION\""

# Read secrets from files
if [ -n "$JAVA_OPTIONS" ]; then
log "JAVA options = \"$JAVA_OPTIONS\""
fi

# Retrieve JD_EMAIL

if [ -f "/run/secrets/JD_EMAIL" ]; then
JD_EMAIL=$(cat /run/secrets/JD_EMAIL)
elif [ -z "$JD_EMAIL" ]; then
log "WARNING" "Secret \"JD_EMAIL\" not found, use environment variable"
else
log "WARNING" "\"JD_EMAIL\" not found"
fi

# Retrieve JD_PASSWORD

if [ -f "/run/secrets/JD_PASSWORD" ]; then
JD_PASSWORD=$(cat /run/secrets/JD_PASSWORD)
elif [ -z "$JD_PASSWORD" ]; then
log "WARNING" "Secret \"JD_PASSWORD\" not found, use environment variable"
else
log "WARNING" "\"JD_PASSWORD\" not found"
fi

# Check environment variables

log "JAVA options = \"$JAVA_OPTIONS\""

if [ -z "$JD_EMAIL" ]; then
log "WARNING" "'JD_EMAIL' Secret file or environment variable is not found"
fi

if [ -z "$JD_PASSWORD" ]; then
log "WARNING" "'JD_PASSWORD' Secret file or environment variable is not found"
fi
# Rerieve JD_DEVICENAME

if [ -z "$JD_DEVICENAME" ]; then
if [ -f "/run/secrets/JD_DEVICENAME" ]; then
JD_DEVICENAME=$(cat /run/secrets/JD_DEVICENAME)
elif [ -z "$JD_DEVICENAME" ]; then
JD_DEVICENAME=$(uname -n)
fi

# UMASK

if [ -n "$UMASK" ]; then
log "Apply umask $UMASK"
umask $UMASK
fi

JDownloaderJarFile="JDownloader.jar"
JDownloaderJarUrl="installer.jdownloader.org/$JDownloaderJarFile"
JDownloaderPidFile="JDownloader.pid"

group "Verify $JDownloaderJarFile"

# Check JDownloader application integrity
unzip -t $JDownloaderJarFile &> /dev/null
unzipExitCode=$?

if [ "$unzipExitCode" -ne 0 ]; then
log "Delete any existing JDownloader installation files"
rm -f -r $JDownloaderJarFile Core.jar ./tmp ./update
Expand All @@ -89,7 +87,7 @@ if [ ! -f "./$JDownloaderJarFile" ]; then
curlExitCode=$?

if [ $curlExitCode -ne 0 ]; then
log "$JDownloaderJarFile download failed: curl exited with code '$curlExitCode'"
log "$JDownloaderJarFile download failed: curl exited with code \"$curlExitCode\""

# If https download failed, we try the http link
if [ ! -f "./$JDownloaderJarFile" ]; then
Expand All @@ -99,26 +97,32 @@ if [ ! -f "./$JDownloaderJarFile" ]; then
curlExitCode=$?

if [ $curlExitCode -ne 0 ]; then
fatal "$JDownloaderJarFile download failed: curl exited with code '$curlExitCode'"
groupEnd
groupEnd
fatal $curlExitCode "$JDownloaderJarFile download failed: curl exited with code \"$curlExitCode\""
fi
fi
fi
fi

./setup.sh "$JD_EMAIL" "$JD_PASSWORD" "$JD_DEVICENAME"
setupShExitCode=$?
groupEnd

if [ $setupShExitCode -ne 0 ]; then
fatal "setup.sh exited with code '$setupShExitCode'"
group "Setup"
./setup.sh "$JD_EMAIL" "$JD_PASSWORD" "$JD_DEVICENAME"
setupExitCode=$?
if [ $setupExitCode -ne 0 ]; then
groupEnd
fatal $setupExitCode "setup.sh exited with code \"$setupExitCode\""
fi
groupEnd

unset JD_EMAIL
unset JD_PASSWORD
unset JD_DEVICENAME

# Request eventscripter install
mkdir -p ./update/versioninfo/JD
echo '["eventscripter"]' > ./update/versioninfo/JD/extensions.requestedinstalls.json
echo "["eventscripter"]" > ./update/versioninfo/JD/extensions.requestedinstalls.json

# Put setup auto-update script
autoUpdateEventScripterSettings="org.jdownloader.extensions.eventscripter.EventScripterExtension.json"
Expand All @@ -130,7 +134,7 @@ if [ ! -f "./cfg/$autoUpdateEventScripterScript" ]; then
cp "./$autoUpdateEventScripterScript" "./cfg/$autoUpdateEventScripterScript"
fi

log "Start JDownloader"
group "Start JDownloader"

# Create logs dir if needed
if [ ! -d "/jdownloader/logs/" ]; then
Expand All @@ -141,6 +145,7 @@ fi
java $JAVA_OPTIONS -Djava.awt.headless=true -jar $JDownloaderJarFile &> "$LOG_FILE" &
pid=$!
lastPid=""
JDownloaderPidFile="JDownloader.pid"

while [ -n "$pid" ]; do
jdrev=$(cat update/versioninfo/JD/rev 2> /dev/null)
Expand All @@ -157,12 +162,16 @@ while [ -n "$pid" ]; do

lastPid="$pid"

# Get the written JDownloader PID or another running Java PID
# Get the written JDownloader PID or another running java PID
pid=$(pgrep -L -F $JDownloaderPidFile 2> /dev/null || pgrep -o java)
done

log "JDownloader stopped"

log "________________________________________ CONTAINER STOPPED _________________________________________"
groupEnd

groupEnd

log "CONTAINER STOPPED"

exit $exitCode
77 changes: 69 additions & 8 deletions functions.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
#!/bin/bash

# This script defines the shared functions
# Useful functions

logIndent=0

# Log
log()
{
echo "$(date -I'seconds')| $@" >&2
echo "$(date -I'seconds')| $(printf %$((2*logIndent))s)$@" >&2
}

# Group (log and increment indent)
group()
{
log $@
((logIndent++))
}

# Fatal error occured, we log and exit the script
# Decrement indent
groupEnd()
{
((logIndent>0 ? logIndent-- : logIndent=0))
}

# Reset indent
groupReset()
{
((logIndent=0))
}

# Fatal error occured : log and exit
fatal()
{
fatal_log="$1"
fatal_exitCode="$1"
fatal_log="$2"

log "FATAL ERROR" "$fatal_log"
log "FATAL ERROR :" "$fatal_log"
log "Get more informations here : https://github.com/antlafarge/jdownloader#troubleshooting"
log "________________________________________ CONTAINER EXITED __________________________________________"
exit 1
groupEnd
log "CONTAINER TERMINATED"
exit ${fatal_exitCode:-1}
}

# Wait for many processes to terminate
Expand All @@ -33,7 +56,7 @@ waitProcess()
sleepExitCode=$?

if [ $sleepExitCode -ne 0 ]; then
fatal "sleep exited with code '$sleepExitCode'"
fatal "sleep exited with code \"$sleepExitCode\""
fi
done
fi
Expand All @@ -47,3 +70,41 @@ killProcess()
log "Send SIGTERM to process $killProcess_pids"
kill -SIGTERM $killProcess_pids 2> /dev/null
}

# Handle signal
handleSignal()
{
handleSignal_signalCode=$1
log "Kill signal \"$handleSignal_signalCode\" received"
if [ -n "$pid" ]; then # If java process found
stop=true
killProcess $pid
else
groupEnd
log "CONTAINER KILLED"
exit $((128 + $handleSignal_signalCode))
fi
}

# Replace a JSON value by searching its field in a JSON file
# Usage : replaceJsonValue file field value
replaceJsonValue()
{
file=$1
field=$(printf "%s" "$2" | sed -e 's/\\/\\\\/g' -e 's/[]\/$*.^[]/\\&/g') # this field will be compared to a value from a json file, so we need to double escape the backslashes \\\\ And this field will be used in a sed regex, so we escape regex special characters ]\/$*.^[
newValue=$(printf "%s" "$3" | sed -e 's/[\/&]/\\&/g' -e 's/"/\\\\"/g') # this value will be used in a sed replace, so we escape replace special characters \/& And this value will be stored in a json file, so finally we double escape the quotes \\\\"

fieldPart="\($field\)" # match the field
valuePart="\([^\\\"]\|\\\\.\)*" # match the value. This looks complicated because it can contain escaped quotes \" because of json format.

search="\"$fieldPart\"\s*:\s*\"$valuePart\""
replace="\"\1\":\"$newValue\""

sed -i "s/$search/$replace/g" $file
sedExitCode=$?

if [ $sedExitCode -ne 0 ]; then
log "ERROR" "sed exited with code \"$sedExitCode\""
exit $sedExitCode
fi
}
Loading

0 comments on commit b6fd2c4

Please sign in to comment.