2022-02-14 16:43:40 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
##@Version : 202207112232-git
|
2022-07-11 13:02:53 -04:00
|
|
|
# @Author : Jason Hempstead
|
|
|
|
# @Contact : jason@casjaysdev.com
|
|
|
|
# @License : LICENSE.md
|
|
|
|
# @ReadME : entrypoint-aria2.sh --help
|
|
|
|
# @Copyright : Copyright: (c) 2022 Jason Hempstead, Casjays Developments
|
2022-07-12 00:25:31 -04:00
|
|
|
# @Created : Monday, Jul 11, 2022 22:32 EDT
|
2022-07-11 13:02:53 -04:00
|
|
|
# @File : entrypoint-aria2.sh
|
|
|
|
# @Description :
|
|
|
|
# @TODO :
|
|
|
|
# @Other :
|
|
|
|
# @Resource :
|
|
|
|
# @sudo/root : no
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
APPNAME="$(basename "$0" 2>/dev/null)"
|
2022-07-12 00:25:31 -04:00
|
|
|
VERSION="202207112232-git"
|
2022-07-11 13:02:53 -04:00
|
|
|
HOME="${USER_HOME:-$HOME}"
|
|
|
|
USER="${SUDO_USER:-$USER}"
|
|
|
|
RUN_USER="${SUDO_USER:-$USER}"
|
2022-02-14 16:43:40 -05:00
|
|
|
SRC_DIR="${BASH_SOURCE%/*}"
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
# Set bash options
|
|
|
|
if [[ "$1" == "--debug" ]]; then shift 1 && set -xo pipefail && export SCRIPT_OPTS="--debug" && export _DEBUG="on"; fi
|
2022-07-12 00:25:31 -04:00
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
# Set functions
|
|
|
|
__exec_bash() {
|
|
|
|
local cmd="${*:-/bin/bash}"
|
|
|
|
local exitCode=0
|
|
|
|
echo "Executing command: $cmd"
|
|
|
|
$cmd || exitCode=10
|
|
|
|
return ${exitCode:-$?}
|
|
|
|
}
|
2022-02-14 16:43:40 -05:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-11 13:02:53 -04:00
|
|
|
__find() { ls -A "$*" 2>/dev/null; }
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Define default variables
|
|
|
|
TZ="${TZ:-America/New_York}"
|
|
|
|
HOSTNAME="${HOSTNAME:-casjaysdev-bin}"
|
|
|
|
BIN_DIR="${BIN_DIR:-/usr/local/bin}"
|
2022-07-11 13:02:53 -04:00
|
|
|
DATA_DIR="${DATA_DIR:-$(__find /data/ 2>/dev/null | grep '^' || false)}"
|
|
|
|
CONFIG_DIR="${CONFIG_DIR:-$(__find /config/ 2>/dev/null | grep '^' || false)}"
|
2022-07-12 00:25:31 -04:00
|
|
|
CONFIG_COPY="${CONFIG_COPY:-false}"
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
# Additional variables
|
2022-07-11 13:02:53 -04:00
|
|
|
ARIA2RPCPORT="${ARIA2RPCPORT:-6800}"
|
2022-07-12 00:25:31 -04:00
|
|
|
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Export variables
|
|
|
|
export TZ HOSTNAME
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
# import variables from file
|
|
|
|
[[ -f "/root/env.sh" ]] && . "/root/env.sh"
|
|
|
|
[[ -f "/config/.env.sh" ]] && . "/config/.env.sh"
|
|
|
|
[[ -f "/root/env.sh" ]] && [[ ! -f "/config/.env.sh" ]] && cp -Rf "/root/env.sh" "/config/.env.sh"
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Set timezone
|
|
|
|
[[ -n "${TZ}" ]] && echo "${TZ}" >/etc/timezone
|
2022-07-11 13:02:53 -04:00
|
|
|
[[ -f "/usr/share/zoneinfo/${TZ}" ]] && ln -sf "/usr/share/zoneinfo/${TZ}" "/etc/localtime"
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Set hostname
|
2022-07-11 13:02:53 -04:00
|
|
|
if [[ -n "${HOSTNAME}" ]]; then
|
|
|
|
echo "${HOSTNAME}" >/etc/hostname
|
2022-07-12 00:25:31 -04:00
|
|
|
echo "127.0.0.1 ${HOSTNAME} localhost ${HOSTNAME}.local" >/etc/hosts
|
2022-07-11 13:02:53 -04:00
|
|
|
fi
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Delete any gitkeep files
|
|
|
|
[[ -n "${CONFIG_DIR}" ]] && { [[ -d "${CONFIG_DIR}" ]] && rm -Rf "${CONFIG_DIR}/.gitkeep" || mkdir -p "/config/"; }
|
|
|
|
[[ -n "${DATA_DIR}" ]] && { [[ -d "${DATA_DIR}" ]] && rm -Rf "${DATA_DIR}/.gitkeep" || mkdir -p "/data/"; }
|
|
|
|
[[ -n "${BIN_DIR}" ]] && { [[ -d "${BIN_DIR}" ]] && rm -Rf "${BIN_DIR}/.gitkeep" || mkdir -p "/bin/"; }
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Copy config files to /etc
|
|
|
|
if [[ -n "${CONFIG_DIR}" ]] && [[ "${CONFIG_COPY}" = "true" ]]; then
|
|
|
|
for config in ${CONFIG_DIR}; do
|
2022-07-11 13:02:53 -04:00
|
|
|
if [[ -d "/config/$config" ]]; then
|
2022-07-12 00:25:31 -04:00
|
|
|
[[ -d "/etc/$config" ]] || mkdir -p "/etc/$config"
|
2022-07-11 13:02:53 -04:00
|
|
|
cp -Rf "/config/$config/." "/etc/$config/"
|
|
|
|
elif [[ -f "/config/$config" ]]; then
|
|
|
|
cp -Rf "/config/$config" "/etc/$config"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2022-07-12 00:25:31 -04:00
|
|
|
[[ -f "/etc/.env.sh" ]] && rm -Rf "/etc/.env.sh"
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# Additional commands
|
2022-02-14 16:43:40 -05:00
|
|
|
if [ -f "/aria2/config/ariang.js" ]; then
|
|
|
|
ln -sf "/aria2/config/ariang.js" "/usr/local/www/ariang/js/aria-ng-f1dd57abb9.min.js"
|
|
|
|
else
|
|
|
|
cp -Rf "/etc/ariang.js" "/aria2/config/ariang.js"
|
|
|
|
ln -sf "/aria2/config/ariang.js" "/usr/local/www/ariang/js/aria-ng-f1dd57abb9.min.js"
|
|
|
|
fi
|
2022-07-12 00:25:31 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-02-14 16:43:40 -05:00
|
|
|
# allow Changing of port [unsupported]
|
|
|
|
if [[ -n "$ARIA2RPCPORT" ]] && [[ "$ARIA2RPCPORT" != "6800" ]]; then
|
|
|
|
echo "Changing rpc request port to $ARIA2RPCPORT"
|
|
|
|
sed -i "s|6800|${ARIA2RPCPORT}|g" "/etc/nginx/nginx.conf"
|
|
|
|
sed -i "ss|6800|${ARIA2RPCPORT}|g" ""
|
|
|
|
fi
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-02-14 16:43:40 -05:00
|
|
|
# set rpc secret
|
|
|
|
if [ -n "$RPC_SECRET" ]; then
|
|
|
|
echo "Changing rpc secret to $RPC_SECRET"
|
|
|
|
grep -sq "rpc-secret=*" "/etc/aria2.conf" ||
|
|
|
|
echo "rpc-secret=$RPC_SECRET" >>"/etc/aria2.conf" ||
|
|
|
|
sed -i "s|rpc-secret=.*|rpc-secret=$RPC_SECRET|g" "/etc/aria2.conf"
|
|
|
|
sed -i "s|secret: |secret: $RPC_SECRET|g" "/usr/local/www/ariang/js/aria-ng-f1dd57abb9.min.js"
|
|
|
|
fi
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-02-14 16:43:40 -05:00
|
|
|
[ -d "/etc/nginx" ] && cp -Rf "/etc/nginx/." "/aria2/config/nginx/"
|
|
|
|
[ -f "/etc/aria2.conf" ] && cp -Rf "/etc/aria2.conf" "/aria2/config/aria2.conf"
|
|
|
|
[ -f "/aria2/config/aria2.session" ] || touch "/aria2/config/aria2.session"
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-02-14 16:43:40 -05:00
|
|
|
case "$1" in
|
2022-07-12 00:25:31 -04:00
|
|
|
--help) # Help message
|
2022-07-11 13:02:53 -04:00
|
|
|
echo 'Docker container for '$APPNAME''
|
|
|
|
echo "Usage: $APPNAME [healthcheck, bash, command]"
|
2022-07-12 00:25:31 -04:00
|
|
|
echo "Failed command will have exit code 10"
|
2022-07-11 13:02:53 -04:00
|
|
|
echo
|
|
|
|
exitCode=$?
|
|
|
|
;;
|
2022-07-12 00:25:31 -04:00
|
|
|
|
|
|
|
healthcheck) # Docker healthcheck
|
2022-02-14 16:43:40 -05:00
|
|
|
if curl -q -LSsf -o /dev/null -s -w "200" "http://localhost:$ARIA2RPCPORT"; then
|
2022-07-12 00:25:31 -04:00
|
|
|
echo "$(uname -s) $(uname -m) is running"
|
2022-02-14 16:43:40 -05:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "FAIL"
|
|
|
|
exit 10
|
|
|
|
fi
|
2022-07-12 00:25:31 -04:00
|
|
|
exitCode=$?
|
2022-02-14 16:43:40 -05:00
|
|
|
;;
|
2022-07-12 00:25:31 -04:00
|
|
|
|
|
|
|
*/bin/sh | */bin/bash | bash | shell | sh) # Launch shell
|
2022-07-11 13:02:53 -04:00
|
|
|
shift 1
|
2022-07-12 00:25:31 -04:00
|
|
|
__exec_bash "${@:-/bin/bash}"
|
2022-07-11 13:02:53 -04:00
|
|
|
exitCode=$?
|
2022-02-14 16:43:40 -05:00
|
|
|
;;
|
2022-07-11 13:02:53 -04:00
|
|
|
|
2022-07-12 00:25:31 -04:00
|
|
|
*) # Execute primary command
|
|
|
|
[ -f "/etc/nginx/nginx.conf" ] && nginx -c /etc/nginx/nginx.conf &
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
|
|
[ -f "/etc/aria2.conf" ] &&
|
|
|
|
__exec_bash aria2c --conf-path="/etc/aria2.conf" || exit 10
|
|
|
|
else
|
|
|
|
__exec_bash "/bin/bash"
|
|
|
|
fi
|
|
|
|
exitCode=$?
|
2022-02-14 16:43:40 -05:00
|
|
|
;;
|
|
|
|
esac
|
2022-07-11 13:02:53 -04:00
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2022-07-12 00:25:31 -04:00
|
|
|
# end of entrypoint
|
2022-07-11 13:02:53 -04:00
|
|
|
exit ${exitCode:-$?}
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|