From 22643914c2fa59e74526ee97f6f8b7ac6d5d1e90 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Sun, 31 May 2020 17:54:52 -0600 Subject: [PATCH] Rewrite 'packages.bash' Signed-off-by: Ethan Dye --- .travis.yml | 34 +- Dockerfile.openhabian-ARMv6hf-emulated | 5 + Dockerfile.openhabian-ARMv6hf-native | 5 + ...ive => Dockerfile.openhabian-aarch64-32bit | 6 +- Dockerfile.openhabian-aarch64-64bit | 9 +- Dockerfile.openhabian-arm32hf-CM3 | 6 +- Dockerfile.openhabian-arm32hf-emulated | 5 + Dockerfile.openhabian-arm32hf-native | 5 + Dockerfile.openhabian-x86 | 5 + build-image/first-boot.bash | 5 +- build-image/openhabian.conf | 6 +- build.bash | 2 +- functions/dev.bats | 9 +- functions/helpers.bash | 57 +- functions/helpers.bats | 4 +- functions/influxdb+grafana.bats | 25 +- functions/java-jre.bash | 325 ++++--- functions/java-jre.bats | 29 +- functions/nodejs-apps.bats | 6 +- functions/packages.bash | 861 +++++++++--------- functions/packages.bats | 29 +- functions/zram.bats | 20 +- openhabian-setup.sh | 2 +- 23 files changed, 762 insertions(+), 698 deletions(-) rename Dockerfile.openhabian-aarch64-native => Dockerfile.openhabian-aarch64-32bit (90%) diff --git a/.travis.yml b/.travis.yml index 0a6403713..158c220b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,14 +44,14 @@ jobs: arch: arm64 script: - sed -i 's/^java_opt=.*/java_opt=Zulu8-64/' build-image/openhabian.conf - - docker build -t openhabian/dev-openhabian -f Dockerfile.openhabian-aarch64-native . + - docker build -t openhabian/dev-openhabian -f Dockerfile.openhabian-aarch64-32bit . - docker run -i openhabian/dev-openhabian bash -c '/usr/local/bin/bats -r -f "dev-." .' - docker run -i openhabian/dev-openhabian bash -c '/usr/local/bin/bats -r -f "unit-." .' - stage: BATS and shellcheck tests env: aarch64 arch: arm64 script: - - docker build -t openhabian/bats-openhabian -f Dockerfile.openhabian-aarch64-native . + - docker build -t openhabian/bats-openhabian -f Dockerfile.openhabian-aarch64-32bit . - docker run --name "bats-inst-tests" -i openhabian/bats-openhabian bash -c '/usr/local/bin/bats -r -f "installation-." .' - docker run --name "bats-dest-tests" -i openhabian/bats-openhabian bash -c '/usr/local/bin/bats -r -f "destructive-." .' - shellcheck -x -s bash openhabian-setup.sh functions/*.bash build-image/*.bash build.bash && echo "shellcheck - OK" @@ -62,9 +62,9 @@ jobs: - export HW=x86 - sed -i 's/^java_opt=.*/java_opt=Zulu8-64/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-x86 . - - echo -e "\n\e[36mopenHABian test installation 64bit Azul Enterprise Java on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 64bit Azul Enterprise Java on virtual $HW ...\e[0m\n" - docker run --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' + - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: arm32hf arch: arm64 script: @@ -72,9 +72,9 @@ jobs: - export HW=rpi01 - sed -i 's/^java_opt=.*/java_opt=Zulu8-32/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-ARMv6hf-native . - - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\e[0m\n" - docker run --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' + - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: arm32hf arch: arm64 script: @@ -82,7 +82,7 @@ jobs: - export HW=rpi2 - sed -i 's/^java_opt=.*/java_opt=Zulu8-32/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-arm32hf-native . - - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\e[0m\n" - docker run --memory="1g" --memory-reservation="1g" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: arm32hf @@ -92,7 +92,7 @@ jobs: - export HW=cm3 - sed -i 's/^java_opt=.*/java_opt=Zulu8-32/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-arm32hf-CM3 . - - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 32bit Azul Embedded Java on virtual $HW ...\e[0m\n" - docker run --memory="1g" --memory-reservation="1g" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: arm32hf @@ -103,8 +103,8 @@ jobs: - export HW=rpi3 - sed -i 's/^java_opt=.*/java_opt=Zulu8-64/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-arm32hf-native . - - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java on virtual $HW ..." - - echo -e "\e[31m ** This is known to not work. openHABian is supposed to force install down to 32bit **\n" + - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java on virtual $HW ...\e[0m" + - echo -e "\e[31m ** This is known to not work. openHABian is supposed to force install down to 32bit **\e[0m\n" - docker run --memory="1g" --memory-reservation="1g" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: aarch64 @@ -114,21 +114,21 @@ jobs: # This is known to not work - openHABian is supposed to downgrade to 32bit on install. - export HW=rpi3 - sed -i 's/^java_opt=.*/java_opt=Zulu8-64/' build-image/openhabian.conf - - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-aarch64-native . - - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java on virtual $HW ..." - - echo -e "\e[31m ** This is known to not work. openHABian is supposed to force install down to 32bit **\n" + - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-aarch64-32bit . + - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java on virtual $HW ...\e[0m" + - echo -e "\e[31m ** This is known to not work. openHABian is supposed to force install down to 32bit **\e[0m\n" - docker run --memory="1g" --memory-reservation="1g" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: aarch64 arch: arm64 script: # 64bit Azul Java on 64bit ARMv8 (RPi3,4) on 64bit OS on Travis Arm64 HW - - export HW=rpi4 + - export HW=rpi3 - sed -i 's/^java_opt=.*/java_opt=Zulu8-64/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-aarch64-64bit . - - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java \e[31m on 64bit OS\e[36m on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 64bit Azul Embedded Java\e[31m on 64bit OS\e[36m on virtual $HW ...\e[0m\n" - docker run --memory="2G" --memory-reservation="2G" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' + - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' - env: aarch64 arch: arm64 script: @@ -136,6 +136,6 @@ jobs: - export HW=rpi2 - sed -i 's/^java_opt=.*/java_opt=Adopt11/' build-image/openhabian.conf - docker build -t openhabian/${HW}-openhabian -f Dockerfile.openhabian-arm32hf-native . - - echo -e "\n\e[36mopenHABian test installation 64bit AdoptOpenJDK 11 Java \e[31m on 32bit OS\e[36m on virtual $HW ...\n" + - echo -e "\n\e[36mopenHABian test installation 64bit AdoptOpenJDK 11 Java\e[31m on 32bit OS\e[36m on virtual $HW ...\e[0m\n" - docker run --memory="1g" --memory-reservation="1g" --name "openhabian-${HW}" -d openhabian/${HW}-openhabian; - docker exec -i "openhabian-${HW}" bash -c './build.bash local-test && mv ~/.profile ~/.bash_profile && /etc/rc.local' diff --git a/Dockerfile.openhabian-ARMv6hf-emulated b/Dockerfile.openhabian-ARMv6hf-emulated index f457d6ee0..a234f05bc 100644 --- a/Dockerfile.openhabian-ARMv6hf-emulated +++ b/Dockerfile.openhabian-ARMv6hf-emulated @@ -7,6 +7,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-ARMv6hf-native b/Dockerfile.openhabian-ARMv6hf-native index aea636bed..ba3b02423 100644 --- a/Dockerfile.openhabian-ARMv6hf-native +++ b/Dockerfile.openhabian-ARMv6hf-native @@ -6,6 +6,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-aarch64-native b/Dockerfile.openhabian-aarch64-32bit similarity index 90% rename from Dockerfile.openhabian-aarch64-native rename to Dockerfile.openhabian-aarch64-32bit index e4d4a32c3..c026dba2d 100644 --- a/Dockerfile.openhabian-aarch64-native +++ b/Dockerfile.openhabian-aarch64-32bit @@ -1,5 +1,4 @@ -# Note RPi3 base (first RPi to have aarch64 available) -# ATTENTION: Zulu Java on raspberrypi3 images fails to run +# Note RPi3 base (first RPi to have aarch64 available) WITH 32bit OS FROM balenalib/raspberrypi3-debian-node:latest-buster-build @@ -7,10 +6,9 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn -# Currently this config is used for BATS tests. # bats tests on a minimal install will require additional packages to run properly: # - lsb-release (influx, homegear) -# - psmisc, i.e. killall (homegear) +# - apt-transport-https (homegear) # - unzip (java) # those packages would normally be included in our standard install RUN apt-get update -qq \ diff --git a/Dockerfile.openhabian-aarch64-64bit b/Dockerfile.openhabian-aarch64-64bit index bf1835633..541e6258c 100644 --- a/Dockerfile.openhabian-aarch64-64bit +++ b/Dockerfile.openhabian-aarch64-64bit @@ -1,13 +1,16 @@ # Note RPi3 base (first RPi to have aarch64 available) WITH 64bit OS -#FROM balenalib/raspberrypi3-debian-node:latest-buster -#FROM balenalib/generic-aarch64-debian-node -FROM balenalib/raspberrypi4-64-debian-node-build +FROM balenalib/raspberrypi3-64-debian-node:latest-buster-build ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-arm32hf-CM3 b/Dockerfile.openhabian-arm32hf-CM3 index a7aa5d064..7dfa349ce 100644 --- a/Dockerfile.openhabian-arm32hf-CM3 +++ b/Dockerfile.openhabian-arm32hf-CM3 @@ -1,5 +1,4 @@ # CM3 is like RPi3 but without BT,WiFi,Ethernet -# ATTENTION: Zulu Java on raspberrypi3 images fails to run FROM balenalib/fincm3-debian-node:latest-buster-build @@ -7,6 +6,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-arm32hf-emulated b/Dockerfile.openhabian-arm32hf-emulated index 60878bd06..1ac9f37df 100644 --- a/Dockerfile.openhabian-arm32hf-emulated +++ b/Dockerfile.openhabian-arm32hf-emulated @@ -7,6 +7,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-arm32hf-native b/Dockerfile.openhabian-arm32hf-native index e2fc7faa2..1a5f4708e 100644 --- a/Dockerfile.openhabian-arm32hf-native +++ b/Dockerfile.openhabian-arm32hf-native @@ -6,6 +6,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/Dockerfile.openhabian-x86 b/Dockerfile.openhabian-x86 index 2a175163f..22cdc5efd 100644 --- a/Dockerfile.openhabian-x86 +++ b/Dockerfile.openhabian-x86 @@ -5,6 +5,11 @@ ENV container docker ENV INITSYSTEM on ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn +# bats tests on a minimal install will require additional packages to run properly: +# - lsb-release (influx, homegear) +# - apt-transport-https (homegear) +# - unzip (java) +# those packages would normally be included in our standard install RUN apt-get update -qq \ && apt-get install --yes -qq --no-install-recommends systemd systemd-sysv git wget python3 python3-pip apt-utils jq virt-what \ lsb-release unzip apt-transport-https \ diff --git a/build-image/first-boot.bash b/build-image/first-boot.bash index fdcaaac06..02948430e 100755 --- a/build-image/first-boot.bash +++ b/build-image/first-boot.bash @@ -8,6 +8,7 @@ export DEBIAN_FRONTEND=noninteractive # Log everything to file exec &> >(tee -a "/boot/first-boot.log") +# Log with timestamp timestamp() { date +"%F_%T_%Z"; } fail_inprogress() { @@ -191,8 +192,8 @@ if hash python3 2>/dev/null; then bash /boot/webif.bash cleanup; fi if [ -z "$SILENT" ]; then echo -e "\\n${COL_CYAN}Memory usage:" - free -m && ps -auxq "$(cat /var/lib/openhab2/tmp/karaf.pid)" |awk '/openhab/ {print "size/res="$5"/"$6" KB"}' - echo "${COL_DEF}" + free -m && ps -auxq "$(cat /var/lib/openhab2/tmp/karaf.pid)" | awk '/openhab/ {print "size/res="$5"/"$6" KB"}' + echo -e "$COL_DEF" fi # vim: filetype=sh diff --git a/build-image/openhabian.conf b/build-image/openhabian.conf index 7828d8bd9..bc53ced40 100644 --- a/build-image/openhabian.conf +++ b/build-image/openhabian.conf @@ -30,8 +30,8 @@ wifi_country="" ipv6=enable # repo and branch to clone from -repositoryurl=https://github.com/openhab/openhabian.git -clonebranch=stable +repositoryurl=https://github.com/ecdye/openhabian.git +clonebranch=fixes-2 # debug verbosity mode # off, on (verbose output in log) or maximum (show every command) @@ -53,6 +53,6 @@ debugmode=on # Java architecture mode # Valid arguments: "Adopt11", "Zulu8-32", "Zulu8-64", "Zulu11-32", or "Zulu11-64" -java_opt=Zulu8-32 +java_opt=Zulu8-64 # vim: filetype=sh diff --git a/build.bash b/build.bash index bd1ffb632..5e804ec50 100755 --- a/build.bash +++ b/build.bash @@ -13,7 +13,7 @@ usage() { # shellcheck disable=SC1090 source "$(dirname "$0")"/functions/helpers.bash -## This format timestamp +# Log with timestamp timestamp() { date +"%F_%T_%Z"; } ## This function format log messages diff --git a/functions/dev.bats b/functions/dev.bats index 3c6fd8abf..52e4edea5 100644 --- a/functions/dev.bats +++ b/functions/dev.bats @@ -1,17 +1,16 @@ -#!/usr/bin/env bats +#!/usr/bin/env bats load helpers @test "dev-query-arch" { if is_arm; then - echo -e "# \e[32mRunning on ARM." >&3 + echo -e "# ${COL_GREEN}Running on ARM.${COL_DEF}" >&3 else - echo -ne "# \e[32mRunning on " >&3 - /usr/bin/arch >&3 + echo -e "# ${COL_GREEN}Running on $(/usr/bin/arch).${COL_DEF}" >&3 fi } @test "dev-query-virt" { VIRT=$(virt-what) - echo -e "# \e[32mRunning on ${VIRT:-native HW}." >&3 + echo -e "# ${COL_GREEN}Running on ${VIRT:-native HW}.${COL_DEF}" >&3 } diff --git a/functions/helpers.bash b/functions/helpers.bash index 998a06aa9..d12e4fb7e 100644 --- a/functions/helpers.bash +++ b/functions/helpers.bash @@ -15,6 +15,9 @@ COL_LGRAY=$ESC"37;01m" COL_DGRAY=$ESC"90;01m" export COL_DEF COL_RED COL_GREEN COL_YELLOW COL_BLUE COL_MAGENTA COL_CYAN COL_LGRAY COL_DGRAY +# Log with timestamp +timestamp() { date +"%F_%T_%Z"; } + cond_redirect() { if [ -n "$SILENT" ]; then "$@" &>/dev/null @@ -38,10 +41,10 @@ cond_echo() { ## add_keys(String url) ## add_keys() { - local repoKey - - repoKey="$(mktemp /tmp/openhabian.XXXXX)" - + local repoKey + + repoKey="$(mktemp "${TEMP:-/tmp}"/openhabian.XXXXX)" + echo -n "$(timestamp) [openHABian] Adding required keys to apt... " cond_redirect wget -qO "$repoKey" "$1" if cond_redirect apt-key add "$repoKey"; then @@ -238,26 +241,46 @@ running_on_github() { [[ -n "$GITHUB_RUN_ID" ]] return $? } + +## Attempt a command "$1" for either a default of 10 times or +## for "$2" times unless "$1" evaulates to 0. +## Sleeps for 1 second or for "$3" seconds between each attempt. +## Returns number of command attemps remaining. +## +## tryUntil(String cmd, int attempts, int interval) +## tryUntil() { - # tryUntil() executes $1 as command - # either $2 times or until cmd evaluates to 0, sleeps $3 seconds inbetween - # returns the number of cmd runs that would have been left + local cmd + local attempts + local interval + local ret + cmd="$1" - count=${2:-10} - local i=$count + attempts=${2:-10} interval=${3:-1} - until [ "$i" -le 0 ]; do - cond_echo "(executing ${cmd}) ${COL_DEF}\c" + + until [ "$attempts" -le 0 ]; do + cond_echo "executing $cmd \c" eval "${cmd}" ret=$? if [ $ret -eq 0 ]; then break; fi - sleep "${interval}" - echo -n ".${i}." - ((i-=1)) + sleep "$interval" + echo "#${attempts}." + ((attempts-=1)) done - return "$i" + + return "$attempts" } + + +## Returns 0 / true if device has less than 900MB of total memory +## Returns 1 / false if device has more than 900MB of total memory +## has_lowmem() { - totalmemory=$(grep MemTotal /proc/meminfo |awk '{print $2}') - if [ "${totalmemory:-1000000}" -lt 900000 ]; then return 0; else return 1; fi + local totalMemory + + totalMemory="$(grep MemTotal /proc/meminfo | awk '{print $2}')" + + if [ -z "$totalMemory" ]; then return 1; fi # assume that device does not have low memory + if [ "$totalMemory" -lt 900000 ]; then return 0; else return 1; fi } diff --git a/functions/helpers.bats b/functions/helpers.bats index fc62c7bbe..91a668cb8 100644 --- a/functions/helpers.bats +++ b/functions/helpers.bats @@ -16,14 +16,14 @@ testAppearingHost() { run testAppearingHost thiswillappear [ "$status" -eq 7 ] - echo -e "# \n\e[32mPing to host appearing after 3 seconds succeeded." >&3 + echo -e "# \n${COL_GREEN}Ping to host appearing after 3 seconds succeeded.${COL_DEF}" >&3 } @test "unit-tryNonExistingSite" { run testNonExistingHost nothisdoesnotexit [ "$status" -eq 0 ] - echo -e "# \n\e[32mPinging to nonexistingsite failed (correctly so)." >&3 + echo -e "# \n${COL_GREEN}Pinging to nonexistingsite failed (correctly so).${COL_DEF}" >&3 } @test "unit-cond_echo" { diff --git a/functions/influxdb+grafana.bats b/functions/influxdb+grafana.bats index cc6562401..19f6298da 100644 --- a/functions/influxdb+grafana.bats +++ b/functions/influxdb+grafana.bats @@ -3,52 +3,47 @@ load influxdb+grafana load helpers -setup_file() { - echo -e "# \e[36mInfluxDB/Grafana test preparation..." >&3 -} - teardown_file() { - echo -e "# \e[36mInfluxDB/Grafana test cleanup..." >&3 systemctl stop influxdb.service || true systemctl stop grafana-server || true } @test "destructive-influxDB_install" { - echo -e "# \e[36mInfluxDB installation starting..." >&3 + echo -e "# ${COL_CYAN}InfluxDB installation starting...${COL_DEF}" >&3 run influxdb_install "Password1234" if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mInfluxDB installation successful." >&3 + echo -e "# ${COL_GREEN}InfluxDB installation successful.${COL_DEF}" >&3 run systemctl is-active --quiet influxdb.service if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mInfluxDB service running." >&3 + echo -e "# ${COL_GREEN}InfluxDB service running.${COL_DEF}" >&3 run curl --retry 5 --retry-connrefused -s --insecure --user "admin:Password1234" "http://localhost:8086/query" >/dev/null if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mInfluxDB service is responding." >&3 + echo -e "# ${COL_GREEN}InfluxDB service is responding.${COL_DEF}" >&3 } @test "destructive-grafana_install" { - echo -e "# \e[36mGrafana installation starting..." >&3 + echo -e "# ${COL_CYAN}Grafana installation starting...${COL_DEF}" >&3 run grafana_install "Password1234" if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mGrafana installation successful." >&3 + echo -e "# ${COL_GREEN}Grafana installation successful.${COL_DEF}" >&3 run systemctl is-active --quiet grafana-server.service if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mGrafana service running." >&3 + echo -e "# ${COL_GREEN}Grafana service running.${COL_DEF}" >&3 run curl --retry 5 --retry-connrefused --user admin:Password1234 --header "Content-Type: application/json" --request PUT --data "{\"password\":\"Password234\"}" http://localhost:3000/api/admin/users/1/password if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mGrafana settings sucessfully changed." >&3 + echo -e "# ${COL_GREEN}Grafana settings sucessfully changed.${COL_DEF}" >&3 run grafana_install "Password3456" if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mGrafana re-installation successful." >&3 + echo -e "# ${COL_GREEN}Grafana re-installation successful.${COL_DEF}" >&3 run curl --retry 5 --retry-connrefused --user admin:Password3456 --header "Content-Type: application/json" --request PUT --data "{\"password\":\"Password234\"}" http://localhost:3000/api/admin/users/1/password if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mGrafana settings sucessfully changed." >&3 + echo -e "# ${COL_GREEN}Grafana settings sucessfully changed.${COL_DEF}" >&3 } diff --git a/functions/java-jre.bash b/functions/java-jre.bash index b919f41cb..e11cde02f 100644 --- a/functions/java-jre.bash +++ b/functions/java-jre.bash @@ -3,14 +3,12 @@ # shellcheck disable=SC2144 # shellcheck disable=SC2069 -timestamp() { date +"%F_%T_%Z"; } - ## Install appropriate Java version based on current choice. ## Valid arguments: "Adopt11", "Zulu8-32", "Zulu8-64", "Zulu11-32", or "Zulu11-64" ## ## java_install_or_update(String type) ## -java_install_or_update(){ +java_install_or_update() { local branch branch=$(git -C "/opt/openhabian" rev-parse --abbrev-ref HEAD) @@ -19,85 +17,85 @@ java_install_or_update(){ if ! [ -x "$(command -v java)" ] || [[ ! "$(java -version 2>&1> /dev/null)" == *"Zulu"* ]] || [[ ! "$(java -version 2>&1> /dev/null)" == *"AdoptOpenJDK"* ]]; then if [ "$1" == "Adopt11" ]; then cond_redirect adoptopenjdk_install_apt - elif ! [ "$1" == "Adopt11" ]; then + elif [ "$1" != "Adopt11" ]; then if [[ "$(java -version 2>&1> /dev/null)" == *"AdoptOpenJDK"* ]] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "$1" fi if [ "$1" == "Zulu8-64" ] || [ "$1" == "Zulu11-64" ]; then if is_aarch64 || is_x86_64 && [ "$(getconf LONG_BIT)" == "64" ]; then if is_x86_64; then if [ "$1" == "Zulu8-64" ]; then - cond_redirect java_zulu_enterprise_apt 8 + cond_redirect java_zulu_enterprise_apt "8" elif [ "$1" == "Zulu11-64" ]; then - cond_redirect java_zulu_enterprise_apt 11 + cond_redirect java_zulu_enterprise_apt "11" fi else if [ "$1" == "Zulu8-64" ]; then - if cond_redirect java_zulu_update_available Zulu8-64; then + if cond_redirect java_zulu_update_available "Zulu8-64"; then echo -n "$(timestamp) [openHABian] Installing Java Zulu 8 64-Bit OpenJDK... " if ! [ -x "$(command -v java)" ] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "Zulu8-64" elif [ "$branch" == "stable" ]; then - java_zulu_stable Zulu8-64 + java_zulu_stable "Zulu8-64" else - cond_redirect java_zulu_fetch Zulu8-64 - cond_redirect java_zulu_install + cond_redirect java_zulu_fetch "Zulu8-64" + cond_redirect java_zulu_install "Zulu8-64" fi fi elif [ "$1" == "Zulu11-64" ]; then - if cond_redirect java_zulu_update_available Zulu11-64; then + if cond_redirect java_zulu_update_available "Zulu11-64"; then echo -n "$(timestamp) [openHABian] Installing Java Zulu 11 64-Bit OpenJDK... " if ! [ -x "$(command -v java)" ] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "Zulu11-64" elif [ "$branch" == "stable" ]; then - java_zulu_stable Zulu11-64 + java_zulu_stable "Zulu11-64" else - cond_redirect java_zulu_fetch Zulu11-64 - cond_redirect java_zulu_install + cond_redirect java_zulu_fetch "Zulu11-64" + cond_redirect java_zulu_install "Zulu11-64" fi fi fi fi else if [ -n "$INTERACTIVE" ]; then - whiptail --title "Incompatible hardware detected" --msgbox "Zulu OpenJDK 64-bit: this option does not currently work on your platform.\\n\\nDefaulting to Java Zulu 8 32-bit installation." 10 60 + whiptail --title "Incompatible hardware detected" --msgbox "Zulu OpenJDK 64-bit: this option does not currently work on your platform.\\n\\nDefaulting to Java Zulu 8 32-bit installation." 9 80 else echo "Zulu OpenJDK 64-bit: this option does not currently work on your platform. Defaulting to Java Zulu 8 32-bit installation." - if cond_redirect java_zulu_update_available Zulu8-32; then + if cond_redirect java_zulu_update_available "Zulu8-32"; then echo -n "$(timestamp) [openHABian] Installing Java Zulu 8 32-Bit OpenJDK... " if ! [ -x "$(command -v java)" ] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "Zulu8-32" elif [ "$branch" == "stable" ]; then - java_zulu_stable Zulu8-32 + java_zulu_stable "Zulu8-32" else - cond_redirect java_zulu_fetch Zulu8-32 - cond_redirect java_zulu_install + cond_redirect java_zulu_fetch "Zulu8-32" + cond_redirect java_zulu_install "Zulu8-32" fi fi fi fi else # Default to 32-bit installation if [ "$1" == "Zulu11-32" ]; then - if cond_redirect java_zulu_update_available Zulu11-32; then + if cond_redirect java_zulu_update_available "Zulu11-32"; then echo -n "$(timestamp) [openHABian] Installing Java Zulu 11 32-Bit OpenJDK... " if ! [ -x "$(command -v java)" ] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "Zulu11-32" elif [ "$branch" == "stable" ]; then - java_zulu_stable Zulu11-32 + java_zulu_stable "Zulu11-32" else - cond_redirect java_zulu_fetch Zulu11-32 - cond_redirect java_zulu_install + cond_redirect java_zulu_fetch "Zulu11-32" + cond_redirect java_zulu_install "Zulu11-32" fi fi - elif cond_redirect java_zulu_update_available Zulu8-32; then + elif cond_redirect java_zulu_update_available "Zulu8-32"; then echo -n "$(timestamp) [openHABian] Installing Java Zulu 8 32-Bit OpenJDK... " if ! [ -x "$(command -v java)" ] && [ -d /opt/jdk/* ]; then - cond_redirect java_zulu_install + cond_redirect java_zulu_install "Zulu8-32" elif [ "$branch" == "stable" ]; then - java_zulu_stable Zulu8-32 + java_zulu_stable "Zulu8-32" else - cond_redirect java_zulu_fetch Zulu8-32 - cond_redirect java_zulu_install + cond_redirect java_zulu_fetch "Zulu8-32" + cond_redirect java_zulu_install "Zulu8-32" fi fi fi @@ -111,7 +109,7 @@ java_install_or_update(){ ## ## java_zulu_stable(String arch) ## -java_zulu_stable(){ +java_zulu_stable() { local updateText local consoleText @@ -123,7 +121,7 @@ java_zulu_stable(){ cond_redirect java_zulu_fetch "$1" cond_redirect java_zulu_install else - echo "$(timestamp) $consoleText" + echo "$(timestamp) [openHABian] $consoleText" cond_redirect java_zulu_fetch "$1" cond_redirect java_zulu_install fi @@ -134,55 +132,60 @@ java_zulu_stable(){ ## ## java_zulu_install(String arch) ## -java_zulu_install(){ +java_zulu_install() { + local jdkArch local jdkBin local jdkLib - local jdkArch - cond_redirect systemctl stop openhab2.service + if [ -f "/opt/openHABian-install-successful" ] ; then + cond_redirect systemctl stop openhab2.service + fi cond_redirect java_alternatives_reset - jdkBin=$(find /opt/jdk/*/bin ... -print -quit) - jdkLib=$(find /opt/jdk/*/lib ... -print -quit) + jdkBin="$(find /opt/jdk/*/bin ... -print -quit)" + jdkLib="$(find /opt/jdk/*/lib ... -print -quit)" + + echo -n "$(timestamp) [openHABian] Installing Java Zulu prerequsites (libc)... " + if [ "$1" == "Zulu8-64" ] || [ "$1" == "Zulu11-64" ]; then + if is_aarch64 && [ "$(getconf LONG_BIT)" == "64" ]; then + jdkArch="aarch64" + dpkg --add-architecture arm64 + if cond_redirect apt-get install --yes libc6:arm64 libncurses5:arm64 libstdc++6:arm64; then echo "OK"; else echo "FAILED"; return 1; fi + elif is_x86_64 && [ "$(getconf LONG_BIT)" == "64" ]; then + jdkArch="amd64" + dpkg --add-architecture amd64 + if cond_redirect apt-get install --yes libc6:amd64 libncurses5:amd64 libstdc++6:amd64; then echo "OK"; else echo "FAILED"; return 1; fi + fi + fi if is_arm; then jdkArch="aarch32" - elif is_aarch64 && [ "$(getconf LONG_BIT)" == "64" ]; then - jdkArch="aarch64" - elif is_x86_64 && [ "$(getconf LONG_BIT)" == "64" ]; then - jdkArch="amd64" + dpkg --add-architecture armhf + if cond_redirect apt-get install --yes libc6:armhf libncurses5:armhf libstdc++6:armhf; then echo "OK"; else echo "FAILED"; return 1; fi else jdkArch="i386" + dpkg --add-architecture i386 + if cond_redirect apt-get install --yes libc6:i386 libncurses5:i386 libstdc++6:i386; then echo "OK"; else echo "FAILED"; return 1; fi fi - if is_aarch64; then - dpkg --add-architecture armhf - cond_redirect apt-get install --yes libc6:armhf libncurses5:armhf libstdc++6:armhf - fi - - if is_x86_64; then - dpkg --add-architecture i386 - cond_redirect apt-get install --yes libc6:i386 libncurses5:i386 libstdc++6:i386 - fi - - update-alternatives --install /usr/bin/java java "$jdkBin"/java 1000000 - update-alternatives --install /usr/bin/jjs jjs "$jdkBin"/jjs 1000000 - update-alternatives --install /usr/bin/jrunscript jrunscript "$jdkBin"/jrunscript 1000000 - update-alternatives --install /usr/bin/keytool keytool "$jdkBin"/keytool 1000000 - update-alternatives --install /usr/bin/pack200 pack200 "$jdkBin"/pack200 1000000 - update-alternatives --install /usr/bin/rmid rmid "$jdkBin"/rmid 1000000 - update-alternatives --install /usr/bin/rmiregistry rmiregistry "$jdkBin"/rmiregistry 1000000 - update-alternatives --install /usr/bin/unpack200 unpack200 "$jdkBin"/unpack200 1000000 - update-alternatives --install /usr/bin/jexec jexec "$jdkLib"/jexec 1000000 + cond_redirect update-alternatives --install /usr/bin/java java "$jdkBin"/java 1000000 + cond_redirect update-alternatives --install /usr/bin/jjs jjs "$jdkBin"/jjs 1000000 + cond_redirect update-alternatives --install /usr/bin/keytool keytool "$jdkBin"/keytool 1000000 + cond_redirect update-alternatives --install /usr/bin/pack200 pack200 "$jdkBin"/pack200 1000000 + cond_redirect update-alternatives --install /usr/bin/rmid rmid "$jdkBin"/rmid 1000000 + cond_redirect update-alternatives --install /usr/bin/rmiregistry rmiregistry "$jdkBin"/rmiregistry 1000000 + cond_redirect update-alternatives --install /usr/bin/unpack200 unpack200 "$jdkBin"/unpack200 1000000 + cond_redirect update-alternatives --install /usr/bin/jexec jexec "$jdkLib"/jexec 1000000 echo "$jdkLib"/"$jdkArch" > /etc/ld.so.conf.d/java.conf echo "$jdkLib"/"$jdkArch"/jli >> /etc/ld.so.conf.d/java.conf ldconfig + if [ $? -eq 0 ]; then echo "OK"; else echo "FAILED"; return 1; fi cond_redirect java_zulu_install_crypto_extension - if [ $? -eq 0 ]; then echo "OK"; else echo "FAILED"; return 1; fi - cond_redirect systemctl start openhab2.service || true - return 0 + if [ -f "/opt/openHABian-install-successful" ]; then + cond_redirect systemctl start openhab2.service + fi } ## Fetch Java Zulu 8 directly from Azul API v1 @@ -190,15 +193,15 @@ java_zulu_install(){ ## ## java_zulu_fetch(String arch) ## -java_zulu_fetch(){ +java_zulu_fetch() { local link8 local link11 local downloadlink local jdkInstallLocation - local jdkTempLocation - link8="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/binary/?jdk_version=8&ext=tar.gz&os=linux" + jdkInstallLocation="/opt/jdk" link11="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/binary/?jdk_version=11&ext=tar.gz&os=linux" + link8="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/binary/?jdk_version=8&ext=tar.gz&os=linux" if [ "$1" == "Zulu8-32" ]; then if is_arm; then @@ -206,45 +209,33 @@ java_zulu_fetch(){ else downloadlink=$(curl "${link8}&arch=x86&hw_bitness=32" -s -L -I -o /dev/null -w '%{url_effective}' | sed -e 's#32sf#32hf#g') fi - elif [ "$1" == "Zulu11-32" ]; then if is_arm; then downloadlink=$(curl "${link11}&arch=arm&hw_bitness=32" -s -L -I -o /dev/null -w '%{url_effective}' | sed -e 's#32sf#32hf#g') else downloadlink=$(curl "${link11}&arch=x86&hw_bitness=32" -s -L -I -o /dev/null -w '%{url_effective}' | sed -e 's#32sf#32hf#g') fi - elif [ "$1" == "Zulu8-64" ]; then if is_arm; then downloadlink=$(curl "${link8}&arch=arm&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}') else downloadlink=$(curl "${link8}&arch=x86&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}') fi - elif [ "$1" == "Zulu11-64" ]; then if is_arm; then downloadlink=$(curl "${link11}&arch=arm&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}') else downloadlink=$(curl "${link11}&arch=x86&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}') fi - - else - echo -n "[DEBUG] Invalid argument to function java_zulu_fetch()" - return 1 fi - if [ -z "$downloadlink" ]; then return 1; fi + if [ -z "$downloadlink" ]; then echo "FAILED (java download link)"; return 1; fi - jdkInstallLocation="/opt/jdk" mkdir -p $jdkInstallLocation - jdkTempLocation="$(mktemp -d /tmp/openhabian.XXXXX)" - if [ -z "$jdkTempLocation" ]; then echo "FAILED"; return 1; fi - cond_redirect wget -nv -O "$jdkTempLocation"/zulu8.tar.gz "$downloadlink" - tar -xpzf "$jdkTempLocation"/zulu8.tar.gz -C "${jdkTempLocation}" - if [ $? -ne 0 ]; then echo "FAILED"; rm -rf "$jdkTempLocation"/zulu8.tar.gz; return 1; fi - rm -rf "$jdkTempLocation"/zulu8.tar.gz "${jdkInstallLocation:?}"/* - mv "${jdkTempLocation}"/* "${jdkInstallLocation}"/ - rmdir "${jdkTempLocation}" - return 0 + rm -rf "${jdkInstallLocation:?}"/* + cond_redirect wget -qO "$jdkInstallLocation"/zulu.tar.gz "$downloadlink" + tar -xpzf "$jdkInstallLocation"/zulu.tar.gz -C "$jdkInstallLocation" + if [ $? -ne 0 ]; then echo "FAILED"; rm -rf "${jdkInstallLocation:?}"/*; return 1; fi + rm -rf "$jdkInstallLocation"/zulu.tar.gz } ## Check if a newer version of Java Zulu 8 is available. @@ -253,23 +244,28 @@ java_zulu_fetch(){ ## ## java_zulu_update_available(String arch) ## -java_zulu_update_available(){ +java_zulu_update_available() { if [ ! -x "$(command -v java)" ] || [[ "$(java -version 2>&1> /dev/null)" == *"AdoptOpenJDK"* ]]; then return 0; fi + local availableVersion - local requestedArch - local javaVersion - local javaArch local filter - local link8 + local javaArch + local jdkBin + local javaVersion local link11 - if [ ! -x "$(command -v jq)" ]; then - cond_redirect apt-get install --yes jq + local link8 + local requestedArch + + if ! [ -x "$(command -v jq)" ]; then + echo -n "$(timestamp) [openHABian] Installing Java Zulu prerequsites (jq)... " + if cond_redirect apt-get install --yes jq; then echo "OK"; else echo "FAILED"; return 1; fi fi filter='[.zulu_version[] | tostring] | join(".")' - link8="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/?jdk_version=8&ext=tar.gz&os=linux" + jdkBin="$(find /opt/jdk/*/bin ... -print -quit)" + javaVersion="$("${jdkBin}"/java -version |& grep -m 1 -o "[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}")" link11="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/?jdk_version=11&ext=tar.gz&os=linux" - javaVersion=$("$(find /opt/jdk/*/bin ... -print -quit)"/java -version |& grep -m 1 -o "[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}") + link8="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/?jdk_version=8&ext=tar.gz&os=linux" if [ "$1" == "Zulu8-32" ]; then if is_arm; then @@ -279,7 +275,6 @@ java_zulu_update_available(){ requestedArch="i686" availableVersion=$(curl -s -H "Accept: application/json" "${link8}&arch=x86&hw_bitness=32" | jq -r "$filter") fi - elif [ "$1" == "Zulu11-32" ]; then if is_arm; then requestedArch="aarch32hf" @@ -288,7 +283,6 @@ java_zulu_update_available(){ requestedArch="i686" availableVersion=$(curl -s -H "Accept: application/json" "${link11}&arch=x86&hw_bitness=32" | jq -r "$filter") fi - elif [ "$1" == "Zulu8-64" ]; then if is_arm; then requestedArch="aarch64" @@ -297,7 +291,6 @@ java_zulu_update_available(){ requestedArch="x64" availableVersion=$(curl -s -H "Accept: application/json" "${link8}&arch=x86&hw_bitness=64" | jq -r "$filter") fi - elif [ "$1" == "Zulu11-64" ]; then if is_arm; then requestedArch="aarch64" @@ -306,20 +299,17 @@ java_zulu_update_available(){ requestedArch="x64" availableVersion=$(curl -s -H "Accept: application/json" "${link11}&arch=x86&hw_bitness=64" | jq -r "$filter") fi - - else - if [ $? -ne 0 ]; then echo "FAILED (java update available)"; return 1; fi fi + if [ -z "$requestedArch" ] && [ -z "$availableVersion" ]; then echo "FAILED (java update available)"; return 1; fi - if [[ "$("$(find /opt/jdk/*/bin ... -print -quit)"/java -version 2>&1> /dev/null)" == *"aarch32hf"* ]]; then javaArch="aarch32hf"; fi - if [[ "$("$(find /opt/jdk/*/bin ... -print -quit)"/java -version 2>&1> /dev/null)" == *"i686"* ]]; then javaArch="i686"; fi - if [[ "$("$(find /opt/jdk/*/bin ... -print -quit)"/java -version 2>&1> /dev/null)" == *"aarch64"* ]]; then javaArch="aarch64"; fi - if [[ "$("$(find /opt/jdk/*/bin ... -print -quit)"/java -version 2>&1> /dev/null)" == *"x64"* ]]; then javaArch="x64"; fi + if [[ "$("${jdkBin}"/java -version 2>&1> /dev/null)" == *"aarch32hf"* ]]; then javaArch="aarch32hf"; fi + if [[ "$("${jdkBin}"/java -version 2>&1> /dev/null)" == *"i686"* ]]; then javaArch="i686"; fi + if [[ "$("${jdkBin}"/java -version 2>&1> /dev/null)" == *"aarch64"* ]]; then javaArch="aarch64"; fi + if [[ "$("${jdkBin}"/java -version 2>&1> /dev/null)" == *"x64"* ]]; then javaArch="x64"; fi if [[ $javaVersion == "$availableVersion" ]] && [[ $javaArch == "$requestedArch" ]]; then return 1 # Java is up-to-date fi - return 0 } ## Install Azul's Java Zulu Enterprise using APT repository. @@ -328,109 +318,100 @@ java_zulu_update_available(){ ## ## java_zulu_enterprise_apt(String ver) ## -java_zulu_enterprise_apt(){ - if [ "$1" == "8" ]; then - if ! dpkg -s 'zulu-8' > /dev/null 2>&1; then # Check if already is installed - echo -n "$(timestamp) [openHABian] Adding Zulu keys to apt... " - cond_redirect apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 219BD9C9 - if [ $? -ne 0 ]; then echo "FAILED (keyserver)"; return 1; fi +java_zulu_enterprise_apt() { + if (! dpkg -s 'zulu-8' > /dev/null 2>&1) || (! dpkg -s 'zulu-11' > /dev/null 2>&1); then # Check if already is installed + echo -n "$(timestamp) [openHABian] Adding Zulu keys to apt... " + if cond_redirect apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 219BD9C9; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Adding Zulu repository to apt... " + if ! echo "deb http://repos.azulsystems.com/debian stable main" > /etc/apt/sources.list.d/zulu-enterprise.list; then echo "FAILED"; return 1; fi + if cond_redirect apt-get update; then echo "OK"; else echo "FAILED (update apt lists)"; return 1; fi + + if [ -f "/opt/openHABian-install-successful" ]; then cond_redirect systemctl stop openhab2.service - echo -n "$(timestamp) [openHABian] Installing Zulu 8 Enterprise 64-Bit OpenJDK... " - echo "deb http://repos.azulsystems.com/debian stable main" > /etc/apt/sources.list.d/zulu-enterprise.list - cond_redirect apt-get update - if cond_redirect apt-get install --yes zulu-8 && java_zulu_install_crypto_extension; then echo "OK"; else echo "FAILED"; return 1; fi - cond_redirect systemctl start openhab2.service fi - elif [ "$1" == "11" ]; then - if ! dpkg -s 'zulu-11' > /dev/null 2>&1; then # Check if already is installed - echo -n "$(timestamp) [openHABian] Adding Zulu keys to apt... " - cond_redirect apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 219BD9C9 - if [ $? -ne 0 ]; then echo "FAILED (keyserver)"; return 1; fi - cond_redirect systemctl stop openhab2.service + if [ "$1" == "8" ]; then + echo -n "$(timestamp) [openHABian] Installing Zulu 8 Enterprise 64-Bit OpenJDK... " + if cond_redirect apt-get install --yes zulu-8; then echo "OK"; else echo "FAILED"; return 1; fi + elif [ "$1" == "11" ]; then echo -n "$(timestamp) [openHABian] Installing Zulu 11 Enterprise 64-Bit OpenJDK... " - echo "deb http://repos.azulsystems.com/debian stable main" > /etc/apt/sources.list.d/zulu-enterprise.list - cond_redirect apt-get update - if cond_redirect apt-get install --yes zulu-11 && java_zulu_install_crypto_extension; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect apt-get install --yes zulu-11; then echo "OK"; else echo "FAILED"; return 1; fi + fi + if [ -f "/opt/openHABian-install-successful" ]; then cond_redirect systemctl start openhab2.service fi + + java_zulu_install_crypto_extension fi } ## Install Zulu Cryptography Extension Kit to enable cryptos using more then 128 bits ## -java_zulu_install_crypto_extension(){ - local jdkPath +java_zulu_install_crypto_extension() { local jdkSecurity local policyTempLocation - jdkPath="$(readlink -f "$(command -v java)")" - jdkSecurity="$(dirname "${jdkPath}")/../lib/security" - mkdir -p "$jdkSecurity" - policyTempLocation="$(mktemp -d /tmp/openhabian.XXXXX)" - if [ -z "$policyTempLocation" ]; then echo "FAILED"; return 1; fi - cond_redirect wget -nv -O "$policyTempLocation"/crypto.zip https://cdn.azul.com/zcek/bin/ZuluJCEPolicies.zip - cond_redirect unzip "$policyTempLocation"/crypto.zip -d "$policyTempLocation" + jdkSecurity="$(dirname "$(readlink -f "$(command -v java)")")/../lib/security" + policyTempLocation="$(mktemp -d "${TEMP:-/tmp}"/openhabian.XXXXX)" - rm -rf "${policyTempLocation}" - return 0 + echo -n "$(timestamp) [openHABian] Installing Java Zulu Cryptography Extension Kit to enable cryptos using more then 128 bits... " + mkdir -p "$jdkSecurity" + if ! cond_redirect wget -qO "$policyTempLocation"/crypto.zip https://cdn.azul.com/zcek/bin/ZuluJCEPolicies.zip; then echo "FAILED (download)"; return 1; fi + if ! cond_redirect unzip "$policyTempLocation"/crypto.zip -d "$policyTempLocation"; then echo "FAILED (unzip)"; return 1; fi + if cond_redirect cp "$policyTempLocation"/ZuluJCEPolicies/*.jar "$jdkSecurity"; then echo "OK"; else echo "FAILED (copy)"; return 1; fi + + rm -rf "$policyTempLocation" } ## Fetch AdoptOpenJDK using APT repository. ## -adoptopenjdk_fetch_apt(){ +adoptopenjdk_fetch_apt() { if ! dpkg -s 'software-properties-common' > /dev/null 2>&1; then - cond_redirect apt-get install --yes software-properties-common + if ! cond_redirect apt-get install --yes software-properties-common; then echo "FAILED (AdoptOpenJDK prerequsites)"; return 1; fi fi - local adoptKey - adoptKey="/tmp/adoptopenjdk.asc" - echo -n "$(timestamp) [openHABian] Adding AdoptOpenJDK keys to apt... " - cond_redirect wget -qO "$adoptKey" https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public - if cond_redirect apt-key add "$adoptKey"; then - echo "OK" - else - echo "FAILED (keyserver)" - rm -f "$adoptKey" - return 1; - fi - echo -n "$(timestamp) [openHABian] Fetching AdoptOpenJDK... " + + if ! add_keys "https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public"; then return 1; fi + + echo -n "$(timestamp) [openHABian] Adding AdoptOpenJDK repository to apt... " echo "deb http://adoptopenjdk.jfrog.io/adoptopenjdk/deb buster main" > /etc/apt/sources.list.d/adoptopenjdk.list - cond_redirect apt-get update + if cond_redirect apt-get update; then echo "OK"; else echo "FAILED (update apt lists)"; return 1; fi + + echo -n "$(timestamp) [openHABian] Fetching AdoptOpenJDK... " if cond_redirect apt-get install --download-only --yes adoptopenjdk-11-hotspot-jre; then echo "OK"; else echo "FAILED"; return 1; fi - return 0 } ## Install AdoptOpenJDK using APT repository. ## -adoptopenjdk_install_apt(){ +adoptopenjdk_install_apt() { + if [ -f "/opt/openHABian-install-successful" ] ; then + cond_redirect systemctl stop openhab2.service + fi if ! dpkg -s 'adoptopenjdk-11-hotspot-jre' > /dev/null 2>&1; then # Check if already is installed - echo -n "$(timestamp) [openHABian] Installing AdoptOpenJDK 11... " adoptopenjdk_fetch_apt - cond_redirect systemctl stop openhab2.service + echo -n "$(timestamp) [openHABian] Installing AdoptOpenJDK 11... " cond_redirect java_alternatives_reset if cond_redirect apt-get install --yes adoptopenjdk-11-hotspot-jre; then echo "OK"; else echo "FAILED"; return 1; fi - cond_redirect systemctl start openhab2.service elif dpkg -s 'adoptopenjdk-11-hotspot-jre' > /dev/null 2>&1; then echo -n "$(timestamp) [openHABian] Reconfiguring AdoptOpenJDK 11... " - cond_redirect systemctl stop openhab2.service cond_redirect java_alternatives_reset if cond_redirect dpkg-reconfigure adoptopenjdk-11-hotspot-jre; then echo "OK"; else echo "FAILED"; return 1; fi + fi + if [ -f "/opt/openHABian-install-successful" ] ; then cond_redirect systemctl start openhab2.service fi - return 0 } ## Reset Java in update-alternatives ## -java_alternatives_reset(){ - update-alternatives --remove-all java - update-alternatives --remove-all jjs - update-alternatives --remove-all jrunscript - update-alternatives --remove-all keytool - update-alternatives --remove-all pack200 - update-alternatives --remove-all policytool - update-alternatives --remove-all rmid - update-alternatives --remove-all rmiregistry - update-alternatives --remove-all unpack200 - update-alternatives --remove-all jexec - update-alternatives --remove-all javac # TODO: remove sometime late 2020 +java_alternatives_reset() { + update-alternatives --quiet --remove-all java + update-alternatives --quiet --remove-all jjs + update-alternatives --quiet --remove-all keytool + update-alternatives --quiet --remove-all pack200 + update-alternatives --quiet --remove-all policytool + update-alternatives --quiet --remove-all rmid + update-alternatives --quiet --remove-all rmiregistry + update-alternatives --quiet --remove-all unpack200 + update-alternatives --quiet --remove-all jexec + update-alternatives --quiet --remove-all javac # TODO: remove sometime late 2020 } diff --git a/functions/java-jre.bats b/functions/java-jre.bats index 45e27629e..6a046f86c 100644 --- a/functions/java-jre.bats +++ b/functions/java-jre.bats @@ -3,17 +3,8 @@ load java-jre load helpers -@test "installation-java_exist" { -# TODO: rewrite test, in CI it fails because java is not installed at this point -skip - run java -version - if [ "$status" -ne 0 ]; then echo "$output" >&3; fi - [ "$status" -eq 0 ] - [[ $output == *"Zulu"* ]] -} - @test "destructive-install_zulu8-64bit" { - echo -e "# \e[36mZulu 8 64-bit Java installation is being (test-)installed..." >&3 + echo -e "# ${COL_CYAN}Zulu 8 64-bit Java installation is being (test-)installed...${COL_DEF}" >&3 case "$(uname -m)" in aarch64|arm64|x86_64|amd64) ;; *) skip ;; @@ -24,11 +15,11 @@ skip run java_zulu_install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mZulu 8 64-bit Java installation successful." >&3 + echo -e "# ${COL_GREEN}Zulu 8 64-bit Java installation successful.${COL_DEF}" >&3 } @test "destructive-install_zulu11-64bit" { - echo -e "# \e[36mZulu 11 64-bit Java installation is being (test-)installed..." >&3 + echo -e "# ${COL_CYAN}Zulu 11 64-bit Java installation is being (test-)installed...${COL_DEF}" >&3 case "$(uname -m)" in aarch64|arm64|x86_64|amd64) ;; *) skip ;; @@ -39,35 +30,35 @@ skip run java_zulu_install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mZulu 11 64-bit Java installation successful." >&3 + echo -e "# ${COL_GREEN}Zulu 11 64-bit Java installation successful.${COL_DEF}" >&3 } @test "destructive-install_zulu8-32bit" { - echo -e "# \e[36mZulu 8 32-bit Java installation is being (test-)installed..." >&3 + echo -e "# ${COL_CYAN}Zulu 8 32-bit Java installation is being (test-)installed...${COL_DEF}" >&3 run java_zulu_fetch Zulu8-32 if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] run java_zulu_install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mZulu 8 32-bit Java installation successful." >&3 + echo -e "# ${COL_GREEN}Zulu 8 32-bit Java installation successful.${COL_DEF}" >&3 } @test "destructive-install_zulu11-32bit" { - echo -e "# \e[36mZulu 11 32-bit Java installation is being (test-)installed..." >&3 + echo -e "# ${COL_CYAN}Zulu 11 32-bit Java installation is being (test-)installed...${COL_DEF}" >&3 run java_zulu_fetch Zulu11-32 if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] run java_zulu_install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mZulu 11 32-bit Java installation successful." >&3 + echo -e "# ${COL_GREEN}Zulu 11 32-bit Java installation successful.${COL_DEF}" >&3 } @test "destructive-install_adopt" { - echo -e "# \e[36mAdoptOpenJDK 11 Java installation is being (test-)installed..." >&3 + echo -e "# ${COL_CYAN}AdoptOpenJDK 11 Java installation is being (test-)installed...${COL_DEF}" >&3 run adoptopenjdk_install_apt if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mAdoptOpenJDK 11 Java installation successful." >&3 + echo -e "# ${COL_GREEN}AdoptOpenJDK 11 Java installation successful.${COL_DEF}" >&3 } diff --git a/functions/nodejs-apps.bats b/functions/nodejs-apps.bats index d947493eb..1e7e211d1 100644 --- a/functions/nodejs-apps.bats +++ b/functions/nodejs-apps.bats @@ -13,13 +13,13 @@ teardown_file() { } @test "installation-frontail_install" { - echo -e "# \e[36mFrontail installation starting...\e[0m" >&3 + echo -e "# ${COL_CYAN}Frontail installation starting...${COL_DEF}" >&3 run frontail_setup if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mFrontail installation successful.\e[0m" >&3 + echo -e "# ${COL_GREEN}Frontail installation successful.${COL_DEF}" >&3 run systemctl is-active --quiet frontail.service if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mFrontail service is running.\e[0m" >&3 + echo -e "# ${COL_GREEN}Frontail service is running.${COL_DEF}" >&3 } diff --git a/functions/packages.bash b/functions/packages.bash index 265946caa..9409b56fe 100644 --- a/functions/packages.bash +++ b/functions/packages.bash @@ -1,103 +1,142 @@ #!/usr/bin/env bash -timestamp() { date +"%F_%T_%Z"; } - samba_setup() { - echo -n "$(timestamp) [openHABian] Setting up Samba network shares... " - if ! command -v samba &>/dev/null; then - if ! cond_redirect apt-get install --yes samba; then echo "FAILED"; return 1; fi + echo -n "$(timestamp) [openHABian] Installing Samba... " + if ! [ -x "$(command -v samba)" ]; then + if cond_redirect apt-get install --yes samba; then echo "OK"; else echo "FAILED"; return 1; fi fi + + echo "$(timestamp) [openHABian] Setting up Samba network shares... " cond_echo "Copying over custom 'smb.conf'... " cp "$BASEDIR"/includes/smb.conf /etc/samba/smb.conf cond_echo "Writing authentication data to openHABian default... " - if ! /usr/bin/smbpasswd -e "$username" &>/dev/null; then + if ! smbpasswd -e "${username:-openhabian}" &> /dev/null; then # shellcheck disable=SC2154 - ( (echo "${userpw}"; echo "${userpw}") | /usr/bin/smbpasswd -s -a "${username}" > /dev/null ) + ( (echo "$userpw"; echo "$userpw") | smbpasswd -s -a "${username:-openhabian}" > /dev/null ) fi - cond_redirect systemctl enable smbd.service - cond_redirect systemctl restart smbd.service - echo "OK" + + echo -n "$(timestamp) [openHABian] Setting up Samba service... " + if ! cond_redirect systemctl enable smbd.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart smbd.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi } firemotd_setup() { - FAILED=0 - echo -n "$(timestamp) [openHABian] Downloading and setting up FireMotD... " - cond_redirect apt-get install --yes bc sysstat jq moreutils - - # fetch and install - if ! cond_redirect curl -s https://raw.githubusercontent.com/OutsideIT/FireMotD/master/FireMotD -o /tmp/FireMotD; then echo "FAILED"; return 1; fi - chmod 755 /tmp/FireMotD - cond_redirect /tmp/FireMotD -I - # generate theme - cond_redirect FireMotD -G Gray - # initial apt updates check - cond_redirect FireMotD -S - # the following is already in bash_profile by default - if ! grep -q "FireMotD" /home/"$username"/.bash_profile; then - echo -e "\\necho\\nFireMotD --theme Gray \\necho" >> /home/"$username"/.bash_profile - fi - - # invoke apt updates check every night + local temp + + temp="$(mktemp "${TEMP:-/tmp}"/openhabian.XXXXX)" + + echo -n "$(timestamp) [openHABian] Installing FireMotD required packages... " + if cond_redirect apt-get install --yes bc sysstat jq moreutils; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Downloading FireMotD... " + if cond_redirect wget -qO "$temp" https://raw.githubusercontent.com/OutsideIT/FireMotD/master/FireMotD; then + echo "OK" + else + echo "FAILED" + rm -f "$temp" + return 1 + fi + + echo -n "$(timestamp) [openHABian] Setting up FireMotD... " + chmod 755 "$temp" + if cond_redirect "$temp" -I; then echo "OK"; rm -f "$temp"; else echo "FAILED"; rm -f "$temp"; return 1; fi + + echo -n "$(timestamp) [openHABian] Generating FireMotD theme... " + if cond_redirect FireMotD -G Gray; then echo "OK"; else echo "FAILED"; return 1; fi + + if ! grep -q "FireMotD" /home/"${username:-openhabian}"/.bash_profile; then + echo -n "$(timestamp) [openHABian] Make FireMotD display on login... " + if echo -e "\\necho\\nFireMotD --theme Gray \\necho" >> /home/"${username:-openhabian}"/.bash_profile; then echo "OK"; else echo "FAILED"; return 1; fi + fi + + echo "$(timestamp) [openHABian] Setting up FireMotD apt updates count service... " + cond_echo "Make FireMotD check for new updates every night... " echo "# FireMotD system updates check (randomly execute between 0:00:00 and 5:59:59)" > /etc/cron.d/firemotd echo "0 0 * * * root perl -e 'sleep int(rand(21600))' && /bin/bash /usr/local/bin/FireMotD -S &>/dev/null" >> /etc/cron.d/firemotd - # invoke apt updates check after every apt action ('apt-get upgrade', ...) + cond_echo "Make FireMotD check for new updates after using apt... " echo "DPkg::Post-Invoke { \"if [ -x /usr/local/bin/FireMotD ]; then echo -n 'Updating FireMotD available updates count ... '; /bin/bash /usr/local/bin/FireMotD --skiprepoupdate -S; echo ''; fi\"; };" > /etc/apt/apt.conf.d/15firemotd - echo "OK" + cond_echo "Initial FireMotD updates check" + if cond_redirect FireMotD -S; then echo "OK"; else echo "FAILED"; return 1; fi +} + +exim_setup() { + echo -n "$(timestamp) [openHABian] Installing MTA required packages... " + if cond_redirect apt-get install --yes exim4 dnsutils mailutils; then + echo "OK" + create_mta_config + else + echo "FAILED" + return 1 + fi } create_mta_config() { - TMP="$(mktemp /tmp/openhabian.XXXXX)" - HOSTNAME=$(/bin/hostname) - INTERFACES="$(/usr/bin/dig +short "$HOSTNAME" | /usr/bin/tr '\n' ';');127.0.0.1;::1" - EXIM_CLIENTCONFIG=/etc/exim4/passwd.client - - introtext1="We will guide you through the install of exim4 as the mail transfer agent on your system and configure it to relay mails through a public service such as Google gmail.\\nPlease -enter the data shown in the next window when being asked for. You will be able to repeat the whole installation if required by selecting the openHABian menu for MTA again." - introtext2="Mail server type: mail sent by smarthost; received via SMTP or fetchmail\\nSystem mail name: FQDN (your full hostname including the domain part)\\nIPs should be allowed by the se -rver: $INTERFACES\\nOther destinations for which mail is accepted: $HOSTNAME\\nMachines to relay mail for: Leave empty\\nIP address or host name of outgoing smarthost: smtp.gmail.com::587\\nHide - local mail name in outgoing mail: No\\nKeep number of DNS-queries minimal: No\\nDelivery method: Select: Maildir format in home directory\\nMinimize number of DNS queries: No\\nSplit configurat -ion into small files: Yes\\n" + local eximConfig + local interfaces + local introtext1 + local introtext2 + local relayPass + local relayUser + local smartHost + local temp + + eximConfig="/etc/exim4/passwd.client" + interfaces="$(dig +short "$HOSTNAME" | tr '\n' ';');127.0.0.1;::1" + introtext1="We will guide you through the install of exim4 as the mail transfer agent on your system and configure it to relay mails through a public service such as Google gmail.\\n\\nPlease enter the data shown in the next window when being asked for. You will be able to repeat the whole installation if required by selecting the openHABian menu for MTA again." + introtext2="Mail server type: mail sent by smarthost (received via SMTP or fetchmail)\\n\\nSystem mail name: FQDN (your full hostname including the domain part)\\n\\nIPs that should be allowed by the server: $interfaces\\n\\nOther destinations for which mail is accepted: $HOSTNAME\\n\\nMachines to relay mail for: Leave empty\\n\\nIP address or host name of outgoing smarthost: smtp.gmail.com::587\\n\\nHide local mail name in outgoing mail: No\\n\\nKeep number of DNS-queries minimal: No\\n\\nDelivery method: Select: Maildir format in home directory\\n\\nMinimize number of DNS queries: No\\n\\nSplit configuration into small files: Yes" + temp="$(mktemp "${TEMP:-/tmp}"/openhabian.XXXXX)" + if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Start" --no-button "don't change MTA" --yesno "$introtext1" 12 78); then echo "CANCELED"; return 0; fi - if ! (whiptail --title "Mail Transfer Agent installation" --msgbox "$introtext2" 18 78); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Begin" --no-button "Cancel" --yesno "$introtext1" 13 80); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext2" 27 80); then echo "CANCELED"; return 0; fi else - echo "Ignoring MTA config creation in unattended install mode." + echo "$(timestamp) [openHABian] Ignoring MTA config creation in unattended install mode." return 0 fi - dpkg-reconfigure exim4-config - if ! SMARTHOST=$(whiptail --title "Enter public mail service smarthost to relay your mails to" --inputbox "\\nEnter the list of smarthost(s) to use your account for" 9 78 "*.google.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if ! RELAYUSER=$(whiptail --title "Enter your public service mail user" --inputbox "\\nEnter the mail username of the public service to relay all outgoing mail to ${SMARTHOST}" 9 78 "yourname@gmail.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if ! RELAYPASS=$(whiptail --title "Enter your public service mail password" --passwordbox "\\nEnter the password used to relay mail as ${RELAYUSER}@${SMARTHOST}" 9 78 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - - grep '^#' "${EXIM_CLIENTCONFIG}" > "$TMP" - echo "${SMARTHOST}:${RELAYUSER}:${RELAYPASS}" >> "$TMP" - cp "$TMP" "${EXIM_CLIENTCONFIG}" - /bin/chmod o-rwx "${EXIM_CLIENTCONFIG}" - { - echo "openhab: ${RELAYUSER}";echo "openhabian: ${RELAYUSER}"; - echo "root: ${RELAYUSER}";echo "backup: ${RELAYUSER}"; - } >> /etc/email-addresses - rm -f "$TMP" -} + echo "$(timestamp) [openHABian] Reconfiguring exim4-config... " + if cond_redirect dpkg-reconfigure exim4-config; then echo "OK"; else echo "FAILED"; return 1; fi -exim_setup() { - if apt-get install --yes exim4 dnsutils mailutils &>/dev/null; then + if ! smartHost=$(whiptail --title "Enter public mail service smarthost to relay your mails to" --inputbox "\\nEnter the list of smarthost(s) to use your account for" 9 80 "*.google.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! relayUser=$(whiptail --title "Enter your public service mail user" --inputbox "\\nEnter the mail username of the public service to relay all outgoing mail to $smartHost" 9 80 "yourname@gmail.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! relayPass=$(whiptail --title "Enter your public service mail password" --passwordbox "\\nEnter the password used to relay mail as ${relayUser}@${smartHost}" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + + echo "$(timestamp) [openHABian] Creating MTA config... " + if { + grep '^#' "$eximConfig" > "$temp" + echo "${smartHost}:${relayUser}:${relayPass}" >> "$temp" + cp "$temp" "$eximConfig" + chmod o-rwx "$eximConfig" + }; then echo "OK" - create_mta_config else echo "FAILED" + rm -f "$temp" + return 1 fi + rm -f "$temp" + + echo "$(timestamp) [openHABian] Adding to $relayUser email to system accounts... " + if { + echo "openhab: $relayUser"; echo "openhabian: $relayUser" + echo "root: $relayUser"; echo "backup: $relayUser" + } >> /etc/email-addresses; then echo "OK"; else echo "FAILED"; return 1; fi } etckeeper_setup() { echo -n "$(timestamp) [openHABian] Installing etckeeper (git based /etc backup)... " - if apt-get install --yes etckeeper &>/dev/null; then - cond_redirect sed -i 's/VCS="bzr"/\#VCS="bzr"/g' /etc/etckeeper/etckeeper.conf - cond_redirect sed -i 's/\#VCS="git"/VCS="git"/g' /etc/etckeeper/etckeeper.conf - if cond_redirect bash -c "cd /etc && etckeeper init && git config user.email 'etckeeper@localhost' && git config user.name 'openhabian' && git commit -m 'initial checkin' && git gc"; then echo "OK"; else echo "FAILED"; fi + if cond_redirect apt-get install --yes etckeeper; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Configuring etckeeper (git based /etc backup)... " + if { + sed -i 's/VCS="bzr"/\#VCS="bzr"/g' /etc/etckeeper/etckeeper.conf + sed -i 's/\#VCS="git"/VCS="git"/g' /etc/etckeeper/etckeeper.conf + bash -c "cd /etc && etckeeper init && git config user.email 'etckeeper@localhost' && git config user.name 'openhabian' && git commit -m 'initial checkin' && git gc" + }; then + echo "OK" else - echo "FAILED"; + echo "FAILED" + return 1 fi } @@ -141,8 +180,8 @@ homegear_setup() { echo -n "$(timestamp) [openHABian] Setting up Homegear service... " cp "$BASEDIR"/includes/homegear.service /lib/systemd/system/homegear.service - if ! systemctl enable homegear.service; then echo "FAILED"; return 1; fi - if systemctl restart homegear.service; then echo "OK"; else echo "FAILED"; return 1; fi + if ! systemctl enable homegear.service; then echo "FAILED (enable service)"; return 1; fi + if systemctl restart homegear.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi if [ -n "$INTERACTIVE" ]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 14 80 @@ -150,464 +189,456 @@ homegear_setup() { } mqtt_setup() { - FAILED=0 - introtext="The MQTT broker Eclipse Mosquitto will be installed through the official repository, as desribed in: https://mosquitto.org/2013/01/mosquitto-debian-repository\\nAdditionally you can activate username:password authentication.\\n\\nHEADS UP: Only proceed when you are aware that this will be in conflict with use of the MQTTv2 binding which will also be using the same ports." - failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - successtext="Setup was successful.\\nEclipse Mosquitto is now up and running in the background. You should be able to make a first connection. -To continue your integration in openHAB 2, please follow the instructions under: https://www.openhab.org/addons/bindings/mqtt/ -" - echo -n "$(timestamp) [openHABian] Setting up the MQTT broker Eclipse Mosquitto... " + local introtext + local mqttpasswd + local mqttuser + local questiontext + local successtext + introtext="The MQTT broker Eclipse Mosquitto will be installed through the official repository, as desribed in: https://mosquitto.org/2013/01/mosquitto-debian-repository\\n\\nAdditionally you can activate username:password authentication.\\n\\nHEADS UP: Only proceed when you are aware that this will be in conflict with use of the MQTTv2 binding which will also be using the same ports." mqttuser="openhabian" - question="Do you want to secure your MQTT broker by a username:password combination? Every client will need to provide these upon connection.\\nUsername will be '$mqttuser', please provide a password (consisting of ASCII printable characters except space). Leave blank for no authentication, run method again to change." - mqttpasswd=$(whiptail --title "MQTT Authentication" --inputbox "$question" 15 80 3>&1 1>&2 2>&3) + questiontext="\\nDo you want to secure your MQTT broker by a username:password combination? Every client will need to provide these upon connection.\\n\\nUsername will be '$mqttuser', please provide a password (consisting of ASCII printable characters except space). Leave blank for no authentication, run setup again to change." + successtext="Setup was successful.\\n\\nEclipse Mosquitto is now up and running in the background. You should be able to make a first connection.\\n\\nTo continue your integration in openHAB, please follow the instructions under: https://www.openhab.org/addons/bindings/mqtt/" + + echo -n "$(timestamp) [openHABian] Beginning the MQTT broker Eclipse Mosquitto installation... " + if [ -n "$INTERACTIVE" ]; then + if ! (whiptail --title "MQTT installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 14 80); then echo "CANCELED"; return 0; fi + fi + if is_jessie; then - cond_redirect wget -O - http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key | apt-key add - + cond_echo "Configuring apt for Jessie install" + if ! add_keys "http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key"; then return 1; fi echo "deb http://repo.mosquitto.org/debian jessie main" > /etc/apt/sources.list.d/mosquitto-jessie.list - cond_redirect apt-get update + if ! cond_redirect apt-get update; then echo "FAILED"; return 1; fi + fi + + echo -n "$(timestamp) [openHABian] Installing MQTT... " + if cond_redirect apt-get install --yes mosquitto mosquitto-clients; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Configuring MQTT... " + if [ -n "$INTERACTIVE" ]; then + if ! mqttpasswd=$(whiptail --title "MQTT Authentication?" --inputbox "$questiontext" 14 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi fi - if ! cond_redirect apt-get install --yes mosquitto mosquitto-clients; then echo "FAILED"; return 1; fi if [ "$mqttpasswd" != "" ]; then if ! grep -q password_file /etc/mosquitto/passwd /etc/mosquitto/mosquitto.conf; then echo -e "\\npassword_file /etc/mosquitto/passwd\\nallow_anonymous false\\n" >> /etc/mosquitto/mosquitto.conf fi echo -n "" > /etc/mosquitto/passwd - if ! cond_redirect mosquitto_passwd -b /etc/mosquitto/passwd "$mqttuser" "$mqttpasswd"; then echo "FAILED"; return 1; fi + if cond_redirect mosquitto_passwd -b /etc/mosquitto/passwd "$mqttuser" "$mqttpasswd"; then echo "OK"; else echo "FAILED"; return 1; fi else - cond_redirect sed -i "/password_file/d" /etc/mosquitto/mosquitto.conf - cond_redirect sed -i "/allow_anonymous/d" /etc/mosquitto/mosquitto.conf - cond_redirect rm -f /etc/mosquitto/passwd + if ! cond_redirect sed -i "/password_file/d" /etc/mosquitto/mosquitto.conf; then echo "FAILED"; return 1; fi + if ! cond_redirect sed -i "/allow_anonymous/d" /etc/mosquitto/mosquitto.conf; then echo "FAILED"; return 1; fi + if cond_redirect rm -f /etc/mosquitto/passwd; then echo "OK"; else echo "FAILED"; return 1; fi fi - cond_redirect systemctl enable mosquitto.service - if cond_redirect systemctl restart mosquitto.service; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Setting up MQTT service... " + if ! cond_redirect systemctl enable mosquitto.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart mosquitto.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 13 80 fi } find_setup() { - introtext="Install and setup the FIND server system to allow for indoor localization of WiFi devices. Please note that FIND will run together with an app that's available on Android ONLY. See further information at http://www.internalpositioning.com" - failtext="Sadly there was a problem setting up the selected option.\\nPlease report this problem in the openHAB community forum or as an openHABian GitHub issue." - successtext="FIND setup was successful. Settings can be configured in '/etc/default/findserver'. Be sure to restart the service after.\\nObtain the FIND app for Android through the Play Store. Check out your FIND server's dashboard at: http://${HOSTNAME}:8003\\nFor further information: http://www.internalpositioning.com" + if [ -z "$INTERACTIVE" ]; then + echo "$(timestamp) [openHABian] FIND setup must be run in interactive mode! Canceling FIND setup!" + echo "CANCELED" + return 0 + fi - matched=false + local brokertext + local FINDADMIN + local FINDADMINPASS + local findArch + local findClient + local findDist + local findPass1 + local findPass2 + local findRelease + local introtext + local mqttmissingtext + local mqttpasswd + local MQTTPORT + local MQTTSERVER + local successtext + local temp - echo -n "$(timestamp) [openHABian] Setting up FIND, the Framework for Internal Navigation and Discovery... " - if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi - fi + echo -n "$(timestamp) [openHABian] Installing FIND required packages... " + if cond_redirect apt-get install --yes libsvm-tools; then echo "OK"; else echo "FAILED"; return 1; fi - # Check for updated versions: https://github.com/schollz/find/releases - FIND_RELEASE="2.4.1" - CLIENT_RELEASE="0.6" + brokertext="You've chosen to work with an external MQTT broker.\\n\\nPlease be aware that you might need to add authentication credentials. You can do so after the installation.\\n\\nConsult with the FIND documentation or the openHAB community for details." if is_arm; then - ARCH="arm" + findArch="arm" else - ARCH="amd64" + findArch="amd64" fi - FIND_SRC="https://github.com/schollz/find/releases/download/v${FIND_RELEASE}/find_${FIND_RELEASE}_linux_${ARCH}.zip" - CLIENT_SRC="https://github.com/schollz/find/releases/download/v${CLIENT_RELEASE}client/fingerprint_${CLIENT_RELEASE}_linux_${ARCH}.zip" + findRelease="https://github.com/schollz/find/releases/download/v2.4.1/find_2.4.1_linux_${findArch}.zip" + findClient="https://github.com/schollz/find/releases/download/v0.6client/fingerprint_0.6_linux_${findArch}.zip" + findDist="/var/lib/findserver" + introtext="Install and setup the FIND server system to allow for indoor localization of WiFi devices.\\n\\nPlease note that FIND will run together with an app that is only available on Android.\\n\\nThere is no iOS app available, for more information see: https://www.internalpositioning.com/faq/#can-i-use-an-iphone" + mqttmissingtext="FIND requires an MQTT broker to run, but Mosquitto could not be found on this system.\\n\\nYou can configure FIND to use any existing MQTT broker (in the next step) or you can go back and install Mosquitto from the openHABian menu.\\n\\nDo you want to continue with the FIND installation?" + mqttpasswd="/etc/mosquitto/passwd" + successtext="FIND setup was successful.\\n\\nSettings can be configured in '/etc/default/findserver'. Be sure to restart the service after.\\n\\nYou can obtain the FIND app for Android through the Play Store. There is no iOS app available, for more information see: https://www.internalpositioning.com/faq/#can-i-use-an-iphone\\n\\nCheck out your FIND server's dashboard at: http://${HOSTNAME}:8003\\n\\nFor further information: https://www.internalpositioning.com" + temp="$(mktemp -d "${TEMP:-/tmp}"/openhabian.XXXXX)" - FIND_SYSTEMCTL="/etc/systemd/system/findserver.service" - FIND_DEFAULT="/etc/default/findserver" - FIND_DSTDIR="/var/lib/findserver" - MOSQUITTO_CONF="/etc/mosquitto/mosquitto.conf" - MOSQUITTO_PASSWD="/etc/mosquitto/passwd" - DEFAULTFINDUSER="find" - FINDSERVER="localhost" - FINDPORT=8003 - FIND_TMP="/tmp/find-latest.$$" - CLIENT_TMP="/tmp/fingerprint-latest.$$" + echo -n "$(timestamp) [openHABian] Beginning setup of FIND, the Framework for Internal Navigation and Discovery... " - if [ ! -f "${MOSQUITTO_CONF}" ]; then - mqttservermessage="FIND requires an MQTT broker to run, but Mosquitto is not installed on this system.\\nYou can configure FIND to use any existing MQTT broker (in the next step) or you can go back and install Mosquitto from the openHABian menu.\\nDo you want to continue with the FIND installation?" - if ! (whiptail --title "Mosquitto not installed, continue?" --yes-button "Continue" --no-button "Back" --yesno "$mqttservermessage" 15 80); then echo "CANCELED"; return 0; fi + if [ ! -f "/etc/mosquitto/mosquitto.conf" ]; then + if ! (whiptail --defaultno --title "Mosquitto not installed, continue?" --yes-button "Continue" --no-button "Cancel" --yesno "$mqttmissingtext" 13 80); then echo "CANCELED"; return 0; fi fi - if ! MQTTSERVER=$(whiptail --title "FIND Setup" --inputbox "Please enter the hostname of the device your MQTT broker is running on:" 15 80 localhost 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if ! MQTTPORT=$(whiptail --title "FIND Setup" --inputbox "Please enter the port number the MQTT broker is listening on:" 15 80 1883 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "FIND installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 14 80); then echo "CANCELED"; return 0; fi + echo -n "$(timestamp) [openHABian] Configuring FIND... " + if ! MQTTSERVER=$(whiptail --title "FIND Setup" --inputbox "\\nPlease enter the hostname of the device your MQTT broker is running on:" 9 80 localhost 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi if [ "$MQTTSERVER" != "localhost" ]; then - brokermessage="You've chosen to work with an external MQTT broker. Please be aware that you might need to add authentication credentials. You can do so after the installation. Consult with the FIND documentation or the openHAB community for details." - whiptail --title "MQTT Broker Notice" --msgbox "$brokermessage" 15 80 - fi - - if [ -f ${MOSQUITTO_PASSWD} ]; then - if ! FINDADMIN=$(whiptail --title "findserver MQTT Setup" --inputbox "Enter a username for FIND to connect with on your MQTT broker:" 15 80 $DEFAULTFINDUSER 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if ! FINDADMINPASS=$(whiptail --title "findserver MQTT Setup" --passwordbox "Enter a password for the FIND user on your MQTT broker:" 15 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if ! secondpassword=$(whiptail --title "findserver MQTT Setup" --passwordbox "Please confirm the password for the FIND user on your MQTT broker:" 15 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - if [ "$FINDADMINPASS" = "$secondpassword" ] && [ -n "$FINDADMINPASS" ]; then - matched=true - else - echo "FAILED (password)" - return 1 - fi - if ! cond_redirect /usr/bin/mosquitto_passwd -b $MOSQUITTO_PASSWD "$FINDADMIN" "$FINDADMINPASS"; then echo "FAILED (mosquitto)"; return 1; fi - cond_redirect systemctl restart mosquitto.service || true + if ! (whiptail --title "MQTT Broker Notice" --yes-button "Continue" --no-button "Cancel" --yesno "$brokertext" 12 80); then echo "CANCELED"; return 0; fi + fi + if ! MQTTPORT=$(whiptail --title "FIND Setup" --inputbox "\\nPlease enter the port number the MQTT broker is listening on:" 9 80 1883 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if [ -f "$mqttpasswd" ]; then + if ! FINDADMIN=$(whiptail --title "findserver MQTT Setup" --inputbox "\\nEnter a username for FIND to connect with on your MQTT broker:" 9 80 find 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + while [ "$findPass1" != "$findPass2" ] && [ -z "$findPass1" ] && [ -z "$findPass2" ]; do + if ! findPass1=$(whiptail --title "findserver MQTT Setup" --passwordbox "\\nEnter a password for the FIND user on your MQTT broker:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! findPass2=$(whiptail --title "findserver MQTT Setup" --passwordbox "\\nPlease confirm the password for the FIND user on your MQTT broker:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if [ "$findPass1" == "$findPass2" ] && [ -n "$findPass1" ]; then + FINDADMINPASS="$findPass1" + else + whiptail --title "findserver MQTT Setup" --msgbox "Password mismatched or blank... Please try again!" 7 80 3>&1 1>&2 2>&3 + fi + done + if ! cond_redirect /usr/bin/mosquitto_passwd -b $mqttpasswd "$FINDADMIN" "$FINDADMINPASS"; then echo "FAILED (mosquitto password)"; return 1; fi + if ! cond_redirect systemctl restart mosquitto.service; then echo "FAILED (restart service)"; return 1; fi fi + echo "OK" - if ! cond_redirect apt-get install --yes libsvm-tools; then echo "FAILED (SVM)"; return 1; fi - - cond_redirect mkdir -p ${FIND_DSTDIR} - cond_redirect wget -O ${FIND_TMP} ${FIND_SRC} - cond_redirect wget -O ${CLIENT_TMP} ${CLIENT_SRC} - cond_redirect unzip -qo ${CLIENT_TMP} fingerprint -d ${FIND_DSTDIR} - cond_redirect unzip -qo ${FIND_TMP} -d ${FIND_DSTDIR} - cond_redirect ln -sf ${FIND_DSTDIR}/findserver /usr/sbin/findserver - cond_redirect ln -sf ${FIND_DSTDIR}/fingerprint /usr/sbin/fingerprint + echo -n "$(timestamp) [openHABian] Installing FIND... " + if ! cond_redirect mkdir -p "$findDist"; then echo "FAILED (create directory)"; return 1; fi + if ! cond_redirect wget -qO "${temp}/find.zip" "$findRelease"; then echo "FAILED (fetch FIND)"; return 1; fi + if ! cond_redirect wget -qO "${temp}/client.zip" "$findClient"; then echo "FAILED (fetch client)"; return 1; fi + if ! cond_redirect unzip -qo "${temp}/find.zip" "$findDist"; then echo "FAILED (unzip FIND)"; return 1; fi + if ! cond_redirect unzip -qo "${temp}/client.zip" fingerprint -d "$findDist"; then echo "FAILED (unzip client)"; return 1; fi + if ! cond_redirect ln -sf "$findDist"/findserver /usr/sbin/findserver; then echo "FAILED (link findserver)"; return 1; fi + if ! cond_redirect ln -sf "$findDist"/fingerprint /usr/sbin/fingerprint; then echo "FAILED (link fingerprint)"; return 1; fi + if cond_redirect rm -rf "$temp"; then echo "OK"; else echo "FAILED"; return 1; fi - cond_echo "Writing service file '$FIND_SYSTEMCTL'" - sed -e "s|%MQTTSERVER|$MQTTSERVER|g" -e "s|%MQTTPORT|$MQTTPORT|g" -e "s|%FINDADMINPASS|$FINDADMINPASS|g" -e "s|%FINDADMIN|$FINDADMIN|g" -e "s|%FINDPORT|$FINDPORT|g" -e "s|%FINDSERVER|$FINDSERVER|g" "${BASEDIR}/includes/findserver.service" > $FIND_SYSTEMCTL - cond_echo "Writing service config file '$FIND_DEFAULT'" - sed -e "s|%MQTTSERVER|$MQTTSERVER|g" -e "s|%MQTTPORT|$MQTTPORT|g" -e "s|%FINDADMINPASS|$FINDADMINPASS|g" -e "s|%FINDADMIN|$FINDADMIN|g" -e "s|%FINDPORT|$FINDPORT|g" -e "s|%FINDSERVER|$FINDSERVER|g" "${BASEDIR}/includes/findserver" > $FIND_DEFAULT + echo -n "$(timestamp) [openHABian] Setting up FIND service... " + if ! (sed -e "s|%MQTTSERVER|$MQTTSERVER|g" -e "s|%MQTTPORT|$MQTTPORT|g" -e "s|%FINDADMINPASS|$FINDADMINPASS|g" -e "s|%FINDADMIN|$FINDADMIN|g" -e "s|%FINDPORT|8003|g" -e "s|%FINDSERVER|localhost|g" "${BASEDIR}/includes/findserver.service" > /etc/systemd/system/findserver.service); then echo "FAILED (service file creation)"; fi + if ! (sed -e "s|%MQTTSERVER|$MQTTSERVER|g" -e "s|%MQTTPORT|$MQTTPORT|g" -e "s|%FINDADMINPASS|$FINDADMINPASS|g" -e "s|%FINDADMIN|$FINDADMIN|g" -e "s|%FINDPORT|8003|g" -e "s|%FINDSERVER|localhost|g" "${BASEDIR}/includes/findserver" > /etc/default/findserver); then echo "FAILED (service configuration creation)"; fi + if ! cond_redirect systemctl enable findserver.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart findserver.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi - cond_redirect rm -f ${FIND_TMP} ${CLIENT_TMP} - cond_redirect systemctl daemon-reload - cond_redirect systemctl restart findserver.service - cond_redirect systemctl status findserver.service - if ! cond_redirect systemctl enable findserver.service; then echo "FAILED (service)"; return 1; fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 + if ! running_in_docker; then + dashboard_add_tile find fi - echo "OK" - dashboard_add_tile find } knxd_setup() { - FAILED=0 - introtext="This will install and setup kndx (successor to eibd) as your EIB/KNX IP gateway and router to support your KNX bus system. This routine was provided by 'Michels Tech Blog': https://goo.gl/qN2t0H" - failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - successtext="Setup was successful. -Please edit '/etc/default/knxd' to meet your interface requirements. For further information on knxd options, please type 'knxd --help' -Further details can be found unter: https://goo.gl/qN2t0H -Integration into openHAB 2 is described here: https://github.com/openhab/openhab/wiki/KNX-Binding -" - - echo -n "$(timestamp) [openHABian] Setting up EIB/KNX IP Gateway and Router with knxd " - echo -n "(http://michlstechblog.info/blog/raspberry-pi-eibknx-ip-gateway-and-router-with-knxd)... " - if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi - fi + local introtext + local successtext + local temp - #TODO serve file from the repository - cond_redirect wget -O /tmp/install_knxd_systemd.sh http://michlstechblog.info/blog/download/electronic/install_knxd_systemd.sh || FAILED=1 + introtext="This will install and setup kndx (successor to eibd) as your EIB/KNX IP gateway and router to support your KNX bus system.\\n\\nThis routine was provided by 'Michels Tech Blog': https://bit.ly/3dzeoKh" + successtext="Setup was successful.\\n\\nPlease edit '/etc/default/knxd' to meet your interface requirements. For further information on knxd options, please type 'knxd --help'\\n\\nFurther details can be found under: https://bit.ly/3dzeoKh\\n\\nIntegration into openHAB 2 is described here: https://github.com/openhab/openhab/wiki/KNX-Binding" + temp="$(mktemp -d "${TEMP:-/tmp}"/openhabian.XXXXX)" - #hotfix for #651: switch to newer libfmt - cond_redirect sed -i 's#git checkout tags/3.0.0#git checkout tags/5.0.0#' /tmp/install_knxd_systemd.sh || FAILED=1 - #shellcheck disable=SC2016 - cond_redirect sed -i 's#cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX fmt/#cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX .#' /tmp/install_knxd_systemd.sh || FAILED=1 - #/hotfix + echo -n "$(timestamp) [openHABian] Beginning setup of EIB/KNX IP Gateway and Router with knxd (https://bit.ly/3dzeoKh)... " + if [ -n "$INTERACTIVE" ]; then + if ! (whiptail --title "knxd installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 10 80); then echo "CANCELED"; return 0; fi + fi - #NOTE install_knxd_systemd.sh currently does not give proper exit status for errors, so installer claims success... - cond_redirect bash /tmp/install_knxd_systemd.sh || FAILED=1 - if [ $FAILED -eq 0 ]; then echo "OK. Please restart your system now..."; else echo "FAILED"; fi - #systemctl start knxd.service - #if [ $? -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + echo -n "$(timestamp) [openHABian] Installing knxd... " + # TODO: serve file from the repository + if ! cond_redirect wget -qO "$temp" https://michlstechblog.info/blog/download/electronic/install_knxd_systemd.sh; then echo "FAILED (fetch installer)"; return 1; fi + # NOTE: install_knxd_systemd.sh currently does not give proper exit status for errors, so installer claims success... + if cond_redirect bash "$temp"; then echo "OK. Please restart your system now..."; else echo "FAILED (install)"; fi if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 fi } 1wire_setup() { - FAILED=0 - introtext="This will install owserver to support 1wire functionality in general, ow-shell and usbutils are helpfull tools to check USB (lsusb) and 1wire function (owdir, owread). For more details, have a look at http://owfs.org" - failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - successtext="Setup was successful. -Next, please configure your system in /etc/owfs.conf. -Use # to comment/deactivate a line. All you should have to change is the following. Deactivate - server: FAKE = DS18S20,DS2405 -and activate one of these most common options (depending on your device): - #server: usb = all - #server: device = /dev/ttyS1 - #server: device = /dev/ttyUSB0 -" + local introtext + local successtext - echo -n "$(timestamp) [openHABian] Installing owserver (1wire)... " + introtext="This will install owserver to support 1wire functionality in general.\\n\\nUse the ow-shell and usbutils tools to check USB (lsusb) and 1wire function (owdir, owread).\\n\\nFor more details, have a look at https://owfs.org" + successtext="Setup was successful.\\n\\nNext, please configure your system in /etc/owfs.conf.\\nUse # to comment/deactivate a line. All you should have to change is the following:\\nDeactivate\\n server: FAKE = DS18S20,DS2405\\nand activate one of these most common options (depending on your device):\\n #server: usb = all\\n #server: device = /dev/ttyS1\\n #server: device = /dev/ttyUSB0" + + echo -n "$(timestamp) [openHABian] Beginning setup of owserver (1wire)... " if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "1wire installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 12 80); then echo "CANCELED"; return 0; fi fi - cond_redirect apt-get install --yes owserver ow-shell usbutils || FAILED=1 - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + echo -n "$(timestamp) [openHABian] Installing owserver (1wire)... " + if cond_redirect apt-get install --yes owserver ow-shell usbutils; then echo "OK"; else echo "FAILED"; return 1; fi if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 17 80 fi } miflora_setup() { - FAILED=0 - DIRECTORY="/opt/miflora-mqtt-daemon" - introtext="[CURRENTLY BROKEN] This will install or update miflora-mqtt-daemon - The Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. See for further details:\\n\\n https://github.com/ThomDietrich/miflora-mqtt-daemon" - failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - successtext="Setup was successful. -The Daemon was installed and the systemd service was set up just as described in it's README. Please add your MQTT broker settings in '$DIRECTORY/config.ini' and add your Mi Flora sensors. After that be sure to restart the daemon to reload it's configuration. -\\nAll details can be found under: https://github.com/ThomDietrich/miflora-mqtt-daemon -The article also contains instructions regarding openHAB integration. -" + local introtext + local mifloraDir + local successtext - echo -n "$(timestamp) [openHABian] Setting up miflora-mqtt-daemon... " + echo -n "$(timestamp) [openHABian] Installing miflora-mqtt-daemon required packages... " + if cond_redirect apt-get install --yes git python3 python3-pip bluetooth bluez; then echo "OK"; else echo "FAILED"; return 1; fi + + introtext="[CURRENTLY BROKEN] This will install or update miflora-mqtt-daemon - The Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.\\n\\nFor further details see:\\nhttps://github.com/ThomDietrich/miflora-mqtt-daemon" + mifloraDir="/opt/miflora-mqtt-daemon" + successtext="Setup was successful.\\n\\nThe Daemon was installed and the systemd service was set up just as described in it's README. Please add your MQTT broker settings in '$mifloraDir/config.ini' and add your Mi Flora sensors. After that be sure to restart the daemon to reload it's configuration.\\n\\nAll details can be found under: https://github.com/ThomDietrich/miflora-mqtt-daemon\\nThe article also contains instructions regarding openHAB integration." + + echo -n "$(timestamp) [openHABian] Beginning setup of miflora-mqtt-daemon... " if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "miflora-mqtt-daemon installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 11 80); then echo "CANCELED"; return 0; fi fi - if ! cond_redirect apt-get install --yes git python3 python3-pip bluetooth bluez; then echo "FAILED (prerequisites)"; return 1; fi - if [ ! -d "$DIRECTORY" ]; then + echo -n "$(timestamp) [openHABian] Setting up miflora-mqtt-daemon... " + if ! [ -d "$mifloraDir" ]; then cond_echo "Fresh Installation... " - cond_redirect git clone https://github.com/ThomDietrich/miflora-mqtt-daemon.git $DIRECTORY - if ! cond_redirect cp "$DIRECTORY"/config.{ini.dist,ini}; then echo "FAILED (git clone)"; return 1; fi + if ! cond_redirect git clone https://github.com/ThomDietrich/miflora-mqtt-daemon.git $mifloraDir; then echo "FAILED (git clone)"; return 1; fi + if ! cond_redirect cp "$mifloraDir"/config.{ini.dist,ini}; then echo "FAILED (copy files)"; return 1; fi else cond_echo "Update... " - if ! cond_redirect git -C "$DIRECTORY" pull --quiet origin; then echo "FAILED (git pull)"; return 1; fi - fi - cond_redirect chown -R "openhab:$username" "$DIRECTORY" - cond_redirect chmod -R ug+wX "$DIRECTORY" - if ! cond_redirect pip3 install -r "$DIRECTORY"/requirements.txt; then echo "FAILED (requirements)"; return 1; fi - cond_redirect cp "$DIRECTORY"/template.service /etc/systemd/system/miflora.service - cond_redirect systemctl daemon-reload - systemctl start miflora.service || true - cond_redirect systemctl status miflora.service - if cond_redirect systemctl enable miflora.service; then echo "OK"; else echo "FAILED"; return 1; fi - if grep -q "dtoverlay=pi3-miniuart-bt" /boot/config.txt; then + if ! cond_redirect git -C "$mifloraDir" pull --quiet origin; then echo "FAILED (git pull)"; return 1; fi + fi + if ! { + cond_redirect chown -R "openhab:${username:-openhabian}" "$mifloraDir" + cond_redirect chmod -R ug+wX "$mifloraDir" + }; then echo "FAILED (permissons)"; fi + cond_echo "Installing required python packages" + if ! cond_redirect pip3 install -r "$mifloraDir"/requirements.txt; then echo "OK"; else echo "FAILED (python packages)"; return 1; fi + + echo -n "$(timestamp) [openHABian] Setting up miflora-mqtt-daemon service... " + if ! (cp "$mifloraDir"/template.service /etc/systemd/system/miflora.service); then echo "FAILED (copy service)"; fi + if ! cond_redirect systemctl enable miflora.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart miflora.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi + + if grep -q "^[[:space:]]*dtoverlay=pi3-miniuart-bt" /boot/config.txt; then cond_echo "Warning! The internal RPi3 Bluetooth module is disabled on your system. You need to enable it before the daemon may use it." fi + if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 16 80 fi } nginx_setup() { - introtext="This will enable you to access the openHAB interface through the normal HTTP/HTTPS ports and optionally secure it with username/password and/or an SSL certificate." - failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - - echo "$(timestamp) [openHABian] Setting up Nginx as reverse proxy with authentication... " - if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi + if [ -z "$INTERACTIVE" ]; then + echo "$(timestamp) [openHABian] Nginx setup must be run in interactive mode! Canceling Nginx setup!" + echo "CANCELED" + return 0 fi + local auth + local authtext + local domain + local domainIP + local domaintext + local httpstext + local introtext + local nginxPass + local nginxPass1 + local nginxPass2 + local nginxUsername + local portwarning + local protocol + local pubIP + local secure + local validDomain + + echo -n "$(timestamp) [openHABian] Installing Nginx required packages... " + if cond_redirect apt-get install --yes dnsutils; then echo "OK"; else echo "FAILED"; return 1; fi + + auth="false" + domaintext="\\nIf you have a registered domain enter it now, if you have a static public IP enter \"IP\", otherwise leave blank:" + introtext="This will enable you to access the openHAB interface through the normal HTTP/HTTPS ports and optionally secure it with username/password and/or an SSL certificate." + secure="false" + validDomain="false" + function comment { - sed -e "/[[:space:]]$1/ s/^#*/#/g" -i "$2" + if ! sed -e "/[[:space:]]$1/ s/^#*/#/g" -i "$2"; then echo "FAILED (comment)"; fi } function uncomment { - sed -e "/$1/s/^$1//g" -i "$2" + if ! sed -e "/$1/s/^$1//g" -i "$2"; then echo "FAILED (uncomment)"; fi } - echo "Installing DNS utilities..." - apt-get -y -q install dnsutils - - AUTH=false - SECURE=false - VALIDDOMAIN=false - matched=false - canceled=false - FAILED=false - - if (whiptail --title "Authentication Setup" --yesno "Would you like to secure your openHAB interface with username and password?" 15 80); then - if username=$(whiptail --title "Authentication Setup" --inputbox "Enter a username to sign into openHAB:" 15 80 openhab 3>&1 1>&2 2>&3); then - while [ "$matched" = false ] && [ "$canceled" = false ]; do - password=$(whiptail --title "Authentication Setup" --passwordbox "Enter a password for $username:" 15 80 3>&1 1>&2 2>&3) - secondpassword=$(whiptail --title "Authentication Setup" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3) - if [ "$password" = "$secondpassword" ] && [ -n "$password" ]; then - matched=true - AUTH=true + echo "$(timestamp) [openHABian] Beginning setup of Nginx as reverse proxy with authentication... " + if ! (whiptail --title "Nginx installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 9 80); then echo "CANCELED"; return 0; fi + + echo "$(timestamp) [openHABian] Configuring Nginx authentication options... " + if (whiptail --title "Authentication Setup" --yesno "Would you like to secure your openHAB interface with username and password?" 7 80); then + if nginxUsername=$(whiptail --title "Authentication Setup" --inputbox "\\nEnter a username to sign into openHAB:" 9 80 openhab 3>&1 1>&2 2>&3); then + while [ "$nginxPass1" != "$nginxPass2" ] && [ -z "$nginxPass1" ] && [ -z "$nginxPass2" ]; do + if ! nginxPass1=$(whiptail --title "Authentication Setup" --passwordbox "\\nEnter a password for ${username}:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! nginxPass2=$(whiptail --title "Authentication Setup" --passwordbox "\\nPlease confirm the password:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if [ "$nginxPass1" == "$nginxPass2" ] && [ -n "$nginxPass1" ]; then + nginxPass="$nginxPass1" else - password=$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3) + whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 7 80 3>&1 1>&2 2>&3 fi done else - canceled=true + echo "CANCELED" + return 0 fi + auth="true" fi - if (whiptail --title "Secure Certificate Setup" --yesno "Would you like to secure your openHAB interface with HTTPS?" 15 80); then - SECURE=true + if (whiptail --title "Secure Certificate Setup" --yesno "Would you like to secure your openHAB interface with HTTPS?" 7 80); then secure="true"; echo "OK"; else echo "CANCELED"; return 0; fi + + if [ "$auth" == "true" ]; then + authtext="Authentication Enabled\\n- Username: $nginxUsername" + else + authtext="Authentication Disabled" + fi + + if [ "$secure" == "true" ]; then + httpstext="Proxy will be secured by HTTPS" + protocol="HTTPS" + portwarning="Important! Before you continue, please make sure that port 80 (HTTP) of this machine is reachable from the internet (portforwarding, etc.). Otherwise the certbot connection test will fail.\\n\\n" + else + httpstext="Proxy will not be secured by HTTPS" + protocol="HTTP" + portwarning="" fi - echo -n "Obtaining public IP address... " - wanip=$(dig +short myip.opendns.com @resolver1.opendns.com |tail -1) - echo "$wanip" + echo "$(timestamp) [openHABian] Configuring Nginx network options... " + cond_echo "Obtaining public IP address... " + if pubIP=$(dig +short myip.opendns.com @resolver1.opendns.com | tail -1); then echo "$pubIP"; else echo "FAILED"; return 1; fi - domain=$(whiptail --title "Domain Setup" --inputbox "If you have a registered domain enter it now, if you have a static public IP enter \"IP\", otherwise leave blank:" 15 80 3>&1 1>&2 2>&3) + cond_echo "Configuring domain settings... " + if ! domain=$(whiptail --title "Domain Setup" --inputbox "$domaintext" 10 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi - while [ "$VALIDDOMAIN" = false ] && [ -n "$domain" ] && [ "$domain" != "IP" ]; do - echo -n "Obtaining domain IP address... " - domainip=$(dig +short "$domain" @resolver1.opendns.com |tail -1) - echo "$domainip" - if [ "$wanip" = "$domainip" ]; then - VALIDDOMAIN=true - echo "Public and domain IP address match" + while [ "$validDomain" == "false" ] && [ -n "$domain" ] && [ "$domain" != "IP" ]; do + cond_echo "Obtaining domain IP address... " + if domainIP=$(dig +short "$domain" @resolver1.opendns.com | tail -1); then echo "$domainIP"; else echo "FAILED"; return 1; fi + if [ "$pubIP" = "$domainIP" ]; then + validDomain="true" + cond_echo "Public and domain IP address match" else - echo "Public and domain IP address mismatch!" - domain=$(whiptail --title "Domain Setup" --inputbox "Domain does not resolve to your public IP address. Please enter a valid domain, if you have a static public IP enter \"IP\",leave blank to not use a domain name:" 15 80 3>&1 1>&2 2>&3) + cond_echo "Public and domain IP address mismatch!" + domain=$(whiptail --title "Domain Setup" --inputbox "\\nDomain does not resolve to your public IP address. Please enter a valid domain.\\n\\n$domaintext" 14 80 3>&1 1>&2 2>&3) fi done - if [ "$VALIDDOMAIN" = false ]; then + if [ "$validDomain" == "false" ]; then if [ "$domain" == "IP" ]; then - echo "Setting domain to static public IP address $wanip" - domain=$wanip + cond_echo "Setting domain to static public IP address $pubIP" + domain="$pubIP" else - echo "Setting no domain nor static public IP address" + cond_echo "Setting domain to localhost" domain="localhost" fi + validDomain="true" fi - if [ "$AUTH" = true ]; then - authtext="Authentication Enabled\\n- Username: $username" - else - authtext="Authentication Disabled" - fi - - if [ "$SECURE" = true ]; then - httpstext="Proxy will be secured by HTTPS" - protocol="HTTPS" - portwarning="Important! Before you continue, please make sure that port 80 (HTTP) of this machine is reachable from the internet (portforwarding, ...). Otherwise the certbot connection test will fail.\\n\\n" - else - httpstext="Proxy will not be secured by HTTPS" - protocol="HTTP" - portwarning="" - fi - - confirmtext="The following settings have been chosen:\\n\\n- $authtext\\n- $httpstext\\n- Domain: $domain (Public IP Address: $wanip) - \\nYou will be able to connect to openHAB on the default $protocol port. - \\n${portwarning}Do you wish to continue and setup an NGINX server now?" + confirmtext="The following settings have been chosen:\\n\\n- $authtext\\n- $httpstext\\n- Domain: $domain (Public IP Address: $pubIP)\\n\\nYou will be able to connect to openHAB on the default $protocol port.\\n\\n${portwarning}Do you wish to continue and setup an Nginx server now?" - if (whiptail --title "Confirmation" --yesno "$confirmtext" 22 80); then - echo "Installing NGINX..." - apt-get -y -q install nginx || FAILED=true + if ! (whiptail --title "Confirmation" --yesno "$confirmtext" 20 80); then echo "CANCELED"; return 0; fi + echo -n "$(timestamp) [openHABian] Installing Nginx..." + if cond_redirect apt-get install --yes install nginx; then echo "OK"; else echo "FAILED"; return 1; fi + echo -n "$(timestamp) [openHABian] Setting up Nginx configuration..." + if { rm -rf /etc/nginx/sites-enabled/default cp "$BASEDIR"/includes/nginx.conf /etc/nginx/sites-enabled/openhab - sed -i "s/DOMAINNAME/${domain}/g" /etc/nginx/sites-enabled/openhab - - if [ "$AUTH" = true ]; then - echo "Installing password utilities..." - apt-get -y -q install apache2-utils || FAILED=true - echo "Creating password file..." - htpasswd -b -c /etc/nginx/.htpasswd "$username" "$password" - uncomment "#AUTH" /etc/nginx/sites-enabled/openhab - fi - - if [ "$SECURE" = true ]; then - if [ "$VALIDDOMAIN" = true ]; then - certbotpackage="python3-certbot-nginx" - if is_debian || is_raspbian; then - gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553 - gpg -a --export 8B48AD6246925553 | apt-key add - - gpg --keyserver pgpkeys.mit.edu --recv-key 7638D0442B90D010 - gpg -a --export 7638D0442B90D010 | apt-key add - - if is_jessie; then - certbotrepo="jessie-backports" - certbotpackage="certbot" - elif is_stretch; then - certbotrepo="stretch-backports" - fi - certbotoption="-t" - if [ -n "$certbotrepo" ]; then - echo -e "# This file was added by openHABian to install certbot\\ndeb http://ftp.debian.org/debian ${certbotrepo} main" > /etc/apt/sources.list.d/backports.list - apt-get update - fi - elif is_ubuntu; then - apt -get -y -q --force-yes install software-properties-common - add-apt-repository ppa:certbot/certbot - fi - echo "Installing certbot..." - apt-get -y -q --force-yes install "${certbotpackage}" "${certbotoption}" "${certbotrepo}" - mkdir -p /var/www/$domain - uncomment "#WEBROOT" /etc/nginx/sites-enabled/openhab - nginx -t && service nginx reload - echo "Creating Let's Encrypt certificate..." - certbot certonly --webroot -w /var/www/$domain -d $domain || FAILED=true #This will cause a prompt - if [ "$FAILED" = false ]; then - certpath="/etc/letsencrypt/live/$domain/fullchain.pem" - keypath="/etc/letsencrypt/live/$domain/privkey.pem" + }; then echo "OK"; else echo "FAILED"; fi + + if [ "$auth" == "true" ]; then + cond_echo "Setting up Nginx password options..." + echo -n "$(timestamp) [openHABian] Installing Nginx password utilities..." + if cond_redirect apt-get install --yes install apache2-utils; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect htpasswd -b -c /etc/nginx/.htpasswd "$nginxUsername" "$nginxPass"; then echo "OK"; else echo "FAILED (password file)"; return 1; fi + uncomment "#AUTH" /etc/nginx/sites-enabled/openhab + fi + + if [ "$secure" == "true" ]; then + cond_echo "Setting up Nginx security options..." + if [ "$validDomain" == "true" ]; then + echo -n "$(timestamp) [openHABian] Installing certbot..." + if is_ubuntu; then + if ! dpkg -s 'software-properties-common' > /dev/null 2>&1; then + if ! cond_redirect apt-get install --yes software-properties-common; then echo "FAILED (Ubuntu prerequsites)"; return 1; fi fi - else - mkdir -p /etc/ssl/certs - certpath="/etc/ssl/certs/openhab.crt" - keypath="/etc/ssl/certs/openhab.key" - password=$(whiptail --title "openSSL Key Generation" --msgbox "openSSL is about to ask for information in the command line, please fill out each line." 15 80 3>&1 1>&2 2>&3) - openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout $keypath -out $certpath || FAILED=true #This will cause a prompt - fi - if [ "$FAILED" = false ]; then - uncomment "#CERT" /etc/nginx/sites-enabled/openhab - sed -i "s|CERTPATH|${certpath}|g" /etc/nginx/sites-enabled/openhab - sed -i "s|KEYPATH|${keypath}|g" /etc/nginx/sites-enabled/openhab - uncomment "#REDIR" /etc/nginx/sites-enabled/openhab - comment "listen" /etc/nginx/sites-enabled/openhab - uncomment "#SSL" /etc/nginx/sites-enabled/openhab + if ! cond_redirect add-apt-repository universe; then echo "FAILED (add universe repo)"; return 1; fi + if ! add-apt-repository ppa:certbot/certbot; then echo "FAILED (add certbot repo)"; return 1; fi + if ! cond_redirect apt-get update; then echo "FAILED (update apt lists)"; return 1; fi fi - fi - - nginx -t && systemctl restart nginx.service || FAILED=true - if [ "$FAILED" = true ]; then - whiptail --title "Operation Failed!" --msgbox "$failtext" 15 80 + if cond_redirect apt-get install --yes certbot python3-certbot-nginx; then echo "OK"; else echo "FAILED"; return 1; fi + + echo -n "$(timestamp) [openHABian] Configuring certbot..." + mkdir -p /var/www/"$domain" + uncomment "#WEBROOT" /etc/nginx/sites-enabled/openhab + if ! { + nginx -t + systemctl reload nginx + }; then echo "FAILED (Nginx reload)"; fi + if cond_redirect certbot certonly --webroot -w /var/www/"$domain" -d "$domain"; then echo "OK"; else echo "FAILED"; return 1; fi + certpath="/etc/letsencrypt/live/${domain}/fullchain.pem" + keypath="/etc/letsencrypt/live/${domain}/privkey.pem" else - whiptail --title "Operation Successful!" --msgbox "Setup successful. Please try entering $protocol://$domain in a browser to test your settings." 15 80 + echo -n "$(timestamp) [openHABian] Configuring openSSL..." + mkdir -p /etc/ssl/certs + certpath="/etc/ssl/certs/openhab.crt" + keypath="/etc/ssl/certs/openhab.key" + whiptail --title "openSSL Key Generation" --msgbox "openSSL is about to ask for information in the command line, please fill out each line." 8 80 3>&1 1>&2 2>&3 + if ! openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout $keypath -out $certpath; then echo "FAILED (openSSL configuration)"; fi fi - else - whiptail --title "Operation Canceled!" --msgbox "Setup was canceled, no changes were made." 15 80 + uncomment "#CERT" /etc/nginx/sites-enabled/openhab + if ! sed -i "s|CERTPATH|${certpath}|g" /etc/nginx/sites-enabled/openhab; then echo "FAILED (certpath config)"; fi + if ! sed -i "s|KEYPATH|${keypath}|g" /etc/nginx/sites-enabled/openhab; then echo "FAILED (keypath config)"; fi + uncomment "#REDIR" /etc/nginx/sites-enabled/openhab + comment "listen" /etc/nginx/sites-enabled/openhab + uncomment "#SSL" /etc/nginx/sites-enabled/openhab fi + + if ! { + nginx -t + systemctl restart nginx.service + }; then echo "FAILED (Nginx service)"; fi + + whiptail --title "Operation Successful!" --msgbox "Setup successful. Please try entering $protocol://$domain in a browser to test your settings." 8 80 } ## Function for installing Telldus Core service for Tellstick USB devices. ## The function can be invoked either INTERACTIVE with userinterface or UNATTENDED. ## -## tellstick_core_setup() +## telldus_core_setup() ## +telldus_core_setup() { + local dist + local introtext + local successtext -tellstick_core_setup() { - FAILED=0 - introtext="This will install tellstick core services to enable support for USB connected Tellstick and Tellstick duo. For more details, have a look at http://developer.telldus.se/" - failtext="Sadly there was a problem setting up tellstick core. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - successtext="Success, please reboot your system to complete the installation. - -Next, add your devices in /etc/tellstick.conf. -To detect device IDs use commanline tool: tdtool-improved --event -When devices are added restart telldusd.service by using: sudo systemctl restart telldusd -or just reboot the system. -" + introtext="This will install Telldus Core services to enable support for USB devices connected Tellstick and Tellstick duo." + successtext="Success, please reboot your system to complete the installation.\\n\\nNext, add your devices in /etc/tellstick.conf.\\n\\nTo detect device IDs issue the command:\\n- tdtool-improved --event\\n\\nWhen devices are added, restart telldusd.service by rebooting the system or using:\\n- sudo systemctl restart telldusd.service" + + echo -n "$(timestamp) [openHABian] Beginning setup of Telldus Core... " if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi + if ! (whiptail --title "Telldus Core installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 8 80); then echo "CANCELED"; return 0; fi fi - echo -n "$(timestamp) [openHABian] Installing tellstick-core... " if is_aarch64 ; then dpkg --add-architecture armhf fi # Maybe add new repository to be able to install libconfuse1 + echo -n "$(timestamp) [openHABian] Adding libconfuse1 repository to apt... " if is_xenial ; then echo 'APT::Default-Release "xenial";' > /etc/apt/apt.conf.d/01release echo "deb http://archive.ubuntu.com/ubuntu/ bionic universe" > /etc/apt/sources.list.d/ubuntu-artful.list - cond_redirect apt-get update - cond_redirect apt-get -y -t=bionic install libconfuse1 + dist="bionic" fi if is_jessie ; then echo 'APT::Default-Release "jessie";' > /etc/apt/apt.conf.d/01release echo "deb http://deb.debian.org/debian stretch main" > /etc/apt/sources.list.d/debian-stretch.list - cond_redirect apt-get update - cond_redirect apt-get -y -t=stretch install libconfuse1 + dist="stretch" fi # libconfuse1 is only available from old stretch repos, but currently still needed if is_buster ; then @@ -617,35 +648,31 @@ or just reboot the system. else echo "deb http://deb.debian.org/debian stretch main" > /etc/apt/sources.list.d/debian-stretch.list fi - cond_redirect apt-get update || FAILED=1 - cond_redirect apt-get -y -t=stretch install libconfuse1 || FAILED=1 - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + dist="stretch" fi + if ! cond_redirect apt-get update; then echo "FAILED (update apt lists)"; return 1; fi + if cond_redirect apt-get install --yes --target-release "$dist" libconfuse1; then echo "OK"; else echo "FAILED"; fi + + if ! add_keys "https://s3.eu-central-1.amazonaws.com/download.telldus.com/debian/telldus-public.key"; then return 1; fi - cond_redirect wget -O - https://s3.eu-central-1.amazonaws.com/download.telldus.com/debian/telldus-public.key | apt-key add - + echo -n "$(timestamp) [openHABian] Adding telldus repository to apt... " echo "deb https://s3.eu-central-1.amazonaws.com/download.telldus.com unstable main" > /etc/apt/sources.list.d/telldus-unstable.list - cond_redirect apt-get update - cond_redirect apt-get install --yes libjna-java telldus-core || FAILED=1 - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if cond_redirect apt-get update; then echo "OK"; else echo "FAILED (update apt lists)"; return 1; fi - echo -n "$(timestamp) [openHABian] Setting up systemd service for telldusd... " - cond_redirect cp "${BASEDIR}"/includes/tellstick-core/telldusd.service /lib/systemd/system/telldusd.service - cond_redirect systemctl daemon-reload || FAILED=1 - cond_redirect systemctl enable telldusd || FAILED=1 - cond_redirect systemctl start telldusd.service || FAILED=1 - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + echo -n "$(timestamp) [openHABian] Installing telldus-core... " + if cond_redirect apt-get install --yes libjna-java telldus-core; then echo "OK"; else echo "FAILED"; fi + + echo -n "$(timestamp) [openHABian] Setting up telldus-core service... " + cp "$BASEDIR"/includes/tellstick-core/telldusd.service /lib/systemd/system/telldusd.service + if ! systemctl enable telldusd.service; then echo "FAILED (enable service)"; return 1; fi + if systemctl restart telldusd.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up tdtool-improved... " - cond_redirect git clone https://github.com/EliasGabrielsson/tdtool-improved.py.git /opt/tdtool-improved - cond_redirect chmod +x /opt/tdtool-improved/tdtool-improved.py - cond_redirect ln -sf /opt/tdtool-improved/tdtool-improved.py /usr/bin/tdtool-improved - echo "OK" + if ! cond_redirect git clone https://github.com/EliasGabrielsson/tdtool-improved.py.git /opt/tdtool-improved; then echo "FAILED (clone)"; fi + chmod +x /opt/tdtool-improved/tdtool-improved.py + if ! cond_redirect ln -sf /opt/tdtool-improved/tdtool-improved.py /usr/bin/tdtool-improved; then echo "OK"; else echo "FAILED (link)"; fi if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + whiptail --title "Operation Successful!" --msgbox "$successtext" 16 80 fi } diff --git a/functions/packages.bats b/functions/packages.bats index 78641524f..b796f45d5 100644 --- a/functions/packages.bats +++ b/functions/packages.bats @@ -11,28 +11,45 @@ teardown_file() { unset BASEDIR systemctl stop homegear.service || true systemctl stop mosquitto.service || true + systemctl stop telldusd.service || true } @test "destructive-homegear_install" { - echo -e "# \e[36mHomegear installation starting...\e[0m" >&3 + echo -e "# ${COL_CYAN}Homegear installation starting...${COL_DEF}" >&3 run homegear_setup if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mHomegear installation successful.\e[0m" >&3 + echo -e "# ${COL_GREEN}Homegear installation successful.${COL_DEF}" >&3 run systemctl is-active --quiet homegear.service if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mHomegear service is running.\e[0m" >&3 + echo -e "# ${COL_GREEN}Homegear service is running.${COL_DEF}" >&3 } @test "destructive-mqtt_install" { - echo -e "# \e[36mMQTT installation starting...\e[0m" >&3 + echo -e "# ${COL_CYAN}MQTT installation starting...${COL_DEF}" >&3 run mqtt_setup if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mMQTT installation successful.\e[0m" >&3 + echo -e "# ${COL_GREEN}MQTT installation successful.${COL_DEF}" >&3 run systemctl is-active --quiet mosquitto.service if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mMQTT service is running.\e[0m" >&3 + echo -e "# ${COL_GREEN}MQTT service is running.${COL_DEF}" >&3 +} + +@test "destructive-1wire_install" { + echo -e "# ${COL_CYAN}1wire installation starting...${COL_DEF}" >&3 + run 1wire_setup + if [ "$status" -ne 0 ]; then echo "$output" >&3; fi + [ "$status" -eq 0 ] + echo -e "# ${COL_GREEN}1wire installation successful.${COL_DEF}" >&3 +} + +@test "destructive-tellstick_install" { + echo -e "# ${COL_CYAN}Telldus Core installation starting...${COL_DEF}" >&3 + run telldus_core_setup + if [ "$status" -ne 0 ]; then echo "$output" >&3; fi + [ "$status" -eq 0 ] + echo -e "# ${COL_GREEN}Telldus Core installation successful.${COL_DEF}" >&3 } diff --git a/functions/zram.bats b/functions/zram.bats index 365eb12f7..35eddabe4 100644 --- a/functions/zram.bats +++ b/functions/zram.bats @@ -68,44 +68,44 @@ check_zram_removal() { @test "destructive-zram" { if ! is_arm; then skip "Not deploying zram because not on ARM (emulated ?) architecture."; fi - echo -e "# \e[36mZRAM test installation starting..." >&3 + echo -e "# ${COL_CYAN}ZRAM test installation starting..." >&3 run init_zram_mounts install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] run check_zram_mounts if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mInstallation and availability of zram mounts verified." >&3 + echo -e "# ${COL_GREEN}Installation and availability of zram mounts verified.${COL_DEF}" >&3 } @test "dev-zram" { if ! is_arm; then skip "Not executing zram test because not on native ARM architecture hardware."; fi - echo -e "# \e[36mZRAM test installation starting..." >&3 + echo -e "# ${COL_CYAN}ZRAM test installation starting...${COL_DEF}" >&3 run init_zram_mounts install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \n\e[32mInitial installation of zram mounts succeeded." >&3 + echo -e "# \n${COL_GREEN}Initial installation of zram mounts succeeded.${COL_DEF}" >&3 run check_zram_mounts if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mAvailability of zram mounts verified." >&3 + echo -e "# ${COL_GREEN}Availability of zram mounts verified.${COL_DEF}" >&3 run init_zram_mounts uninstall if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mUninstall of zram mounts succeeded." >&3 + echo -e "# ${COL_GREEN}Uninstall of zram mounts succeeded.${COL_DEF}" >&3 run check_zram_removal if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mUninstall of zram mounts verified - none remaining." >&3 + echo -e "# ${COL_GREEN}Uninstall of zram mounts verified - none remaining.${COL_DEF}" >&3 run init_zram_mounts install if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mSecond installation of zram mounts succeeded." >&3 + echo -e "# ${COL_GREEN}Second installation of zram mounts succeeded.${COL_DEF}" >&3 run check_zram_mounts if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] - echo -e "# \e[32mAvailability of 2nd zram mounts verified." >&3 + echo -e "# ${COL_GREEN}Availability of 2nd zram mounts verified.${COL_DEF}" >&3 - echo -e "# \e[32mInstallation and availability of zram mounts verified." >&3 + echo -e "# ${COL_GREEN}Installation and availability of zram mounts verified.${COL_DEF}" >&3 } diff --git a/openhabian-setup.sh b/openhabian-setup.sh index b79b9551d..e7101417e 100755 --- a/openhabian-setup.sh +++ b/openhabian-setup.sh @@ -59,7 +59,7 @@ elif [[ "$debugmode" = "on" ]]; then elif [[ "$debugmode" = "maximum" ]]; then unset SILENT DEBUGMAX=1 - + set -x fi