2017-02-11 12:06:24 +00:00
|
|
|
#!/bin/bash -x
|
2017-12-22 00:19:34 +00:00
|
|
|
|
2018-06-22 21:55:23 +00:00
|
|
|
interactive=$(if test -t 0; then echo true; else echo false; fi)
|
2017-02-11 12:06:24 +00:00
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
2018-01-12 19:46:37 +00:00
|
|
|
# Install Java unlimited strength cryptography
|
|
|
|
if [ "${CRYPTO_POLICY}" = "unlimited" ] && [ ! -f "${JAVA_HOME}/jre/lib/security/README.txt" ]; then
|
|
|
|
echo "Installing Zulu Cryptography Extension Kit (\"CEK\")..."
|
|
|
|
wget -q -O /tmp/ZuluJCEPolicies.zip https://cdn.azul.com/zcek/bin/ZuluJCEPolicies.zip
|
2018-12-24 12:33:51 +00:00
|
|
|
unzip -jo -d "${JAVA_HOME}/jre/lib/security" /tmp/ZuluJCEPolicies.zip
|
2018-01-12 19:46:37 +00:00
|
|
|
rm /tmp/ZuluJCEPolicies.zip
|
|
|
|
fi
|
|
|
|
|
2017-12-21 21:25:46 +00:00
|
|
|
# Deleting instance.properties to avoid karaf PID conflict on restart
|
|
|
|
# See: https://github.com/openhab/openhab-docker/issues/99
|
|
|
|
rm -f /openhab/runtime/instances/instance.properties
|
|
|
|
|
2018-11-07 20:41:02 +00:00
|
|
|
# The instance.properties file in openHAB 2.x is installed in the tmp
|
2017-12-21 21:25:46 +00:00
|
|
|
# directory
|
|
|
|
rm -f /openhab/userdata/tmp/instances/instance.properties
|
|
|
|
|
2017-02-11 12:06:24 +00:00
|
|
|
# Add openhab user & handle possible device groups for different host systems
|
|
|
|
# Container base image puts dialout on group id 20, uucp on id 10
|
|
|
|
# GPIO Group for RPI access
|
|
|
|
NEW_USER_ID=${USER_ID:-9001}
|
2017-09-14 20:56:19 +00:00
|
|
|
NEW_GROUP_ID=${GROUP_ID:-$NEW_USER_ID}
|
|
|
|
echo "Starting with openhab user id: $NEW_USER_ID and group id: $NEW_GROUP_ID"
|
2017-02-11 12:06:24 +00:00
|
|
|
if ! id -u openhab >/dev/null 2>&1; then
|
2019-01-07 23:03:21 +00:00
|
|
|
if [ -z "$(getent group $NEW_GROUP_ID)" ]; then
|
|
|
|
echo "Create group openhab with id ${NEW_GROUP_ID}"
|
|
|
|
groupadd -g $NEW_GROUP_ID openhab
|
|
|
|
else
|
|
|
|
group_name=$(getent group $NEW_GROUP_ID | cut -d: -f1)
|
|
|
|
echo "Rename group $group_name to openhab"
|
|
|
|
groupmod --new-name openhab $group_name
|
|
|
|
fi
|
2017-06-28 21:39:27 +00:00
|
|
|
echo "Create user openhab with id ${NEW_USER_ID}"
|
2018-12-24 12:33:51 +00:00
|
|
|
adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab
|
2017-12-21 21:25:46 +00:00
|
|
|
groupadd -g 14 uucp2
|
|
|
|
groupadd -g 16 dialout2
|
|
|
|
groupadd -g 18 dialout3
|
|
|
|
groupadd -g 32 uucp3
|
|
|
|
groupadd -g 997 gpio
|
|
|
|
adduser openhab dialout
|
|
|
|
adduser openhab uucp
|
|
|
|
adduser openhab uucp2
|
|
|
|
adduser openhab dialout2
|
|
|
|
adduser openhab dialout3
|
|
|
|
adduser openhab uucp3
|
|
|
|
adduser openhab gpio
|
2017-02-11 12:06:24 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Copy initial files to host volume
|
|
|
|
case ${OPENHAB_VERSION} in
|
2018-11-07 20:41:02 +00:00
|
|
|
1.*)
|
2017-02-11 12:06:24 +00:00
|
|
|
if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then
|
2018-11-07 20:41:02 +00:00
|
|
|
# Copy userdata dir for openHAB 1.x
|
2017-02-11 12:06:24 +00:00
|
|
|
echo "No configuration found... initializing."
|
|
|
|
cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/"
|
|
|
|
fi
|
|
|
|
;;
|
2018-11-07 20:41:02 +00:00
|
|
|
2.*)
|
2017-02-11 12:06:24 +00:00
|
|
|
# Initialize empty host volumes
|
|
|
|
if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then
|
2018-11-07 20:41:02 +00:00
|
|
|
# Copy userdata dir for openHAB 2.x
|
2017-02-11 12:06:24 +00:00
|
|
|
echo "No userdata found... initializing."
|
|
|
|
cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/"
|
|
|
|
fi
|
|
|
|
|
2018-01-10 19:16:32 +00:00
|
|
|
# Upgrade userdata if versions do not match
|
|
|
|
if [ ! -z $(cmp "${APPDIR}/userdata/etc/version.properties" "${APPDIR}/userdata.dist/etc/version.properties") ]; then
|
|
|
|
echo "Image and userdata versions differ! Starting an upgrade."
|
|
|
|
|
|
|
|
# Make a backup of userdata
|
2018-03-21 22:23:11 +00:00
|
|
|
backupFile=userdata-$(date +"%FT%H-%M-%S").tar
|
2018-01-10 19:16:32 +00:00
|
|
|
if [ ! -d "${APPDIR}/userdata/backup" ]; then
|
|
|
|
mkdir "${APPDIR}/userdata/backup"
|
|
|
|
fi
|
|
|
|
tar --exclude="${APPDIR}/userdata/backup" -c -f "${APPDIR}/userdata/backup/${backupFile}" "${APPDIR}/userdata"
|
|
|
|
echo "You can find backup of userdata in ${APPDIR}/userdata/backup/${backupFile}"
|
|
|
|
|
|
|
|
# Copy over the updated files
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/all.policy" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/branding.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/branding-ssh.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/config.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/custom.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
if [ -f "${APPDIR}/userdata.dist/etc/custom.system.properties" ]; then
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/custom.system.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
fi
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/distribution.info" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/jre.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/org.apache.karaf"* "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/org.ops4j.pax.url.mvn.cfg" "${APPDIR}/userdata/etc/"
|
|
|
|
if [ -f "${APPDIR}/userdata.dist/etc/overrides.properties" ]; then
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/overrides.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
fi
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/profile.cfg" "${APPDIR}/userdata/etc/"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/startup.properties" "${APPDIR}/userdata/etc"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/system.properties" "${APPDIR}/userdata/etc"
|
|
|
|
cp "${APPDIR}/userdata.dist/etc/version.properties" "${APPDIR}/userdata/etc/"
|
|
|
|
echo "Replaced files in userdata/etc with newer versions"
|
|
|
|
|
2018-03-25 21:49:23 +00:00
|
|
|
# Remove necessary files after installation
|
|
|
|
rm -rf "${APPDIR}/userdata/etc/org.openhab.addons.cfg"
|
|
|
|
if [ ! -f "${APPDIR}/userdata.dist/etc/overrides.properties" ]; then
|
|
|
|
rm -rf "${APPDIR}/userdata/etc/overrides.properties"
|
|
|
|
fi
|
|
|
|
|
2018-01-10 19:16:32 +00:00
|
|
|
# Clear the cache and tmp
|
|
|
|
rm -rf "${APPDIR}/userdata/cache"
|
|
|
|
rm -rf "${APPDIR}/userdata/tmp"
|
|
|
|
mkdir "${APPDIR}/userdata/cache"
|
|
|
|
mkdir "${APPDIR}/userdata/tmp"
|
|
|
|
echo "Cleared the cache and tmp"
|
|
|
|
fi
|
|
|
|
|
2017-02-11 12:06:24 +00:00
|
|
|
if [ -z "$(ls -A "${APPDIR}/conf")" ]; then
|
2018-11-07 20:41:02 +00:00
|
|
|
# Copy userdata dir for openHAB 2.x
|
2017-02-11 12:06:24 +00:00
|
|
|
echo "No configuration found... initializing."
|
|
|
|
cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
2018-12-24 12:33:51 +00:00
|
|
|
echo "openHAB version ${OPENHAB_VERSION} not supported!"
|
2017-02-11 12:06:24 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2018-11-12 23:08:06 +00:00
|
|
|
# Run s6-style init continuation scripts if existent
|
|
|
|
if [ -d /etc/cont-init.d ]
|
|
|
|
then
|
2018-11-18 22:38:04 +00:00
|
|
|
for script in $(find /etc/cont-init.d -type f | grep -v \~ | sort)
|
2018-11-12 23:08:06 +00:00
|
|
|
do
|
2018-12-24 12:33:51 +00:00
|
|
|
. "${script}"
|
2018-11-12 23:08:06 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2017-02-11 12:06:24 +00:00
|
|
|
# Set openhab folder permission
|
2018-12-24 12:33:51 +00:00
|
|
|
chown -R openhab:openhab "${APPDIR}"
|
2018-10-22 21:24:00 +00:00
|
|
|
sync
|
2017-02-11 12:06:24 +00:00
|
|
|
|
2018-06-22 21:55:23 +00:00
|
|
|
# Use server mode with the default command when there is no pseudo-TTY
|
2018-12-04 22:56:22 +00:00
|
|
|
if [ "$interactive" == "false" ] && [ "$(IFS=" "; echo "$@")" == "gosu openhab tini -s ./start.sh" ]; then
|
2018-06-22 21:55:23 +00:00
|
|
|
command=($@ server)
|
|
|
|
exec "${command[@]}"
|
|
|
|
else
|
|
|
|
exec "$@"
|
|
|
|
fi
|