diff --git a/1.8.3/alpine/Dockerfile-amd64 b/1.8.3/alpine/Dockerfile-amd64 index 08e6e400..ac129f4e 100644 --- a/1.8.3/alpine/Dockerfile-amd64 +++ b/1.8.3/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -73,19 +77,20 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/alpine/Dockerfile-arm64 b/1.8.3/alpine/Dockerfile-arm64 index 3fc71beb..108e4bc5 100644 --- a/1.8.3/alpine/Dockerfile-arm64 +++ b/1.8.3/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -73,19 +77,20 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/alpine/Dockerfile-armhf b/1.8.3/alpine/Dockerfile-armhf index 08d3977e..2491dbef 100644 --- a/1.8.3/alpine/Dockerfile-armhf +++ b/1.8.3/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -73,19 +77,20 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/alpine/entrypoint.sh b/1.8.3/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/1.8.3/alpine/entrypoint.sh +++ b/1.8.3/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/1.8.3/debian/Dockerfile-amd64 b/1.8.3/debian/Dockerfile-amd64 index 9b959477..d2f53e72 100644 --- a/1.8.3/debian/Dockerfile-amd64 +++ b/1.8.3/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -77,19 +81,20 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/debian/Dockerfile-arm64 b/1.8.3/debian/Dockerfile-arm64 index b3f1a6cd..7a33449c 100644 --- a/1.8.3/debian/Dockerfile-arm64 +++ b/1.8.3/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -77,19 +81,20 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/debian/Dockerfile-armhf b/1.8.3/debian/Dockerfile-armhf index f695aea5..23cbcdb9 100644 --- a/1.8.3/debian/Dockerfile-armhf +++ b/1.8.3/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -77,19 +81,20 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/configurations ${APPDIR}/addons +VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons # Expose HTTP and HTTPS ports EXPOSE 8080 8443 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/1.8.3/debian/entrypoint.sh b/1.8.3/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/1.8.3/debian/entrypoint.sh +++ b/1.8.3/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.0.0/alpine/Dockerfile-amd64 b/2.0.0/alpine/Dockerfile-amd64 index 968f9851..944d25f6 100644 --- a/2.0.0/alpine/Dockerfile-amd64 +++ b/2.0.0/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/alpine/Dockerfile-arm64 b/2.0.0/alpine/Dockerfile-arm64 index af49f5ea..146f6872 100644 --- a/2.0.0/alpine/Dockerfile-arm64 +++ b/2.0.0/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/alpine/Dockerfile-armhf b/2.0.0/alpine/Dockerfile-armhf index cf3f81ce..483a4bfb 100644 --- a/2.0.0/alpine/Dockerfile-armhf +++ b/2.0.0/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/alpine/entrypoint.sh b/2.0.0/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.0.0/alpine/entrypoint.sh +++ b/2.0.0/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.0.0/alpine/update.sh b/2.0.0/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.0.0/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.0.0/debian/Dockerfile-amd64 b/2.0.0/debian/Dockerfile-amd64 index b12b030d..6d0fa838 100644 --- a/2.0.0/debian/Dockerfile-amd64 +++ b/2.0.0/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/debian/Dockerfile-arm64 b/2.0.0/debian/Dockerfile-arm64 index 7aa294d1..18535a96 100644 --- a/2.0.0/debian/Dockerfile-arm64 +++ b/2.0.0/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/debian/Dockerfile-armhf b/2.0.0/debian/Dockerfile-armhf index 0932911d..1ce06713 100644 --- a/2.0.0/debian/Dockerfile-armhf +++ b/2.0.0/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.0.0/debian/entrypoint.sh b/2.0.0/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.0.0/debian/entrypoint.sh +++ b/2.0.0/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.0.0/debian/update.sh b/2.0.0/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.0.0/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.1.0/alpine/Dockerfile-amd64 b/2.1.0/alpine/Dockerfile-amd64 index d5d1ff19..a1a57d69 100644 --- a/2.1.0/alpine/Dockerfile-amd64 +++ b/2.1.0/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/alpine/Dockerfile-arm64 b/2.1.0/alpine/Dockerfile-arm64 index 25df1ae3..fe61f8f9 100644 --- a/2.1.0/alpine/Dockerfile-arm64 +++ b/2.1.0/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/alpine/Dockerfile-armhf b/2.1.0/alpine/Dockerfile-armhf index 48bf7c66..941349ae 100644 --- a/2.1.0/alpine/Dockerfile-armhf +++ b/2.1.0/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/alpine/entrypoint.sh b/2.1.0/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.1.0/alpine/entrypoint.sh +++ b/2.1.0/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.1.0/alpine/update.sh b/2.1.0/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.1.0/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.1.0/debian/Dockerfile-amd64 b/2.1.0/debian/Dockerfile-amd64 index 0f3fff74..e745a1e5 100644 --- a/2.1.0/debian/Dockerfile-amd64 +++ b/2.1.0/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/debian/Dockerfile-arm64 b/2.1.0/debian/Dockerfile-arm64 index e95b0435..b9e45e8f 100644 --- a/2.1.0/debian/Dockerfile-arm64 +++ b/2.1.0/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/debian/Dockerfile-armhf b/2.1.0/debian/Dockerfile-armhf index 575c201b..3e52ebfa 100644 --- a/2.1.0/debian/Dockerfile-armhf +++ b/2.1.0/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS and Console ports EXPOSE 8080 8443 8101 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.1.0/debian/entrypoint.sh b/2.1.0/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.1.0/debian/entrypoint.sh +++ b/2.1.0/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.1.0/debian/update.sh b/2.1.0/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.1.0/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.2.0/alpine/Dockerfile-amd64 b/2.2.0/alpine/Dockerfile-amd64 index dae7f560..cc380aac 100644 --- a/2.2.0/alpine/Dockerfile-amd64 +++ b/2.2.0/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/alpine/Dockerfile-arm64 b/2.2.0/alpine/Dockerfile-arm64 index e3a50acb..74b3fb53 100644 --- a/2.2.0/alpine/Dockerfile-arm64 +++ b/2.2.0/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/alpine/Dockerfile-armhf b/2.2.0/alpine/Dockerfile-armhf index d322d667..738de335 100644 --- a/2.2.0/alpine/Dockerfile-armhf +++ b/2.2.0/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/alpine/entrypoint.sh b/2.2.0/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.2.0/alpine/entrypoint.sh +++ b/2.2.0/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.2.0/alpine/update.sh b/2.2.0/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.2.0/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.2.0/debian/Dockerfile-amd64 b/2.2.0/debian/Dockerfile-amd64 index e0736c95..31751483 100644 --- a/2.2.0/debian/Dockerfile-amd64 +++ b/2.2.0/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/debian/Dockerfile-arm64 b/2.2.0/debian/Dockerfile-arm64 index e499c5aa..1375a81a 100644 --- a/2.2.0/debian/Dockerfile-arm64 +++ b/2.2.0/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/debian/Dockerfile-armhf b/2.2.0/debian/Dockerfile-armhf index 93ab9266..32341bc6 100644 --- a/2.2.0/debian/Dockerfile-armhf +++ b/2.2.0/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.2.0/debian/entrypoint.sh b/2.2.0/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.2.0/debian/entrypoint.sh +++ b/2.2.0/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.2.0/debian/update.sh b/2.2.0/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.2.0/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.3.0/alpine/Dockerfile-amd64 b/2.3.0/alpine/Dockerfile-amd64 index cf7771f4..d5cedf20 100644 --- a/2.3.0/alpine/Dockerfile-amd64 +++ b/2.3.0/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/alpine/Dockerfile-arm64 b/2.3.0/alpine/Dockerfile-arm64 index 10a53c4c..81f2dc96 100644 --- a/2.3.0/alpine/Dockerfile-arm64 +++ b/2.3.0/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/alpine/Dockerfile-armhf b/2.3.0/alpine/Dockerfile-armhf index 7b236c9f..f41da0da 100644 --- a/2.3.0/alpine/Dockerfile-armhf +++ b/2.3.0/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/alpine/entrypoint.sh b/2.3.0/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.3.0/alpine/entrypoint.sh +++ b/2.3.0/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.3.0/alpine/update.sh b/2.3.0/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.3.0/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.3.0/debian/Dockerfile-amd64 b/2.3.0/debian/Dockerfile-amd64 index 94adda38..e460f25c 100644 --- a/2.3.0/debian/Dockerfile-amd64 +++ b/2.3.0/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/debian/Dockerfile-arm64 b/2.3.0/debian/Dockerfile-arm64 index 413ab925..0488cc19 100644 --- a/2.3.0/debian/Dockerfile-arm64 +++ b/2.3.0/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/debian/Dockerfile-armhf b/2.3.0/debian/Dockerfile-armhf index e33e79eb..3f0e5280 100644 --- a/2.3.0/debian/Dockerfile-armhf +++ b/2.3.0/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.3.0/debian/entrypoint.sh b/2.3.0/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.3.0/debian/entrypoint.sh +++ b/2.3.0/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.3.0/debian/update.sh b/2.3.0/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.3.0/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.4.0/alpine/Dockerfile-amd64 b/2.4.0/alpine/Dockerfile-amd64 index 06ece8d8..acbd1147 100644 --- a/2.4.0/alpine/Dockerfile-amd64 +++ b/2.4.0/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/alpine/Dockerfile-arm64 b/2.4.0/alpine/Dockerfile-arm64 index 3524bd88..28866442 100644 --- a/2.4.0/alpine/Dockerfile-arm64 +++ b/2.4.0/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/alpine/Dockerfile-armhf b/2.4.0/alpine/Dockerfile-armhf index 5e807043..04babb26 100644 --- a/2.4.0/alpine/Dockerfile-armhf +++ b/2.4.0/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/alpine/entrypoint.sh b/2.4.0/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.4.0/alpine/entrypoint.sh +++ b/2.4.0/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.4.0/alpine/update.sh b/2.4.0/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.4.0/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.4.0/debian/Dockerfile-amd64 b/2.4.0/debian/Dockerfile-amd64 index 85dabc40..cdbad729 100644 --- a/2.4.0/debian/Dockerfile-amd64 +++ b/2.4.0/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/debian/Dockerfile-arm64 b/2.4.0/debian/Dockerfile-arm64 index 9cb97f08..bcbe0deb 100644 --- a/2.4.0/debian/Dockerfile-arm64 +++ b/2.4.0/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/debian/Dockerfile-armhf b/2.4.0/debian/Dockerfile-armhf index 4880fd31..d994ca71 100644 --- a/2.4.0/debian/Dockerfile-armhf +++ b/2.4.0/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.4.0/debian/entrypoint.sh b/2.4.0/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.4.0/debian/entrypoint.sh +++ b/2.4.0/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.4.0/debian/update.sh b/2.4.0/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.4.0/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.5.0-snapshot/alpine/Dockerfile-amd64 b/2.5.0-snapshot/alpine/Dockerfile-amd64 index d9f5a873..3657c840 100644 --- a/2.5.0-snapshot/alpine/Dockerfile-amd64 +++ b/2.5.0-snapshot/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/alpine/Dockerfile-arm64 b/2.5.0-snapshot/alpine/Dockerfile-arm64 index f0c00d08..0dbbb08f 100644 --- a/2.5.0-snapshot/alpine/Dockerfile-arm64 +++ b/2.5.0-snapshot/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/alpine/Dockerfile-armhf b/2.5.0-snapshot/alpine/Dockerfile-armhf index d805c355..4a196b56 100644 --- a/2.5.0-snapshot/alpine/Dockerfile-armhf +++ b/2.5.0-snapshot/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/alpine/entrypoint.sh b/2.5.0-snapshot/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.5.0-snapshot/alpine/entrypoint.sh +++ b/2.5.0-snapshot/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.5.0-snapshot/alpine/update.sh b/2.5.0-snapshot/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.5.0-snapshot/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.5.0-snapshot/debian/Dockerfile-amd64 b/2.5.0-snapshot/debian/Dockerfile-amd64 index 05bc23c6..ef4e5bf3 100644 --- a/2.5.0-snapshot/debian/Dockerfile-amd64 +++ b/2.5.0-snapshot/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/debian/Dockerfile-arm64 b/2.5.0-snapshot/debian/Dockerfile-arm64 index deafbdc8..4d394db5 100644 --- a/2.5.0-snapshot/debian/Dockerfile-arm64 +++ b/2.5.0-snapshot/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/debian/Dockerfile-armhf b/2.5.0-snapshot/debian/Dockerfile-armhf index 4e91e019..b1e196b0 100644 --- a/2.5.0-snapshot/debian/Dockerfile-armhf +++ b/2.5.0-snapshot/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0-snapshot/debian/entrypoint.sh b/2.5.0-snapshot/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.5.0-snapshot/debian/entrypoint.sh +++ b/2.5.0-snapshot/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.5.0-snapshot/debian/update.sh b/2.5.0-snapshot/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.5.0-snapshot/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.5.0.M1/alpine/Dockerfile-amd64 b/2.5.0.M1/alpine/Dockerfile-amd64 index 139049ad..59128eff 100644 --- a/2.5.0.M1/alpine/Dockerfile-amd64 +++ b/2.5.0.M1/alpine/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/alpine/Dockerfile-arm64 b/2.5.0.M1/alpine/Dockerfile-arm64 index 0281720d..c0797042 100644 --- a/2.5.0.M1/alpine/Dockerfile-arm64 +++ b/2.5.0.M1/alpine/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/alpine/Dockerfile-armhf b/2.5.0.M1/alpine/Dockerfile-armhf index f37453b7..0ca85f91 100644 --- a/2.5.0.M1/alpine/Dockerfile-armhf +++ b/2.5.0.M1/alpine/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -74,22 +78,26 @@ RUN rm -r "$JAVA_HOME/jre/lib/security/policy/unlimited" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/alpine/entrypoint.sh b/2.5.0.M1/alpine/entrypoint.sh index cfcbb44c..40f5bbfc 100755 --- a/2.5.0.M1/alpine/entrypoint.sh +++ b/2.5.0.M1/alpine/entrypoint.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.5.0.M1/alpine/update.sh b/2.5.0.M1/alpine/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.5.0.M1/alpine/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/2.5.0.M1/debian/Dockerfile-amd64 b/2.5.0.M1/debian/Dockerfile-amd64 index a9e6b72e..321ffeb1 100644 --- a/2.5.0.M1/debian/Dockerfile-amd64 +++ b/2.5.0.M1/debian/Dockerfile-amd64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/debian/Dockerfile-arm64 b/2.5.0.M1/debian/Dockerfile-arm64 index cf622b5e..21a2a596 100644 --- a/2.5.0.M1/debian/Dockerfile-arm64 +++ b/2.5.0.M1/debian/Dockerfile-arm64 @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/debian/Dockerfile-armhf b/2.5.0.M1/debian/Dockerfile-armhf index 126fbfe0..c800c734 100644 --- a/2.5.0.M1/debian/Dockerfile-armhf +++ b/2.5.0.M1/debian/Dockerfile-armhf @@ -16,15 +16,19 @@ ENV \ # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -78,22 +82,26 @@ RUN wget -nv -O /tmp/java.tar.gz "${JAVA_URL}" && \ # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc +COPY update.sh ${OPENHAB_HOME}/runtime/bin/update +RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update # Expose volume with configuration and userdata dir -VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons +VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons # Expose HTTP, HTTPS, Console and LSP ports EXPOSE 8080 8443 8101 5007 # Set working directory and entrypoint -WORKDIR ${APPDIR} +WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/2.5.0.M1/debian/entrypoint.sh b/2.5.0.M1/debian/entrypoint.sh index eec023f9..cf6edb69 100755 --- a/2.5.0.M1/debian/entrypoint.sh +++ b/2.5.0.M1/debian/entrypoint.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/2.5.0.M1/debian/update.sh b/2.5.0.M1/debian/update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/2.5.0.M1/debian/update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/README.md b/README.md index 4cc6903f..15103186 100644 --- a/README.md +++ b/README.md @@ -382,22 +382,26 @@ The following addons are known to depend on the unlimited cryptographic strength ## Upgrading Upgrading OH requires changes to the user mapped in userdata folder. -The container will perform these steps automatically when it detects that the `userdata/etc/version.properties` is different from the version in `userdata.dist/etc/version.properties` in the Docker image. -The steps performed are: +The container will perform these steps automatically when it detects that the `userdata/etc/version.properties` is different from the version in `dist/userdata/etc/version.properties` in the Docker image. +The steps performed are: * Create a `userdata/backup` folder if one does not exist. * Create a full backup of userdata as a dated tar file saved to `userdata/backup`. The `userdata/backup` folder is excluded from this backup. -* Copy over the relevant files from `userdata.dist/etc` to `userdata/etc`. +* Show update notes and warnings. +* Execute update pre/post commands. +* Copy userdata system files from `dist/userdata/etc` to `userdata/etc`. +* Update KAR files in `addons`. * Delete the contents of `userdata/cache` and `userdata/tmp`. The steps performed are the same as those performed by running the upgrade script that comes with OH, except the backup is performed differently and the latest openHAB runtime is not downloaded. +All messages shown during the update are also logged to `userdata/logs/update.log`. ## Building the images -Checkout the GitHub repository, change to a directory with a Dockerfile and then run these commands: +Checkout the GitHub repository, change to a directory containing Dockerfiles (e.g. `2.4.0/debian`) and then run these commands to build and run a amd64 image: ```shell -$ docker build -t openhab/openhab . +$ docker build -f Dockerfile-amd64 -t openhab/openhab . $ docker run openhab/openhab ``` @@ -510,5 +514,4 @@ setcap 'cap_net_bind_service=+ep' /usr/lib/java-8/bin/java ## License -When not explicitly set, files are placed under [![EPL-2.0 license](https://img.shields.io/badge/license-EPL--2.0-blue.svg)](https://raw.githubusercontent.com/openhab/openhab-docker/master/LICENSE). - +When not explicitly set, files are placed under [![EPL-2.0 license](https://img.shields.io/badge/license-EPL--2.0-blue.svg)](https://raw.githubusercontent.com/openhab/openhab-docker/master/LICENSE) diff --git a/entrypoint-alpine.sh b/entrypoint-alpine.sh index cfcbb44c..40f5bbfc 100755 --- a/entrypoint-alpine.sh +++ b/entrypoint-alpine.sh @@ -13,11 +13,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 14 @@ -35,80 +35,44 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID -D -g '' -h ${APPDIR} -G openhab openhab + adduser -u $NEW_USER_ID -D -g '' -h ${OPENHAB_HOME} -G openhab openhab adduser openhab dialout adduser openhab uucp fi -# Copy initial files to host volume + +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - if [ ! -d "${APPDIR}/userdata/backup" ]; then - mkdir "${APPDIR}/userdata/backup" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi - tar -c -f "${APPDIR}/userdata/backup/${backupFile}" --exclude "backup/*" "${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" - - # 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 - - # 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 + tar -c -f "${OPENHAB_BACKUPS}/${backup_file}" --exclude "backup/*" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -117,7 +81,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/entrypoint-debian.sh b/entrypoint-debian.sh index eec023f9..cf6edb69 100755 --- a/entrypoint-debian.sh +++ b/entrypoint-debian.sh @@ -14,11 +14,11 @@ fi # 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 +rm -f "${OPENHAB_HOME}/runtime/instances/instance.properties" # The instance.properties file in openHAB 2.x is installed in the tmp # directory -rm -f /openhab/userdata/tmp/instances/instance.properties +rm -f "${OPENHAB_USERDATA}/tmp/instances/instance.properties" # Add openhab user & handle possible device groups for different host systems # Container base image puts dialout on group id 20, uucp on id 10 @@ -36,7 +36,7 @@ if ! id -u openhab >/dev/null 2>&1; then groupmod --new-name openhab $group_name fi echo "Create user openhab with id ${NEW_USER_ID}" - adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${APPDIR}" --gid $NEW_GROUP_ID openhab + adduser -u $NEW_USER_ID --disabled-password --gecos '' --home "${OPENHAB_HOME}" --gid $NEW_GROUP_ID openhab groupadd -g 14 uucp2 groupadd -g 16 dialout2 groupadd -g 18 dialout3 @@ -51,75 +51,38 @@ if ! id -u openhab >/dev/null 2>&1; then adduser openhab gpio fi -# Copy initial files to host volume +initialize_volume() { + volume="$1" + source="$2" + + if [ -z "$(ls -A "$volume")" ]; then + echo "Initializing empty volume ${volume} ..." + cp -av "${source}/." "${volume}/" + fi +} + +# Initialize empty volumes and update userdata case ${OPENHAB_VERSION} in 1.*) - if [ -z "$(ls -A "${APPDIR}/configurations")" ]; then - # Copy userdata dir for openHAB 1.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/configurations.dist/." "${APPDIR}/configurations/" - fi + initialize_volume "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist/configurations" ;; 2.*) - # Initialize empty host volumes - if [ -z "$(ls -A "${APPDIR}/userdata")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No userdata found... initializing." - cp -av "${APPDIR}/userdata.dist/." "${APPDIR}/userdata/" - fi + initialize_volume "${OPENHAB_CONF}" "${OPENHAB_HOME}/dist/conf" + initialize_volume "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist/userdata" - # 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." + # Update userdata if versions do not match + if [ ! -z $(cmp "${OPENHAB_USERDATA}/etc/version.properties" "${OPENHAB_HOME}/dist/userdata/etc/version.properties") ]; then + echo "Image and userdata versions differ! Starting an upgrade." | tee "${OPENHAB_LOGDIR}/update.log" # Make a backup of userdata - backupFile=userdata-$(date +"%FT%H-%M-%S").tar - 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" - - # 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" + backup_file=userdata-$(date +"%FT%H-%M-%S").tar + if [ ! -d "${OPENHAB_BACKUPS}" ]; then + mkdir "${OPENHAB_BACKUPS}" fi + tar --exclude="${OPENHAB_BACKUPS}" -c -f "${OPENHAB_BACKUPS}/${backup_file}" "${OPENHAB_USERDATA}" + echo "You can find backup of userdata in ${OPENHAB_BACKUPS}/${backup_file}" | tee -a "${OPENHAB_LOGDIR}/update.log" - # 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 - - if [ -z "$(ls -A "${APPDIR}/conf")" ]; then - # Copy userdata dir for openHAB 2.x - echo "No configuration found... initializing." - cp -av "${APPDIR}/conf.dist/." "${APPDIR}/conf/" + exec "${OPENHAB_HOME}/runtime/bin/update" 2>&1 | tee -a "${OPENHAB_LOGDIR}/update.log" fi ;; *) @@ -128,7 +91,7 @@ case ${OPENHAB_VERSION} in esac # Set openhab folder permission -chown -R openhab:openhab "${APPDIR}" +chown -R openhab:openhab "${OPENHAB_HOME}" sync # Run s6-style init continuation scripts if existent diff --git a/openhab2-update.sh b/openhab2-update.sh new file mode 100755 index 00000000..f1ecf4cd --- /dev/null +++ b/openhab2-update.sh @@ -0,0 +1,194 @@ +#!/bin/sh + +setup() { + # Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + current_version="$(awk '/openhab-distro/{print $3}' "${OPENHAB_USERDATA}/etc/version.properties")" + oh_version="$(echo "${OPENHAB_VERSION}" | sed 's/snapshot/SNAPSHOT/')" + milestone_version="$(echo "${oh_version}" | awk -F'.' '{print $4}')" + + # Choose bintray for releases, jenkins for snapshots and artifactory for milestones or release candidates. + if test "${oh_version#*-SNAPSHOT}" != "${oh_version}"; then + addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons/target/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://ci.openhab.org/job/openHAB-Distribution/lastSuccessfulBuild/artifact/distributions/openhab-addons-legacy/target/openhab-addons-legacy-${oh_version}.kar" + elif [ "${oh_version}" = "$current_version" ]; then + echo "You are already on openHAB $current_version" >&2 + exit 1 + elif [ -n "$milestone_version" ]; then + addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons/${oh_version}/openhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://openhab.jfrog.io/openhab/libs-milestone-local/org/openhab/distro/openhab-addons-legacy/${oh_version}/openhab-addons-legacy-${oh_version}.kar" + else + addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons%2F${oh_version}%2Fopenhab-addons-${oh_version}.kar" + legacy_addons_download_location="https://bintray.com/openhab/mvn/download_file?file_path=org%2Fopenhab%2Fdistro%2Fopenhab-addons-legacy%2F${oh_version}%2Fopenhab-addons-legacy-${oh_version}.kar" + fi +} + +run_command() { + string="$1" + string="$(echo "$string" | sed "s:\$OPENHAB_USERDATA:${OPENHAB_USERDATA:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_CONF:${OPENHAB_CONF:?}:g")" + string="$(echo "$string" | sed "s:\$OPENHAB_HOME:${OPENHAB_HOME:?}:g")" + + command="$(echo "$string" | awk -F';' '{print $1}')" + param1="$(echo "$string" | awk -F';' '{print $2}')" + param2="$(echo "$string" | awk -F';' '{print $3}')" + param3="$(echo "$string" | awk -F';' '{print $4}')" + + case $command in + 'DEFAULT') + # Just rename the file, the update process adds back the new version + echo " Adding '.bak' to $param1" + mv "$param1" "$param1.bak" + ;; + 'DELETE') + # We should be strict and specific here, i.e only delete one file. + if [ -f "$param1" ]; then + echo " Deleting File: $param1" + rm -f "$param1" + fi + ;; + 'DELETEDIR') + # We should be strict and specific here, i.e only delete one directory. + if [ -d "$param1" ]; then + echo " Deleting Directory: $param1" + rm -rf "$param1" + fi + ;; + 'MOVE') + echo " Moving: From $param1 to $param2" + mv "$param1" "$param2" + ;; + 'REPLACE') + # Avoid error if file does not exist + if [ -f "$param3" ]; then + echo " Replacing: String $param1 to $param2 in file $param3" + sed -i "s:$param1:$param2:g" "$param3" + fi + ;; + 'NOTE') printf ' \033[32mNote:\033[m %s\n' "$param1";; + 'ALERT') printf ' \033[31mWarning:\033[m %s\n' "$param1";; + esac +} + +get_version_number() { + first_part="$(echo "$1" | awk -F'.' '{print $1}')" + second_part="$(echo "$1" | awk -F'.' '{print $2}')" + third_part="$(echo "$1" | awk -F'.' '{print $3}')" + third_part="${third_part%%-*}" + echo $((first_part*10000+second_part*100+third_part)) +} + +scan_versioning_list() { + section="$1" + version_message="$2" + in_section=false + in_new_version=false + + # Read the file line by line. + while IFS= read -r line + do + case $line in + '') + continue + ;; + # Flag to run the relevant [[section]] only. + "[[$section]]") + in_section=true + ;; + # Stop reading the file if another [[section]] starts. + "[["*"]]") + if $in_section; then + break + fi + ;; + # Detect the [version] and execute the line if relevant. + '['*'.'*'.'*']') + if $in_section; then + line_version="$(echo "$line" | awk -F'[][]' '{print $2}')" + line_version_number=$(get_version_number "$line_version") + if [ "$current_version_number" -lt "$line_version_number" ]; then + in_new_version=true + echo "" + echo "$version_message $line_version:" + else + in_new_version=false + fi + fi + ;; + *) + if $in_section && $in_new_version; then + run_command "$line" + fi + ;; + esac + done < "${OPENHAB_HOME}/runtime/bin/update.lst" +} + +echo "" +echo "################################################" +echo " openHAB 2.x.x Docker update script " +echo "################################################" +echo "" + +# Run the initialisation functions defined above +setup + +current_version_number=$(get_version_number "$current_version") +case $current_version in + *"-"* | *"."*"."*"."*) current_version_number=$((current_version_number-1));; +esac + +# Notify the user of important changes first +echo "The script will attempt to update openHAB to version ${oh_version}" +printf 'Please read the following \033[32mnotes\033[m and \033[31mwarnings\033[m:\n' +scan_versioning_list "MSG" "Important notes for version" +echo "" + +# Perform version specific pre-update commands +scan_versioning_list "PRE" "Performing pre-update tasks for version" + +echo "Replacing userdata system files with newer versions..." +while IFS= read -r file_name +do + full_path="${OPENHAB_HOME}/dist/userdata/etc/${file_name}" + if [ -f "$full_path" ]; then + cp "$full_path" "${OPENHAB_USERDATA}/etc/" + fi +done < "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" + +# Clearing the cache and tmp folders is necessary for upgrade. +echo "Clearing cache..." +rm -rf "${OPENHAB_USERDATA:?}/cache" +rm -rf "${OPENHAB_USERDATA:?}/tmp" + +# Perform version specific post-update commands +scan_versioning_list "POST" "Performing post-update tasks for version" + +# If there's an existing addons file, we need to replace it with the correct version. +addons_file="${OPENHAB_HOME}/addons/openhab-addons-${current_version}.kar" +if [ -f "$addons_file" ]; then + echo "Found an openHAB addons file, replacing with new version..." + rm -f "${addons_file:?}" + curl -Lf# "$addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-${oh_version}.kar" || { + echo "Download of addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi + +# Do the same for the legacy addons file. +legacy_addons_file="${OPENHAB_HOME}/addons/openhab-addons-legacy-${current_version}.kar" +if [ -f "$legacy_addons_file" ]; then + echo "Found an openHAB legacy addons file, replacing with new version..." + rm -f "${legacy_addons_file:?}" + curl -Lf# "$legacy_addons_download_location" -o "${OPENHAB_HOME}/addons/openhab-addons-legacy-${oh_version}.kar" || { + echo "Download of legacy addons file failed, please find it on the openHAB website (www.openhab.org)" >&2 + } +fi +echo "" + +echo "" +echo "SUCCESS: openHAB updated from ${current_version} to ${oh_version}" +echo "" diff --git a/update-docker-files.sh b/update-docker-files.sh index 94fec675..1d551d68 100755 --- a/update-docker-files.sh +++ b/update-docker-files.sh @@ -90,15 +90,19 @@ print_basemetadata() { cat >> $1 <<-'EOI' # Set variables and locales ENV \ - APPDIR="/openhab" \ CRYPTO_POLICY="limited" \ EXTRA_JAVA_OPTS="" \ KARAF_EXEC="exec" \ LC_ALL="en_US.UTF-8" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US.UTF-8" \ + OPENHAB_BACKUPS="/openhab/userdata/backup" \ + OPENHAB_CONF="/openhab/conf" \ + OPENHAB_HOME="/openhab" \ OPENHAB_HTTP_PORT="8080" \ - OPENHAB_HTTPS_PORT="8443" + OPENHAB_HTTPS_PORT="8443" \ + OPENHAB_LOGDIR="/openhab/userdata/logs" \ + OPENHAB_USERDATA="/openhab/userdata" # Set arguments on build ARG BUILD_DATE @@ -206,9 +210,10 @@ print_openhab_install_oh1() { cat >> $1 <<-'EOI' # Install openHAB RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" && \ rm /tmp/openhab.zip && \ - cp -a "${APPDIR}/configurations" "${APPDIR}/configurations.dist" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_HOME}/configurations" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc EOI @@ -220,13 +225,17 @@ print_openhab_install_oh2() { # Install openHAB # Set permissions for openHAB. Export TERM variable. See issue #30 for details! RUN wget -nv -O /tmp/openhab.zip "${OPENHAB_URL}" && \ - unzip -q /tmp/openhab.zip -d "${APPDIR}" -x "*.bat" && \ + unzip -q /tmp/openhab.zip -d "${OPENHAB_HOME}" -x "*.bat" "*.ps1" "*.psm1" && \ rm /tmp/openhab.zip && \ - mkdir -p "${APPDIR}/userdata/logs" && \ - touch "${APPDIR}/userdata/logs/openhab.log" && \ - cp -a "${APPDIR}/userdata" "${APPDIR}/userdata.dist" && \ - cp -a "${APPDIR}/conf" "${APPDIR}/conf.dist" && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/update.lst" ]; then touch "${OPENHAB_HOME}/runtime/bin/update.lst"; fi && \ + if [ ! -f "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" ]; then wget -nv -O "${OPENHAB_HOME}/runtime/bin/userdata_sysfiles.lst" "https://raw.githubusercontent.com/openhab/openhab-distro/2.4.0/distributions/openhab/src/main/resources/bin/userdata_sysfiles.lst"; fi && \ + mkdir -p "${OPENHAB_LOGDIR}" && \ + touch "${OPENHAB_LOGDIR}/openhab.log" && \ + mkdir -p "${OPENHAB_HOME}/dist" && \ + cp -a "${OPENHAB_CONF}" "${OPENHAB_USERDATA}" "${OPENHAB_HOME}/dist" && \ echo 'export TERM=${TERM:=dumb}' | tee -a ~/.bashrc + COPY update.sh ${OPENHAB_HOME}/runtime/bin/update + RUN chmod +x ${OPENHAB_HOME}/runtime/bin/update EOI } @@ -235,7 +244,7 @@ EOI print_volumes_oh1() { cat >> $1 <<-'EOI' # Expose volume with configuration and userdata dir - VOLUME ${APPDIR}/configurations ${APPDIR}/addons + VOLUME ${OPENHAB_HOME}/configurations ${OPENHAB_HOME}/addons EOI } @@ -244,7 +253,7 @@ EOI print_volumes_oh2() { cat >> $1 <<-'EOI' # Expose volume with configuration and userdata dir - VOLUME ${APPDIR}/conf ${APPDIR}/userdata ${APPDIR}/addons + VOLUME ${OPENHAB_CONF} ${OPENHAB_USERDATA} ${OPENHAB_HOME}/addons EOI } @@ -280,7 +289,7 @@ EOI print_entrypoint() { cat >> $1 <<-'EOI' # Set working directory and entrypoint - WORKDIR ${APPDIR} + WORKDIR ${OPENHAB_HOME} COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] @@ -412,15 +421,17 @@ do generate_docker_files # Generate multi-architecture manifest - file="$version/$base/manifest.yml" - generate_manifest $file + generate_manifest "$version/$base/manifest.yml" # Copy base specific entrypoint.sh - file="$version/$base/entrypoint.sh" - if [ "$base" == "alpine" ]; then - cp entrypoint-alpine.sh $file - else - cp entrypoint-debian.sh $file - fi + case $base in + alpine) cp "entrypoint-alpine.sh" "$version/$base/entrypoint.sh";; + debian) cp "entrypoint-debian.sh" "$version/$base/entrypoint.sh";; + esac + + # Copy version specific update script + case $version in + 2.*) cp "openhab2-update.sh" "$version/$base/update.sh";; + esac done done