144 lines
4.9 KiB
Bash
144 lines
4.9 KiB
Bash
#!/bin/sh
|
|
|
|
# Try different way to get the memory allocated to the container
|
|
get_max_mem(){
|
|
if [ -n "${NOMAD_MEMORY_LIMIT}" ]; then
|
|
echo ${NOMAD_MEMORY_LIMIT}
|
|
elif [ -e /sys/fs/cgroup/memory.max ]; then
|
|
# Read /sys/fs/cgroup/memory.max
|
|
MAX=$(cat /sys/fs/cgroup/memory.max)
|
|
# If it's "max", then the container has no limit, and we must detect the available RAM
|
|
if [ "${MAX}" = "max" ]; then
|
|
echo $(($(cat /proc/meminfo | grep MemTotal | sed -E 's/MemTotal:\s+([0-9]+)\s+kB/\1/')/1024))
|
|
else
|
|
echo $(($(cat /sys/fs/cgroup/memory.max)/1024/1024))
|
|
fi
|
|
# memory.limit_in_bytes can contain the max memory allocated to the container
|
|
# but if the container has no limit, it'll contain 9223372036854771712 in which case we must fallback to /proc/meminfo
|
|
elif [ -e /sys/fs/cgroup/memory/memory.limit_in_bytes -a "$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)" != "9223372036854771712" ]; then
|
|
echo $(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)/1024/1024))
|
|
else
|
|
echo $(($(cat /proc/meminfo | grep MemTotal | sed -E 's/MemTotal:\s+([0-9]+)\s+kB/\1/')/1024))
|
|
fi
|
|
}
|
|
|
|
# Parse an URL
|
|
# Largely inspired by https://stackoverflow.com/questions/6174220/parse-url-in-shell-script
|
|
parse_url(){
|
|
local URL=$1
|
|
local PROTO="$(echo ${URL} | sed -nr 's,^(.*://).*,\1,p')"
|
|
URL="$(echo ${URL/${PROTO}/})"
|
|
local USER_PASS="$(echo ${URL} | sed -nr 's,^(.*)@.*,\1,p')"
|
|
local USER="$(echo ${USER_PASS} | cut -d: -f1)"
|
|
local PASSWORD="$(echo ${USER_PASS} | cut -d: -f2 | sed -E 's,@$,,')"
|
|
URL="$(echo ${URL/${USER_PASS}/})"
|
|
local PORT="$(echo ${URL} | sed -nr 's,.*:([0-9]+).*,\1,p')"
|
|
URL="$(echo ${URL/${PORT}/})"
|
|
local URL_PATH="$(echo ${URL} | sed -nr 's,[^/:]*([/:].*),\1,p')"
|
|
local HOST="$(echo ${URL/$URL_PATH/} | sed -E 's,^@,,')"
|
|
case $2 in
|
|
proto)
|
|
echo -n "${PROTO}"
|
|
;;
|
|
user)
|
|
echo -n "${USER}"
|
|
;;
|
|
password)
|
|
echo -n "${PASSWORD}"
|
|
;;
|
|
port)
|
|
echo -n "${PORT}"
|
|
;;
|
|
host)
|
|
echo -n "${HOST}"
|
|
;;
|
|
path)
|
|
echo -n "${PATH}"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
if [ -n "${TMPDIR}" ]; then
|
|
[ -d "${TMPDIR}" ] || mkdir -p ${TMPDIR}
|
|
JAVA_OPTS="$JAVA_OPTS -Djava.io.tmpdir=${TMPDIR}"
|
|
fi
|
|
|
|
JAVA_OPTS="${JAVA_OPTS:-} -Djava.awt.headless=true -Dfile.encoding=UTF8"
|
|
|
|
# Disable class sharing (as only one JVM is running within a container)
|
|
JAVA_OPTS="${JAVA_OPTS} -Xshare:off"
|
|
|
|
# Enable container detection suport from JVM
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:+UseContainerSupport"
|
|
|
|
# Enable Native memory tracking
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:NativeMemoryTracking=summary"
|
|
|
|
# Force container to stop on OOM
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:+ExitOnOutOfMemoryError"
|
|
|
|
|
|
if [ -z "${JVM_XMX}" ]; then
|
|
export JVM_XMX=$(printf "%.0f" $(echo $(get_max_mem)*${JVM_XMX_RATIO} | bc))m
|
|
fi
|
|
|
|
if [ -n "${JVM_MAXRAM}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:MaxRAM=${JVM_MAXRAM}"
|
|
if [ -n "${JVM_MINRAM_PERCENTAGE}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:MinRAMPercentage=${JVM_MINRAM_PERCENTAGE}"
|
|
fi
|
|
if [ -n "${JVM_MAXRAM_PERCENTAGE}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -XX:MaxRAMPercentage=${JVM_MAXRAM_PERCENTAGE}"
|
|
fi
|
|
else
|
|
if [ -n "${JVM_XMX}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Xmx${JVM_XMX}"
|
|
fi
|
|
if [ -n "${JVM_XMS}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Xms${JVM_XMS}"
|
|
fi
|
|
if [ -n "${JVM_XSS}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Xss${JVM_XSS}"
|
|
fi
|
|
fi
|
|
|
|
if [ -n "${HTTP_MAX_CONNECTIONS}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttp.maxConnections=${HTTP_MAX_CONNECTIONS}"
|
|
fi
|
|
|
|
# Handle proxy options
|
|
# Parse standard HTTP_PROXY which is usually passed as https://proxy:3128 and extract the host and the port
|
|
if [ -n "${HTTP_PROXY}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttp.proxyHost=$(parse_url "${HTTP_PROXY}" "host")"
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttp.proxyPort=$(parse_url "${HTTP_PROXY}" "port")"
|
|
USER=$(parse_url "${HTTP_PROXY}" "user")
|
|
PASSWORD=$(parse_url "${HTTP_PROXY}" "password")
|
|
if [ -n "${USER}" -a -n "${PASSWORD}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttp.proxyUser=${USER} -Dhttp.proxyPassword='${PASSWORD}'"
|
|
fi
|
|
fi
|
|
# Same for HTTPS_PROXY
|
|
if [ -n "${HTTPS_PROXY}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttps.proxyHost=$(parse_url "${HTTPS_PROXY}" "host")"
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttps.proxyPort=$(parse_url "${HTTPS_PROXY}" "port")"
|
|
USER=$(parse_url "${HTTPS_PROXY}" "user")
|
|
PASSWORD=$(parse_url "${HTTPS_PROXY}" "password")
|
|
if [ -n "${USER}" -a -n "${PASSWORD}" ]; then
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttps.proxyUser=${USER} -Dhttps.proxyPassword='${PASSWORD}'"
|
|
fi
|
|
fi
|
|
# For NO_PROXY, we need to replace , with |
|
|
# but also handle wildcards like .sub.domain.com should become *.sub.domain.com
|
|
if [ -n "${NO_PROXY}" ]; then
|
|
JAVA_NO_PROXY=''
|
|
for NP in $(echo "${NO_PROXY}" | sed -E 's/,/ /g'); do
|
|
JAVA_NO_PROXY="${JAVA_NO_PROXY}|$(echo -n ${NP} | sed -E 's/^\./*./')"
|
|
done
|
|
# Remove leading |
|
|
JAVA_NO_PROXY=$(echo -n ${JAVA_NO_PROXY} | sed -E 's/^\|//')
|
|
JAVA_OPTS="${JAVA_OPTS} -Dhttp.nonProxyHosts=${JAVA_NO_PROXY}"
|
|
fi
|
|
|
|
echo "JAVA_OPTS=${JAVA_OPTS}"
|
|
export JAVA_OPTS=${JAVA_OPTS}
|