From 8fcc0687fbf90b63fc278f749205551f477ad1e9 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Mon, 20 Jul 2020 19:09:02 -0600 Subject: [PATCH] Continue Standardization and Rewrite This contains further rewrites that improve the consistency of the codebase. These were made in addition to the changes that were first started in #938. Related #937 Signed-off-by: Ethan Dye --- .github/issue_template.md | 3 +- CONTRIBUTING.md | 6 + LICENSE.md | 4 +- NEWS.md | 13 +- build-image/first-boot.bash | 32 +- build.bash | 11 +- docker-tests/entry.sh | 4 +- docs/NEWSLOG.md | 12 +- docs/exim.md | 28 +- functions/backup.bash | 34 +- functions/config.bash | 61 ++-- functions/ext-storage.bash | 4 +- functions/helpers.bash | 44 ++- functions/influxdb+grafana.bash | 16 +- functions/influxdb+grafana.bats | 2 +- functions/java-jre.bash | 16 +- functions/menu.bash | 102 +++--- functions/nodejs-apps.bash | 49 +-- functions/openhab.bash | 308 +++++++++-------- functions/openhabian.bash | 265 ++++++++------- functions/packages.bash | 244 +++++++------- functions/passwords.bash | 143 ++++---- functions/system.bash | 567 ++++++++++++++++++++------------ functions/vpn.bash | 33 +- functions/wifi.bash | 182 ++++++---- functions/zram.bash | 125 +++++-- functions/zram.bats | 6 +- includes/azul_zulu_license.md | 31 +- includes/bash.bashrc | 2 +- includes/frontail-preset.json | 2 + openhabian-setup.sh | 17 +- openhabian.conf.dist | 2 +- test.bash | 2 +- 33 files changed, 1382 insertions(+), 988 deletions(-) diff --git a/.github/issue_template.md b/.github/issue_template.md index 8d48559da..54765041c 100644 --- a/.github/issue_template.md +++ b/.github/issue_template.md @@ -8,8 +8,7 @@ Please be as detailed as possible to help us help you. Also, please be sure to check that your issue has not already been observed and reported to the GitHub issue tracker (you should remove the `is:open` filter from the search bar to let you see closed issues). Please follow the guidelines outlined in the -[openHAB community](https://community.openhab.org/t/how-to-ask-a-good-question-help-us-help-you/58396) -on how to help us help you. +[openHAB community](https://community.openhab.org/t/how-to-ask-a-good-question-help-us-help-you/58396) on how to help us help you. ## Debug information: Please provide all of the output provided by utilizing the `debugmode=maximum` diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bf2c795a7..774102a5e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -146,6 +146,12 @@ Universally formatted code promotes ease of writing, reading, and maintenance. * When a function is used across many files, include it in the `helpers.bash` file. +* Functions should be named using underscores. For example, `new_function`, or + `openhabian_update`. + +* Variables should be named using camelCase. For example, `newVariable`, or + `requestedArch`. + * Always refuse to allow the running of package setup scripts that require user input in unattended mode. diff --git a/LICENSE.md b/LICENSE.md index 0e3e3b2ca..324e8b27b 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,9 +1,7 @@ Internet Systems Consortium license =================================== -Copyright (c) 2020 Thomas Dietrich [thomas@nurzen.de](mailto:thomas@nurzen.de) - Markus Storm [markus.storm@gmx.net](mailto:markus.storm@gmx.net) - and others +Copyright (c) 2010-2020 Contributors to the openHAB project Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice diff --git a/NEWS.md b/NEWS.md index 8eee34fed..b12aa2692 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,15 +7,15 @@ lookup. ### Wireguard VPN (ALPHA) Wireguard can be deployed to enable for VPN access to your openHABian box when it's located in some remote location. -You need to install the Wireguard client from http://www.wireguard.com/install +You need to install the Wireguard client from to your local PC or mobile device that you want to use for access. -Copy the configuration file /etc/wireguard/wg0-client.conf from this box to load -the tunnel. +Copy the configuration file '/etc/wireguard/wg0-client.conf' from this box or +transmit QR code to load the tunnel. Note this is an ALPHA test so don't expect it to work out of the box. Any feedback is highly appreciated on the forum. ### New Java providers now out of beta -Java 11 has proven to work with 2.5 +Java 11 has been proven to work with openHAB 2.5. ## June 17, 2020 @@ -28,6 +28,5 @@ In short, PINE A64 is no longer supported and OS releases other than the current work or not. The current and previous Debian / Raspberry Pi OS (previously called Raspbian) -releases are 10 ("buster") and 9 ("stretch"). Most current Ubuntu LTS releases -are 20.04 ("focal") and 18.04 ("bionic"). - +releases are 10 ("buster") and 9 ("stretch"). The most current Ubuntu LTS +releases are 20.04 ("focal") and 18.04 ("bionic"). diff --git a/build-image/first-boot.bash b/build-image/first-boot.bash index a0acf031a..95c4a0d9a 100755 --- a/build-image/first-boot.bash +++ b/build-image/first-boot.bash @@ -78,7 +78,7 @@ if [[ -z $wifi_ssid ]]; then # Actually check if ethernet is working echo -n "$(timestamp) [openHABian] Setting up Ethernet connection... " if grep -q "up" /sys/class/net/eth0/operstate; then echo "OK"; else echo "FAILED"; fi -elif grep -q "openHABian" /etc/wpa_supplicant/wpa_supplicant.conf && ! grep -q "^[[:space:]]*dtoverlay=disable-wifi" /boot/config.txt; then +elif grep -q "openHABian" /etc/wpa_supplicant/wpa_supplicant.conf && ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then echo -n "$(timestamp) [openHABian] Setting up Wi-Fi connection... " if iwlist wlan0 scanning |& grep -q "Interface doesn't support scanning"; then # wifi might be blocked @@ -132,7 +132,7 @@ fi echo -n "$(timestamp) [openHABian] Ensuring network connectivity... " -if tryUntil "ping -c1 9.9.9.9 > /dev/null || wget -S -t 3 --waitretry=4 http://www.msftncsi.com/ncsi.txt |& grep -q 'Microsoft NCSI'" 30 1; then +if tryUntil "ping -c1 www.example.com &> /dev/null || curl --silent --head http://www.example.com |& grep -qs 'HTTP/1.1 200 OK'" 30 1; then echo "FAILED" if grep -q "openHABian" /etc/wpa_supplicant/wpa_supplicant.conf && iwconfig |& grep -q "ESSID:off"; then echo "$(timestamp) [openHABian] I was not able to connect to the configured Wi-Fi. Please check your signal quality. Reachable Wi-Fi networks are:" @@ -152,12 +152,12 @@ echo -n "$(timestamp) [openHABian] Waiting for dpkg/apt to get ready... " if wait_for_apt_to_be_ready; then echo "OK"; else echo "FAILED"; fi echo -n "$(timestamp) [openHABian] Updating repositories and upgrading installed packages... " -apt-get --yes --fix-broken install &> /dev/null +apt-get install --fix-broken --yes &> /dev/null if [[ $(eval "$(apt-get --yes upgrade &> /dev/null)") -eq 100 ]]; then - cond_echo "CONTINUING... ${COL_DEF}\c" - dpkg --configure -a &> /dev/null - apt-get --yes --fix-broken install &> /dev/null - if apt-get --yes upgrade &> /dev/null; then echo "OK"; else echo "FAILED"; fi + echo -n "CONTINUING... " + dpkg --configure --pending &> /dev/null + apt-get install --fix-broken --yes &> /dev/null + if apt-get upgrade --yes &> /dev/null; then echo "OK"; else echo "FAILED"; fi else echo "OK" fi @@ -170,7 +170,7 @@ if ! dpkg -s 'git' &> /dev/null; then fi # shellcheck disable=SC2154 -echo -n "$(timestamp) [openHABian] Cloning myself from ${repositoryurl:-https://github.com/openhab/openhabian.git}, ${clonebranch:-stable} branch... " +echo -n "$(timestamp) [openHABian] Updating myself from ${repositoryurl:-https://github.com/openhab/openhabian.git}, ${clonebranch:-stable} branch... " type openhabian_update &> /dev/null && if ! openhabian_update &> /dev/null; then echo "FAILED" echo "$(timestamp) [openHABian] The git repository on the public internet is not reachable." @@ -181,20 +181,20 @@ fi ln -sfn /opt/openhabian/openhabian-setup.sh /usr/local/bin/openhabian-config # shellcheck disable=SC2154 -echo "$(timestamp) [openHABian] Executing openhabian-setup.sh unattended..." -if (bash /opt/openhabian/openhabian-setup.sh unattended); then +echo "$(timestamp) [openHABian] Starting execution of 'openhabian-config unattended'... OK" +if (openhabian-config unattended); then rm -f /opt/openHABian-install-inprogress touch /opt/openHABian-install-successful else - echo "$(timestamp) [openHABian] We tried to get your system installed, but without proper internet connectivity this is not guaranteed to work." + echo "$(timestamp) [openHABian] We tried to get your system installed, but without proper internet connectivity this may not have worked properly." #fail_inprogress fi -echo "$(timestamp) [openHABian] Execution of 'openhabian-setup.sh unattended' completed." +echo "$(timestamp) [openHABian] Execution of 'openhabian-config unattended' completed." -echo -n "$(timestamp) [openHABian] Waiting for openHAB to become ready on ${HOSTNAME}... " +echo -n "$(timestamp) [openHABian] Waiting for openHAB to become ready on ${HOSTNAME:-openhab}... " # this took ~130 seconds on a RPi2 -if ! tryUntil "wget -S --spider -t 3 --waitretry=4 http://${HOSTNAME}:8080/start/index |& grep -q 'HTTP/1.1 200 OK'" 20 10; then echo "OK"; else echo "FAILED"; exit 1; fi +if ! tryUntil "curl --silent --head http://${HOSTNAME:-openhab}:8080/start/index |& grep -qs 'HTTP/1.1 200 OK'" 20 10; then echo "OK"; else echo "FAILED"; exit 1; fi echo "$(timestamp) [openHABian] Visit the openHAB dashboard now: http://${HOSTNAME:-openhab}:8080" echo "$(timestamp) [openHABian] To gain access to a console, simply reconnect." @@ -204,9 +204,9 @@ if [[ -x $(command -v python3) ]]; then bash /boot/webif.bash inst_done; fi sleep 12 if running_in_docker; then - PID="${OPENHAB_USERDATA}"/tmp/karaf.pid + PID="/var/lib/openhab2/tmp/karaf.pid" echo -e "\\n${COL_CYAN}Memory usage:" && free -m - if [[ -d "$PID" ]]; then + if [[ -f "$PID" ]]; then ps -auxq "$(cat "$PID")" | awk '/openhab/ {print "size/res="$5"/"$6" KB"}' else echo -e "\\n${COL_RED}Karaf PID missing, openHAB process not running (yet?)." diff --git a/build.bash b/build.bash index aa1d8ef68..08fb398eb 100755 --- a/build.bash +++ b/build.bash @@ -209,7 +209,7 @@ else exit 0 fi -# Check if a specific repository shall be included +# Check if a specific repository should be included if [ "$2" == "dev-git" ]; then # Use current git repo and branch as a development image file_tag="custom" get_git_repo @@ -291,12 +291,9 @@ if [[ $hw_platform == "pi-raspios32" ]] || [[ $hw_platform == "pi-raspios64beta" fi echo_process "Verifying signature of downloaded image... " - curl -s "$zipurl".sig -o "$buildfolder"/"$zipfile".sig - # verify signature with key from website - this is probably not the safest way to obtain the key, - # but at least it is on a different download site - curl -s -o "$buildfolder"/raspberrypi_downloads.gpg.key https://www.raspberrypi.org/raspberrypi_downloads.gpg.key - gpg -q --no-default-keyring --keyring "$buildfolder"/raspberrypiorg_downloads.keyring --import "$buildfolder"/raspberrypi_downloads.gpg.key 2> /dev/null || true - gpg -q --trust-model always --no-default-keyring --keyring "$buildfolder"/raspberrypiorg_downloads.keyring --verify "$buildfolder/$zipfile".sig "$buildfolder/$zipfile" 2> /dev/null || exit 1 + curl -s "$zipurl".sig -o "$buildfolder"/"$zipfile".sig + if ! gpg -q --keyserver keyserver.ubuntu.com --recv-key 0x8738CD6B956F460C; then echo "FAILED (download public key)"; exit 1; fi + if gpg -q --trust-model always --verify "$buildfolder/$zipfile".sig "$buildfolder/$zipfile"; then echo "OK"; else echo "FAILED (signature)"; exit 1; fi echo_process "Unpacking image... " unzip -q "$buildfolder/$zipfile" -d $buildfolder diff --git a/docker-tests/entry.sh b/docker-tests/entry.sh index 8df4aa8ff..9cce93c9c 100755 --- a/docker-tests/entry.sh +++ b/docker-tests/entry.sh @@ -35,7 +35,7 @@ function start_udev() if [ "$UDEV" == "on" ]; then if $PRIVILEGED; then mount_dev - if command -v udevd &>/dev/null; then + if command -v udevd &> /dev/null; then unshare --net udevd --daemon &> /dev/null else unshare --net /lib/systemd/systemd-udevd --daemon &> /dev/null @@ -50,7 +50,7 @@ function start_udev() function init() { # echo error message, when executable file doesn't exist. - if CMD=$(command -v "$1" 2>/dev/null); then + if CMD=$(command -v "$1" 2> /dev/null); then shift exec "$CMD" "$@" else diff --git a/docs/NEWSLOG.md b/docs/NEWSLOG.md index 4af7ae3cf..12f5afbd4 100644 --- a/docs/NEWSLOG.md +++ b/docs/NEWSLOG.md @@ -2,15 +2,15 @@ ### Wireguard VPN (ALPHA) Wireguard can be deployed to enable for VPN access to your openHABian box when it's located in some remote location. -You need to install the Wireguard client from http://www.wireguard.com/install +You need to install the Wireguard client from to your local PC or mobile device that you want to use for access. -Copy the configuration file /etc/wireguard/wg0-client.conf from this box to load -the tunnel. +Copy the configuration file '/etc/wireguard/wg0-client.conf' from this box or +transmit QR code to load the tunnel. Note this is an ALPHA test so don't expect it to work out of the box. Any feedback is highly appreciated on the forum. ### New Java providers now out of beta -Java 11 has proven to work with 2.5. +Java 11 has been proven to work with openHAB 2.5. ## June 17, 2020 @@ -23,8 +23,8 @@ In short, PINE A64 is no longer supported and OS releases other than the current work or not. The current and previous Debian / Raspberry Pi OS (previously called Raspbian) -releases are 10 ("buster") and 9 ("stretch"). Most current Ubuntu LTS releases -are 20.04 ("focal") and 18.04 ("bionic"). +releases are 10 ("buster") and 9 ("stretch"). The most current Ubuntu LTS +releases are 20.04 ("focal") and 18.04 ("bionic"). ## June 10, 2020 diff --git a/docs/exim.md b/docs/exim.md index d583e9787..0f6f57fc9 100644 --- a/docs/exim.md +++ b/docs/exim.md @@ -4,18 +4,16 @@ When you choose the "Mail Transfer Agent" install option in openhabian-config me In case you enter anything wrong you can re-initiate the installation process from the openHABian menu Here's what you will need to enter: -* Mail server type: mail sent by smarthost (received via SMTP or fetchmail) -* System mail name: FQDN (your full hostname including the domain part) -* IPs that should be allowed by the server: 127.0.0.1; ::1; 192.168.xxx.yyy (replace with your hosts's IP) -* Other destinations for which mail is accepted: ` . ` -* Machines to relay mail for: Leave empty -* IP address or host name of the outgoing smarthost: `smtp.gmail.com::587` -* Hide local mail name in outgoing mail: No -* Keep number of DNS-queries minimal: No -* Delivery method: Select: Maildir format in home directory -* Split configuration into small files: Yes -* List of smarthost(s) to use your account for: `*` -* Mail username of the public service to relay all outgoing mail to: - Your username for the mail relay service to use such as `my_id@gmail.com` - - +* Mail server type: mail sent by smarthost (received via SMTP or fetchmail) +* System mail name: FQDN (your full hostname including the domain part) +* IPs that should be allowed by the server: 127.0.0.1; ::1; 192.168.xxx.yyy (replace with your hosts's IP) +* Other destinations for which mail is accepted: ` . ` +* Machines to relay mail for: Leave empty +* IP address or host name of the outgoing smarthost: `smtp.gmail.com::587` +* Hide local mail name in outgoing mail: No +* Keep number of DNS-queries minimal: No +* Delivery method: Select: Maildir format in home directory +* Split configuration into small files: Yes +* List of smarthost(s) to use your account for: `*` +* Mail username of the public service to relay all outgoing mail to: + Your username for the mail relay service to use such as `my_id@gmail.com` diff --git a/functions/backup.bash b/functions/backup.bash index 6cc594c0e..28b44c58c 100644 --- a/functions/backup.bash +++ b/functions/backup.bash @@ -3,7 +3,7 @@ filebrowser() { bkppath=/var/lib/openhab2/backups # shellcheck disable=SC2164,SC2012 - bkpfile=$(cd ${bkppath}; ls -lthp openhab2-backup-* 2>/dev/null | head -20 | awk -F ' ' '{ print $9 " " $5 }') + bkpfile=$(cd ${bkppath}; ls -lthp openhab2-backup-* 2> /dev/null | head -20 | awk -F ' ' '{ print $9 " " $5 }') } backup_openhab_config() { @@ -76,7 +76,7 @@ create_backup_config() { # fi fi - /bin/sed -e "s|%CONFIG|${config}|g" -e "s|%CONFDIR|${confdir}|g" -e "s|%ADMIN|${adminmail}|g" -e "s|%TAPES|${tapes}|g" -e "s|%SIZE|${size}|g" -e "s|%TAPETYPE|${tapetype}|g" -e "s|%TPCHANGER|${tpchanger}|g" "${BASEDIR}"/includes/amanda.conf_template >"${confdir}"/amanda.conf + /bin/sed -e "s|%CONFIG|${config}|g" -e "s|%CONFDIR|${confdir}|g" -e "s|%ADMIN|${adminmail}|g" -e "s|%TAPES|${tapes}|g" -e "s|%SIZE|${size}|g" -e "s|%TAPETYPE|${tapetype}|g" -e "s|%TPCHANGER|${tpchanger}|g" "${BASEDIR:-/opt/openhabian}"/includes/amanda.conf_template >"${confdir}"/amanda.conf if [ "${config}" = "openhab-AWS" ]; then { echo "device_property \"S3_BUCKET_LOCATION\" \"${S3site}\" # Your S3 bucket location (site)"; \ @@ -91,7 +91,7 @@ create_backup_config() { /bin/rm -f "${confdir}"/disklist # don't backup SD by default as this can cause problems for large cards - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "Backup raw SD card, too ?" --yes-button "Backup SD" --no-button "Do not backup SD." --yesno "Do you want to create raw disk backups of your SD card ? Only recommended if it's 16GB or less, otherwise this can take too long. You can change this at any time by editing ${confdir}/disklist." 15 80); then echo "${hostname} /dev/mmcblk0 comp-amraw" >>"${confdir}"/disklist fi @@ -104,7 +104,7 @@ create_backup_config() { { echo "${hostname} /boot ${dumptype}";\ echo "${hostname} /etc/openhab2 ${dumptype}";\ echo "${hostname} /var/lib/openhab2 ${dumptype}";\ - echo "${hostname} /var/lib/openhab2/persistence ${dumptype}"; } >>"${confdir}"/disklist + echo "${hostname} /var/lib/openhab2/persistence ${dumptype}"; } >>"${confdir}"/disklist echo "index_server \"localhost\"" >"${confdir}"/amanda-client.conf echo "tapedev \"changer\"" >"${confdir}"/amanda-client.conf @@ -115,7 +115,7 @@ create_backup_config() { else introtext="${introtext}\\nFor permanent storage such as USB or NAS mounted storage, as well as for cloud based storage, we will create ${tapes} virtual containers." fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if ! (whiptail --title "Storage container creation" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi fi @@ -128,7 +128,7 @@ create_backup_config() { else if [ "${config}" = "openhab-local-SD" ]; then introtext="Please insert your removable storage medium number ${counter}." - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if ! (whiptail --title "Correct SD card inserted?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi /bin/su - "${backupuser}" -c "/usr/sbin/amlabel ${config} ${config}-${counter} slot ${counter}" fi @@ -140,12 +140,12 @@ create_backup_config() { done # create cronjob to save copies of the Amanda database - echo "0 1 * * * ${backupuser} /usr/sbin/amdump ${config} >/dev/null 2>&1" > /etc/cron.d/amanda - echo "0 18 * * * ${backupuser} /usr/sbin/amcheck -m ${config} >/dev/null 2>&1" >> /etc/cron.d/amanda + echo "0 1 * * * ${backupuser} /usr/sbin/amdump ${config} &> /dev/null" > /etc/cron.d/amanda + echo "0 18 * * * ${backupuser} /usr/sbin/amcheck -m ${config} &> /dev/null" >> /etc/cron.d/amanda if [ "${tapetype}" = "DIRECTORY" ]; then mkdir -p "${storage}"/amanda-backups chown "${backupuser}":backup "${storage}"/amanda-backups - echo "0 2 * * * root (cd /; /bin/tar czf ${storage}/amanda-backups/amanda_data_\$(date +\\%Y\\%m\\%d\\%H\\%M\\%S).tar.gz etc/amanda var/lib/amanda var/log/amanda; find ${storage} -name amanda_data_\\* -mtime +30 -delete) >/dev/null 2>&1" >> /etc/cron.d/amanda + echo "0 2 * * * root (cd /; /bin/tar czf ${storage}/amanda-backups/amanda_data_\$(date +\\%Y\\%m\\%d\\%H\\%M\\%S).tar.gz etc/amanda var/lib/amanda var/log/amanda; find ${storage} -name amanda_data_\\* -mtime +30 -delete) &> /dev/null" >> /etc/cron.d/amanda fi } @@ -154,12 +154,12 @@ amanda_setup() { local introtext="This will setup a backup mechanism to allow for saving your openHAB setup and modifications to either USB attached or Amazon cloud storage.\\nYou can add your own files/directories to be backed up, and you can store and create clones of your openHABian SD card to have an all-ready replacement in case of card failures." local successtext="Setup was successful. Amanda backup tool is now taking backups at 01:00. For further readings, start at http://wiki.zmanda.com/index.php/User_documentation." - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if ! (whiptail --title "Amanda backup installation" --yes-button "Yes" --no-button "No, I'll go read it" --defaultno --yesno "$querytext" 10 80); then return 0; fi fi - if ! exim --version >&/dev/null; then - if [ -n "$INTERACTIVE" ]; then + if ! [[ -x $(command -v exim) ]]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "No exim mail transfer agent" --yes-button "Install EXIM4" --no-button "MTA already exist, ignore installation" --defaultyes --yesno "Seems exim is not installed as a mail transfer agent.\\nAmanda needs one to be able to send emails.\\nOnly choose to ignore if you know there's a working mail transfer agent other than exim on your system.\\nDo you want to continue with EXIM4 installation ?" 15 80); then exim_setup fi @@ -176,7 +176,7 @@ amanda_setup() { local matched=false local canceled=false - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then while [ "$matched" = false ] && [ "$canceled" = false ]; do password=$(whiptail --title "Authentication Setup" --passwordbox "Enter a password for user ${backupuser}.\\nRemember to select a safe password as you (and others) can use this to login to your openHABian box." 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) @@ -197,7 +197,7 @@ amanda_setup() { /bin/rm -f /etc/cron.d/amanda; /usr/bin/touch /etc/cron.d/amanda # no SD set based config for now, requires latest Amanda which is not available as a package yet -# if [ -n "$INTERACTIVE" ]; then +# if [[ -n $INTERACTIVE ]]; then # if (whiptail --title "Create SD card set based backup" --yes-button "Yes" --no-button "No" --yesno "Setup a backup mechanism based on a locally attached SD card writer and a set of SD cards. You can also use USB sticks, BUT you must ensure that the device name to access ALWAYS is the same. This is not guaranteed if you use different USB ports." 15 80); then # config=openhab-local-SD # sddev=$(whiptail --title "Card writer device" --inputbox "What's the device name of your SD card writer?" 10 60 3>&1 1>&2 2>&3) @@ -207,7 +207,7 @@ amanda_setup() { # fi # fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "Create file storage area based backup" --yes-button "Yes" --no-button "No" --yesno "Setup a backup mechanism based on locally attached or NAS mounted storage." 15 80); then config=openhab-dir dir=$(whiptail --title "Storage directory" --inputbox "What's the directory to store backups into?\\nYou can specify any locally accessible directory, no matter if it's located on the internal SD card, an external USB-attached device such as a USB stick or HDD, or a NFS or CIFS share mounted off a NAS or other server in the network." 10 60 3>&1 1>&2 2>&3) @@ -220,7 +220,7 @@ amanda_setup() { fi fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "Create Amazon S3 based backup" --yes-button "Yes" --no-button "No" --yesno "Setup a backup mechanism based on Amazon Web Services. You can get 5 GB of S3 cloud storage for free on https://aws.amazon.com/. For hints see http://markelov.org/wiki/index.php?title=Backup_with_Amanda:_tape,_NAS,_Amazon_S3#Amazon_S3\\n\\nPlease setup your S3 bucket on Amazon Web Services NOW if you have not done so. Remember the name has to be unique in AWS namespace.\\nContinue with Amanda installation ?" 15 80); then config=openhab-AWS S3site=$(whiptail --title "S3 bucket location site" --inputbox "Enter the S3 site (e.g. \"eu-central-1\") you want to use:" 10 60 3>&1 1>&2 2>&3) @@ -235,7 +235,7 @@ amanda_setup() { fi fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 fi } diff --git a/functions/config.bash b/functions/config.bash index 68f59ddca..99b53b77a 100644 --- a/functions/config.bash +++ b/functions/config.bash @@ -1,50 +1,65 @@ #!/usr/bin/env bash +# shellcheck source=/etc/openhabian.conf disable=SC1091 +## Load, copy, or create a new configuration file for openHABian. +## +## load_create_config() +## load_create_config() { - if [ -f "$CONFIGFILE" ]; then - echo -n "$(timestamp) [openHABian] Loading configuration file '$CONFIGFILE'... " - elif [ ! -f "$CONFIGFILE" ] && [ -f /boot/installer-config.txt ]; then - echo -n "$(timestamp) [openHABian] Copying and loading configuration file '$CONFIGFILE'... " - cp /boot/installer-config.txt "$CONFIGFILE" - elif [ ! -f "$CONFIGFILE" ] && [ -n "$UNATTENDED" ]; then - echo "$(timestamp) [openHABian] Error in unattended mode: Configuration file '$CONFIGFILE' not found... FAILED" 1>&2 + if [[ -f $CONFIGFILE ]]; then + echo -n "$(timestamp) [openHABian] Loading configuration file '${CONFIGFILE}'... " + elif ! [[ -f $CONFIGFILE ]] && [[ -f /boot/installer-config.txt ]]; then + echo -n "$(timestamp) [openHABian] Copying and loading configuration file '${CONFIGFILE}'... " + if ! cond_redirect cp /boot/installer-config.txt "$CONFIGFILE"; then echo "FAILED (copy configuration)"; exit 1; fi + elif ! [[ -f $CONFIGFILE ]] && [[ -n $UNATTENDED ]]; then + echo "$(timestamp) [openHABian] Error in unattended mode: Configuration file '${CONFIGFILE}' not found... FAILED" 1>&2 exit 1 else + local question + local input + + question="\\nWelcome to openHABian!\\n\\nPlease provide the name of your Linux user i.e. the account you normally log in with.\\n\\nTypical user names are 'pi' or 'ubuntu'." + echo -n "$(timestamp) [openHABian] Setting up and loading configuration file '$CONFIGFILE' in manual setup... " - question="Welcome to openHABian!\\n\\nPlease provide the name of your Linux user i.e. the account you normally log in with.\\nTypical user names are 'pi' or 'ubuntu'." - input=$(whiptail --title "openHABian Configuration Tool - Manual Setup" --inputbox "$question" 15 80 3>&1 1>&2 2>&3) - if ! id -u "$input" &>/dev/null; then + if input=$(whiptail --title "openHABian Configuration Tool - Manual Setup" --inputbox "$question" 14 80 3>&1 1>&2 2>&3) && id -u "$input" &> /dev/null; then + if ! cond_redirect cp "${BASEDIR:-/opt/openhabian}"/openhabian.conf.dist "$CONFIGFILE"; then echo "FAILED (copy configuration)"; exit 1; fi + if ! cond_redirect sed -i 's|username=.*$|username='"${input}"'|g' "$CONFIGFILE"; then echo "FAILED (configure username)"; exit 1; fi + else echo "FAILED" - echo "$(timestamp) [openHABian] Error: The provided user name is not a valid system user. Please try again. Exiting ..." 1>&2 + echo "$(timestamp) [openHABian] Error: The provided user name is not a valid system user. Please try again. Exiting..." 1>&2 exit 1 fi - cp "$BASEDIR"/openhabian.conf.dist "$CONFIGFILE" - sed -i "s/username=.*/username=$input/g" "$CONFIGFILE" fi - # shellcheck source=/etc/openhabian.conf disable=SC1091 source "$CONFIGFILE" echo "OK" } +## Removes the password from '$CONFIGFILE'. +## This is run at the end of a fresh install for security reasons. +## +## clean_config_userpw() +## clean_config_userpw() { - cond_redirect sed -i "s/^userpw=.*/\\#userpw=xxxxxxxx/g" "$CONFIGFILE" + cond_redirect sed -i 's|^userpw=.*$|\#userpw=xxxxxx|g' "$CONFIGFILE" } -## Update java architecture in config file +## Update requested version of Java in '$CONFIGFILE'. ## Valid arguments: "Adopt11", "Zulu8-32", "Zulu8-64", "Zulu11-32", or "Zulu11-64" +## +## update_config_java(String type) +## update_config_java() { - if [ "$1" == "Zulu8-64" ] || [ "$1" == "Zulu11-64" ]; then - if (! is_x86_64 && ! [ "$(getconf LONG_BIT)" == "64" ]) || (! is_aarch64 && ! [ "$(getconf LONG_BIT)" == "64" ]); then - 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 + if [[ $1 == "Zulu8-64" ]] || [[ $1 == "Zulu11-64" ]]; then + if ! is_aarch64 || ! is_x86_64 && ! [[ $(getconf LONG_BIT) == 64 ]]; then + 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." 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." fi - cond_redirect grep -q '^java_opt' "$CONFIGFILE" && sed -i "s/^java_opt.*/java_opt=Zulu8-32/" "$CONFIGFILE" || echo "java_opt=Zulu8-32" >> "$CONFIGFILE" + cond_redirect sed -i 's|^java_opt.*$|java_opt=Zulu8-32|' "$CONFIGFILE" fi else - cond_redirect grep -q '^java_opt' "$CONFIGFILE" && sed -i "s/^java_opt.*/java_opt=$1/" "$CONFIGFILE" || echo "java_opt=$1" >> "$CONFIGFILE" + cond_redirect sed -i 's|^java_opt.*$|java_opt='"${1}"'|' "$CONFIGFILE" fi - # shellcheck disable=SC1090 source "$CONFIGFILE" } diff --git a/functions/ext-storage.bash b/functions/ext-storage.bash index a77b252cb..75913708f 100644 --- a/functions/ext-storage.bash +++ b/functions/ext-storage.bash @@ -7,14 +7,14 @@ move_root2usb() { infotext="DANGEROUS OPERATION, USE WITH PRECAUTION!\\n\\nThis will move your system root from your SD card to a USB device like an SSD or a USB stick.\\nATTENTION: this is NOT the recommended method to reduce wearout and failure of the SD card. If that is your intention, stop here and go for ZRAM (menu option 38).\\n\\nIf you still want to proceed,\\n1.) Ensure your RPi model can boot from a device other than the internal SD card reader.\\n2.) Make a backup of your SD card\\n3.) Remove all USB mass storage devices from your Pi\\n4.) Insert the USB device to be used for the new system root.\\n\\nTHIS DEVICE WILL BE COMPLETELY DELETED\\n\\nDo you want to continue at your own risk?" if ! is_pi; then - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Incompatible hardware detected" --msgbox "Move root to USB: this option is for the Raspberry Pi only." 10 60 fi echo "FAILED"; return 1 fi if [ -f /etc/ztab ]; then - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Incompatible selection detected" --msgbox "Move root to USB must not be used together with ZRAM.\\nIf you want to mitigate SD card corruption, don't move root but stay with ZRAM, it is the proper choice.\\nIf you want to move for other reasons, uninstall ZRAM first then return here." 10 65 fi echo "FAILED"; return 1 diff --git a/functions/helpers.bash b/functions/helpers.bash index 71c16f879..4076b4162 100644 --- a/functions/helpers.bash +++ b/functions/helpers.bash @@ -27,7 +27,7 @@ cond_redirect() { "$@" &> /dev/null return $? else - echo -e "\\n$COL_DGRAY\$ $* $COL_DEF" + echo -e "\\n${COL_DGRAY}\$ ${*} ${COL_DEF}" "$@" return $? fi @@ -61,6 +61,44 @@ add_keys() { fi } +## Update given git repo and switch to specfied branch / tag +## +## update_git_repo(String path, String branch) +## +update_git_repo() { + local branch + local path + + branch="$2" + path="$1" + + echo -n "$(timestamp) [openHABian] Updating $(basename "$path"), ${branch} branch from git... " + + if ! cond_redirect git -C "$path" fetch origin; then echo "FAILED (fetch origin)"; return 1; fi + if ! cond_redirect git -C "$path" reset --hard "origin/${branch}"; then echo "FAILED (reset to origin)"; return 1; fi + if ! cond_redirect git -C "$path" clean --force -x -d; then echo "FAILED (clean)"; return 1; fi + if cond_redirect git -C "$path" checkout "${branch}"; then echo "OK"; else echo "FAILED (checkout ${branch})"; return 1; fi +} + +## Function to get public IP +## +## get_public_ip() +## +get_public_ip() { + local pubIP + + if pubIP="$(dig +short myip.opendns.com @resolver1.opendns.com | tail -1)"; then echo "$pubIP"; return 0; else return 1; fi + if [[ -z $pubIP ]]; then + if pubIP="$(dig -4 +short myip.opendns.com @resolver1.opendns.com | tail -1)"; then echo "$pubIP"; return 0; else return 1; fi + fi + if [[ -z $pubIP ]]; then + if pubIP="$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"')"; then echo "$pubIP"; return 0; else return 1; fi + if [[ -z $pubIP ]]; then + if pubIP="$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"')"; then echo "$pubIP"; return 0; else return 1; fi + fi + fi +} + # fingerprinting based on # https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md is_pizero() { @@ -320,7 +358,7 @@ wait_for_apt_to_be_ready() { until [[ $attempts -le 0 ]]; do apt-get update &> /dev/null & pid=$! - if [[ $(eval "$(timeout 60 tail --pid=$pid -f /dev/null)") -eq 0 ]]; then return 0; fi + if [[ $(eval "$(tail --pid=$pid -f /dev/null)") -eq 0 ]]; then return 0; fi sleep "$interval" ((attempts-=1)) done @@ -346,5 +384,5 @@ wait_for_apt_to_finish_update() { if [[ -z $PID_APT ]]; then apt_update fi - if timeout 60 tail --pid=$PID_APT -f /dev/null; then echo "OK"; else echo "FAILED"; fi + if tail --pid=$PID_APT -f /dev/null; then echo "OK"; else echo "FAILED"; return 1; fi } diff --git a/functions/influxdb+grafana.bash b/functions/influxdb+grafana.bash index e4967a703..e53c9187f 100644 --- a/functions/influxdb+grafana.bash +++ b/functions/influxdb+grafana.bash @@ -37,7 +37,7 @@ influxdb_grafana_setup() { textSuccess="Setup successful. Please continue with the instructions you can find here:\\n\\nhttps://community.openhab.org/t/13761/1" echo "$(timestamp) [openHABian] Setting up InfluxDB and Grafana... " - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$textIntro" 15 80); then echo "CANCELED"; return 0; fi if is_armv61; then whiptail --title "Unsupported Hardware" --msgbox "$textUnsupported" 14 80 @@ -204,7 +204,7 @@ influxdb_grafana_setup() { fi cond_echo "" - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if [ $FAILED -eq 0 ]; then whiptail --title "Operation Successful!" --msgbox "$textSuccess" 15 80 else @@ -251,7 +251,7 @@ influxdb_install() { # disable authentication, to allow changes in existing installations cond_redirect sed -i 's/auth-enabled = true/# auth-enabled = false/g' /etc/influxdb/influxdb.conf || FAILED=1 - cond_redirect systemctl -q daemon-reload &>/dev/null + cond_redirect systemctl -q daemon-reload &> /dev/null sleep 2 cond_redirect systemctl enable influxdb.service sleep 2 @@ -273,7 +273,7 @@ influxdb_install() { if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi # check if service is running echo -n "Waiting for InfluxDB service... " - curl --retry 6 --retry-connrefused -s --insecure --user "admin:$1" $influxDBAddress/query >/dev/null || FAILED=1 + curl --retry 6 --retry-connrefused -s --insecure --user "admin:$1" $influxDBAddress/query > /dev/null || FAILED=1 if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi # else # echo "SKIPPED" @@ -314,14 +314,14 @@ grafana_install(){ # shellcheck disable=SC2174 mkdir -p -m 750 /var/run/grafana/ && chown grafana:grafana /var/run/grafana/ - cond_redirect systemctl -q daemon-reload &>/dev/null + cond_redirect systemctl -q daemon-reload &> /dev/null cond_redirect systemctl enable grafana-server.service cond_redirect systemctl start grafana-server.service if [ $FAILED -eq 2 ]; then echo -n "FAILED "; grafana_debug_info; return 2; else echo -n "OK "; fi cond_echo "" echo -n "Wait for Grafana to start... " - tryUntil "curl -s http://localhost:3000 >/dev/null" 10 10 && FAILED=2 + tryUntil "curl -s http://localhost:3000 > /dev/null" 10 10 && FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; curl -s -S http://localhost:3000; grafana_debug_info; return 2; else echo -n "OK "; fi sleep 5 cond_echo "" @@ -336,7 +336,7 @@ grafana_install(){ echo -n "Restarting Grafana... " cond_redirect systemctl restart grafana-server.service || FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; grafana_debug_info; return 2; else echo -n "OK "; fi - tryUntil "curl -s http://localhost:3000 >/dev/null" 10 10 && FAILED=2 + tryUntil "curl -s http://localhost:3000 > /dev/null" 10 10 && FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; grafana_debug_info; return 2; else echo -n "OK "; fi sleep 2 @@ -353,7 +353,7 @@ grafana_install(){ sleep 2 # check if service is running echo -n "Waiting for Grafana service... " - tryUntil "curl -s http://localhost:3000 >/dev/null" 10 10 && FAILED=2 + tryUntil "curl -s http://localhost:3000 > /dev/null" 10 10 && FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; grafana_debug_info; return 2; else echo -n "OK "; fi cond_echo "" } diff --git a/functions/influxdb+grafana.bats b/functions/influxdb+grafana.bats index 36a512d0f..1335a33d9 100644 --- a/functions/influxdb+grafana.bats +++ b/functions/influxdb+grafana.bats @@ -18,7 +18,7 @@ teardown_file() { if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] InfluxDB service running.${COL_DEF}" >&3 - run curl --retry 5 --retry-connrefused -s --insecure --user "admin:Password1234" "http://localhost:8086/query" >/dev/null + 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 "# ${COL_GREEN}$(timestamp) [openHABian] InfluxDB service is responding.${COL_DEF}" >&3 diff --git a/functions/java-jre.bash b/functions/java-jre.bash index 6bb38ec4e..08fb84450 100644 --- a/functions/java-jre.bash +++ b/functions/java-jre.bash @@ -199,7 +199,7 @@ java_zulu_install() { return 1 fi - if [[ -z $UNATTENDED ]] && [[ -z $BATS_TEST_NAME ]]; then + if dpkg -s 'openhab2' &> /dev/null; then cond_redirect systemctl stop openhab2.service fi cond_redirect java_alternatives_reset @@ -225,7 +225,7 @@ java_zulu_install() { java_zulu_install_crypto_extension - if [[ -z $UNATTENDED ]] && [[ -z $BATS_TEST_NAME ]]; then + if dpkg -s 'openhab2' &> /dev/null; then cond_redirect systemctl start openhab2.service fi } @@ -276,9 +276,9 @@ java_zulu_fetch() { if ! mkdir -p $jdkInstallLocation; then echo "FAILED (create directory)"; return 1; fi if ! rm -rf "${jdkInstallLocation:?}"/*; then echo "FAILED (clean directory)"; return 1; fi - if ! cond_redirect wget -qO "$jdkInstallLocation"/zulu.tar.gz "$downloadLink"; then echo "FAILED (download)"; rm -rf "${jdkInstallLocation:?}"/*; return 1; fi - if ! tar -xpzf "$jdkInstallLocation"/zulu.tar.gz -C "$jdkInstallLocation"; then echo "FAILED (extract)"; rm -rf "${jdkInstallLocation:?}"/*; return 1; fi - if rm -rf "$jdkInstallLocation"/zulu.tar.gz; then echo "OK"; else echo "FAILED (cleanup)"; return 1; fi + if ! cond_redirect wget -O "$jdkInstallLocation"/zulu.tar.gz "$downloadLink"; then echo "FAILED (download)"; rm -rf "${jdkInstallLocation:?}"/*; return 1; fi + if ! cond_redirect tar -xpzf "$jdkInstallLocation"/zulu.tar.gz -C "$jdkInstallLocation"; then echo "FAILED (extract)"; rm -rf "${jdkInstallLocation:?}"/*; return 1; fi + if cond_redirect rm -rf "$jdkInstallLocation"/zulu.tar.gz; then echo "OK"; else echo "FAILED (cleanup)"; return 1; fi } ## Check if a newer version of Java Zulu 8 is available. @@ -397,13 +397,13 @@ java_zulu_install_crypto_extension() { policyTempLocation="$(mktemp -d "${TMPDIR:-/tmp}"/openhabian.XXXXX)" echo -n "$(timestamp) [openHABian] Installing Java Zulu CEK to enable unlimited cipher strength... " - if ! mkdir -p "$jdkSecurity"; then echo "FAILED (create directory)"; return 1; fi - if ! rm -rf "${jdkSecurity:?}"/*; then echo "FAILED (clean directory)"; return 1; fi + if ! cond_redirect mkdir -p "$jdkSecurity"; then echo "FAILED (create directory)"; return 1; fi + if ! cond_redirect rm -rf "${jdkSecurity:?}"/*; then echo "FAILED (clean directory)"; return 1; fi 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" + cond_redirect rm -rf "$policyTempLocation" } ## Fetch AdoptOpenJDK using APT repository. diff --git a/functions/menu.bash b/functions/menu.bash index 5a18d3c60..f5c43ad94 100644 --- a/functions/menu.bash +++ b/functions/menu.bash @@ -17,7 +17,7 @@ Menu 60 finally is a shortcut to offer all option for (un)installation in a sing \\nVisit these sites for more information: - Documentation: https://www.openhab.org/docs/installation/openhabian.html - Development: http://github.com/openhab/openhabian - - Discussion: https://community.openhab.org/t/13379" 27 110 + - Discussion: https://community.openhab.org/t/13379" 27 116 RET=$? if [ $RET -eq 255 ]; then # key pressed. @@ -62,11 +62,11 @@ show_main_menu() { elif [[ "$choice" == "03"* ]]; then wait_for_apt_to_finish_update - openhab2_setup + openhab2_setup "stable" elif [[ "$choice" == "10"* ]]; then choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 12 116 5 --cancel-button Back --ok-button Execute \ - "11 | Packages " "Install needed and recommended system packages" \ + "11 | Packages" "Install needed and recommended system packages" \ "12 | Bash&Vim Settings" "Update customized openHABian settings for bash, vim and nano" \ "13 | System Tweaks" "Add /srv mounts and update settings typical for openHAB" \ "14 | Fix Permissions" "Update file permissions of commonly used files and folders" \ @@ -86,11 +86,11 @@ show_main_menu() { elif [[ "$choice" == "20"* ]]; then choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 18 116 11 --cancel-button Back --ok-button Execute \ - "21 | Log Viewer " "openHAB Log Viewer webapp (frontail)" \ + "21 | Log Viewer" "openHAB Log Viewer webapp (frontail)" \ "22 | miflora-mqtt-daemon" "Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon" \ "23 | Mosquitto" "MQTT broker Eclipse Mosquitto" \ "24 | InfluxDB+Grafana" "A powerful persistence and graphing solution" \ - "25 | NodeRED" "Flow-based programming for the Internet of Things" \ + "25 | Node-RED" "Flow-based programming for the Internet of Things" \ "26 | Homegear" "Homematic specific, the CCU2 emulation software Homegear" \ "27 | knxd" "KNX specific, the KNX router/gateway daemon knxd" \ "28 | 1wire" "1wire specific, owserver and related packages" \ @@ -117,18 +117,21 @@ show_main_menu() { esac elif [[ "$choice" == "30"* ]]; then - choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 18 117 11 --cancel-button Back --ok-button Execute \ - "31 | Change hostname " "Change the name of this system, currently '$(hostname)'" \ - "32 | Set system locale" "Change system language, currently '$(env | grep "LANG=" | sed 's/LANG=//')'" \ - "33 | Set system timezone" "Change the your timezone, execute if it's not '$(date +%H:%M)' now" \ - "34 | Change passwords" "Change passwords for Samba, openHAB Console or the system user" \ - "35 | Serial port" "Prepare serial ports for peripherals like Razberry, SCC, Pine64 ZWave, ..." \ - "36 | WiFi setup" "Configure wireless network connection" \ - "37 | Move root to USB" "Move the system root from the SD card to a USB device (SSD or stick)" \ - "38 | Use ZRAM" "Use compressed RAM/disk sync for active directories to avoid SD card corruption" \ - " | Uninstall ZRAM" "Don't use compressed memory (back to standard Raspberry Pi OS filesystem layout)" \ - "39 | Setup VPN access" "Setup Wireguard to enable secure remote access to your openHABian box (ALPHA)" \ - " | Uninstall Wireguard VPN" "Remove Wireguard VPN from your openHABian box" \ + choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 21 116 14 --cancel-button Back --ok-button Execute \ + "31 | Change hostname" "Change the name of this system, currently '$(hostname)'" \ + "32 | Set system locale" "Change system language, currently '$(env | grep "^[[:space:]]*LANG=" | sed 's|LANG=||g')'" \ + "33 | Set system timezone" "Change the your timezone, execute if it's not '$(date +%H:%M)' now" \ + " | Enable NTP" "Enable time synchronization via systemd-timesyncd to NTP servers" \ + " | Disable NTP" "Disable time synchronization via systemd-timesyncd to NTP servers" \ + "34 | Change passwords" "Change passwords for Samba, openHAB Console or the system user" \ + "35 | Serial port" "Prepare serial ports for peripherals like Razberry, SCC, Pine64 ZWave, ..." \ + "36 | WiFi setup" "Configure wireless network connection" \ + " | Disable WiFi" "Disable wireless network connection" \ + "37 | Move root to USB" "Move the system root from the SD card to a USB device (SSD or stick)" \ + "38 | Use ZRAM" "Use compressed RAM/disk sync for active directories to avoid SD card corruption" \ + " | Uninstall ZRAM" "Don't use compressed memory (back to standard Raspberry Pi OS filesystem layout)" \ + "39 | Setup VPN access" "Setup Wireguard to enable secure remote access to openHABian (ALPHA)" \ + " | Remove Wireguard VPN" "Remove Wireguard VPN from openHABian" \ 3>&1 1>&2 2>&3) if [ $? -eq 1 ] || [ $? -eq 255 ]; then return 0; fi wait_for_apt_to_finish_update @@ -136,12 +139,15 @@ show_main_menu() { 31\ *) hostname_change ;; 32\ *) locale_setting ;; 33\ *) timezone_setting ;; + *Enable\ NTP) setup_ntp "enable" ;; + *Disable\ NTP) setup_ntp "disable" ;; 34\ *) change_password ;; 35\ *) prepare_serial_port ;; - 36\ *) wifi_setup ;; + 36\ *) configure_wifi setup;; + *Disable\ WiFi) configure_wifi disable ;; 37\ *) move_root2usb ;; - 38\ *) init_zram_mounts install;; - *Uninstall\ ZRAM) init_zram_mounts remove;; + 38\ *) init_zram_mounts "install" ;; + *Uninstall\ ZRAM) init_zram_mounts "uninstall" ;; 39\ *) if install_wireguard install; then setup_wireguard; fi;; *Uninstall\ Wireguard) install_wireguard remove;; "") return 0 ;; @@ -149,7 +155,7 @@ show_main_menu() { esac elif [[ "$choice" == "40"* ]]; then - choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 20 116 13 --cancel-button Back --ok-button Execute \ + choice2=$(whiptail --title "Welcome to the openHABian Configuration Tool $(get_git_revision)" --menu "Setup Options" 19 116 12 --cancel-button Back --ok-button Execute \ "41 | openHAB release" "Install or switch to the latest openHAB release" \ " | openHAB testing" "Install or switch to the latest openHAB testing build" \ " | openHAB snapshot" "Install or switch to the latest openHAB SNAPSHOT build" \ @@ -167,9 +173,9 @@ show_main_menu() { wait_for_apt_to_finish_update # shellcheck disable=SC2154 case "$choice2" in - 41\ *) openhab2_setup ;; - *openHAB\ testing) openhab2_setup testing ;; - *openHAB\ snapshot) openhab2_setup unstable ;; + 41\ *) openhab2_setup "stable" ;; + *openHAB\ testing) openhab2_setup "testing" ;; + *openHAB\ snapshot) openhab2_setup "unstable" ;; 42\ *) openhab_shell_interfaces ;; 43\ *) nginx_setup ;; *Delay\ rules\ load) delayed_rules yes;; @@ -199,25 +205,25 @@ show_main_menu() { esac elif [[ "$choice" == "60"* ]]; then - choosenComponents=$(whiptail --title "Manual/Fresh Setup" --checklist "Choose which system components to install or configure:" 23 117 16 --cancel-button Back --ok-button Execute \ - "62 | Packages" "Install needed and recommended system packages " OFF \ - "63 | Zulu 8 OpenJDK 32-bit" "Install Zulu 8 32-bit OpenJDK as primary Java provider" OFF \ - " | Zulu 8 OpenJDK 64-bit" "Install Zulu 8 64-bit OpenJDK as primary Java provider" OFF \ - " | Zulu 11 OpenJDK 32-bit" "Install Zulu 11 32-bit OpenJDK as primary Java provider (beta)" OFF \ - " | Zulu 11 OpenJDK 64-bit" "Install Zulu 11 64-bit OpenJDK as primary Java provider (beta)" OFF \ - " | AdoptOpenJDK 11" "Install AdoptOpenJDK 11 as primary Java provider (beta)" OFF \ - "64 | openHAB stable" "Install the latest openHAB release" OFF \ - " | openHAB testing" "Install the latest openHAB testing (milestone) build" OFF \ - " | openHAB unstable" "(Alternative) Install the latest openHAB SNAPSHOT build" OFF \ - "65 | System Tweaks" "Configure system permissions and settings typical for openHAB " OFF \ - "66 | Samba" "Install the Samba file sharing service and set up openHAB 2 shares " OFF \ - "67 | Log Viewer" "The openHAB Log Viewer webapp (frontail) " OFF \ - "68 | FireMotD" "Configure FireMotD to present a system overview on SSH login (optional) " OFF \ - "69 | Bash&Vim Settings" "Apply openHABian settings for bash, vim and nano (optional) " OFF \ - "6A | Use ZRAM" "Use compressed RAM/disk sync for active directories (mitigates SD card wear)" OFF \ - " | Uninstall ZRAM" "Don't use compressed memory (back to standard Raspberry Pi OS filesystem layout)" OFF \ - "6B | Setup VPN access" "Setup Wireguard to enable secure remote access to your openHABian box (ALPHA)" OFF \ - " | Uninstall Wireguard VPN" "Remove Wireguard from your openHABian box" OFF \ + choosenComponents=$(whiptail --title "Manual/Fresh Setup" --checklist "Choose which system components to install or configure:" 23 116 16 --cancel-button Back --ok-button Execute \ + "62 | Packages" "Install needed and recommended system packages " OFF \ + "63 | Zulu 8 OpenJDK 32-bit" "Install Zulu 8 32-bit OpenJDK as primary Java provider" OFF \ + " | Zulu 8 OpenJDK 64-bit" "Install Zulu 8 64-bit OpenJDK as primary Java provider" OFF \ + " | Zulu 11 OpenJDK 32-bit" "Install Zulu 11 32-bit OpenJDK as primary Java provider (beta)" OFF \ + " | Zulu 11 OpenJDK 64-bit" "Install Zulu 11 64-bit OpenJDK as primary Java provider (beta)" OFF \ + " | AdoptOpenJDK 11" "Install AdoptOpenJDK 11 as primary Java provider (beta)" OFF \ + "64 | openHAB stable" "Install the latest openHAB release" OFF \ + " | openHAB testing" "Install the latest openHAB testing (milestone) build" OFF \ + " | openHAB unstable" "(Alternative) Install the latest openHAB SNAPSHOT build" OFF \ + "65 | System Tweaks" "Configure system permissions and settings typical for openHAB " OFF \ + "66 | Samba" "Install the Samba file sharing service and set up openHAB 2 shares " OFF \ + "67 | Log Viewer" "The openHAB Log Viewer webapp (frontail) " OFF \ + "68 | FireMotD" "Configure FireMotD to present a system overview on SSH login (optional) " OFF \ + "69 | Bash&Vim Settings" "Apply openHABian settings for bash, vim and nano (optional) " OFF \ + "6A | Use ZRAM" "Use compressed RAM/disk sync for active directories (mitigates SD card wear)" OFF \ + " | Uninstall ZRAM" "Don't use compressed memory (back to standard Raspberry Pi OS filesystem layout)" OFF \ + "6B | Setup VPN access" "Setup Wireguard to enable secure remote access openHABian (ALPHA)" OFF \ + " | Remove Wireguard VPN" "Remove Wireguard VPN from openHABian" OFF \ 3>&1 1>&2 2>&3) if [ $? -eq 1 ] || [ $? -eq 255 ]; then return 0; fi wait_for_apt_to_finish_update @@ -228,16 +234,16 @@ show_main_menu() { if [[ $choosenComponents == *"Zulu 11 OpenJDK 32-bit"* ]]; then update_config_java "Zulu11-32" && java_install_or_update "Zulu11-32"; fi if [[ $choosenComponents == *"Zulu 11 OpenJDK 64-bit"* ]]; then update_config_java "Zulu11-64" && java_install_or_update "Zulu11-64"; fi if [[ $choosenComponents == *"AdoptOpenJDK 11"* ]]; then update_config_java "Adopt11" && java_install_or_update "Adopt11"; fi - if [[ $choosenComponents == *"64"* ]]; then openhab2_setup; fi - if [[ $choosenComponents == *"openHAB testing"* ]]; then openhab2_setup testing; fi - if [[ $choosenComponents == *"openHAB unstable"* ]]; then openhab2_setup unstable; fi + if [[ $choosenComponents == *"64"* ]]; then openhab2_setup "stable"; fi + if [[ $choosenComponents == *"openHAB testing"* ]]; then openhab2_setup "testing"; fi + if [[ $choosenComponents == *"openHAB unstable"* ]]; then openhab2_setup "unstable"; fi if [[ $choosenComponents == *"65"* ]]; then srv_bind_mounts && permissions_corrections && misc_system_settings; fi if [[ $choosenComponents == *"66"* ]]; then samba_setup; fi if [[ $choosenComponents == *"67"* ]]; then frontail_setup; fi if [[ $choosenComponents == *"68"* ]]; then firemotd_setup; fi if [[ $choosenComponents == *"69"* ]]; then bashrc_copy && vimrc_copy && vim_openhab_syntax && nano_openhab_syntax && multitail_openhab_scheme; fi - if [[ $choosenComponents == *"6A"* ]]; then init_zram_mounts install; fi - if [[ $choosenComponents == *"Uninstall ZRAM"* ]]; then init_zram_mounts remove; fi + if [[ $choosenComponents == *"6A"* ]]; then init_zram_mounts "install"; fi + if [[ $choosenComponents == *"Uninstall ZRAM"* ]]; then init_zram_mounts "uninstall"; fi if [[ $choosenComponents == *"6B"* ]]; then install_wireguard install; setup_wireguard; fi if [[ $choosenComponents == *"Uninstall Wireguard"* ]]; then install_wireguard remove; fi diff --git a/functions/nodejs-apps.bash b/functions/nodejs-apps.bash index 0d2b8e7b3..99b6d6142 100644 --- a/functions/nodejs-apps.bash +++ b/functions/nodejs-apps.bash @@ -39,49 +39,57 @@ nodejs_setup() { fi } +## Function for installing frontail to enable the openHAB log viewer web application. +## +## frontail_setup() +## frontail_setup() { local frontailBase - if ! [ -x "$(command -v npm)" ]; then - echo "$(timestamp) [openHABian] Installing Frontail prerequsites (NodeJS)... " - nodejs_setup + if ! [[ -x $(command -v npm) ]]; then + echo -n "$(timestamp) [openHABian] Installing Frontail prerequsites (NodeJS)... " + if cond_redirect nodejs_setup; then echo "OK"; else echo "FAILED"; return 1; fi fi frontailBase="$(npm list -g | head -n 1)/node_modules/frontail" - echo "$(timestamp) [openHABian] Beginning setup of the openHAB Log Viewer (frontail)... " - - if [ -x "$(command -v frontail)" ]; then + if [[ -x $(command -v frontail) ]]; then echo -n "$(timestamp) [openHABian] Updating openHAB Log Viewer (frontail)... " if cond_redirect npm update --force -g frontail; then echo "OK"; else echo "FAILED"; return 1; fi else - if [ -d "$frontailBase" ]; then + echo -n "$(timestamp) [openHABian] Installing openHAB Log Viewer (frontail)... " + if [[ -d $frontailBase ]]; then cond_echo "Removing any old installations..." cond_redirect npm uninstall -g frontail fi - echo -n "$(timestamp) [openHABian] Installing openHAB Log Viewer (frontail)... " if ! cond_redirect npm install --force -g frontail; then echo "FAILED (install)"; return 1; fi if cond_redirect npm update --force -g frontail; then echo "OK"; else echo "FAILED (update)"; return 1; fi fi echo -n "$(timestamp) [openHABian] Configuring openHAB Log Viewer (frontail)... " - mkdir -p "$frontailBase"/preset "$frontailBase"/web/assets/styles - cp "$BASEDIR"/includes/frontail-preset.json "$frontailBase"/preset/openhab.json - cp "$BASEDIR"/includes/frontail-theme.css "$frontailBase"/web/assets/styles/openhab.css - if [ $? -eq 0 ]; then echo "OK"; else echo "FAILED"; return 1; fi + if ! cond_redirect mkdir -p "$frontailBase"/preset "$frontailBase"/web/assets/styles; then echo "FAILED (create directory)"; return 1; fi + if ! cond_redirect rm -rf "${frontailBase:?}"/preset/* "${frontailBase:?}"/web/assets/styles/*; then echo "FAILED (clean directory)"; return 1; fi + if ! cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/frontail-preset.json "$frontailBase"/preset/openhab.json; then echo "FAILED (copy presets)"; return 1; fi + if cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/frontail-theme.css "$frontailBase"/web/assets/styles/openhab.css; then echo "OK"; else echo "FAILED (copy theme)"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up openHAB Log Viewer (frontail) service... " - if ! (sed -e "s|%FRONTAILBASE|${frontailBase}|g" "$BASEDIR"/includes/frontail.service > /etc/systemd/system/frontail.service); then echo "FAILED (service file creation)"; fi + if ! (sed -e "s|%FRONTAILBASE|${frontailBase}|g" "${BASEDIR:-/opt/openhabian}"/includes/frontail.service > /etc/systemd/system/frontail.service); then echo "FAILED (service file creation)"; return 1; fi + if ! cond_redirect chmod 644 /etc/systemd/system/frontail.service; then echo "FAILED (permissions)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null if ! cond_redirect systemctl enable frontail.service; then echo "FAILED (enable service)"; return 1; fi if cond_redirect systemctl restart frontail.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi - if [ -z "$BATS_TEST_NAME" ]; then + if dpkg -s 'openhab2' &> /dev/null; then dashboard_add_tile frontail fi } +## Function for installing Node-RED a flow based programming interface for IoT devices. +## +## nodered_setup() +## nodered_setup() { - if [ -z "$INTERACTIVE" ]; then + if [[ -z $INTERACTIVE ]]; then echo "$(timestamp) [openHABian] Node-RED setup must be run in interactive mode! Canceling Node-RED setup!" echo "CANCELED" return 0 @@ -89,6 +97,10 @@ nodered_setup() { local temp + if ! [[ -x $(command -v npm) ]]; then + echo -n "$(timestamp) [openHABian] Installing Frontail prerequsites (NodeJS)... " + if cond_redirect nodejs_setup; then echo "OK"; else echo "FAILED"; return 1; fi + fi if ! dpkg -s 'build-essential' &> /dev/null; then echo -n "$(timestamp) [openHABian] Installing Node-RED required packages (build-essential)... " if cond_redirect apt-get install --yes build-essential; then echo "OK"; else echo "FAILED"; return 1; fi @@ -96,9 +108,7 @@ nodered_setup() { temp="$(mktemp "${TMPDIR:-/tmp}"/openhabian.XXXXX)" - echo "$(timestamp) [openHABian] Beginning setup of Node-RED... " - - echo "$(timestamp) [openHABian] Downloading Node-RED setup script... " + echo -n "$(timestamp) [openHABian] Downloading Node-RED setup script... " if cond_redirect wget -qO "$temp" https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered; then echo "OK" else @@ -119,10 +129,11 @@ nodered_setup() { if cond_redirect npm update -g node-red-contrib-openhab2; then echo "OK"; else echo "FAILED (update openhab2 addon)"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up Node-RED service... " + cond_redirect systemctl -q daemon-reload &> /dev/null if ! cond_redirect systemctl enable nodered.service; then echo "FAILED (enable service)"; return 1; fi if cond_redirect systemctl restart nodered.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi - if [ -z "$BATS_TEST_NAME" ]; then + if dpkg -s 'openhab2' &> /dev/null; then dashboard_add_tile nodered fi } diff --git a/functions/openhab.bash b/functions/openhab.bash index 050c883ec..8e6e4798b 100644 --- a/functions/openhab.bash +++ b/functions/openhab.bash @@ -1,202 +1,246 @@ #!/usr/bin/env bash -# /etc/systemd/system/openhab2.service.d/override.conf to quickly rename rules forth and back after 2 minutes +## Function to quicky rename openHAB rules back and forth after two minutes to +## speed up startup of openHAB. +## This is done using /etc/systemd/system/openhab2.service.d/override.conf +## Valid arguments: "yes" or "no" +## +## delayed_rules() +## delayed_rules() { - local targetdir=/etc/systemd/system/openhab2.service.d + if ! openhab_is_installed; then return 0; fi - if [ "$1" == "yes" ]; then - mkdir -p $targetdir - cp "${BASEDIR}"/includes/systemd-override.conf ${targetdir}/override.conf - else - rm ${targetdir}/override.conf + local targetDir + + targetDir="/etc/systemd/system/openhab2.service.d" + + if [[ $1 == "yes" ]]; then + echo -n "$(timestamp) [openHABian] Adding delay on loading openHAB rules... " + if ! cond_redirect mkdir -p $targetDir; then echo "FAILED (prepare directory)"; return 1; fi + if ! cond_redirect rm -f "${targetDir}"/override.conf; then echo "FAILED (clean directory)"; return 1; fi + if cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/systemd-override.conf "${targetDir}"/override.conf; then echo "OK"; else echo "FAILED (copy configuration)"; return 1; fi + elif [[ $1 == "no" ]]; then + echo -n "$(timestamp) [openHABian] Removing delay on loading openHAB rules... " + if cond_redirect rm -f "${targetDir}"/override.conf; then echo "OK"; else echo "FAILED (remove configuration)"; return 1; fi fi - cond_redirect systemctl -q daemon-reload &>/dev/null + cond_redirect systemctl -q daemon-reload &> /dev/null cond_redirect systemctl restart openhab2.service } +## Function to install / upgrade / downgrade the installed openHAB version +## Valid arguments: "unstable", "stable", or "testing" +## +## openhab2_setup(String version) +## openhab2_setup() { + local introText + local successText + local repo local openhabVersion - local RepoKey=/tmp/openhab-key.asc - introtext_stable="You are about to install or upgrade to the latest stable openHAB release.\\n -Please be aware that downgrading from a newer unstable snapshot build is not officially supported. Please consult with the documentation or community forum and be sure to take a full openHAB configuration backup first!" - successtext_stable="The stable release of openHAB is now installed on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you did changes to files below '/var/lib/openhab2' before, they were replaced but you can restore them from backup files next to the originals. -Check the \"openHAB Release Notes\" and the official announcements to learn about additons, fixes and changes." - introtext_testing="You are about to install or upgrade to the latest milestone (testing) openHAB build. It contains the latest features and is supposed to run stable, but if you experience bugs or incompatibilities, please help enhancing openHAB by posting them on the community forum or by raising a Github issue.\\n - Please be aware that downgrading from a newer build is not officially supported. Please consult with the documentation or community forum and be sure to take a full openHAB configuration backup first!" -successtext_testing="The testing release of openHAB is now installed on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you did changes to files below '/var/lib/openhab2' before, they were replaced but you can restore them from backup files next to the originals. -Check the \"openHAB Release Notes\" and the official announcements to learn about additons, fixes and changes." - introtext_unstable="Proceed with caution!\\nYou are about to switch over to the latest openHAB 2 unstable snapshot build. The daily snapshot builds contain the latest features and improvements but might also suffer from bugs or incompatibilities. Please be sure to take a full openHAB configuration backup first!" - successtext_unstable="The latest unstable snapshot build of openHAB 2 is now running on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you did changes to files below '/var/lib/openhab2' before, they were replaced but you can restore them from backup files next to the originals.\\nIf you find any problem or bug, please report it and state the snapshot version you are on. To stay up-to-date with improvements and bug fixes you should upgrade your packages (the openhab2 and openhab2-addons packages) regularly." - - if [ "$1" == "unstable" ]; then - UNSTABLE=1 + + if [[ $1 == "unstable" ]]; then + introText="Proceed with caution!\\nYou are about to switch over to the latest openHAB 2 unstable snapshot build. The daily snapshot builds contain the latest features and improvements but might also suffer from bugs or incompatibilities. Please be sure to take a full openHAB configuration backup first!" + successText="The latest unstable snapshot build of openHAB 2 is now running on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you made changes to the files in '/var/lib/openhab2' they were replaced, but you can restore them from backup files next to the originals.\\nIf you find any problems or bugs, please report them and state the snapshot version you are on. To stay up-to-date with improvements and bug fixes you should upgrade your packages (using menu option 02) regularly." + repo="deb https://openhab.jfrog.io/openhab/openhab-linuxpkg unstable main" + elif [[ $1 == "stable" ]]; then + introText="You are about to install or upgrade to the latest stable openHAB release.\\n\\nPlease be aware that downgrading from a newer unstable snapshot build is not officially supported. Please consult with the documentation or community forum and be sure to take a full openHAB configuration backup first!" + successText="The stable release of openHAB is now installed on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you made changes to the files in '/var/lib/openhab2' they were replaced, but you can restore them from backup files next to the originals.\\nCheck the \"openHAB Release Notes\" and the official announcements to learn about additons, fixes and changes." + repo="deb https://dl.bintray.com/openhab/apt-repo2 stable main" + elif [[ $1 == "testing" ]]; then + introText="You are about to install or upgrade to the latest milestone (testing) openHAB build. It contains the latest features and is supposed to run stable, but if you experience bugs or incompatibilities, please help with enhancing openHAB by posting them on the community forum or by raising a GitHub issue.\\n\\nPlease be aware that downgrading from a newer build is not officially supported. Please consult with the documentation or community forum and be sure to take a full openHAB configuration backup first!" + successText="The testing release of openHAB is now installed on your system. Please test the correct behavior of your setup. You might need to adapt your configuration, if available. If you made changes to the files in '/var/lib/openhab2' they were replaced, but you can restore them from backup files next to the originals.\\nCheck the \"openHAB Release Notes\" and the official announcements to learn about additons, fixes and changes." + repo="deb https://openhab.jfrog.io/openhab/openhab-linuxpkg testing main" fi - if [ "$1" == "testing" ]; then - TESTING=1 + if [[ $1 == "unstable" ]]; then + echo -n "$(timestamp) [openHABian] Beginning install of latest openHAB snapshot (unstable)... " + elif [[ $1 == "stable" ]]; then + echo -n "$(timestamp) [openHABian] Beginning install of latest openHAB release (stable)... " + elif [[ $1 == "testing" ]]; then + echo -n "$(timestamp) [openHABian] Beginning install of latest openHAB milestone release (testing)... " fi - if [ -z "$UNSTABLE" ]; then - if [ -z "$TESTING" ]; then - echo -n "$(timestamp) [openHABian] Installing or upgrading to latest openHAB release (stable)... " - introtext=$introtext_stable - successtext=$successtext_stable - REPO="deb https://dl.bintray.com/openhab/apt-repo2 stable main" - else - echo -n "$(timestamp) [openHABian] Installing or upgrading to latest openHAB milestone release (testing)... " - introtext=$introtext_testing - successtext=$successtext_testing - REPO="deb https://openhab.jfrog.io/openhab/openhab-linuxpkg testing main" - fi + if [[ -n $INTERACTIVE ]]; then + if (whiptail --title "openHAB software change, Continue?" --yes-button "Continue" --no-button "Cancel" --yesno "$introText" 15 80); then echo "OK"; else echo "CANCELED"; return 0; fi else - echo -n "$(timestamp) [openHABian] Installing or switching to latest openHAB snapshot (unstable)... " - introtext=$introtext_unstable - successtext=$successtext_unstable - REPO="deb https://openhab.jfrog.io/openhab/openhab-linuxpkg unstable main" + echo "OK" fi - if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "openHAB software change, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80); then echo "CANCELED"; return 0; fi - fi + if ! add_keys "https://bintray.com/user/downloadSubjectPublicKey?username=openhab"; then return 1; fi - wget --no-check-certificate -qO "$RepoKey" 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' - cond_redirect apt-key add "$RepoKey" - rm -f "$RepoKey" + echo "$repo" > /etc/apt/sources.list.d/openhab2.list - echo "$REPO" > /etc/apt/sources.list.d/openhab2.list - cond_redirect apt-get update + echo -n "$(timestamp) [openHABian] Installing selected openHAB version... " + if ! cond_redirect apt-get update; then echo "FAILED (update apt lists)"; return 1; fi openhabVersion="$(apt-cache madison openhab2 | head -n 1 | cut -d'|' -f2 | xargs)" + if cond_redirect apt-get install --allow-downgrades --yes "openhab2=${openhabVersion}" "openhab2-addons=${openhabVersion}"; then echo "OK"; else echo "FAILED"; return 1; fi - if ! cond_redirect apt-get -y --allow-downgrades install "openhab2=${openhabVersion}" "openhab2-addons=${openhabVersion}"; then echo "FAILED (apt)"; return 1; fi - cond_redirect systemctl -q daemon-reload &>/dev/null - if cond_redirect systemctl enable --now openhab2; then echo "OK"; else echo "FAILED (service)"; return 1; fi - - if is_pi; then - echo -n "$(timestamp) [openHABian] Optimizing Java to run on low memory single board computers... " - if has_lowmem; then - if cond_redirect sed -i 's|^EXTRA_JAVA_OPTS=.*$|EXTRA_JAVA_OPTS="-Xms16m -Xmx256m"|g' /etc/default/openhab2; then echo "OK"; else echo "FAILED"; return 1; fi - else - if cond_redirect sed -i 's|^EXTRA_JAVA_OPTS=.*$|EXTRA_JAVA_OPTS="-Xms192m -Xmx320m"|g' /etc/default/openhab2; then echo "OK"; else echo "FAILED"; return 1; fi - fi - fi - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - fi + echo -n "$(timestamp) [openHABian] Setting up openHAB service... " + cond_redirect systemctl -q daemon-reload &> /dev/null + if cond_redirect systemctl enable openhab2.service; then echo "OK"; else echo "FAILED (enable service)"; return 1; fi + + openhab_java_optimize + delayed_rules "yes" + dashboard_add_tile "openhabiandocs" - delayed_rules yes - dashboard_add_tile openhabiandocs + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Operation Successful!" --msgbox "$successText" 15 80 + fi } +## Function that binds openHAB console to interfaces making it available over the network +## +## openhab_shell_interfaces() +## openhab_shell_interfaces() { - introtext="The openHAB remote console is a powerful tool for every openHAB user. It allows you too have a deeper insight into the internals of your setup. Further details: https://www.openhab.org/docs/administration/console.html -\\nThis routine will bind the console to all interfaces and thereby make it available to other devices in your network. Please provide a secure password for this connection (letters and numbers only! default: habopen):" - successtext="The openHAB remote console was successfully opened on all interfaces. openHAB has been restarted. You should be able to reach the console via: -\\n'ssh://openhab:@ -p 8101'\\n -Please be aware, that the first connection attempt may take a few minutes or may result in a timeout due to key generation." + local introText + local sshPass + local sshPass1 + local sshPass2 + local successText + + introText="\\nThe openHAB remote console is a powerful tool for every openHAB user. It allows you too have a deeper insight into the internals of your setup. Further details: https://www.openhab.org/docs/administration/console.html\\n\\nThis routine will bind the console to all interfaces and thereby make it available to other devices in your network. Please provide a secure password for this connection (letters and numbers only!):" + successText="The openHAB remote console was successfully opened on all interfaces. openHAB has been restarted. You should be able to reach the console via:\\n\\n'ssh://openhab:@ -p 8101'\\n\\nPlease be aware, that the first connection attempt may take a few minutes or may result in a timeout due to key generation.\\n\\nThe default password is habopen." echo -n "$(timestamp) [openHABian] Binding the openHAB remote console on all interfaces... " - if [ -n "$INTERACTIVE" ]; then - sshPassword=$(whiptail --title "Bind Remote Console, Password?" --inputbox "$introtext" 20 60 3>&1 1>&2 2>&3) - exitstatus=$? - if [ $exitstatus -ne 0 ]; then - echo "aborted" - return 0 - fi + + if [[ -n $INTERACTIVE ]]; then + while [[ -z $sshPass ]]; do + if ! sshPass1=$(whiptail --title "Authentication Setup" --passwordbox "$introText" 15 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! sshPass2=$(whiptail --title "Authentication Setup" --passwordbox "\\nPlease confirm the password:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if [[ $sshPass1 == "$sshPass2" ]] && [[ ${#sshPass1} -ge 8 ]] && [[ ${#sshPass2} -ge 8 ]]; then + sshPass="$sshPass1" + else + whiptail --title "Authentication Setup" --msgbox "Password mismatched, blank, or less than 8 characters... Please try again!" 7 80 + fi + done + fi + if [[ -z $sshPass ]]; then + sshPass="habopen" fi - [[ -z "${sshPassword// }" ]] && sshPassword="habopen" - cond_redirect sed -i "s/sshHost = 127.0.0.1/sshHost = 0.0.0.0/g" /var/lib/openhab2/etc/org.apache.karaf.shell.cfg - cond_redirect sed -i "s/openhab = .*,/openhab = $sshPassword,/g" /var/lib/openhab2/etc/users.properties + if ! cond_redirect sed -i 's|^sshHost = 127.0.0.1.*$|sshHost = 0.0.0.0|g' /var/lib/openhab2/etc/org.apache.karaf.shell.cfg; then echo "FAILED (sshHost)"; return 1; fi + if cond_redirect sed -i 's|openhab = .*,|openhab = '"${sshPass}"',|g' /var/lib/openhab2/etc/users.properties; then echo "OK"; else echo "FAILED (sshPass)"; return 1; fi cond_redirect systemctl restart openhab2.service - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Operation Successful!" --msgbox "$successText" 15 80 fi - echo "OK" } +## Function to download and install special vim syntax for openHAB files. +## +## vim_openhab_syntax() +## vim_openhab_syntax() { echo -n "$(timestamp) [openHABian] Adding openHAB syntax to vim editor... " - # these may go to "/usr/share/vim/vimfiles" ? - mkdir -p /home/"${username:-openhabian}"/.vim/{ftdetect,syntax} - cond_redirect wget -O "/home/$username/.vim/syntax/openhab.vim" https://raw.githubusercontent.com/cyberkov/openhab-vim/master/syntax/openhab.vim - cond_redirect wget -O "/home/$username/.vim/ftdetect/openhab.vim" https://raw.githubusercontent.com/cyberkov/openhab-vim/master/ftdetect/openhab.vim - chown -R "$username:$username" /home/"$username"/.vim - echo "OK" + if ! cond_redirect mkdir -p /home/"${username:-openhabian}"/.vim/{ftdetect,syntax}; then echo "FAILED (prepare dirs)"; return 1; fi + if ! cond_redirect wget -O "/home/${username:-openhabian}/.vim/syntax/openhab.vim" https://github.com/cyberkov/openhab-vim/raw/master/syntax/openhab.vim; then echo "FAILED (syntax)"; return 1; fi + if ! cond_redirect wget -O "/home/${username:-openhabian}/.vim/ftdetect/openhab.vim" https://github.com/cyberkov/openhab-vim/raw/master/ftdetect/openhab.vim; then echo "FAILED (ftdetect)"; return 1; fi + if chown -R "${username:-openhabian}:${username:-openhabian}" /home/"${username:-openhabian}"/.vim; then echo "OK"; else echo "FAILED (permissions)"; return 1; fi } +## Function to download and install special nano syntax for openHAB files. +## +## nano_openhab_syntax() +## nano_openhab_syntax() { - # add nano syntax highlighting echo -n "$(timestamp) [openHABian] Adding openHAB syntax to nano editor... " - cond_redirect wget -O /usr/share/nano/openhab.nanorc https://raw.githubusercontent.com/airix1/openhabnano/master/openhab.nanorc - echo -e "\\n## openHAB files\\ninclude \"/usr/share/nano/openhab.nanorc\"" >> /etc/nanorc - echo "OK" + if ! cond_redirect wget -O /usr/share/nano/openhab.nanorc https://github.com/airix1/openhabnano/raw/master/openhab.nanorc; then echo "FAILED (download)"; return 1; fi + if echo -e "\\n## openHAB syntax\\ninclude \"/usr/share/nano/openhab.nanorc\"" >> /etc/nanorc; then echo "OK"; else echo "FAILED (nanorc)"; return 1; fi } +## Function to install a special multitail theme for openHAB logs. +## +## multitail_openhab_scheme() +## multitail_openhab_scheme() { echo -n "$(timestamp) [openHABian] Adding openHAB scheme to multitail... " - cp "$BASEDIR"/includes/multitail.openhab.conf /etc/multitail.openhab.conf - sed -i "/^.*multitail.*openhab.*$/d" /etc/multitail.conf - sed -i "s|# misc|include:/etc/multitail.openhab.conf\\n#\\n# misc|g" /etc/multitail.conf - echo "OK" + if ! cp "${BASEDIR:-/opt/openhabian}"/includes/multitail.openhab.conf /etc/multitail.openhab.conf; then echo "FAILED (copy)"; return 1; fi + if ! cond_redirect sed -i '/^.*multitail.*openhab.*$/d' /etc/multitail.conf; then echo "FAILED (remove default configuration)"; return 1; fi + if cond_redirect sed -i 's|^# misc.*$|# openHAB logs\\ninclude:/etc/multitail.openhab.conf\\n#\\n# misc|g' /etc/multitail.conf; then echo "OK"; else echo "FAILED (include)"; return 1; fi } +## Function to check if openHAB has been installed on the current system. Returns +## 0 / true if openHAB is installed and 1 / false if not. +## +## openhab_is_installed() +## openhab_is_installed() { - dpkg-query -l "openhab2" &>/dev/null - return $? + if dpkg -s 'openhab2' &> /dev/null; then return 0; else return 1; fi } +## Function to check if openHAB is running on the current system. Returns +## 0 / true if openHAB is running and 1 / false if not. +## +## openhab_is_running() +## openhab_is_running() { - if [ "$(systemctl is-active openhab2)" != "active" ]; then return 1; fi - if [ -r /etc/default/openhab2 ]; then - # shellcheck source=/etc/default/openhab2 disable=SC1091 - source /etc/default/openhab2 + if [[ $(systemctl is-active openhab2) != "active" ]]; then return 1; fi + + echo -n "$(timestamp) [openHABian] Setting openHAB HTTP/HTTPS ports... " + if ! cond_redirect sed -i 's|^#*.*OPENHAB_HTTP_PORT=.*$|OPENHAB_HTTP_PORT=8080|g' /etc/default/openhab2; then echo "FAILED"; return 1; fi + if cond_redirect sed -i 's|^#*.*OPENHAB_HTTPS_PORT=.*$|OPENHAB_HTTPS_PORT=8443|g' /etc/default/openhab2; then echo "OK"; else echo "FAILED"; return 1; fi +} + +## Optimize openHAB Java for low memory SBC's +## +## openhab_java_optimize() +## +openhab_java_optimize() { + if ! is_arm; then return 0; fi + + echo -n "$(timestamp) [openHABian] Optimizing openHAB to run on low memory single board computers... " + if has_lowmem; then + if cond_redirect sed -i 's|^EXTRA_JAVA_OPTS=.*$|EXTRA_JAVA_OPTS="-Xms16m -Xmx256m"|g' /etc/default/openhab2; then echo "OK"; else echo "FAILED"; return 1; fi + else + if cond_redirect sed -i 's|^EXTRA_JAVA_OPTS=.*$|EXTRA_JAVA_OPTS="-Xms192m -Xmx320m"|g' /etc/default/openhab2; then echo "OK"; else echo "FAILED"; return 1; fi fi - # Read and set openHAB variables set in /etc/default/ scripts - if [ -z "${OPENHAB_HTTP_PORT}" ]; then OPENHAB_HTTP_PORT=8080; fi - if [ -z "${OPENHAB_HTTPS_PORT}" ]; then OPENHAB_HTTPS_PORT=8443; fi - return 0; } -# The function has one non-optional parameter for the application to create a tile for +## Create a openHAB dashboard title and image for the input application. +## Valid arguments: "grafana", "frontail", "nodered", "find", or "openhabiandocs" +## +## dashboard_add_tile(String application) +## dashboard_add_tile() { - tile_name="$1" - echo -n "$(timestamp) [openHABian] Adding an openHAB dashboard tile for '$tile_name'... " - openhab_config_folder="/etc/openhab2" - dashboard_file="$openhab_config_folder/services/dashboard.cfg" - case $tile_name in + local application + local dashboardConfig + local openhabConfig + local tileDesc + local tileImg + local tileURL + + application="$1" + openhabConfig="/etc/openhab2" + dashboardConfig="${openhabConfig}/services/dashboard.cfg" + tileDesc="$(grep "^[[:space:]]*tile_desc_${application}" "${BASEDIR:-/opt/openhabian}"/includes/dashboard-imagedata.sh | sed 's|tile_desc_'"${application}"'=||g; s|"||g')" + tileImg="$(grep "^[[:space:]]*tile_imagedata_${application}" "${BASEDIR:-/opt/openhabian}"/includes/dashboard-imagedata.sh | sed 's|tile_imagedata_'"${application}"'=||g; s|"||g')" + tileURL="$(grep "^[[:space:]]*tile_url_${application}" "${BASEDIR:-/opt/openhabian}"/includes/dashboard-imagedata.sh | sed 's|tile_url_'"${application}"'=||g; s|"||g')" + + echo -n "$(timestamp) [openHABian] Adding an openHAB dashboard tile for '${application}'... " + + case $application in grafana|frontail|nodered|find|openhabiandocs) true ;; *) echo "FAILED (tile name not valid)"; return 1 ;; esac - if ! openhab_is_installed || [ ! -d "$openhab_config_folder/services" ]; then + if ! openhab_is_installed || ! [[ -d "${openhabConfig}/services" ]]; then echo "FAILED (openHAB or config folder missing)" return 1 fi - touch $dashboard_file - if grep -q "$tile_name.link" $dashboard_file; then + + touch $dashboardConfig + if grep -qs "${application}.link" $dashboardConfig; then echo -n "Replacing... " - sed -i "/^$tile_name.link.*$/d" $dashboard_file + cond_redirect sed -i '/^'"${application}"'.link.*$/d' $dashboardConfig fi - # shellcheck source=includes/dashboard-imagedata.sh disable=SC1091 - source "$BASEDIR/includes/dashboard-imagedata.sh" - tile_desc=$(eval echo "\$tile_desc_$tile_name") - tile_url=$(eval echo "\$tile_url_$tile_name") - tile_imagedata=$(eval echo "\$tile_imagedata_$tile_name") - if [ -z "$tile_desc" ] || [ -z "$tile_url" ] || [ -z "$tile_imagedata" ]; then + if [[ -z $tileDesc ]] || [[ -z $tileURL ]] || [[ -z $tileImg ]]; then echo "FAILED (data missing)" return 1 fi - { - echo "" - echo "$tile_name.link-name=$tile_desc" - echo "$tile_name.link-url=$tile_url" - echo "$tile_name.link-imageurl=$tile_imagedata" - } >> $dashboard_file - - echo "OK" - return 0 + if echo -e "\\n${application}.link-name=${tileDesc}\\n${application}.link-url=${tileURL}\\n${application}.link-imageurl=${tileImg}" >> $dashboardConfig; then echo "OK"; else echo "FAILED"; return 1; fi } diff --git a/functions/openhabian.bash b/functions/openhabian.bash index 96e37dcc1..756c81407 100644 --- a/functions/openhabian.bash +++ b/functions/openhabian.bash @@ -1,172 +1,216 @@ #!/usr/bin/env bash +## Display a nicely formatted git revision. +## +## get_git_revision() +## get_git_revision() { - local branch shorthash revcount latesttag - branch=$(git -C "$BASEDIR" rev-parse --abbrev-ref HEAD) - shorthash=$(git -C "$BASEDIR" log --pretty=format:'%h' -n 1) - revcount=$(git -C "$BASEDIR" log --oneline | wc -l) - latesttag=$(git -C "$BASEDIR" describe --tags --abbrev=0) - echo "[$branch]$latesttag-$revcount($shorthash)" + local branch + local latestTag + local revCount + local shorthash + + branch="$(git -C "${BASEDIR:-/opt/openhabian}" rev-parse --abbrev-ref HEAD)" + latestTag="$(git -C "${BASEDIR:-/opt/openhabian}" describe --tags --abbrev=0)" + revCount="$(git -C "${BASEDIR:-/opt/openhabian}" log --oneline | wc -l)" + shorthash="$(git -C "${BASEDIR:-/opt/openhabian}" log --pretty=format:'%h' -n 1)" + + echo "[${branch}]${latestTag}-${revCount}(${shorthash})" } +## Cleanup apt after installation. +## +## install_cleanup() +## install_cleanup() { - echo "$(timestamp) [openHABian] Cleaning up ... " - cond_redirect systemctl -q daemon-reload &>/dev/null - cond_redirect apt-get autoremove --yes + echo -n "$(timestamp) [openHABian] Cleaning up... " + cond_redirect systemctl -q daemon-reload &> /dev/null + if cond_redirect apt-get autoremove --yes; then echo "OK"; else echo "FAILED"; return 1; fi } +## If needed, displays announcements that have been created by the openHABian developers. +## +## openhabian_announcements() +## openhabian_announcements() { - local newsfile="${BASEDIR}/NEWS.md" - local readnews="${BASEDIR}/docs/LASTNEWS.md" + if [[ -z $INTERACTIVE ]]; then return 1; fi - if [[ -z "$INTERACTIVE" ]]; then return 1; fi + local newsFile + local readNews - if ! diff -q "$newsfile" "$readnews" >/dev/null 2>&1; then + newsFile="${BASEDIR:-/opt/openhabian}/NEWS.md" + readNews="${BASEDIR:-/opt/openhabian}/docs/LASTNEWS.md" + + if ! cmp --silent "$newsFile" "$readNews" &> /dev/null; then # shellcheck disable=SC2086 - if (whiptail --title "openHABian announcements" --yes-button "Stop Displaying" --no-button "Keep Displaying" --defaultno --scrolltext --yesno "$(cat $newsfile)" 27 85); then - cp "$newsfile" "$readnews"; + if (whiptail --title "openHABian announcements" --yes-button "Stop Displaying" --no-button "Keep Displaying" --defaultno --scrolltext --yesno "$(cat $newsFile)" 27 85); then + cp "$newsFile" "$readNews" fi fi } +## Displays a warning if the current console may exibit issues displaing the +## openHABian menus. +## +## openhabian_console_check() +## openhabian_console_check() { - if [ "$(tput cols)" -lt 120 ]; then - warningtext="We detected that you use a console which is less than 120 columns wide. This tool is designed for a minimum of 120 columns and therefore some menus may not be presented correctly. Please increase the width of your console and rerun this tool. - \\nEither resize the window or consult the preferences of your console application." - whiptail --title "Compatibility Warning" --msgbox "$warningtext" 15 76 - fi + if [[ -z $INTERACTIVE ]]; then return 1; fi + if [[ $(tput cols) -ge 120 ]]; then return 0; fi + + local warningText + + warningText="We detected that you use a console which is less than 120 columns wide. This tool is designed for a minimum of 120 columns and therefore some menus may not be presented correctly. Please increase the width of your console and rerun this tool.\\n\\nEither resize the window or consult the preferences of your console application." + + whiptail --title "Compatibility Warning" --msgbox "$warningText" 13 80 } +## Check for updates to the openhabian git repository, if so then issue an update. +## This also ensures that announcements will be displayed and that we stay on the correct branch. +## +## openhabian_update_check() +## openhabian_update_check() { + if [[ -z $INTERACTIVE ]]; then return 0; fi + local branch - local introtext="Additions, improvements or fixes were added to the openHABian configuration tool. Would you like to update now and benefit from them? The update will not automatically apply changes to your system.\\n\\nUpdating is recommended." - local unsupportedhwtext="You are running on old hardware that is no longer officially supported.\\nopenHABian may still work with this or not.\\nWe recommend to replace your hardware with a current SBC such as a RPi4/2GB.\\nDo you really want to continue using openHABian on this system ?" - local unsupportedostext="You are running an old Linux release that is no longer officially supported.\\nWe recommend upgrading to the most current stable release of your distribution (or current Long Term Support version for distributions to offer LTS).\\nDo you really want to continue using openHABian on this system ?" + local introText + local unsupportedHWText + local unsupportedOSText + + branch="${clonebranch:-HEAD}" + introText="Additions, improvements or fixes were added to the openHABian configuration tool. Would you like to update now and benefit from them? The update will not automatically apply changes to your system.\\n\\nUpdating is recommended." + unsupportedHWText="You are running on old hardware that is no longer officially supported.\\nopenHABian may still work with this or not.\\nWe recommend that you replace your hardware with a current SBC such as a RPi4/2GB.\\nDo you really want to continue using openHABian on this system?" + unsupportedOSText="You are running an old Linux release that is no longer officially supported.\\nWe recommend upgrading to the most current stable release of your distribution (or current Long Term Support version for distributions that offer LTS).\\nDo you really want to continue using openHABian on this system?" + + echo "$(timestamp) [openHABian] openHABian configuration tool version: $(get_git_revision)" + echo -n "$(timestamp) [openHABian] Checking for changes in origin branch ${branch}... " if is_pine64; then - if ! (whiptail --title "Unsupported hardware" --yes-button "Yes, Continue" --no-button "No, Exit" --defaultno --yesno "$unsupportedhwtext" 13 85); then echo "SKIP"; exit 0; fi + if ! (whiptail --title "Unsupported hardware" --yes-button "Yes, Continue" --no-button "No, Exit" --defaultno --yesno "$unsupportedHWText" 13 80); then echo "SKIP"; exit 0; fi fi if is_jessie || is_xenial; then - if ! (whiptail --title "Unsupported Linux release" --yes-button "Yes, Continue" --no-button "No, Exit" --defaultno --yesno "$unsupportedostext" 13 85); then echo "SKIP"; exit 0; fi + if ! (whiptail --title "Unsupported Linux release" --yes-button "Yes, Continue" --no-button "No, Exit" --defaultno --yesno "$unsupportedOSText" 13 80); then echo "SKIP"; exit 0; fi fi - FAILED=0 - echo "$(timestamp) [openHABian] openHABian configuration tool version: $(get_git_revision)" - branch=${clonebranch:-HEAD} - echo -n "$(timestamp) [openHABian] Checking for changes in origin branch $branch ... " - git -C "$BASEDIR" config user.email 'openhabian@openHABian' - git -C "$BASEDIR" config user.name 'openhabian' - git -C "$BASEDIR" fetch --quiet origin || FAILED=1 - # shellcheck disable=SC2046 - if [ $(git -C "$BASEDIR" rev-parse "$branch") == $(git -C "$BASEDIR" rev-parse @\{u\}) ]; then + if ! git -C "${BASEDIR:-/opt/openhabian}" config user.email 'openhabian@openHABian'; then echo "FAILED (git email)"; return 1; fi + if ! git -C "${BASEDIR:-/opt/openhabian}" config user.name 'openhabian'; then echo "FAILED (git user)"; return 1; fi + if ! git -C "${BASEDIR:-/opt/openhabian}" fetch --quiet origin; then echo "FAILED (fetch origin)"; return 1; fi + + if [[ $(git -C "${BASEDIR:-/opt/openhabian}" rev-parse "$branch") == $(git -C "${BASEDIR:-/opt/openhabian}" rev-parse @\{u\}) ]]; then echo "OK" else echo -n "Updates available... " - if ! (whiptail --title "openHABian Update Available" --yes-button "Continue" --no-button "Skip" --yesno "$introtext" 15 80); then echo "SKIP"; return 0; fi - echo "" - openhabian_update "$branch" + if (whiptail --title "openHABian Update Available" --yes-button "Continue" --no-button "Skip" --yesno "$introText" 11 80); then echo "UPDATING"; else echo "SKIP"; return 0; fi + openhabian_update fi openhabian_announcements - echo -n "$(timestamp) [openHABian] Switching to branch $clonebranch ... " - # shellcheck disable=SC2015 - git -C "$BASEDIR" checkout --quiet "$clonebranch" && echo "OK" || (FAILED=1; echo "FAILED"; return 0) + echo -n "$(timestamp) [openHABian] Switching to branch ${clonebranch:-stable}... " + if git -C "${BASEDIR:-/opt/openhabian}" checkout --quiet "${clonebranch:-stable}"; then echo "OK"; else echo "FAILED"; return 1; fi } +## Updates the current openhabian repository to the most current version of the +## current branch. +## +## openhabian_update() +## openhabian_update() { - local branch shorthash_before - - export BASEDIR="${BASEDIR:-/opt/openhabian}" - current=$(git -C "${BASEDIR}" rev-parse --abbrev-ref HEAD) - if [ "$current" == "master" ]; then - local introtext="You are currently using the very latest (\"master\") version of openHABian.\\nThis is providing you with the latest features but less people have tested it so it is a little more likely that you run into errors.\\nWould you like to step back a little now and switch to use the stable version ?\\nYou can switch at any time by selecting this menu option again or by setting the clonebranch= parameter in /etc/openhabian.conf.\\n" + local branch + local current + local introText + local key + local selection + local shorthashAfter + local shorthashBefore + + current="$(git -C "${BASEDIR:-/opt/openhabian}" rev-parse --abbrev-ref HEAD)" + if [[ $current == "master" ]]; then + introText="You are currently using the very latest (\"master\") version of openHABian.\\nThis is providing you with the latest features but less people have tested it so it is a little more likely that you run into errors.\\nWould you like to step back a little now and switch to use the stable version ?\\nYou can switch at any time by selecting this menu option again or by setting the 'clonebranch=' parameter in '/etc/openhabian.conf'.\\n" else - if [ "$current" == "stable" ]; then - local introtext="You are currently using the stable version of openHABian.\\nAccess to the latest features would require you to switch to the latest version.\\nWould you like to step back a little now and switch to use the stable version ?\\nYou can switch versions at any time by selecting this menu option again or by setting the clonebranch= parameter in /etc/openhabian.conf.\\n" - else - local introtext="You are currently using neither the stable version nor the latest (\"master\") version of openHABian.\\nAccess to the latest features would require you to switch to master while the default is to use the stable version.\\nWould you like to step back a little now and switch to use the stable version ?\\nYou can switch versions at any time by selecting this menu option again or by setting the clonebranch= parameter in /etc/openhabian.conf.\\n" - fi + introText="You are currently using neither the stable version nor the latest (\"master\") version of openHABian.\\nAccess to the latest features would require you to switch to master while the default is to use the stable version.\\nWould you like to step back a little now and switch to use the stable version ?\\nYou can switch versions at any time by selecting this menu option again or by setting the 'clonebranch=' parameter in '/etc/openhabian.conf'.\\n" fi - FAILED=0 - if [[ -n "$INTERACTIVE" ]]; then - if [[ "$current" == "stable" || "$current" == "master" ]]; then - if ! sel=$(whiptail --title "openHABian version" --radiolist "$introtext" 14 75 2 stable "recommended standard version of openHABian" on master "very latest version of openHABian" off 3>&1 1>&2 2>&3); then return 0; fi + echo -n "$(timestamp) [openHABian] Updating myself... " + + if [[ -n $INTERACTIVE ]]; then + if [[ $current == "stable" || $current == "master" ]]; then + if ! selection=$(whiptail --title "openHABian version" --radiolist "$introText" 14 80 2 stable "recommended standard version of openHABian" on master "very latest version of openHABian" off 3>&1 1>&2 2>&3); then return 0; fi else - if ! sel=$(whiptail --title "openHABian version" --radiolist "$introtext" 14 75 3 stable "recommended standard version of openHABian" off master "very latest version of openHABian" off "$current" "some other version you fetched yourself" on 3>&1 1>&2 2>&3); then return 0; fi + if ! selection=$(whiptail --title "openHABian version" --radiolist "$introText" 14 80 3 stable "recommended standard version of openHABian" off master "very latest version of openHABian" off "$current" "some other version you fetched yourself" on 3>&1 1>&2 2>&3); then return 0; fi fi - sed -i "s@^clonebranch=.*@clonebranch=$sel@g" "/etc/openhabian.conf" - echo -n "$(timestamp) [openHABian] Updating myself... " + if ! sed -i 's|^clonebranch=.*$|clonebranch='"${selection}"'|g' "$CONFIGFILE"; then echo "FAILED (configure clonebranch)"; exit 1; fi read -r -t 1 -n 1 key - if [ "$key" != "" ]; then + if [[ -n $key ]]; then echo -e "\\nRemote git branches available:" - git -C "$BASEDIR" branch -r + git -C "${BASEDIR:-/opt/openhabian}" branch -r read -r -e -p "Please enter the branch to checkout: " branch branch="${branch#origin/}" - if ! git -C "$BASEDIR" branch -r | grep -q "origin/$branch"; then - echo "FAILED - The custom branch does not exist." + if ! git -C "${BASEDIR:-/opt/openhabian}" branch -r | grep -q "origin/$branch"; then + echo "FAILED (custom branch does not exist)" return 1 fi else - branch="${sel:-stable}" + branch="${selection:-stable}" fi else - branch=${clonebranch:-stable} + branch="${clonebranch:-stable}" fi - shorthash_before=$(git -C "$BASEDIR" log --pretty=format:'%h' -n 1) - git -C "$BASEDIR" fetch --quiet origin || FAILED=1 - git -C "$BASEDIR" reset --quiet --hard "origin/$branch" || FAILED=1 - git -C "$BASEDIR" clean --quiet --force -x -d || FAILED=1 - git -C "$BASEDIR" checkout --quiet "$branch" || FAILED=1 - if [ $FAILED -eq 1 ]; then - echo "FAILED - There was a problem fetching the latest changes for the openHABian configuration tool. Please check your internet connection and try again later..." - return 1 - fi - shorthash_after=$(git -C "$BASEDIR" log --pretty=format:'%h' -n 1) - if [ "$shorthash_before" == "$shorthash_after" ]; then + shorthashBefore="$(git -C "${BASEDIR:-/opt/openhabian}" log --pretty=format:'%h' -n 1)" + if ! cond_redirect update_git_repo "${BASEDIR:-/opt/openhabian}" "$branch"; then echo "FAILED (update git repo)"; return 1; fi + shorthashAfter="$(git -C "${BASEDIR:-/opt/openhabian}" log --pretty=format:'%h' -n 1)" + + if [[ $shorthashBefore == "$shorthashAfter" ]]; then echo "OK - No remote changes detected. You are up to date!" return 0 else - echo "OK - Commit history (oldest to newest):" - echo -e "\\n" - git -C "$BASEDIR" --no-pager log --pretty=format:'%Cred%h%Creset - %s %Cgreen(%ar) %C(bold blue)<%an>%Creset %C(dim yellow)%G?' --reverse --abbrev-commit --stat "$shorthash_before..$shorthash_after" - echo -e "\\n" - echo "openHABian configuration tool successfully updated." - if [[ -n "$INTERACTIVE" ]]; then - # shellcheck disable=SC2154 - echo "Visit the development repository for more details: $repositoryurl" - echo "The tool will now restart to load the updates... " - echo -e "\\n" - exec "$BASEDIR/$SCRIPTNAME" + echo -e "OK - Commit history (oldest to newest):\\n" + git -C "${BASEDIR:-/opt/openhabian}" --no-pager log --pretty=format:'%Cred%h%Creset - %s %Cgreen(%ar) %C(bold blue)<%an>%Creset %C(dim yellow)%G?' --reverse --abbrev-commit --stat "$shorthashBefore..$shorthashAfter" + echo -e "\\nopenHABian configuration tool successfully updated." + if [[ -n $INTERACTIVE ]]; then + echo "Visit the development repository for more details: ${repositoryurl:-https://github.com/openhab/openhabian.git}" + echo "The tool will now restart to load the updates... OK" + exec "${BASEDIR:-/opt/openhabian}/$SCRIPTNAME" exit 0 fi fi } +## Check for default system password and if found issue a warning and suggest +## changing the password. +## +## system_check_default_password() +## system_check_default_password() { - introtext="The default password was detected on your system! That's a serious security concern. Others or malicious programs in your subnet are able to gain root access! - \\nPlease set a strong password by typing the command 'passwd'!" - - echo -n "$(timestamp) [openHABian] Checking for default openHABian username:password combination... " - if is_pi && id -u pi &>/dev/null; then - USERNAME="pi" - PASSWORD="raspberry" + if ! is_pi; then return 0; fi + + local algo + local defaultPassword + local defaultUser + local generatedPassword + local introText + local originalPassword + local salt + + if is_pi && id -u pi &> /dev/null; then + defaultUser="pi" + defaultPassword="raspberry" elif is_pi; then - USERNAME="openhabian" - PASSWORD="openhabian" - else - echo "SKIPPED (method not implemented)" - return 0 + defaultUser="openhabian" + defaultPassword="openhabian" fi - if ! id -u $USERNAME &>/dev/null; then echo "OK (unknown user)"; return 0; fi - ORIGPASS=$(grep -w "$USERNAME" /etc/shadow | cut -d: -f2) - ALGO=$(echo "$ORIGPASS" | cut -d'$' -f2) - SALT=$(echo "$ORIGPASS" | cut -d'$' -f3) - export PASSWORD ALGO SALT - GENPASS=$(perl -le 'print crypt("$ENV{PASSWORD}","\$$ENV{ALGO}\$$ENV{SALT}\$")') - if [ "$GENPASS" == "$ORIGPASS" ]; then - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Default Password Detected!" --msgbox "$introtext" 12 70 + originalPassword="$(grep -w "$defaultUser" /etc/shadow | cut -d: -f2)" + algo="$(echo "$originalPassword" | cut -d'$' -f2)" + introText="The default password was detected on your system! That is a serious security concern. Others or malicious programs in your subnet are able to gain root access!\\n\\nPlease set a strong password by typing the command 'passwd'!" + salt="$(echo "$originalPassword" | cut -d'$' -f3)" + export algo defaultPassword salt + generatedPassword="$(perl -le 'print crypt("$ENV{defaultPassword}","\$$ENV{algo}\$$ENV{salt}\$")')" + + echo -n "$(timestamp) [openHABian] Checking for default openHABian username:password combination... " + if ! id -u $defaultUser &> /dev/null; then echo "OK (unknown user)"; return 0; fi + if [[ $generatedPassword == "$originalPassword" ]]; then + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Default Password Detected!" --msgbox "$introText" 11 80 fi echo "FAILED" else @@ -174,13 +218,6 @@ system_check_default_password() { fi } -ua-netinst_check() { - if [ -f "/boot/config-reinstall.txt" ]; then - introtext="Attention: It was brought to our attention that the old openHABian ua-netinst based image has a problem with a lately updated Linux package.\\nIf you upgrade(d) the package 'raspberrypi-bootloader-nokernel' your Raspberry Pi will run into a Kernel Panic upon reboot!\\nDo not upgrade, do not reboot!\\nA preliminary solution is to not upgrade the system (via the Upgrade menu entry or 'apt-get upgrade') or to modify a configuration file. In the long run we would recommend to switch over to the new openHABian Raspbian based system image! This error message will keep reapearing even after you fixed the issue at hand.\\nPlease find all details regarding the issue and the resolution of it at: https://github.com/openhab/openhabian/issues/147" - if ! (whiptail --title "openHABian Raspberry Pi ua-netinst image detected" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 20 80); then return 0; fi - fi -} - ## Enable / Disable IPv6 according to the users configured option in '$CONFIGFILE' ## ## config_ipv6() diff --git a/functions/packages.bash b/functions/packages.bash index c8ea884e2..8389f5c2a 100644 --- a/functions/packages.bash +++ b/functions/packages.bash @@ -6,21 +6,23 @@ ## samba_setup() ## samba_setup() { - if ! [ -x "$(command -v samba)" ]; then + if ! [[ -x $(command -v samba) ]]; then echo -n "$(timestamp) [openHABian] Installing Samba... " 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... " + echo -n "$(timestamp) [openHABian] Setting up Samba network shares... " + cond_echo "\nCopying over custom 'smb.conf'... " + cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/smb.conf /etc/samba/smb.conf + cond_echo "\nWriting authentication data to openHABian default... " if ! smbpasswd -e "${username:-openhabian}" &> /dev/null; then # shellcheck disable=SC2154 - ( (echo "$userpw"; echo "$userpw") | smbpasswd -s -a "${username:-openhabian}" > /dev/null ) + (echo "$userpw"; echo "$userpw") | smbpasswd -s -a "${username:-openhabian}" &> /dev/null fi + echo "OK" echo -n "$(timestamp) [openHABian] Setting up Samba service... " + cond_redirect systemctl -q daemon-reload &> /dev/null 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 } @@ -35,13 +37,13 @@ firemotd_setup() { temp="$(mktemp "${TMPDIR:-/tmp}"/openhabian.XXXXX)" - if ! dpkg -s 'bc' 'sysstat' 'jq' 'moreutils' > /dev/null 2>&1; then - echo -n "$(timestamp) [openHABian] Installing FireMotD required packages... " + if ! dpkg -s 'bc' 'sysstat' 'jq' 'moreutils' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing FireMotD required packages (bc, sysstat, jq, moreutils)... " if cond_redirect apt-get install --yes bc sysstat jq moreutils; then echo "OK"; else echo "FAILED"; return 1; fi fi echo -n "$(timestamp) [openHABian] Downloading FireMotD... " - if cond_redirect wget -qO "$temp" https://raw.githubusercontent.com/OutsideIT/FireMotD/master/FireMotD; then + if cond_redirect wget -O "$temp" https://raw.githubusercontent.com/OutsideIT/FireMotD/master/FireMotD; then echo "OK" else echo "FAILED" @@ -64,7 +66,7 @@ firemotd_setup() { echo -n "$(timestamp) [openHABian] Setting up FireMotD apt updates count service... " cond_echo "\nMake 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 + echo "0 0 * * * root perl -e 'sleep int(rand(21600))' && /bin/bash /usr/local/bin/FireMotD -S &> /dev/null" >> /etc/cron.d/firemotd cond_echo "\nMake 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 cond_echo "\nInitial FireMotD updates check" @@ -77,9 +79,8 @@ firemotd_setup() { ## exim_setup() ## exim_setup() { - if [ -z "$INTERACTIVE" ]; then + if [[ -z $INTERACTIVE ]]; then echo "$(timestamp) [openHABian] MTA setup must be run in interactive mode! Canceling MTA setup!" - echo "CANCELED" return 0 fi @@ -91,8 +92,8 @@ exim_setup() { local smartHost local temp - if ! dpkg -s 'mailutils' 'exim4' 'dnsutils' > /dev/null 2>&1; then - echo -n "$(timestamp) [openHABian] Installing MTA required packages... " + if ! dpkg -s 'mailutils' 'exim4' 'dnsutils' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing MTA required packages (mailutils, exim4, dnsutils,)... " if cond_redirect apt-get install --yes exim4 dnsutils mailutils; then echo "OK"; else echo "FAILED"; return 1; fi fi @@ -105,8 +106,8 @@ exim_setup() { if (whiptail --title "Mail Transfer Agent installation" --yes-button "Begin" --no-button "Cancel" --yesno "$introtext" 17 80); then echo "OK"; else echo "CANCELED"; return 0; fi echo "$(timestamp) [openHABian] Reconfiguring exim4-config... " - whiptail --title "exim4 Configuration" --msgbox "exim4-config is about to ask for information, please fill out each line." 7 80 3>&1 1>&2 2>&3 - if dpkg-reconfigure exim4-config; then echo "OK"; else echo "CANCELED"; return 1; fi + whiptail --title "exim4 Configuration" --msgbox "exim4-config is about to ask for information, please fill out each line." 7 80 + if dpkg-reconfigure exim4-config; then echo "OK"; else echo "CANCELED"; return 0; fi 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 @@ -115,8 +116,8 @@ exim_setup() { echo "$(timestamp) [openHABian] Creating MTA config... " if ! grep '^#' "$eximConfig" > "$temp"; then echo "FAILED (configuration)"; rm -f "$temp"; return 1; fi if ! echo "${smartHost}:${relayUser}:${relayPass}" >> "$temp"; then echo "FAILED (configuration)"; rm -f "$temp"; return 1; fi - if ! cp "$temp" "$eximConfig"; then echo "FAILED (copy)"; rm -f "$temp"; return 1; fi - rm -f "$temp" + if ! cond_redirect cp "$temp" "$eximConfig"; then echo "FAILED (copy)"; rm -f "$temp"; return 1; fi + cond_redirect rm -f "$temp" if cond_redirect chmod o-rwx "$eximConfig"; then echo "OK"; else echo "FAILED (permissions)"; return 1; fi echo "$(timestamp) [openHABian] Adding to $relayUser email to system accounts... " @@ -132,7 +133,7 @@ exim_setup() { ## etckeeper_setup() ## etckeeper_setup() { - if ! [ -x "$(command -v etckeeper)" ]; then + if ! [[ -x $(command -v etckeeper) ]]; then echo -n "$(timestamp) [openHABian] Installing etckeeper (git based /etc backup)... " if cond_redirect apt-get install --yes etckeeper; then echo "OK"; else echo "FAILED"; return 1; fi fi @@ -140,7 +141,7 @@ etckeeper_setup() { echo -n "$(timestamp) [openHABian] Configuring etckeeper (git based /etc backup)... " if ! cond_redirect sed -i 's/VCS="bzr"/\#VCS="bzr"/g' /etc/etckeeper/etckeeper.conf; then echo "FAILED"; return 1; fi if ! cond_redirect sed -i 's/\#VCS="git"/VCS="git"/g' /etc/etckeeper/etckeeper.conf; then echo "FAILED"; return 1; fi - 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"; return 1; fi + if cond_redirect bash -c "cd /etc && etckeeper init && git config user.email 'etckeeper@openHABian' && git config user.name 'openhabian' && git commit -m 'initial checkin' && git gc"; then echo "OK"; else echo "FAILED"; return 1; fi } ## Function for installing Homegear, the Homematic CCU2 emulation software. @@ -153,10 +154,9 @@ homegear_setup() { local myOS local myRelease local successtext -# local rundir - if ! [ -x "$(command -v lsb_release)" ]; then - echo -n "$(timestamp) [openHABian] Installing Homegear required packages... " + if ! [[ -x $(command -v lsb_release) ]]; then + echo -n "$(timestamp) [openHABian] Installing Homegear required packages (lsb-release)... " if cond_redirect apt-get install --yes lsb-release; then echo "OK"; else echo "FAILED"; return 1; fi fi @@ -164,10 +164,9 @@ homegear_setup() { myOS="$(lsb_release -si)" myRelease="$(lsb_release -sc)" successtext="Setup was successful.\\n\\nHomegear is now up and running. Next you might want to edit the configuration file '/etc/homegear/families/homematicbidcos.conf' or adopt devices through the homegear console, reachable by 'sudo homegear -r'.\\n\\nPlease read up on the homegear documentation for more details: https://doc.homegear.eu/data/homegear\\n\\nTo continue your integration in openHAB 2, please follow the instructions under: https://www.openhab.org/addons/bindings/homematic/" -# rundir=/run/homegear echo -n "$(timestamp) [openHABian] Beginning Homematic CCU2 emulation software Homegear install... " - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "Homegear installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 8 80); then echo "OK"; else echo "CANCELED"; return 0; fi fi @@ -182,16 +181,16 @@ homegear_setup() { if ! cond_redirect adduser "${username:-openhabian}" homegear; then echo "FAILED"; return 1; fi if cond_redirect adduser openhab homegear; then echo "OK"; else echo "FAILED"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up Homegear service... " - cp "$BASEDIR"/includes/homegear*.service /etc/systemd/system/ - if running_in_docker; then sed -i '/RuntimeDirectory/d' /lib/systemd/system/homegear*; fi - cond_redirect systemctl -q daemon-reload &>/dev/null - if ! systemctl enable --now homegear homegear-management; then echo "FAILED (enable service)"; return 1; fi - - if [[ -n "$INTERACTIVE" ]]; then + if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/homegear.service /etc/systemd/system/homegear.service; then echo "FAILED (copy service)"; return 1; fi + if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/homegear-management.service /etc/systemd/system/homegear-management.service; then echo "FAILED (copy service)"; return 1; fi + if running_in_docker; then sed -i '/RuntimeDirectory/d' /etc/systemd/system/homegear*; fi + cond_redirect systemctl -q daemon-reload &> /dev/null + if ! cond_redirect systemctl enable homegear.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect 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 fi - - return 0 } ## Function for installing MQTT Eclipse Mosquitto through the official repository. @@ -212,21 +211,21 @@ mqtt_setup() { 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 [[ -n $INTERACTIVE ]]; then if (whiptail --title "MQTT installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 14 80); then echo "OK"; else echo "CANCELED"; return 0; fi fi - if ! dpkg -s 'mosquitto' 'mosquitto-clients' > /dev/null 2>&1; then + if ! dpkg -s 'mosquitto' 'mosquitto-clients' &> /dev/null; then 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 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 + if [[ -n $INTERACTIVE ]]; then + if ! mqttpasswd=$(whiptail --title "MQTT Authentication?" --passwordbox "$questiontext" 14 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi fi - if [ "$mqttpasswd" != "" ]; then - if ! grep -q password_file /etc/mosquitto/passwd /etc/mosquitto/mosquitto.conf; then + if [[ -n $mqttpasswd ]]; then + if ! grep -qs 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 @@ -238,10 +237,11 @@ mqtt_setup() { fi echo -n "$(timestamp) [openHABian] Setting up MQTT service... " + cond_redirect systemctl -q daemon-reload &> /dev/null 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 [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 13 80 fi } @@ -252,9 +252,8 @@ mqtt_setup() { ## find_setup() ## find_setup() { - if [ -z "$INTERACTIVE" ]; then + if [[ -z $INTERACTIVE ]]; then echo "$(timestamp) [openHABian] FIND setup must be run in interactive mode! Canceling FIND setup!" - echo "CANCELED" return 0 fi @@ -275,7 +274,7 @@ find_setup() { local successtext local temp - if ! dpkg -s 'libsvm-tools' > /dev/null 2>&1; then + if ! dpkg -s 'libsvm-tools' &> /dev/null; then 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 fi @@ -305,22 +304,22 @@ find_setup() { 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 + if [[ $MQTTSERVER != "localhost" ]]; then 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 [[ -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 + while [[ -z $FINDADMINPASS ]]; 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 + if [[ $findPass1 == "$findPass2" ]] && [[ ${#findPass1} -ge 8 ]] && [[ ${#findPass2} -ge 8 ]]; 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 + whiptail --title "findserver MQTT Setup" --msgbox "Password mismatched, blank, or less than 8 characters... Please try again!" 7 80 fi done - if ! cond_redirect /usr/bin/mosquitto_passwd -b $mqttpasswd "$FINDADMIN" "$FINDADMINPASS"; then echo "FAILED (mosquitto password)"; return 1; fi + if ! cond_redirect 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" @@ -336,14 +335,16 @@ find_setup() { if cond_redirect rm -rf "$temp"; then echo "OK"; else echo "FAILED"; return 1; fi 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 ! (sed -e 's|%MQTTSERVER|'"${MQTTSERVER}"'|g; s|%MQTTPORT|'"${MQTTPORT}"'|g; s|%FINDADMINPASS|'"${FINDADMINPASS}"'|g; s|%FINDADMIN|'"${FINDADMIN}"'|g; s|%FINDPORT|8003|g; s|%FINDSERVER|localhost|g' "${BASEDIR:-/opt/openhabian}"/includes/findserver.service > /etc/systemd/system/findserver.service); then echo "FAILED (service file creation)"; return 1; fi + if ! cond_redirect chmod 644 /etc/systemd/system/findserver.service; then echo "FAILED (permissions)"; return 1; fi + if ! (sed -e 's|%MQTTSERVER|'"${MQTTSERVER}"'|g; s|%MQTTPORT|'"${MQTTPORT}"'|g; s|%FINDADMINPASS|'"${FINDADMINPASS}"'|g; s|%FINDADMIN|'"${FINDADMIN}"'|g; s|%FINDPORT|8003|g; s|%FINDSERVER|localhost|g' "${BASEDIR:-/opt/openhabian}"/includes/findserver > /etc/default/findserver); then echo "FAILED (service configuration creation)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null 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 whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 - if [ -z "$BATS_TEST_NAME" ]; then + if dpkg -s 'openhab2' &> /dev/null; then dashboard_add_tile find fi } @@ -363,17 +364,19 @@ knxd_setup() { temp="$(mktemp "${TMPDIR:-/tmp}"/openhabian.XXXXX)" echo -n "$(timestamp) [openHABian] Beginning setup of EIB/KNX IP Gateway and Router with knxd (https://bit.ly/3dzeoKh)... " - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "knxd installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 10 80); then echo "OK"; else echo "CANCELED"; return 0; fi + else + echo "OK" 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 + if ! cond_redirect wget -O "$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 rm -f "$temp"; echo "OK. Please restart your system now..."; else echo "FAILED (install)"; fi + if cond_redirect bash "$temp"; then rm -f "$temp"; echo "OK (Reboot needed)"; else echo "FAILED (install)"; return 1; fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80 fi } @@ -391,16 +394,18 @@ knxd_setup() { 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 [[ -n $INTERACTIVE ]]; then if (whiptail --title "1wire installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 12 80); then echo "OK"; else echo "CANCELED"; return 0; fi + else + echo "OK" fi - if ! dpkg -s 'owserver' 'ow-shell' 'usbutils' > /dev/null 2>&1; then + if ! dpkg -s 'owserver' 'ow-shell' 'usbutils' &> /dev/null; then 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 fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 17 80 fi } @@ -411,11 +416,16 @@ knxd_setup() { ## miflora_setup() ## miflora_setup() { + if ! is_pizerow && ! is_pithree && ! is_pithreeplus && ! is_pifour; then + echo "$(timestamp) [openHABian] Beginning setup of miflora-mqtt-daemon... SKIPPED (no Bluetooth support)" + return 0 + fi + local introtext local mifloraDir local successtext - if ! dpkg -s 'git' 'python3' 'python3-pip' 'bluetooth' 'bluez' > /dev/null 2>&1; then + if ! dpkg -s 'git' 'python3' 'python3-pip' 'bluetooth' 'bluez' &> /dev/null; then 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 fi @@ -425,37 +435,40 @@ miflora_setup() { 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 [[ -n $INTERACTIVE ]]; then if (whiptail --title "miflora-mqtt-daemon installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 11 80); then echo "OK"; else echo "CANCELED"; return 0; fi + else + echo "OK" fi echo -n "$(timestamp) [openHABian] Setting up miflora-mqtt-daemon... " - if ! [ -d "$mifloraDir" ]; then + if ! [[ -d $mifloraDir ]]; then cond_echo "\nFresh Installation... " 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 "OK"; else echo "FAILED (copy files)"; return 1; fi else cond_echo "\nUpdate... " - if cond_redirect git -C "$mifloraDir" pull --quiet origin; then echo "OK"; else echo "FAILED (git pull)"; return 1; fi + if ! cond_redirect update_git_repo "$mifloraDir" "master"; then echo "FAILED (update git repo)"; return 1; fi fi + if cond_redirect cp "$mifloraDir"/config.{ini.dist,ini}; then echo "OK"; else echo "FAILED (copy files)"; return 1; fi cond_echo "Filesystem permissions corrections... " - if ! cond_redirect chown -R "openhab:${username:-openhabian}" "$mifloraDir"; then echo "FAILED (permissons)"; fi - if ! cond_redirect chmod -R ug+wX "$mifloraDir"; then echo "FAILED (permissons)"; fi + if ! cond_redirect chown -R "openhab:${username:-openhabian}" "$mifloraDir"; then echo "FAILED (permissions)"; return 1; fi + if ! cond_redirect chmod -R ug+wX "$mifloraDir"; then echo "FAILED (permissons)"; return 1; 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 install -m 644 "$mifloraDir"/template.service /etc/systemd/system/miflora.service; then echo "FAILED (copy service)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null 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." + if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt" /boot/config.txt; then + cond_echo "Warning! The internal RPi Bluetooth module is disabled on your system. You need to enable it before the daemon may use it." fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 16 80 fi } @@ -466,9 +479,8 @@ miflora_setup() { ## nginx_setup() ## nginx_setup() { - if [ -z "$INTERACTIVE" ]; then + if [[ -z $INTERACTIVE ]]; then echo "$(timestamp) [openHABian] Nginx setup must be run in interactive mode! Canceling Nginx setup!" - echo "CANCELED" return 0 fi @@ -489,7 +501,7 @@ nginx_setup() { local secure local validDomain - if ! [ -x "$(command -v dig)" ]; then + if ! [[ -x $(command -v dig) ]]; then echo -n "$(timestamp) [openHABian] Installing Nginx required packages (dnsutils)... " if cond_redirect apt-get install --yes dnsutils; then echo "OK"; else echo "FAILED"; return 1; fi fi @@ -501,10 +513,10 @@ nginx_setup() { validDomain="false" function comment { - if ! sed -e "/[[:space:]]$1/ s/^#*/#/g" -i "$2"; then echo "FAILED (comment)"; fi + if ! sed -e "/[[:space:]]$1/ s/^#*/#/g" -i "$2"; then echo "FAILED (comment)"; return 1; fi } function uncomment { - if ! sed -e "/$1/s/^$1//g" -i "$2"; then echo "FAILED (uncomment)"; fi + if ! sed -e "/$1/s/^$1//g" -i "$2"; then echo "FAILED (uncomment)"; return 1; fi } echo -n "$(timestamp) [openHABian] Beginning setup of Nginx as reverse proxy with authentication... " @@ -513,13 +525,13 @@ nginx_setup() { 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 + while [[ -z $nginxPass ]]; do + if ! nginxPass1=$(whiptail --title "Authentication Setup" --passwordbox "\\nEnter a password for ${nginxUsername}:" 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 + if [[ $nginxPass1 == "$nginxPass2" ]] && [[ ${#nginxPass1} -ge 8 ]] && [[ ${#nginxPass2} -ge 8 ]]; then nginxPass="$nginxPass1" else - whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 7 80 3>&1 1>&2 2>&3 + whiptail --title "Authentication Setup" --msgbox "Password mismatched, blank, or less than 8 characters... Please try again!" 7 80 fi done else @@ -531,13 +543,13 @@ nginx_setup() { 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 + if [[ $auth == "true" ]]; then authtext="Authentication Enabled\\n- Username: $nginxUsername" else authtext="Authentication Disabled" fi - if [ "$secure" == "true" ]; then + 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" @@ -549,28 +561,25 @@ nginx_setup() { echo "$(timestamp) [openHABian] Configuring Nginx network options... " cond_echo "Obtaining public IP address... " - if pubIP=$(dig -4 +short myip.opendns.com @resolver1.opendns.com | tail -1); then echo "$pubIP"; fi - if [ -z "$pubIP" ]; then - if pubIP=$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com|tr -d '"'); then echo "$pubIP"; else echo "FAILED"; return 1; fi - fi + if ! pubIP="$(get_public_ip)"; then echo "FAILED (public ip)"; return 1; fi 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 + 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 + while [[ $validDomain == "false" ]] && [[ -n $domain ]] && [[ $domain != "IP" ]]; do cond_echo "Obtaining domain IP address... " - if domainIP=$(dig -4 +short "$domain" @resolver1.opendns.com | tail -1); then echo "$domainIP"; else echo "FAILED"; return 1; fi - if [ "$pubIP" = "$domainIP" ]; then + if domainIP="$(dig -4 +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 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) + if ! 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); then echo "CANCELED"; return 0; fi fi done - if [ "$validDomain" == "false" ]; then - if [ "$domain" == "IP" ]; then + if [[ $validDomain == "false" ]]; then + if [[ $domain == "IP" ]]; then cond_echo "Setting domain to static public IP address $pubIP" domain="$pubIP" else @@ -587,11 +596,11 @@ nginx_setup() { if cond_redirect apt-get install --yes 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; then echo "FAILED (remove default)"; return 1; fi - if ! cp "$BASEDIR"/includes/nginx.conf /etc/nginx/sites-enabled/openhab; then echo "FAILED (copy configuration)"; return 1; fi - if sed -i "s/DOMAINNAME/${domain}/g" /etc/nginx/sites-enabled/openhab; then echo "OK"; else echo "FAILED (set domain name)"; fi + if ! cond_redirect rm -rf /etc/nginx/sites-enabled/default; then echo "FAILED (remove default)"; return 1; fi + if ! cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/nginx.conf /etc/nginx/sites-enabled/openhab; then echo "FAILED (copy configuration)"; return 1; fi + if cond_redirect sed -i 's|DOMAINNAME|'"${domain}"'|g' /etc/nginx/sites-enabled/openhab; then echo "OK"; else echo "FAILED (set domain name)"; return 1; fi - if [ "$auth" == "true" ]; then + 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 apache2-utils; then echo "OK"; else echo "FAILED"; return 1; fi @@ -599,12 +608,12 @@ nginx_setup() { uncomment "#AUTH" /etc/nginx/sites-enabled/openhab fi - if [ "$secure" == "true" ]; then + if [[ $secure == "true" ]]; then cond_echo "Setting up Nginx security options..." - if [ "$validDomain" == "true" ]; then + 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 ! dpkg -s 'software-properties-common' &> /dev/null; then if ! cond_redirect apt-get install --yes software-properties-common; then echo "FAILED (Ubuntu prerequsites)"; return 1; fi fi if ! cond_redirect add-apt-repository universe; then echo "FAILED (add universe repo)"; return 1; fi @@ -616,8 +625,8 @@ nginx_setup() { echo -n "$(timestamp) [openHABian] Configuring certbot... " mkdir -p /var/www/"$domain" uncomment "#WEBROOT" /etc/nginx/sites-enabled/openhab - if ! nginx -t; then echo "FAILED (Nginx configuration test)"; fi - if ! cond_redirect systemctl -q reload nginx &>/dev/null; then echo "FAILED (nginx reload)"; fi + if ! nginx -t; then echo "FAILED (nginx configuration test)"; return 1; fi + if ! cond_redirect systemctl -q reload nginx.service &> /dev/null; then echo "FAILED (nginx reload)"; return 1; 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" @@ -626,19 +635,19 @@ nginx_setup() { 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 + whiptail --title "openSSL Key Generation" --msgbox "openSSL is about to ask for information in the command line, please fill out each line." 8 80 + if ! openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout $keypath -out $certpath; then echo "FAILED (openSSL configuration)"; return 1; fi fi 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 + if ! cond_redirect sed -i "s|CERTPATH|${certpath}|g" /etc/nginx/sites-enabled/openhab; then echo "FAILED (certpath config)"; return 1; fi + if ! cond_redirect sed -i "s|KEYPATH|${keypath}|g" /etc/nginx/sites-enabled/openhab; then echo "FAILED (keypath config)"; return 1; 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; then echo "FAILED (Nginx configuration test)"; fi - if ! cond_redirect systemctl restart nginx; then echo "FAILED (Nginx restart)"; fi + if ! nginx -t; then echo "FAILED (nginx configuration test)"; return 1; fi + if ! cond_redirect systemctl restart nginx.service; then echo "FAILED (nginx restart)"; return 1; fi whiptail --title "Operation Successful!" --msgbox "Setup successful. Please try entering $protocol://$domain in a browser to test your settings." 8 80 } @@ -658,11 +667,13 @@ telldus_core_setup() { telldusDir="/opt/tdtool-improved" echo -n "$(timestamp) [openHABian] Beginning setup of Telldus Core... " - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then if (whiptail --title "Telldus Core installation?" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext" 8 80); then echo "OK"; else echo "CANCELED"; return 0; fi + else + echo "OK" fi - if is_aarch64 ; then + if is_arm; then dpkg --add-architecture armhf fi @@ -680,7 +691,7 @@ telldus_core_setup() { fi echo -n "$(timestamp) [openHABian] Installing libconfuse1... " if ! cond_redirect apt-get update; then echo "FAILED (update apt lists)"; return 1; fi - if cond_redirect apt-get install --yes --target-release "stretch" libconfuse1; then echo "OK"; else echo "FAILED"; fi + if cond_redirect apt-get install --yes --target-release "stretch" libconfuse1; then echo "OK"; else echo "FAILED"; return 1; fi if ! add_keys "https://s3.eu-central-1.amazonaws.com/download.telldus.com/debian/telldus-public.key"; then return 1; fi @@ -689,25 +700,26 @@ telldus_core_setup() { if cond_redirect apt-get update; then echo "OK"; else echo "FAILED (update apt lists)"; return 1; 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 + if cond_redirect apt-get install --yes libjna-java telldus-core; then echo "OK"; else echo "FAILED"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up telldus-core service... " - cp "$BASEDIR"/includes/telldusd.service /lib/systemd/system/telldusd.service + if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/telldusd.service /etc/systemd/system/telldusd.service; then echo "FAILED (copy service)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null if ! cond_redirect systemctl enable telldusd.service; then echo "FAILED (enable service)"; return 1; fi if cond_redirect systemctl restart telldusd.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi echo -n "$(timestamp) [openHABian] Setting up tdtool-improved... " - if ! [ -d "$telldusDir" ]; then + if ! [[ -d $telldusDir ]]; then cond_echo "\nFresh Installation... " if ! cond_redirect git clone https://github.com/EliasGabrielsson/tdtool-improved.py.git $telldusDir; then echo "FAILED (git clone)"; return 1; fi else cond_echo "\nUpdate... " - if ! cond_redirect git -C "$telldusDir" pull --quiet origin; then echo "FAILED (git pull)"; return 1; fi + if ! cond_redirect update_git_repo "$telldusDir" "master"; then echo "FAILED (update git repo)"; return 1; fi fi - chmod +x /opt/tdtool-improved/tdtool-improved.py + cond_redirect 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)"; return 1; fi - if [ -n "$INTERACTIVE" ]; then + if [[ -n $INTERACTIVE ]]; then whiptail --title "Operation Successful!" --msgbox "$successtext" 16 80 fi } diff --git a/functions/passwords.bash b/functions/passwords.bash index 0f1585b19..75565cbf9 100644 --- a/functions/passwords.bash +++ b/functions/passwords.bash @@ -8,118 +8,93 @@ ## ## change_password(String password) ## - change_password() { - introtext="Choose which services to change passwords for:" - failtext="Something went wrong in the password change process. Please report this problem in the openHAB community forum or as a openHABian GitHub issue." - - matched=false - canceled=false - allAccounts=("Linux system" "openHAB Console" "Samba" "Amanda backup") - FAILED=0 + local accounts + local chosenAccounts + local nginxUsername + local pass + local pass1 + local pass2 + local whipParams + accounts=("Linux system" "openHAB Console" "Samba" "Amanda backup") # BUILD LIST WITH INSTALLED SERVICES - whipParams=( --title "Change password function" --ok-button "Execute" --cancel-button "Back" --checklist "$introtext" 14 90 6) - whipParams+=("Linux system" "Account; \"$username\" used for login to this computer" off ) - whipParams+=("openHAB Console" "Remote console account; \"openhab\" for manage openHAB" off ) - whipParams+=("Samba" "Fileshare account; \"$username\" for configuration files" off ) - whipParams+=("Amanda backup" "User account; \"backup\" to handle openHAB backups" off ) - - if [ -f /etc/nginx/.htpasswd ]; then - nginxuser="$(cut -d: -f1 /etc/nginx/.htpasswd | head -1)" - whipParams+=("Ngnix HTTP/HTTPS" "User; \"$nginxuser\" used for logon to openHAB web services" off ) - allAccounts+=( "Ngnix HTTP/HTTPS" ) + whipParams=("Linux system" "Account: \"${username:-openhabian}\" used for login to this computer" OFF) + whipParams+=("openHAB Console" "Remote console account: \"openhab\" used for managing openHAB" OFF) + whipParams+=("Samba" "Fileshare account: \"${username:-openhabian}\" used for remote openHAB configuration" OFF) + whipParams+=("Amanda backup" "User account: \"backup\" used for managing backup configuration" OFF) + if [[ -f /etc/nginx/.htpasswd ]]; then + nginxUsername="$(cut -d: -f1 /etc/nginx/.htpasswd | head -1)" + accounts+=("Ngnix proxy") + whipParams+=("Ngnix proxy" "Nginx user: \"${nginxUsername}\" used for logging into openHAB web services" OFF) fi - - if [ -f /etc/influxdb/influxdb.conf ]; then - whipParams+=("InfluxDB" "User; \"admin\" used for database configuration " off ) - allAccounts+=( "InfluxDB" ) + if [[ -f /etc/influxdb/influxdb.conf ]]; then + accounts+=("InfluxDB") + whipParams+=("InfluxDB" "InfluxDB user: \"admin\" used for database configuration" OFF) fi - - if [ -f /etc/grafana/grafana.ini ]; then - whipParams+=("Grafana" "User; \"admin\" used for manage graphs and the server " off ) - allAccounts+=( "Grafana" ) + if [[ -f /etc/grafana/grafana.ini ]]; then + accounts+=("Grafana") + whipParams+=("Grafana" "Grafana user: \"admin\" used for managing graphs and the server" OFF) fi - if [ -n "$INTERACTIVE" ]; then - accounts="$(whiptail "${whipParams[@]}" 3>&1 1>&2 2>&3)" - exitstatus=$? + if [[ -n $INTERACTIVE ]]; then + if ! chosenAccounts="$(whiptail --title "Change password function" --checklist "\\nChoose which services to change passwords for:" 15 100 7 --ok-button "Continue" --cancel-button "Cancel" "${whipParams[@]}" 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi # COLLECT NEW PASSWORD - if [ $exitstatus = 0 ]; then - while [ "$matched" = false ] && [ "$canceled" = false ]; do - passwordChange="$(whiptail --title "Authentication Setup" --passwordbox "Enter a new password: " 15 80 3>&1 1>&2 2>&3)" - if [[ "$?" == 1 ]]; then return 0; fi - if [ ! ${#passwordChange} -ge 10 ]; then - whiptail --title "Authentication Setup" --msgbox "Password must at least be 10 characters long... Please try again!" 15 80 3>&1 1>&2 2>&3 - else - secondpasswordChange="$(whiptail --title "Authentication Setup" --passwordbox "Please confirm the new password:" 15 80 3>&1 1>&2 2>&3)" - if [[ "$?" == 1 ]]; then return 0; fi - if [ "$passwordChange" = "$secondpasswordChange" ] && [ -n "$passwordChange" ]; then - matched=true - else - whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3 - fi - fi - done - else - return 0 - fi + while [[ -z $pass ]]; do + if ! pass1=$(whiptail --title "Authentication Setup" --passwordbox "\\nEnter a new password:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if ! pass2=$(whiptail --title "Authentication Setup" --passwordbox "\\nPlease confirm the password:" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if [[ $pass1 == "$pass2" ]] && [[ ${#pass1} -ge 10 ]] && [[ ${#pass2} -ge 10 ]]; then + pass="$pass1" + else + whiptail --title "Authentication Setup" --msgbox "Password mismatched, blank, or less than 10 characters... Please try again!" 7 80 + fi + done else # NON INTERACTIVE FUNCTION INVOKED - passwordChange=$1 - accounts=allAccounts + if [[ ${#1} -le 9 ]]; then echo "FAILED (invalid password, password must be greater than 10 characters)"; return 1; fi + pass="$1" + chosenAccounts="${accounts[*]}" fi # CHANGE CHOOSEN PASSWORDS - if [[ $accounts == *"Linux system"* ]]; then - echo -n "$(timestamp) [openHABian] Changing password for linux account \"$username\"... " - echo "$username:$passwordChange" | chpasswd - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if [[ $chosenAccounts == *"Linux system"* ]]; then + echo -n "$(timestamp) [openHABian] Changing password for Linux account \"${username:-openhabian}\"... " + if echo "${username:-openhabian}:$pass" | chpasswd; then echo "OK"; else echo "FAILED"; return 1; fi fi - if [[ $accounts == *"Samba"* ]]; then - echo -n "$(timestamp) [openHABian] Changing password for samba (fileshare) account \"$username\"... " - (echo "$passwordChange"; echo "$passwordChange") | /usr/bin/smbpasswd -s -a "$username" - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if [[ $chosenAccounts == *"Samba"* ]]; then + echo -n "$(timestamp) [openHABian] Changing password for Samba (fileshare) account \"${username:-openhabian}\"... " + if (echo "$pass"; echo "$pass") | smbpasswd -s -a "${username:-openhabian}" &> /dev/null; then echo "OK"; else echo "FAILED"; return 1; fi fi - if [[ $accounts == *"openHAB Console"* ]]; then + if [[ $chosenAccounts == *"openHAB Console"* ]]; then echo -n "$(timestamp) [openHABian] Changing password for openHAB console account \"openhab\"... " - sed -i "s/openhab = .*,/openhab = $passwordChange,/g" /var/lib/openhab2/etc/users.properties + if sed -i 's|openhab = .*,|openhab = '"${pass}"',|g' /var/lib/openhab2/etc/users.properties; then echo "OK"; else echo "FAILED"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null cond_redirect systemctl restart openhab2.service - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi fi - if [[ $accounts == *"Amanda backup"* ]]; then - echo -n "$(timestamp) [openHABian] Changing password for linux account \"backup\"... " - echo "backup:$passwordChange" | chpasswd - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if [[ $chosenAccounts == *"Amanda backup"* ]]; then + echo -n "$(timestamp) [openHABian] Changing password for Linux account \"backup\"... " + if echo "backup:$pass" | chpasswd; then echo "OK"; else echo "FAILED"; return 1; fi fi - if [[ $accounts == *"Ngnix HTTP/HTTPS"* ]]; then - echo -n "$(timestamp) [openHABian] Changing password for nginx web authentication account \"$nginxuser\"... " - echo "$passwordChange" | htpasswd -i /etc/nginx/.htpasswd "$nginxuser" - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if [[ $chosenAccounts == *"Ngnix proxy"* ]]; then + echo -n "$(timestamp) [openHABian] Changing password for Nginx web authentication account \"${nginxUsername}\"... " + if echo "$pass" | htpasswd -i /etc/nginx/.htpasswd "${nginxUsername}"; then echo "OK"; else echo "FAILED"; return 1; fi fi - if [[ $accounts == *"InfluxDB"* ]]; then + if [[ $chosenAccounts == *"InfluxDB"* ]]; then echo -n "$(timestamp) [openHABian] Changing password for InfluxDB administration account \"admin\"... " sed -i "s/.*auth-enabled = .*/ auth-enabled = false/g" /etc/influxdb/influxdb.conf - cond_redirect systemctl restart influxdb.service + if ! cond_redirect systemctl restart influxdb.service; then echo "FAILED (InfluxDB restart)"; return 1; fi sleep 1 - influx -execute "SET PASSWORD FOR admin = '$passwordChange'" + influx -execute "SET PASSWORD FOR admin = '$pass'" sed -i "s/.*auth-enabled = .*/ auth-enabled = true/g" /etc/influxdb/influxdb.conf - cond_redirect systemctl restart influxdb.service - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if cond_redirect systemctl restart influxdb.service; then echo "OK"; else echo "FAILED (InfluxDB restart)"; return 1; fi fi - if [[ $accounts == *"Grafana"* ]]; then + if [[ $chosenAccounts == *"Grafana"* ]]; then echo -n "$(timestamp) [openHABian] Changing password for Grafana admininistration account \"admin\"... " - grafana-cli admin reset-admin-password --homepath "/usr/share/grafana" --config "/etc/grafana/grafana.ini" "$passwordChange" - if [ $FAILED -eq 0 ]; then echo "OK"; else echo "FAILED"; fi + if grafana-cli admin reset-admin-password --homepath "/usr/share/grafana" --config "/etc/grafana/grafana.ini" "$pass"; then echo "OK"; else echo "FAILED"; return 1; fi fi - - if [ -n "$INTERACTIVE" ]; then - if [ $FAILED -eq 0 ]; then - whiptail --title "Operation Successful!" --msgbox "Password successfully set for: $accounts" 15 80 - else - whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60 - fi + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Operation Successful!" --msgbox "Password(s) successfully changed for: $chosenAccounts" 8 80 fi } diff --git a/functions/system.bash b/functions/system.bash index 6bee11349..915dea934 100644 --- a/functions/system.bash +++ b/functions/system.bash @@ -1,353 +1,494 @@ #!/usr/bin/env bash +## Function for checking for and installing whiptail. +## This enables the diplay of the interactive menu used by openHABian. +## +## whiptail_check() +## whiptail_check() { - if ! command -v whiptail &>/dev/null; then + if ! [[ -x $(command -v whiptail) ]]; then echo -n "$(timestamp) [openHABian] Installing whiptail... " if cond_redirect apt-get install --yes whiptail; then echo "OK"; else echo "FAILED"; exit 1; fi fi } +## Function for upgrading installed packages from apt. +## Additionally, this also updates Java if an update is available. +## +## system_upgrade() +## system_upgrade() { echo -n "$(timestamp) [openHABian] Updating repositories and upgrading installed packages... " - cond_redirect apt-get --yes upgrade - # shellcheck disable=SC2154 - if cond_redirect java_install_or_update "$java_opt"; then echo "OK"; else echo "FAILED"; return 1; fi + if ! cond_redirect apt-get upgrade --yes; then echo "FAILED"; return 1; fi + if cond_redirect java_install_or_update "${java_opt:-Zulu8-32}"; then echo "OK"; else echo "FAILED"; return 1; fi } +## Function for installing basic Linux packages. +## +## basic_packages() +## basic_packages() { echo -n "$(timestamp) [openHABian] Installing basic can't-be-wrong packages (screen, vim, ...)... " - apt-get remove -y raspi-config &>/dev/null || true - if cond_redirect apt-get install --yes screen vim nano mc vfu bash-completion htop curl wget multitail git util-linux \ - bzip2 zip unzip xz-utils software-properties-common man-db whiptail acl usbutils dirmngr arping; \ - then echo "OK"; else echo "FAILED"; return 1; fi + if [[ -x $(command -v raspi-config) ]]; then + if ! cond_redirect apt-get purge --yes raspi-config; then echo "FAILED (remove raspi-config)"; return 1; fi + fi + if cond_redirect apt-get install --yes screen vim nano mc vfu bash-completion \ + htop curl wget multitail git util-linux bzip2 zip unzip xz-utils \ + software-properties-common man-db whiptail acl usbutils dirmngr arping; \ + then echo "OK"; else echo "FAILED"; exit 1; fi } +## Function for installing additional needed Linux packages. +## +## needed_packages() +## needed_packages() { + local bluetoothPackages + + bluetoothPackages="bluez python3-dev libbluetooth-dev raspberrypi-sys-mods pi-bluetooth" + # Install apt-transport-https - update packages through https repository # Install bc + sysstat - needed for FireMotD # Install avahi-daemon - hostname based discovery on local networks - # Install python/python3-pip - for python packages + # Install python3/python3-pip/python3-wheel/python3-setuptools - for python packages echo -n "$(timestamp) [openHABian] Installing additional needed packages... " - if cond_redirect apt-get install --yes apt-transport-https bc sysstat avahi-daemon python3 python3-pip avahi-autoipd fontconfig; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect apt-get install --yes apt-transport-https bc sysstat \ + avahi-daemon python3 python3-pip python3-wheel python3-setuptools \ + avahi-autoipd fontconfig; \ + then echo "OK"; else echo "FAILED"; return 1; fi if is_pizerow || is_pithree || is_pithreeplus || is_pifour; then echo -n "$(timestamp) [openHABian] Installing additional bluetooth packages... " - local BTPKGS - BTPKGS="bluez python3-dev libbluetooth-dev raspberrypi-sys-mods pi-bluetooth" - # phython3-bluez not available in stretch, but in newer distros + # phython3-bluez is not available in stretch so only add it if we are runnning on buster or later if ! is_stretch; then - BTPKGS="$BTPKGS python3-bluez" + bluetoothPackages+=" python3-bluez" fi # shellcheck disable=SC2086 - if cond_redirect apt-get install --yes $BTPKGS; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect apt-get install --yes $bluetoothPackages; then echo "OK"; else echo "FAILED"; return 1; fi fi } +## Function for setting the timezone of the current system. +## +## The timezone setting will default to the users choice on an INTERACTIVE setup, +## or it will use whatever value is provided in the openhabian.conf file. +## +## As a last resort, it will default to using the IP geolocation of the user to +## determine the current timezone. +## +## timezone_setting() +## timezone_setting() { - # shellcheck disable=SC2154 - if [ -n "$INTERACTIVE" ]; then + # shellcheck source=/etc/openhabian.conf disable=SC2154 + if [[ -n $INTERACTIVE ]]; then echo -n "$(timestamp) [openHABian] Setting timezone based on user choice... " - dpkg-reconfigure tzdata - elif [ -n "${timezone+x}" ]; then + if dpkg-reconfigure tzdata; then echo "OK ($(cat /etc/timezone))"; else echo "FAILED"; return 1; fi + elif [[ -n $timezone ]]; then echo -n "$(timestamp) [openHABian] Setting timezone based on openhabian.conf... " - cond_redirect timedatectl set-timezone "$timezone" + if cond_redirect timedatectl set-timezone "$timezone"; then echo "OK ($(cat /etc/timezone))"; else echo "FAILED"; return 1; fi else - echo -n "$(timestamp) [openHABian] Setting timezone based on IP geolocation... " - if ! command -v tzupdate &>/dev/null; then - cond_redirect apt-get install --yes python3-pip python3-wheel python3-setuptools - if ! cond_redirect pip3 install --upgrade tzupdate; then echo "FAILED (pip3)"; return 1; fi + echo "$(timestamp) [openHABian] Beginning setup of timezone based on IP geolocation... OK" + if ! dpkg -s 'python3' 'python3-pip' 'python3-wheel' 'python3-setuptools' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing Python for needed packages... " + if cond_redirect apt-get install --yes python3 python3-pip python3-wheel python3-setuptools; then echo "OK"; else echo "FAILED"; return 1; fi fi - cond_redirect pip3 install --upgrade tzupdate - cond_redirect tzupdate + echo -n "$(timestamp) [openHABian] Setting timezone based on IP geolocation... " + if ! cond_redirect pip3 install --upgrade tzupdate; then echo "FAILED (update tzupdate)"; return 1; fi + if cond_redirect tzupdate; then echo "OK ($(cat /etc/timezone))"; else echo "FAILED"; return 1; fi fi - # shellcheck disable=SC2181 - if [ $? -eq 0 ]; then echo -e "OK ($(cat /etc/timezone))"; else echo "FAILED"; return 1; fi } -## enable time synchronization via systemd-timesyncd to NTP servers obtained via DHCP +## Enable time synchronization via systemd-timesyncd to NTP servers obtained via DHCP ## RPis have no RTC (hw clock) +## Valid arguments: "enable" or "disable" ## -## enable_ntp() +## setup_ntp(String option) ## -enable_ntp() { - cp "$BASEDIR"/includes/50-timesyncd.conf /lib/dhcpcd/dhcpcd-hooks/ - timedatectl set-ntp true +setup_ntp() { + if running_in_docker; then echo "$(timestamp) [openHABian] Enabling time synchronization using NTP... SKIPPED"; return 0; fi + + if [[ $1 == "enable" ]]; then + echo -n "$(timestamp) [openHABian] Enabling time synchronization using NTP... " + if ! cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/50-timesyncd.conf /lib/dhcpcd/dhcpcd-hooks/; then echo "FAILED (copy)"; return 1; fi + if cond_redirect timedatectl set-ntp true; then echo "OK"; else echo "FAILED (enable)"; return 1; fi + elif [[ $1 == "disable" ]]; then + echo -n "$(timestamp) [openHABian] Disabling time synchronization using NTP... " + if ! cond_redirect rm -f /lib/dhcpcd/dhcpcd-hooks/50-timesyncd.conf; then echo "FAILED (delete)"; return 1; fi + if cond_redirect timedatectl set-ntp false; then echo "OK"; else echo "FAILED (disable)"; return 1; fi + fi } +## Function for setting the locale of the current system. +## +## The locale setting will default to the users choice on an INTERACTIVE setup, +## or it will use whatever value is provided in the openhabian.conf file. +## +## locale_setting() +## locale_setting() { - cond_redirect apt-get -q -y install locales - if [ -n "$INTERACTIVE" ]; then - echo "$(timestamp) [openHABian] Setting locale based on user choice... " - dpkg-reconfigure locales - loc=$(grep "^[[:space:]]*LANG=" /etc/default/locale | sed 's/LANG=//g') - cond_redirect update-locale LANG="$loc" LC_ALL="$loc" LC_CTYPE="$loc" LANGUAGE="$loc" - whiptail --title "Change Locale" --msgbox "For the locale change to take effect, please reboot your system now." 10 60 - return 0 + local locale + + if ! dpkg -s 'locales' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing locales from apt... " + if cond_redirect apt-get install --yes locales; then echo "OK"; else echo "FAILED"; return 1; fi fi - echo -n "$(timestamp) [openHABian] Setting locale based on openhabian.conf... " - if is_ubuntu; then - # shellcheck disable=2086,2154 - cond_redirect locale-gen $locales + if [[ -n $INTERACTIVE ]]; then + echo -n "$(timestamp) [openHABian] Setting locale based on user choice... " + if ! dpkg-reconfigure locales; then echo "FAILED (reconfigure locales)"; return 1; fi else - touch /etc/locale.gen - for loc in $locales; do sed -i "/$loc/s/^# //g" /etc/locale.gen; done - cond_redirect locale-gen + echo -n "$(timestamp) [openHABian] Setting locale based on openhabian.conf... " + # shellcheck disable=SC2154,SC2086 + if is_ubuntu; then + if ! cond_redirect locale-gen $locales; then echo "FAILED (locale-gen)"; return 1; fi + else + for loc in $locales; do + sed -i '/^#[[:space:]]'"${loc}"'/s/^#[[:space:]]//' /etc/locale.gen + done + if ! cond_redirect locale-gen; then echo "FAILED (locale-gen)"; return 1; fi + fi + if ! cond_redirect dpkg-reconfigure --frontend=noninteractive locales; then echo "FAILED (reconfigure locales)"; return 1; fi + fi + + if ! locale="$(grep "^[[:space:]]*LANG=" /etc/default/locale | sed 's|LANG=||g')"; then echo "FAILED"; return 1; fi + if cond_redirect update-locale LANG="${locale:-${system_default_locale:-en_US.UTF-8}}" LC_ALL="${locale:-${system_default_locale:-en_US.UTF-8}}" LC_CTYPE="${locale:-${system_default_locale:-en_US.UTF-8}}" LANGUAGE="${locale:-${system_default_locale:-en_US.UTF-8}}"; then echo "OK (Reboot needed)"; else echo "FAILED"; return 1; fi + + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Change Locale" --msgbox "For the locale change to take effect, please reboot your system now." 7 80 fi - cond_redirect dpkg-reconfigure --frontend=noninteractive locales - cond_redirect "LANG=${system_default_locale:-en_US.UTF-8}" - cond_redirect "LC_ALL=${system_default_locale:-en_US.UTF-8}" - cond_redirect "LC_CTYPE=${system_default_locale:-en_US.UTF-8}" - cond_redirect "LANGUAGE=${system_default_locale:-en_US.UTF-8}" - export LANG LC_ALL LC_CTYPE LANGUAGE - if cond_redirect update-locale LANG="$system_default_locale" LC_ALL="$system_default_locale" LC_CTYPE="$system_default_locale" LANGUAGE="$system_default_locale"; then echo "OK"; else echo "FAILED"; fi } +## Function for setting the hostname of the current system. +## +## The hostname setting will default to the users choice on an INTERACTIVE setup, +## or it will use whatever value is provided in the openhabian.conf file. +## +## hostname_change() +## hostname_change() { - echo -n "$(timestamp) [openHABian] Setting hostname of the base system... " - if [ -n "$INTERACTIVE" ]; then - if ! new_hostname=$(whiptail --title "Change Hostname" --inputbox "Please enter the new system hostname (no special characters, no spaces):" 10 60 3>&1 1>&2 2>&3); then return 1; fi - if ( echo "$new_hostname" | grep -q ' ' ) || [ -z "$new_hostname" ]; then - whiptail --title "Change Hostname" --msgbox "The hostname you've entered is not a valid hostname. Please try again." 10 60 + if running_in_docker; then echo "$(timestamp) [openHABian] Setting hostname of the base system... SKIPPED"; return 0; fi + + local newHostname + + if [[ -n $INTERACTIVE ]]; then + echo -n "$(timestamp) [openHABian] Setting hostname of the base system based on user choice... " + if ! newHostname=$(whiptail --title "Change Hostname" --inputbox "\\nPlease enter the new system hostname (no special characters, no spaces):" 9 80 3>&1 1>&2 2>&3); then echo "FAILED"; return 1; fi + if [[ -z $newHostname ]] || ( echo "$newHostname" | grep -q ' ' ); then + whiptail --title "Change Hostname" --msgbox "The hostname you have entered is not a valid hostname. Please try again." 7 80 echo "FAILED" return 1 fi else - new_hostname="${hostname:-openhab}" + echo -n "$(timestamp) [openHABian] Setting hostname of the base system based on openhabian.conf... " + newHostname="${hostname:-openhab}" fi - hostnamectl set-hostname "$new_hostname" &>/dev/null - hostname "$new_hostname" &>/dev/null - echo "$new_hostname" > /etc/hostname - TMP="$(mktemp /tmp/openhabian.XXXXX)" - sed "s/127.0.1.1.*/127.0.1.1 $new_hostname/g" /etc/hosts >"$TMP" && cp "$TMP" /etc/hosts - rm -f "$TMP" - - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Change Hostname" --msgbox "For the hostname change to take effect, please reboot your system now." 10 60 + + if ! cond_redirect hostnamectl set-hostname "$newHostname"; then echo "FAILED (hostnamectl)"; return 1; fi + if sed -i 's|127.0.1.1.*$|127.0.1.1 '"${newHostname}"'|g' /etc/hosts; then echo "OK"; else echo "FAILED (edit hosts)"; return 1; fi + + if [[ -n $INTERACTIVE ]]; then + whiptail --title "Change Hostname" --msgbox "For the hostname change to take effect, please reboot your system now." 7 80 fi - echo "OK" } +## Function for adding tuned bash configuration files to the current system. +## +## bashrc_copy() +## bashrc_copy() { - echo -n "$(timestamp) [openHABian] Adding slightly tuned bash config files to system... " - cp "$BASEDIR"/includes/bash.bashrc /etc/bash.bashrc - cp "$BASEDIR"/includes/bashrc-root /root/.bashrc - cp "$BASEDIR"/includes/bash_profile /home/"${username:-openhabian}"/.bash_profile - chown "$username:$username" /home/"$username"/.bash_profile - echo "OK" + echo -n "$(timestamp) [openHABian] Adding slightly tuned bash configuration files to system... " + if ! cp "${BASEDIR:-/opt/openhabian}"/includes/bash.bashrc /etc/bash.bashrc; then echo "FAILED (user bashrc)"; return 1; fi + if ! cp "${BASEDIR:-/opt/openhabian}"/includes/bashrc-root /root/.bashrc; then echo "FAILED (root bashrc)"; return 1; fi + if ! cp "${BASEDIR:-/opt/openhabian}"/includes/bash_profile /home/"${username:-openhabian}"/.bash_profile; then echo "FAILED (user bash_profile)"; return 1; fi + if chown "${username:-openhabian}:${username:-openhabian}" /home/"${username:-openhabian}"/.bash_profile; then echo "OK"; else echo "FAILED (permissions)"; return 1; fi } +## Function for adding a tuned vim configuration file to the current system. +## +## vimrc_copy() +## vimrc_copy() { - echo -n "$(timestamp) [openHABian] Adding slightly tuned vim config file to system... " - cp "$BASEDIR/includes/vimrc" /etc/vim/vimrc - echo "OK" + echo -n "$(timestamp) [openHABian] Adding slightly tuned vim configuration file to system... " + if cp "${BASEDIR:-/opt/openhabian}/includes/vimrc" /etc/vim/vimrc; then echo "OK"; else echo "FAILED"; return 1; fi } +## Function for adding a mountpoint to the system. +## +## create_mount(String source, String destination) +## create_mount() { - F=/etc/systemd/system/$(systemd-escape --path /srv/openhab2-"${2}").mount - sed -e "s|%SRC|$1|g" -e "s|%DEST|$2|g" "$BASEDIR"/includes/mount_template > "$F" - systemctl -q enable "srv-openhab2\\x2d$2.mount" - systemctl -q start "srv-openhab2\\x2d$2.mount" + # Docker systemctl replacement does not support mount services + if running_in_docker; then + echo "$(timestamp) [openHABian] Creating mount $2 in '/srv/openhab2-${1}'... SKIPPED" + return 0 + fi + + local destination + local mountPoint + local source + + destination="$2" + mountPoint="$(systemd-escape --path "/srv/openhab2-${destination}" --suffix "mount")" + source="$1" + + echo -n "$(timestamp) [openHABian] Creating mount $source in '/srv/openhab2-${destination}'... " + if ! sed -e 's|%SRC|'"${source}"'|g; s|%DEST|'"${destination}"'|g' "${BASEDIR:-/opt/openhabian}"/includes/mount_template > /etc/systemd/system/"$mountPoint"; then echo "FAILED (sed)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null + if ! cond_redirect systemctl enable "$mountPoint"; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart "$mountPoint"; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi } +## Function for adding openHAB folder mountpoints to the /srv/ folder. +## +## srv_bind_mounts() +## srv_bind_mounts() { - echo -n "$(timestamp) [openHABian] Preparing openHAB folder mounts under /srv/... " - cond_redirect systemctl is-active --quiet smbd && systemctl stop smbd - cond_redirect systemctl is-active --quiet zram-config && systemctl stop zram-config + echo -n "$(timestamp) [openHABian] Preparing openHAB folder mounts under '/srv/openhab2-*'... " + if [[ -f /etc/samba/smb.conf ]] && [[ $(systemctl is-active --quiet smbd) ]]; then + cond_redirect systemctl stop smbd + fi + if [[ -f /etc/ztab ]] && [[ $(systemctl is-active --quiet zram-config) ]]; then + cond_redirect systemctl stop zram-config + fi + cond_redirect umount -q /srv/openhab2-{sys,conf,userdata,logs,addons} - cond_redirect rm -f /etc/systemd/system/srv*.mount - cond_redirect mkdir -p /srv/openhab2-{sys,conf,userdata,logs,addons} - cond_redirect cp "$BASEDIR"/includes/srv_readme.txt /srv/README.txt - cond_redirect chmod ugo+w /srv /srv/README.txt - - cond_redirect create_mount /usr/share/openhab2 sys - cond_redirect create_mount /etc/openhab2 conf - cond_redirect create_mount /var/lib/openhab2 userdata - cond_redirect create_mount /var/log/openhab2 logs - cond_redirect create_mount /usr/share/openhab2/addons addons - - if [ -f /etc/ztab ]; then systemctl start zram-config; fi - if [ -f /etc/samba/smb.conf ]; then systemctl start smbd; fi - echo "OK" + if ! cond_redirect rm -f /etc/systemd/system/srv*.mount; then echo "FAILED (clean mounts)"; return 1; fi + if ! cond_redirect mkdir -p /srv/openhab2-{sys,conf,userdata,logs,addons}; then echo "FAILED (prepare dirs)"; return 1; fi + if ! cond_redirect cp "${BASEDIR:-/opt/openhabian}"/includes/srv_readme.txt /srv/README.txt; then echo "FAILED (copy readme)"; return 1; fi + if ! cond_redirect chmod ugo+w /srv /srv/README.txt; then echo "FAILED (permissions for readme)"; return 1; fi + + if ! cond_redirect create_mount "/usr/share/openhab2" "sys"; then echo "FAILED (sys)"; return 1; fi + if ! cond_redirect create_mount "/etc/openhab2" "conf"; then echo "FAILED (conf)"; return 1; fi + if ! cond_redirect create_mount "/var/lib/openhab2" "userdata"; then echo "FAILED (userdata)"; return 1; fi + if ! cond_redirect create_mount "/var/log/openhab2" "logs"; then echo "FAILED (logs)"; return 1; fi + if cond_redirect create_mount "/usr/share/openhab2/addons" "addons"; then echo "OK"; else echo "FAILED (addons)"; return 1; fi + + if [[ -f /etc/ztab ]]; then systemctl restart zram-config; fi + if [[ -f /etc/samba/smb.conf ]]; then systemctl restart smbd; fi } +## Function for applying common user account permission settings to system folders. +## +## permissions_corrections() +## permissions_corrections() { + local openhabFolders + + openhabFolders=(/etc/openhab2 /var/lib/openhab2 /var/log/openhab2 /usr/share/openhab2) + echo -n "$(timestamp) [openHABian] Applying file permissions recommendations... " - if ! id -u openhab &>/dev/null; then - echo "FAILED (please execute after openHAB was installed)" + if ! id -u openhab &> /dev/null; then + echo "FAILED (please execute after openHAB is installed)" return 1 fi for pGroup in audio bluetooth dialout gpio tty do - if getent group "$pGroup" > /dev/null 2>&1 ; then - cond_redirect adduser openhab "$pGroup" - cond_redirect adduser "$username" "$pGroup" + if getent group "$pGroup" &> /dev/null ; then + if ! cond_redirect adduser --quiet openhab "$pGroup"; then echo "FAILED (openhab ${pGroup})"; return 1; fi + if ! cond_redirect adduser --quiet "${username:-openhabian}" "$pGroup"; then echo "FAILED (${username:-openhabian} ${pGroup})"; return 1; fi fi done - cond_redirect adduser "$username" openhab - - openhab_folders=(/etc/openhab2 /var/lib/openhab2 /var/log/openhab2 /usr/share/openhab2) - cond_redirect chown openhab:openhab /srv /srv/README.txt /opt - cond_redirect chmod ugo+w /srv /srv/README.txt - cond_redirect chown -R openhab:openhab "${openhab_folders[@]}" - cond_redirect chmod -R ug+wX /opt "${openhab_folders[@]}" - cond_redirect chown -R "$username:$username" "/home/$username" - - cond_redirect setfacl -R --remove-all "${openhab_folders[@]}" - cond_redirect setfacl -R -m g::rwX "${openhab_folders[@]}" - if cond_redirect setfacl -R -m d:g::rwX "${openhab_folders[@]}"; then echo "OK"; else echo "FAILED"; fi + if ! cond_redirect adduser --quiet "${username:-openhabian}" openhab; then echo "FAILED (${username:-openhabian} openhab)"; return 1; fi + + if ! cond_redirect chown openhab:openhab /srv /srv/README.txt /opt; then echo "FAILED (openhab server mounts)"; return 1; fi + if ! cond_redirect chmod ugo+w /srv /srv/README.txt; then echo "FAILED (server mounts)"; return 1; fi + if ! cond_redirect chown -R openhab:openhab "${openhabFolders[@]}"; then echo "FAILED (openhab folders)"; return 1; fi + if ! cond_redirect chmod -R ug+wX /opt "${openhabFolders[@]}"; then echo "FAILED (folders)"; return 1; fi + if ! cond_redirect chown -R "${username:-openhabian}:${username:-openhabian}" "/home/${username:-openhabian}"; then echo "FAILED (${username:-openhabian} own $HOME)"; return 1; fi + + if ! cond_redirect setfacl -R --remove-all "${openhabFolders[@]}"; then echo "FAILED (reset file access)"; return 1; fi + if ! cond_redirect setfacl -R -m g::rwX "${openhabFolders[@]}"; then echo "FAILED (set file access)"; return 1; fi + if cond_redirect setfacl -R -m d:g::rwX "${openhabFolders[@]}"; then echo "OK"; else echo "FAILED"; return 1; fi } +## Function for applying miscellaneous system settings. +## +## misc_system_settings() +## misc_system_settings() { echo -n "$(timestamp) [openHABian] Applying miscellaneous system settings... " - cond_redirect setcap 'cap_net_raw,cap_net_admin=+eip cap_net_bind_service=+ep' "$(realpath /usr/bin/java)" - cond_redirect setcap 'cap_net_raw,cap_net_admin=+eip cap_net_bind_service=+ep' /usr/sbin/arping - # user home note - echo -e "This is your linux user's \"home\" folder.\\nPlace personal files, programs or scripts here." > "/home/$username/README.txt" - # prepare SSH key file for the end user - mkdir -p /home/"$username"/.ssh - chmod 700 /home/"$username"/.ssh - touch /home/"$username"/.ssh/authorized_keys - chmod 600 /home/"$username"/.ssh/authorized_keys - chown -R "$username:$username" /home/"$username"/.ssh + # Set Java and arping file capabilites + cond_echo "Setting Java and arping file capabilites" + if ! cond_redirect setcap 'cap_net_raw,cap_net_admin=+eip cap_net_bind_service=+ep' "$(realpath /usr/bin/java)"; then echo "FAILED (setcap java)"; return 1; fi + if ! cond_redirect setcap 'cap_net_raw,cap_net_admin=+eip cap_net_bind_service=+ep' /usr/sbin/arping; then echo "FAILED (setcap arping)"; return 1; fi + + # Add README.txt note to the end user's home folder + cond_echo "Creating a README note for end user's home folder" + echo -e "This is your linux user's \"home\" folder.\\nPlace personal files, programs or scripts here." > "/home/${username:-openhabian}/README.txt" + + # Create a SSH key file for the end user + cond_echo "Creating SSH key files" + if ! cond_redirect mkdir -p /home/"${username:-openhabian}"/.ssh; then echo "FAILED (create .ssh)"; return 1; fi + if ! cond_redirect chmod 700 /home/"${username:-openhabian}"/.ssh; then echo "FAILED (set .ssh permissions)"; return 1; fi + if ! cond_redirect touch /home/"${username:-openhabian}"/.ssh/authorized_keys; then echo "FAILED (create authorized_keys)"; return 1; fi + if ! cond_redirect chmod 600 /home/"${username:-openhabian}"/.ssh/authorized_keys; then echo "FAILED (set authorized_keys permissions)"; return 1; fi + if ! cond_redirect chown -R "${username:-openhabian}:${username:-openhabian}" /home/"${username:-openhabian}"/.ssh; then echo "FAILED (chown .ssh)"; return 1; fi + # By default, systemd logs are kept in volatile memory. Relocate to persistent memory to allow log rotation and archiving - cond_redirect echo "Creating persistent systemd journal folder location: /var/log/journal" - mkdir -p /var/log/journal - systemd-tmpfiles --create --prefix /var/log/journal - cond_redirect echo "Keeping at most 30 days of systemd journal entries" - journalctl -q --vacuum-time=30d + cond_echo "Creating persistent systemd journal folder location: /var/log/journal" + if ! cond_redirect mkdir -p /var/log/journal; then echo "FAILED (create /var/log/journal)"; return 1; fi + if ! cond_redirect systemd-tmpfiles --create --prefix /var/log/journal; then echo "FAILED (systemd-tmpfiles)"; return 1; fi + cond_echo "Keeping at most 30 days of systemd journal entries" + if ! cond_redirect journalctl --vacuum-time=30d; then echo "FAILED (journalctl)"; return 1; fi + # A distinguishable apt User-Agent - echo "Acquire { http::User-Agent \"Debian APT-HTTP/1.3 openHABian\"; };" > /etc/apt/apt.conf.d/02useragent - # - echo "OK" + cond_echo "Setting a distinguishable apt User-Agent" + if echo "Acquire { http::User-Agent \"Debian APT-HTTP/1.3 openHABian\"; };" > /etc/apt/apt.conf.d/02useragent; then echo "OK"; else echo "FAILED (apt User-Agent)"; return 1; fi } -## change system swap size dependent on free space on / -## swap on SD (/var/swap per default) only used after ZRAM swap full if that exists +## Change system swap size dependent on free space on '/swap' on SD +## ('/var/swap' per default) only used after ZRAM swap is full if ZRAM is enabled. ## ## change_swapsize(int size in MB) ## change_swapsize() { - local totalMemory + if ! is_pi; then return 0; fi - if ! is_raspbian; then return 0; fi + local free + local minFree + local swap + local totalMemory totalMemory="$(grep MemTotal /proc/meminfo | awk '{print $2}')" - if [ -z "$totalMemory" ]; then return 1; fi - - swap=$((2*totalMemory)) - minfree=$((2*swap)) - free=$(df -hk / | awk '/dev/ { print $4 }') - if [ "$free" -ge "$minfree" ]; then + if [[ -z $totalMemory ]]; then return 1; fi + swap="$((2*totalMemory))" + minFree="$((2*swap))" + free="$(df -hk / | awk '/dev/ { print $4 }')" + if [[ $free -ge "$minFree" ]]; then size=$swap - elif [ "$free" -ge "$swap" ]; then + elif [[ $free -ge "$swap" ]]; then size=$totalMemory else return 0 fi ((size/=1024)) - echo "$(timestamp) [openHABian] Adjusting swap size to $size MB ... OK" - # shellcheck disable=SC2086 - sed -i 's/^#*.*CONF_SWAPSIZE=.*/CONF_SWAPSIZE='"$size"'/g' /etc/dphys-swapfile + echo -n "$(timestamp) [openHABian] Adjusting swap size to $size MB... " + if ! cond_redirect dphys-swapfile swapoff; then echo "FAILED (swapoff)"; return 1; fi + if ! cond_redirect sed -i 's|^#*.*CONF_SWAPSIZE=.*$|CONF_SWAPSIZE='"${size}"'|g' /etc/dphys-swapfile; then echo "FAILED (swapfile)"; return 1; fi + if cond_redirect dphys-swapfile swapon; then echo "OK (Reboot needed)"; else echo "FAILED (swapon)"; return 1; fi } -# RPi specific function +## Reduce the RPi GPU memory to the minimum to allow for the system to utilize +## the maximum amount of memory for Linux operations. +## +## memory_split() +## memory_split() { + if ! is_pi; then return 0; fi echo -n "$(timestamp) [openHABian] Setting the GPU memory split down to 16MB for headless system... " if grep -qs "^[[:space:]]*gpu_mem" /boot/config.txt; then - sed -i 's/gpu_mem=.*/gpu_mem=16/g' /boot/config.txt + if cond_redirect sed -i 's|gpu_mem=.*$|gpu_mem=16|g' /boot/config.txt; then echo "OK"; else echo "FAILED"; return 1; fi else - echo "gpu_mem=16" >> /boot/config.txt + if echo "gpu_mem=16" >> /boot/config.txt; then echo "OK"; else echo "FAILED"; return 1; fi fi - echo "OK" } -# RPi specific function +## Enable audio output on the RPi +## +## enable_rpi_audio() +## enable_rpi_audio() { + if ! is_pi; then return 0; fi echo -n "$(timestamp) [openHABian] Enabling Audio output... " - if ! grep -q "^[[:space:]]*dtparam=audio" /boot/config.txt; then - echo "dtparam=audio=on" >> /boot/config.txt + if grep -qs "^[[:space:]]*dtparam=audio" /boot/config.txt; then + if ! cond_redirect sed -i 's|dtparam=audio.*$|dtparam=audio=on|g' /boot/config.txt; then echo "FAILED"; return 1; fi + else + if ! echo "dtparam=audio=on" >> /boot/config.txt; then echo "FAILED"; return 1; fi fi - cond_redirect adduser "$username" audio - echo "OK" + if cond_redirect adduser "${username:-openhabian}" audio; then echo "OK"; else echo "FAILED"; return 1; fi } +## Configure serial port options on the RPi +## +## prepare_serial_port() +## prepare_serial_port() { - introtext="Proceeding with this routine, the serial console normally provided by a Raspberry Pi can be disabled for the sake of a usable serial port. The provided port can henceforth be used by devices like RaZberry, UZB or Busware SCC. -On a Raspberry Pi 3 and 4 the Bluetooth module should be disabled to ensure the operation of a RaZberry or other HAT. Usage of BT and HATs to use serial is mutually exclusive. -\\nPlease make your choice:" -# 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="All done. After a reboot the serial console will be available via /dev/ttyAMA0 or /dev/ttyS0 (depends on your device)." - # \nThis might be a good point in time to update your Raspberry Pi firmware (if this is a RPi) and reboot:\n - # sudo rpi-update - # sudo reboot" + if ! is_pi; then return 0; fi + if [[ -z $INTERACTIVE ]]; then + echo "$(timestamp) [openHABian] Serial port setup must be run in interactive mode! Canceling Serial port setup!" + return 0 + fi - echo -n "$(timestamp) [openHABian] Configuring serial console for serial port peripherals... " + local introText + local successText + local optionOne + local optionTwo + local selection + introText="\\nProceeding with this routine, the serial console normally provided by a Raspberry Pi can be disabled for the sake of a usable serial port. The provided port can henceforth be used by devices like RaZberry, UZB or Busware SCC.\\n\\nOn a Raspberry Pi 3 or 4 the Bluetooth module should be disabled to ensure the proper operation of a RaZberry or other HAT. Usage of Bluetooth and HATs that use serial is mutually exclusive.\\n\\nPlease make your choice:" + successText="The serial options have successfully been configured!\\n\\nPlease reboot for changes to take effect." # Find current settings - if is_pi && grep -q "^[[:space:]]*enable_uart=1" /boot/config.txt; then sel_1="ON"; else sel_1="OFF"; fi - if is_pithree || is_pithreeplus && grep -q "^[[:space:]]*dtoverlay=pi3-miniuart-bt" /boot/config.txt; then sel_2="ON"; else sel_2="OFF"; fi - - if [ -n "$INTERACTIVE" ]; then - if ! selection=$(whiptail --title "Prepare Serial Port" --checklist --separate-output "$introtext" 20 78 3 \ - "1" "(RPi) Disable serial console (RaZberry, SCC, Enocean)" $sel_1 \ - "2" "(RPi3/4) Disable Bluetooth module (RaZberry)" $sel_2 \ - 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi + if grep -qs "^[[:space:]]*enable_uart=1" /boot/config.txt; then optionOne="ON"; else optionOne="OFF"; fi + if is_pithree || is_pithreeplus || is_pifour; then + if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt" /boot/config.txt; then + optionTwo="ON" + else + optionTwo="OFF" + fi else - echo "SKIPPED" - return 0 + optionTwo="OFF" fi - if [[ $selection == *"1"* ]] && is_pi; then - cond_echo "" - cond_echo "Adding 'enable_uart=1' to /boot/config.txt" - if grep -Eq "^[[:space:]]*enable_uart" /boot/config.txt; then - sed -i 's/^.*enable_uart=.*$/enable_uart=1/g' /boot/config.txt + echo -n "$(timestamp) [openHABian] Beginning configuration of serial console for serial port peripherals... " + if selection=$(whiptail --title "Prepare Serial Port" --checklist --separate-output "$introText" 19 80 2 \ + "1" "(RPi) Disable serial console (RaZberry, SCC, Enocean)" $optionOne \ + "2" "(RPi3/4) Disable Bluetooth module (RaZberry)" $optionTwo \ + 3>&1 1>&2 2>&3); then echo "OK"; else echo "CANCELED"; return 0; fi + + + if [[ $selection == *"1"* ]]; then + echo -n "$(timestamp) [openHABian] Enabling serial port and disabling serial console... " + if grep -qs "^[[:space:]]*enable_uart" /boot/config.txt; then + if ! cond_redirect sed -i 's|^#*.*enable_uart=.*$|enable_uart=1|g' /boot/config.txt; then echo "FAILED (uart)"; return 1; fi else - echo "enable_uart=1" >> /boot/config.txt + if ! echo "enable_uart=1" >> /boot/config.txt; then echo "FAILED (uart)"; return 1; fi fi - cond_echo "Removing serial console and login shell from /boot/cmdline.txt and /etc/inittab" - cp /boot/cmdline.txt /boot/cmdline.txt.bak - cp /etc/inittab /etc/inittab.bak &>/dev/null - sed -i 's/console=tty.*console=tty1/console=tty1/g' /boot/cmdline.txt - sed -i 's/console=serial.*console=tty1/console=tty1/g' /boot/cmdline.txt - sed -i 's/^T0/\#T0/g' /etc/inittab &>/dev/null + if ! cond_redirect cp /boot/cmdline.txt /boot/cmdline.txt.bak; then echo "FAILED (backup cmdline.txt)"; return 1; fi + if ! cond_redirect sed -i 's|console=tty.*console=tty1|console=tty1|g' /boot/cmdline.txt; then echo "FAILED (console)"; return 1; fi + if ! cond_redirect sed -i 's|console=serial.*console=tty1|console=tty1|g' /boot/cmdline.txt; then echo "FAILED (serial)"; return 1; fi cond_echo "Disabling serial-getty service" - cond_redirect systemctl stop serial-getty@ttyAMA0.service - cond_redirect systemctl disable serial-getty@ttyAMA0.service - cond_redirect systemctl stop serial-getty@serial0.service - cond_redirect systemctl disable serial-getty@serial0.service - cond_redirect systemctl stop serial-getty@ttyS0.service - cond_redirect systemctl disable serial-getty@ttyS0.service + if ! cond_redirect systemctl stop serial-getty@ttyAMA0.service; then echo "FAILED (stop serial-getty@ttyAMA0.service)"; return 1; fi + if ! cond_redirect systemctl disable serial-getty@ttyAMA0.service; then echo "FAILED (disable serial-getty@ttyAMA0.service)"; return 1; fi + if ! cond_redirect systemctl stop serial-getty@serial0.service; then echo "FAILED (stop serial-getty@serial0.service)"; return 1; fi + if ! cond_redirect systemctl disable serial-getty@serial0.service; then echo "FAILED (disable serial-getty@serial0.service)"; return 1; fi + if ! cond_redirect systemctl stop serial-getty@ttyS0.service; then echo "FAILED (stop serial-getty@ttyS0.service)"; return 1; fi + if cond_redirect systemctl disable serial-getty@ttyS0.service; then echo "OK (Reboot needed)"; else echo "FAILED (disable serial-getty@ttyS0.service)"; return 1; fi else - cond_echo "ATTENTION: This function is not yet implemented." - #TODO this needs to be tested when/if someone actually cares... - #cp /boot/cmdline.txt.bak /boot/cmdline.txt - #cp /etc/inittab.bak /etc/inittab + if [[ -f /boot/cmdline.txt.bak ]]; then + echo -n "$(timestamp) [openHABian] Disabling serial port and enabling serial console... " + if ! cond_redirect sed -i '/^#*.*enable_uart=.*$/d' /boot/config.txt; then echo "FAILED (uart)"; return 1; fi + if ! cond_redirect cp /boot/cmdline.txt.bak /boot/cmdline.txt; then echo "FAILED (restore cmdline.txt)"; return 1; fi + if ! cond_redirect rm -f /boot/cmdline.txt.bak; then echo "FAILED (remove backup)"; return 1; fi + cond_echo "Enabling serial-getty service" + if ! cond_redirect systemctl enable serial-getty@ttyAMA0.service; then echo "FAILED (enable serial-getty@ttyAMA0.service)"; return 1; fi + if ! cond_redirect systemctl restart serial-getty@ttyAMA0.service; then echo "FAILED (restart serial-getty@ttyAMA0.service)"; return 1; fi + if ! cond_redirect systemctl enable serial-getty@serial0.service; then echo "FAILED (enable serial-getty@serial0.service)"; return 1; fi + if ! cond_redirect systemctl restart serial-getty@serial0.service; then echo "FAILED (restart serial-getty@serial0.service)"; return 1; fi + if ! cond_redirect systemctl enable serial-getty@ttyS0.service; then echo "FAILED (enable serial-getty@ttyS0.service)"; return 1; fi + if cond_redirect systemctl restart serial-getty@ttyS0.service; then echo "OK (Reboot needed)"; else echo "FAILED (restart serial-getty@ttyS0.service)"; return 1; fi + fi fi if [[ $selection == *"2"* ]]; then if is_pithree || is_pithreeplus || is_pifour; then - #cond_redirect systemctl stop hciuart &>/dev/null - #cond_redirect systemctl disable hciuart &>/dev/null - cond_echo "Adding 'dtoverlay=miniuart-bt' to /boot/config.txt (RPi3/4)" - if ! grep -Eq "^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt" /boot/config.txt; then - echo "dtoverlay=miniuart-bt" >> /boot/config.txt + echo -n "$(timestamp) [openHABian] Making Bluetooth use mini-UART... " + if ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt" /boot/config.txt; then + if echo "dtoverlay=miniuart-bt" >> /boot/config.txt; then echo "OK (Reboot needed)"; else echo "FAILED"; return 1; fi + else + echo "OK" fi else - cond_echo "Option only available for Raspberry Pi 3/4." + echo "$(timestamp) [openHABian] Making Bluetooth use mini-UART... SKIPPED" + return 0 fi else - if is_pithree || is_pithreeplus || is_pifour; then - cond_echo "Removing 'dtoverlay=miniuart-bt' from /boot/config.txt" - sed -i -E '/^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt/d' /boot/config.txt + if is_pithree || is_pithreeplus || is_pifour && grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt" /boot/config.txt; then + echo -n "$(timestamp) [openHABian] Making Bluetooth use UART... " + if cond_redirect sed -i -E '/^[[:space:]]*dtoverlay=(pi3-)?miniuart-bt/d' /boot/config.txt; then echo "OK (Reboot needed)"; else echo "FAILED"; return 1; fi fi fi - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Operation Successful!" --msgbox "$successtext" 16 80 - fi - echo "OK (Reboot needed)" + whiptail --title "Operation Successful!" --msgbox "$successText" 9 80 } diff --git a/functions/vpn.bash b/functions/vpn.bash index 3d819a067..a9b4e3552 100644 --- a/functions/vpn.bash +++ b/functions/vpn.bash @@ -10,26 +10,25 @@ install_wireguard() { local textReady local textInstallation - configdir="/etc/wireguard" textReady="In order to access your system from the Internet using Wireguard, you need to setup a couple of prerequisites. Do so now if you have not already done so.\\nYou need to have a (dynamically adapting) DNS name point to your router. Get it from any of the free providers such as DuckDNS or selfhost.de.\\nYou also need to forward an UDP port from the router to your system to allow for establishing the VPN (default 51900/UDP).\\nYou need to have this information available and your router should be setup to forward the VPN port. Are you ready to proceed ?" textInstallation="We will now install Wireguard VPN on your system. That'll take some time.\\n\\nMake use of this waiting time to install the client side part.\\nYou need to install the Wireguard client from either http://www.wireguard.com/install to your local PC or from PlayStore/AppStore to your mobile device.\\nopenHABian will display a QR code at the end of this installation to let you easily transfer the configuration." - if [[ "$1" == "remove" ]]; then - echo -n "$(timestamp) [openHABian] Removing Wireguard and VPN access... " - apt remove --yes wireguard wireguard-dkms wireguard-tools + if [[ $1 == "remove" ]]; then + echo -n "$(timestamp) [openHABian] Removing Wireguard service... " + if ! cond_redirect systemctl stop wg-quick@wg0.service; then echo "FAILED (stop service)"; return 1; fi + if ! rm -f /lib/systemd/system/wg-quick*; then echo "OK"; else echo "FAILED (remove service)"; return 1; fi + cond_redirect systemctl -q daemon-reload &> /dev/null - systemctl stop wg-quick@wg0 - rm -f /lib/systemd/system/wg-quick* - systemctl -q daemon-reload &>/dev/null - rmmod wireguard + echo -n "$(timestamp) [openHABian] Uninstalling Wireguard... " + if ! cond_redirect apt-get remove --yes wireguard wireguard-dkms wireguard-tools; then echo "FAILED"; return 1; fi + if ! rm -f /etc/apt/sources.list.d/wireguard.list; then echo "FAILED (remove apt list)"; return 1; fi + if ! cond_redirect rmmod wireguard; then echo "FAILED (remove module)"; return 1; fi + if cond_redirect apt-get update; then echo "OK"; else echo "FAILED (update apt lists)"; return 1; fi - rm -f /etc/apt/sources.list.d/wireguard.list - if ! cond_redirect apt-get update; then echo "FAILED (update apt lists)"; return 1; fi if [[ -n "$INTERACTIVE" ]]; then - whiptail --title "Wireguard VPN removed" --msgbox "We permanently removed the Wireguard installation from your system." 8 80 + whiptail --title "Wireguard VPN removed" --msgbox "We permanently removed the Wireguard installation from your system." 7 80 fi - echo "OK" return 0 fi if [[ $1 != "install" ]]; then return 1; fi @@ -45,8 +44,8 @@ install_wireguard() { else if is_pi || is_raspbian || is_raspios; then echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/wireguard.list - cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC - cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138 + cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC + cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138 # important to avoid release mixing: # prevent RPi from using the Debian distro for normal Raspbian packages @@ -114,11 +113,8 @@ create_wireguard_config() { echo -n "$(timestamp) [openHABian] Installing Wireguard required packages (dnsutils)... " if cond_redirect apt-get install --yes dnsutils; then echo "OK"; else echo "FAILED"; return 1; fi fi - pubIP=$(dig -4 +short myip.opendns.com @resolver1.opendns.com | tail -1) - if [ -z "$pubIP" ]; then - if pubIP=$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com|tr -d '"'); then echo "$pubIP"; else echo "FAILED"; return 1; fi - fi + if ! pubIP="$(get_public_ip)"; then echo "FAILED (public ip)"; return 1; fi configdir=/etc/wireguard IFACE=${1:-eth0} PORT="${2:-51900}" @@ -130,7 +126,6 @@ create_wireguard_config() { CLIENTPRIVATE=$(cat "$configdir"/client_private_key) CLIENTPUBLIC=$(cat "$configdir"/client_public_key) - mkdir -p "$configdir" sed -e "s|%IFACE|${IFACE}|g" -e "s|%PORT|${PORT}|g" -e "s|%VPNSERVER|${VPNSERVER}|g" -e "s|%WGSERVERIP|${WGSERVERIP}|g" -e "s|%WGCLIENTIP|${WGCLIENTIP}|g" -e "s|%SERVERPRIVATE|${SERVERPRIVATE}|g" -e "s|%CLIENTPUBLIC|${CLIENTPUBLIC}|g" "${BASEDIR:-/opt/openhabian}"/includes/wireguard-server.conf-template > "$configdir"/wg0.conf sed -e "s|%IFACE|${IFACE}|g" -e "s|%PORT|${PORT}|g" -e "s|%VPNSERVER|${VPNSERVER}|g" -e "s|%WGSERVERIP|${WGSERVERIP}|g" -e "s|%WGCLIENTIP|${WGCLIENTIP}|g" -e "s|%SERVERPUBLIC|${SERVERPUBLIC}|g" -e "s|%CLIENTPRIVATE|${CLIENTPRIVATE}|g" "${BASEDIR:-/opt/openhabian}"/includes/wireguard-client.conf-template > "$configdir"/wg0-client.conf diff --git a/functions/wifi.bash b/functions/wifi.bash index a1963b255..b1027e873 100644 --- a/functions/wifi.bash +++ b/functions/wifi.bash @@ -1,91 +1,141 @@ #!/usr/bin/env bash -wifi_setup() { - local question="We could not detect any WiFi hardware on your system.\\nYou are not running any of supported systems RPi4, RPi3, RPi0W to have WiFi builtin, but we cannot detect all possible WiFi hardware.\\nDo you really want to continue and have openHABian try to setup WiFi ?" - echo -n "$(timestamp) [openHABian] Setting up WiFi ... " +## Enable or disable the RPi WiFi module +## Valid arguments: "enable" or "disable" +## +## enable_disable_wifi(String option) +## +enable_disable_wifi() { + if ! is_pi; then return 0; fi - if ! is_pifour && ! is_pithree && ! is_pithreeplus && ! is_pizerow ; then - if [ -n "$INTERACTIVE" ]; then - if ! (whiptail --title "No WiFi Hardware Detected" --yesno "$question" 10 80); then echo "FAILED"; return 1; fi + if [[ $1 == "enable" ]]; then + echo -n "$(timestamp) [openHABian] Enabling WiFi... " + if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then + if sed -i -E '/^[[:space:]]*dtoverlay=(pi3-)?disable-wifi/d' /boot/config.txt; then echo "OK (Reboot needed)"; else echo "FAILED"; return 1; fi + else + echo "OK" + fi + elif [[ $1 == "disable" ]]; then + echo -n "$(timestamp) [openHABian] Disabling WiFi... " + if ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then + if echo "dtoverlay=disable-wifi" >> boot/config.txt; then echo "OK (Reboot needed)"; else echo "FAILED"; return 1; fi + else + echo "OK" fi fi +} + +## Configure WiFi setup on current system +## Valid arguments: "setup" or "disable" +## +## configure_wifi(String option) +## +configure_wifi() { + if [[ -z $INTERACTIVE ]]; then + echo "$(timestamp) [openHABian] WiFi setup must be run in interactive mode! Canceling WiFi setup!" + return 0 + fi - if [ -n "$INTERACTIVE" ]; then - if grep -q "^[[:space:]]*dtoverlay=disable-wifi" /boot/config.txt; then - if (whiptail --title "WiFi is currently disabled" --yesno "WiFi is currently disabled on your box. Enable and switch over, i.e. disable Ethernet ?" 7 55); then - cond_echo "Removing 'dtoverlay=disable-wifi' from /boot/config.txt" - sed -i '/^[[:space:]]*dtoverlay=disable-wifi/d' /boot/config.txt - fi - else - if (whiptail --title "WiFi is currently ON" --defaultno --yesno "WiFi is currently enabled on your box.\\n\\nATTENTION:\\nDo you want to disable it and return to Ethernet ?" 10 50); then - cond_echo "Adding 'dtoverlay=disable-wifi' to /boot/config.txt (RPi0W/3/4)" - echo "dtoverlay=disable-wifi" >> /boot/config.txt - sed -i '/wlan0/d; /wpa-roam/d; /iface default inet dhcp/d' /etc/network/interfaces + local disabledText + local enabledText + local questionText + local wifiConfig + local wifiCountry + local wifiNetworkList + local wifiPassword + local wifiSSID - whiptail --title "Operation successful!" --msgbox "Please reboot now to switch back from WiFi to Ethernet." 7 70 + disabledText="WiFi is currently disabled on your box.\\n\\nATTENTION:\\nWould you like to enable WiFi and continue setup?" + enabledText="WiFi is currently enabled on your box.\\n\\nATTENTION:\\nWould you like to disable WiFi and use Ethernet?" + questionText="We could not detect any WiFi hardware on your system.\\nYou are not running any of supported systems (RPi4, RPi3, RPi0W) which have WiFi builtin. However, we cannot detect all possible WiFi hardware.\\nDo you really want to continue and have openHABian try to setup WiFi?" + + echo -n "$(timestamp) [openHABian] Beginning WiFi configuration... " + + if ! is_pizerow && ! is_pithree && ! is_pithreeplus && ! is_pifour; then + if (whiptail --title "No WiFi Hardware Detected" --yesno "$questionText" 10 80); then echo "OK"; else echo "CANCELED"; return 0; fi + else + echo "OK" + fi + + if [[ $1 == "setup" ]]; then + if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then + if (whiptail --title "WiFi is currently disabled" --yesno "$disabledText" 10 80); then + cond_redirect enable_disable_wifi "enable" + else + echo "CANCELED" return 0 fi fi - if ! SSID=$(whiptail --title "Wifi Setup" --inputbox "Which Wifi (SSID) do you want to connect to?" 10 60 3>&1 1>&2 2>&3); then return 1; fi - if ! PASS=$(whiptail --title "Wifi Setup" --inputbox "What's the password for that Wifi?" 10 60 3>&1 1>&2 2>&3); then return 1; fi - # use wpa_passphrase to escape the password, it handles special characters - how - if ! WNET=$(wpa_passphrase "${SSID}" "${PASS}"); then - whiptail --title "WiFi Setup" --msgbox "wpa_passphrase failed,\\n \"${WNET}\"" 10 60 - echo "wpa_passphrase failed, \"${WNET}\""; return 1 + if is_pifour || is_pithree || is_pithreeplus || is_pizerow; then + if ! dpkg -s 'firmware-brcm80211' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing WiFi firmware... " + if cond_redirect apt-get install --yes firmware-brcm80211; then echo "OK"; else echo "FAILED"; return 1; fi + fi fi - if ! WIFICOUNTRY=$(whiptail --title "Wifi Setup" --inputbox "Please enter the two-letter country code matching your region eg. US DE NZ AU...\\nSee https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" 10 60 3>&1 1>&2 2>&3); then return 1; fi - # check the user input for the country code - # check: from the start of line, the uppercased input must be followed by a whitespace - if grep -q "^${WIFICOUNTRY^^}\\s" /usr/share/zoneinfo/zone.tab; then - WIFICOUNTRY=${WIFICOUNTRY^^} - else - whiptail --title "WiFi Setup" --msgbox "${WIFICOUNTRY} is not a valid country code found in /usr/share/zoneinfo/zone.tab" 10 60 - echo "${WIFICOUNTRY} is not a valid country code found in /usr/share/zoneinfo/zone.tab"; return 1 + + if ! dpkg -s 'wpasupplicant' 'wireless-tools' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing WiFi prerequisites (wpasupplicant, wireless-tools)... " + if cond_redirect apt-get install --yes wpasupplicant wireless-tools; then echo "OK"; else echo "FAILED"; return 1; fi fi - else - echo -n "Setting default SSID and password in 'wpa_supplicant.conf' " - SSID="myWiFiSSID" - PASS="myWiFiPassword" - fi - if is_pifour || is_pithree || is_pithreeplus; then cond_redirect apt-get install --yes firmware-brcm80211; fi - if is_pithreeplus || is_pifour; then - if iwlist wlan0 scanning 2>&1 | grep -q "Interface doesn't support scanning"; then - # wifi might be blocked + + echo -n "$(timestamp) [openHABian] Checking if WiFi is working... " + if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then + # WiFi might be blocked rfkill unblock wifi ifconfig wlan0 up - if iwlist wlan0 scanning 2>&1 | grep -q "Interface doesn't support scanning"; then + if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then echo "FAILED" - echo "" - echo "I was not able to turn on the wifi" - echo "Here is some more information" - echo "" + echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n" rfkill list all ifconfig return 1 + else + echo "OK" fi + else + echo "OK" fi - fi - cond_redirect apt-get install --yes wpasupplicant wireless-tools - mkdir -p /etc/wpa_supplicant - echo -e "# config generated by openHABian wifi function" > /etc/wpa_supplicant/wpa_supplicant.conf - echo -e "country=$WIFICOUNTRY\\nctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\\nupdate_config=1" >> /etc/wpa_supplicant/wpa_supplicant.conf - echo -e "# network config created by wpa_passphrase to ensure correct handling of special characters\\n${WNET//\}/\\tkey_mgmt=WPA-PSK\\n\}}" >> /etc/wpa_supplicant/wpa_supplicant.conf - sed -i "s/REGDOMAIN=.*/REGDOMAIN=${WIFICOUNTRY}/g" /etc/default/crda + echo -n "$(timestamp) [openHABian] Configuring WiFi... " + wifiNetworkList="$(iwlist wlan0 scan | grep ESSID | sed 's/^[[:space:]]*ESSID://g; s/"//g; /^[[:space:]]*$/d')" + if [[ -z $wifiNetworkList ]]; then echo "FAILED (no networks found)"; return 1; fi + if ! wifiSSID="$(whiptail --title "Wifi Setup" --inputbox "\\nWhich WiFi network would you like do you want to connect to?\\n\\nNetwork List:\\n${wifiNetworkList}" 19 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi + if [[ -z $wifiSSID ]]; then echo "FAILED (blank SSID)"; return 1; fi + if ! wifiPassword="$(whiptail --title "Wifi Setup" --passwordbox "\\nWhat's the password for ${wifiSSID}?" 9 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi + if ! wifiConfig="$(wpa_passphrase "${wifiSSID}" "${wifiPassword}")"; then echo "FAILED (${wifiConfig})"; return 1; fi + if ! wifiCountry="$(whiptail --title "Wifi Setup" --inputbox "\\nPlease enter the two-letter country code matching your region (US, DE, NZ, AU)...\\n\\nSee https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" 12 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi + # Check if the country code is valid, valid country codes are followed by spaces in /usr/share/zoneinfo/zone.tab + if grep -qs "^${wifiCountry^^}[[:space:]]" /usr/share/zoneinfo/zone.tab; then + wifiCountry="${wifiCountry^^}" + else + echo "FAILED (${wifiCountry} is not a valid country code found in '/usr/share/zoneinfo/zone.tab')" + return 1 + fi + if ! cond_redirect mkdir -p /etc/wpa_supplicant; then echo "FAILED (create directory)"; return 1; fi + if ! echo -e "# WiFi configuration generated by openHABian\\ncountry=$wifiCountry\\nctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\\nupdate_config=1\\n# Network configuration was created by wpa_passphrase to ensure correct handling of special characters\\n${wifiConfig//\}/\\tkey_mgmt=WPA-PSK\\n\}}" > /etc/wpa_supplicant/wpa_supplicant.conf; then echo "FAILED (create configuration)"; return 1; fi + if cond_redirect sed -i 's|REGDOMAIN=.*$|REGDOMAIN='"${wifiCountry}"'|g' /etc/default/crda; then echo "OK"; else echo "FAILED (set country)"; return 1; fi - if grep -q "wlan0" /etc/network/interfaces; then - cond_echo "" - cond_echo "Not writing to '/etc/network/interfaces', wlan0 entry already available. You might need to check, adopt or remove these lines." - cond_echo "" - else - echo -e "\\nallow-hotplug wlan0\\niface wlan0 inet manual\\nwpa-roam /etc/wpa_supplicant/wpa_supplicant.conf\\niface default inet dhcp" >> /etc/network/interfaces - fi - cond_redirect wpa_cli reconfigure - cond_redirect ifdown wlan0 - cond_redirect ifup wlan0 - if [ -n "$INTERACTIVE" ]; then - whiptail --title "Operation Successful!" --msgbox "Setup was successful. Your WiFi credentials were NOT tested. Please reboot now." 15 80 + echo -n "$(timestamp) [openHABian] Configuring network... " + if grep -qs "wlan0" /etc/network/interfaces; then + cond_echo "\\nNot writing to '/etc/network/interfaces', wlan0 entry already available. You might need to check, adopt or remove these lines." + else + echo -e "\\nallow-hotplug wlan0\\niface wlan0 inet manual\\nwpa-roam /etc/wpa_supplicant/wpa_supplicant.conf\\niface default inet dhcp" >> /etc/network/interfaces + fi + if ! cond_redirect wpa_cli reconfigure; then echo "FAILED (reconfigure)"; return 1; fi + if ! cond_redirect ifconfig wlan0 down; then echo "FAILED (down)"; return 1; fi + if cond_redirect ifconfig wlan0 up; then echo "OK (reboot now)"; else echo "FAILED (up)"; return 1; fi + + whiptail --title "Operation Successful!" --msgbox "Setup was successful. The credentials provided were not tested. Please reboot now." 7 80 + elif [[ $1 == "disable" ]]; then + if (whiptail --title "WiFi is currently enabled" --defaultno --yesno "$enabledText" 10 80); then + cond_redirect enable_disable_wifi "disable" + echo -n "$(timestamp) [openHABian] Cleaning up old WiFi configuration... " + if cond_redirect sed -i '/allow-hotplug wlan0/d; /iface wlan0 inet manual/d; /wpa-roam \/etc\/wpa_supplicant\/wpa_supplicant.conf/d; /iface default inet dhcp/d' /etc/network/interfaces; then echo "OK (reboot now)"; else echo "FAILED"; return 1; fi + whiptail --title "Operation Successful!" --msgbox "Setup was successful. Please reboot now." 7 80 + else + echo "CANCELED" + return 0 + fi fi - echo "OK (Reboot needed)" } diff --git a/functions/zram.bash b/functions/zram.bash index e6ab28ec8..803ec67cb 100644 --- a/functions/zram.bash +++ b/functions/zram.bash @@ -1,63 +1,116 @@ #!/usr/bin/env bash -## install code needed to compile ZRAM tools at installation time -## can be called standalone from build.bash or during install from init_zram_mounts() -## argument is destination directory +## Install code needed to compile ZRAM tools at installation time this +## can be called standalone from build.bash or during install from init_zram_mounts(). +## The argument is the destination directory. ## -## install_zram_code(String dir) +## install_zram_code(String dir) ## install_zram_code() { - local ZRAMGIT=https://github.com/mstormi/openhabian-zram - local OVERLAYFSGIT=https://github.com/kmxz/overlayfs-tools - local TAG=openhabian_v1.6 - #cond_redirect apt-get install -y -q --no-install-recommends make libattr1-dev - - mkdir -p "$1" - git clone -q "$OVERLAYFSGIT" "$1"/overlayfs-tools - git clone -c advice.detachedHead=false -q --branch "$TAG" "$ZRAMGIT" "$1"/openhabian-zram + local overlayfsGit + local zramGit + + overlayfsGit="https://github.com/kmxz/overlayfs-tools" + zramGit="https://github.com/mstormi/openhabian-zram" + + echo -n "$(timestamp) [openHABian] Installing ZRAM code... " + if ! cond_redirect mkdir -p "$1"; then echo "FAILED (create directory)"; return 1; fi + + if [[ -d "${1}/overlayfs-tools" ]]; then + if ! cond_redirect update_git_repo "${1}/overlayfs-tools" "master"; then echo "FAILED (update overlayfs)"; return 1; fi + else + if ! cond_redirect git clone "$overlayfsGit" "$1"/overlayfs-tools; then echo "FAILED (clone overlayfs)"; return 1; fi + fi + + if [[ -d "${1}/openhabian-zram" ]]; then + if cond_redirect update_git_repo "${1}/openhabian-zram" "master"; then echo "OK"; else echo "FAILED (update zram)"; return 1; fi + else + if cond_redirect git clone "$zramGit" "$1"/openhabian-zram; then echo "OK"; else echo "FAILED (clone zram)"; return 1; fi + fi } +## Setup ZRAM for openHAB specific usage +## Valid arguments: "install" or "uninstall" +## +## init_zram_mounts(String option) +## init_zram_mounts() { - local introtext="You are about to activate the ZRAM feature.\\nBe aware you do this at your own risk of data loss.\\nPlease check out the \"ZRAM status\" thread at https://community.openhab.org/t/zram-status/80996 before proceeding." - local text_lowmem="Your system has less than 1 GB of RAM. It is definitely NOT recommended to run ZRAM (AND openHAB) on your box. If you proceed now you will do so at your own risk !" - local ZRamInstallLocation + if ! is_arm; then return 0; fi + + local introText + local lowMemText + local zramInstallLocation - if [ "$1" == "install" ]; then - if [ -z "$UNATTENDED" ]; then + introText="You are about to activate the ZRAM feature.\\nBe aware you do this at your own risk of data loss.\\nPlease check out the \"ZRAM status\" thread at https://community.openhab.org/t/zram-status/80996 before proceeding." + lowMemText="Your system has less than 1 GB of RAM. It is definitely NOT recommended to run ZRAM (AND openHAB) on your box. If you proceed now you will do so at your own risk!" + zramInstallLocation="/opt/zram" + + if [[ $1 == "install" ]] && ! [[ -f /etc/ztab ]]; then + if [[ -n $INTERACTIVE ]]; then # display warn disclaimer and point to ZRAM status thread on forum - if ! (whiptail --title "Install ZRAM, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80) then echo "CANCELED"; return 0; fi + if ! (whiptail --title "Install ZRAM, Continue?" --yes-button "Continue" --no-button "Cancel" --yesno "$introText" 10 80); then echo "CANCELED"; return 0; fi # double check if there's enough RAM to run ZRAM if has_lowmem; then - if ! (whiptail --title "WARNING, Continue?" --yes-button "REALLY Continue" --no-button "Step Back" --yesno --defaultno "$text_lowmem" 15 80) then echo "CANCELED"; return 0; fi + if ! (whiptail --title "WARNING, Continue?" --yes-button "REALLY Continue" --no-button "Cancel" --yesno --defaultno "$lowMemText" 10 80); then echo "CANCELED"; return 0; fi fi fi - ZRamInstallLocation=/opt/zram - cond_redirect apt-get install -y -q --no-install-recommends make libattr1-dev + if ! dpkg -s 'make' 'libattr1-dev' &> /dev/null; then + echo -n "$(timestamp) [openHABian] Installing ZRAM required packages (make, libattr1-dev)... " + if cond_redirect apt-get install --yes make libattr1-dev; then echo "OK"; else echo "FAILED"; return 1; fi + fi + + install_zram_code "$zramInstallLocation" + + echo -n "$(timestamp) [openHABian] Setting up OverlayFS... " + if ! cond_redirect make --always-make --directory="$zramInstallLocation"/overlayfs-tools; then echo "FAILED (make overlayfs)"; return 1; fi + if ! mkdir -p /usr/local/lib/zram-config/; then echo "FAILED (create directory)"; return 1; fi + if cond_redirect install -m 755 "$zramInstallLocation"/overlayfs-tools/overlay /usr/local/lib/zram-config/overlay; then echo "OK"; else echo "FAILED (install overlayfs)"; return 1; fi + + echo -n "$(timestamp) [openHABian] Setting up ZRAM... " + if ! install -m 755 "$zramInstallLocation"/openhabian-zram/zram-config /usr/local/bin/; then echo "FAILED (zram-config)"; return 1; fi + if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/ztab /etc/ztab; then echo "FAILED (ztab)"; return 1; fi + if ! mkdir -p /usr/local/share/zram-config/log; then echo "FAILED (create directory)"; return 1; fi + if ! cond_redirect install -m 644 "$zramInstallLocation"/openhabian-zram/ro-root.sh /usr/local/share/zram-config/ro-root.sh; then echo "FAILED (ro-root)"; return 1; fi + if cond_redirect install -m 644 "$zramInstallLocation"/openhabian-zram/zram-config.logrotate /etc/logrotate.d/zram-config; then echo "OK"; else echo "FAILED (logrotate)"; return 1; fi - install_zram_code "$ZRamInstallLocation" - cd "$ZRamInstallLocation"/overlayfs-tools || return 1 - make - cd "$ZRamInstallLocation"/openhabian-zram || return 1 - /bin/sh ./install.sh - /usr/bin/install -m 644 "${BASEDIR:=/opt/openhabian}"/includes/ztab /etc/ztab + if ! dpkg -s 'openhab2' &> /dev/null; then + sed -i 's|dir lz4 150M 500M /var/lib/openhab2/persistence /persistence.bind||g' /etc/ztab + fi + + echo -n "$(timestamp) [openHABian] Setting up ZRAM service... " + if ! cond_redirect install -m 644 "$zramInstallLocation"/openhabian-zram/zram-config.service /etc/systemd/system/zram-config.service; then echo "FAILED (copy service)"; return 1; fi + if ! cond_redirect systemctl enable zram-config.service; then echo "FAILED (enable service)"; return 1; fi + if cond_redirect systemctl restart zram-config.service; then echo "OK"; else echo "FAILED (restart service)"; return 1; fi + elif [[ $1 == "uninstall" ]]; then + echo -n "$(timestamp) [openHABian] Removing ZRAM service... " + if ! cond_redirect systemctl stop zram-config.service; then echo "FAILED (stop service)"; return 1; fi + if ! cond_redirect systemctl disable zram-config.service; then echo "FAILED (disable service)"; return 1; fi + if rm -f /etc/systemd/system/zram-config.service; then echo "OK"; else echo "FAILED (remove service)"; fi - cond_redirect systemctl start zram-config + echo -n "$(timestamp) [openHABian] Removing ZRAM... " + if ! rm -f /usr/local/bin/zram-config; then echo "FAILED (zram-config)"; return 1; fi + if ! rm -f /etc/ztab; then echo "FAILED (ztab)"; return 1; fi + if ! rm -rf /usr/local/share/zram-config; then echo "FAILED (zram-config share)"; return 1; fi + if ! rm -rf /usr/local/lib/zram-config; then echo "FAILED (zram-config lib)"; return 1; fi + if rm -f /etc/logrotate.d/zram-config; then echo "OK"; else echo "FAILED (logrotate)"; return 1; fi else - cond_redirect systemctl stop zram-config - /bin/sh /usr/local/share/zram-config/uninstall.sh - rm -f /etc/ztab + echo "$(timestamp) [openHABian] Refusing to install ZRAM as it is already installed, please uninstall and then try again... EXITING" + return 1 fi } zram_setup() { if is_arm; then if ! has_lowmem && ! is_pione && ! is_cmone && ! is_pizero && ! is_pizerow; then - cond_redirect systemctl stop openhab2 - - echo "$(timestamp) [openHABian] Installing ZRAM ..." - cond_redirect init_zram_mounts install - cond_redirect systemctl start openhab2 + if dpkg -s 'openhab2' &> /dev/null; then + cond_redirect systemctl stop openhab2.service + fi + echo -n "$(timestamp) [openHABian] Installing ZRAM... " + if cond_redirect init_zram_mounts "install"; then echo "OK"; else echo "FAILED"; return 1; fi + if dpkg -s 'openhab2' &> /dev/null; then + cond_redirect systemctl stop openhab2.service + fi else echo "$(timestamp) [openHABian] Skipping ZRAM install on ARM hardware without enough memory." fi diff --git a/functions/zram.bats b/functions/zram.bats index 646217b68..e416e1b10 100644 --- a/functions/zram.bats +++ b/functions/zram.bats @@ -73,7 +73,7 @@ check_zram_removal() { if ! is_arm; then skip "Not executing zram test because not on native ARM architecture hardware."; fi echo -e "# ${COL_CYAN}$(timestamp) [openHABian] ZRAM test installation starting...${COL_DEF}" >&3 - run init_zram_mounts install 3>&- + run init_zram_mounts "install" 3>&- if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Initial installation of zram mounts succeeded.${COL_DEF}" >&3 @@ -81,7 +81,7 @@ check_zram_removal() { if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Availability of zram mounts verified.${COL_DEF}" >&3 - run init_zram_mounts uninstall 3>&- + run init_zram_mounts "uninstall" 3>&- if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Uninstall of zram mounts succeeded.${COL_DEF}" >&3 @@ -89,7 +89,7 @@ check_zram_removal() { if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Uninstall of zram mounts verified - none remaining.${COL_DEF}" >&3 - run init_zram_mounts install 3>&- + run init_zram_mounts "install" 3>&- if [ "$status" -ne 0 ]; then echo "$output" >&3; fi [ "$status" -eq 0 ] echo -e "# ${COL_GREEN}$(timestamp) [openHABian] Second installation of zram mounts succeeded.${COL_DEF}" >&3 diff --git a/includes/azul_zulu_license.md b/includes/azul_zulu_license.md index 69a26bb84..00a4cd9e2 100644 --- a/includes/azul_zulu_license.md +++ b/includes/azul_zulu_license.md @@ -1,11 +1,30 @@ # Zulu Terms of Use -Zulu Community is copyrighted software based on OpenJDK () and is 100% open source. -By downloading, installing, or otherwise using Zulu Community builds of OpenJDK, you hereby agree as follows: +Zulu Community is copyrighted software based on OpenJDK +() and is 100% open source. -* Zulu Community is provided “AS IS” and without warranty of any kind, express or implied, including without limitation any warranty of merchantability, fitness for a particular purpose, or noninfringement, each of which are hereby expressly disclaimed. You acknowledge and agree that any use of Zulu Community is at your sole risk. -* Azul will not be liable to you (whether under contract, tort, strict liability, negligence, or any other legal or equitable theory) for (i) any indirect, incidental, consequential, special, punitive, or reliance damages relating to your download, use or installation of Zulu Community (including without limitation any loss of or damage to data or systems), or (ii) for any direct damages in excess of (in the aggregate) one hundred U.S. dollars ($100). Some states do not allow the exclusion or limitation of incidental or consequential damages, so the above limitations may not apply to you. -* Azul is not obligated to provide you with any support for Zulu Community (unless you have entered into a separate written agreement for such support, in which case such separate agreement shall apply). -* You also represent and warrant that you do not intend to distribute the software in a manner that is not compliant with relevant export control laws or regulations administered by the U.S. Commerce Department, OFAC, or any other government agency. +By downloading, installing, or otherwise using Zulu Community builds of OpenJDK, +you hereby agree as follows: + +* Zulu Community is provided “AS IS” and without warranty of any kind, express + or implied, including without limitation any warranty of merchantability, + fitness for a particular purpose, or non-infringement, each of which are + hereby expressly disclaimed. You acknowledge and agree that any use of + Zulu Community is at your sole risk. +* Azul will not be liable to you (whether under contract, tort, strict + liability, negligence, or any other legal or equitable theory) for (i) any + indirect, incidental, consequential, special, punitive, or reliance damages + relating to your download, use or installation of Zulu Community (including + without limitation any loss of or damage to data or systems), or (ii) for + any direct damages in excess of (in the aggregate) one hundred U.S. dollars + ($100). Some states do not allow the exclusion or limitation of incidental + or consequential damages, so the above limitations may not apply to you. +* Azul is not obligated to provide you with any support for Zulu Community + (unless you have entered into a separate written agreement for such support, + in which case such separate agreement shall apply). +* You also represent and warrant that you do not intend to distribute the + software in a manner that is not compliant with relevant export control laws + or regulations administered by the U.S. Commerce Department, OFAC, or any + other government agency. diff --git a/includes/bash.bashrc b/includes/bash.bashrc index bc3ecf397..c3d2657d4 100644 --- a/includes/bash.bashrc +++ b/includes/bash.bashrc @@ -47,7 +47,7 @@ esac force_color_prompt=yes if [ -n "$force_color_prompt" ]; then - if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then + if [ -x /usr/bin/tput ] && tput setaf 1 &> /dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf.) diff --git a/includes/frontail-preset.json b/includes/frontail-preset.json index 60d9b1bdc..cc85d266c 100644 --- a/includes/frontail-preset.json +++ b/includes/frontail-preset.json @@ -6,7 +6,9 @@ "Node is DEAD": "color: red;", "[GroupItemStateChangedEvent]": "color: #0026FF; font-weight: bold;", "[ItemStateChangedEvent ]": "color: #0026FF;", + "[vent.ItemStateChangedEvent]": "color: #0026FF;", "[ItemCommandEvent ]": "color: #00137F;", + "[ome.event.ItemCommandEvent]": "color: #00137F;", "[hingStatusInfoChangedEvent]": "color: #007F46;", "received command ON": "color: black;", "received command OFF": "color: black;", diff --git a/openhabian-setup.sh b/openhabian-setup.sh index 2323fac7e..0afd07714 100755 --- a/openhabian-setup.sh +++ b/openhabian-setup.sh @@ -19,7 +19,7 @@ SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do BASEDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCE="$(readlink "$SOURCE")" - [[ $SOURCE != /* ]] && SOURCE="$BASEDIR/$SOURCE" + [[ $SOURCE != /* ]] && SOURCE="${BASEDIR:-/opt/openhabian}/$SOURCE" done BASEDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SCRIPTNAME="$(basename "$SOURCE")" @@ -71,7 +71,7 @@ export UNATTENDED SILENT DEBUGMAX INTERACTIVE # Include all subscripts # shellcheck source=/dev/null -for shfile in "$BASEDIR"/functions/*.bash; do source "$shfile"; done +for shfile in "${BASEDIR:-/opt/openhabian}"/functions/*.bash; do source "$shfile"; done # avoid potential crash when deleting directory we started from OLDWD=$(pwd) && cd /opt || exit 1 @@ -82,22 +82,22 @@ config_ipv6 if [[ -n "$UNATTENDED" ]]; then # apt/dpkg commands will not try interactive dialogs export DEBIAN_FRONTEND=noninteractive - cond_redirect apt-get update + wait_for_apt_to_finish_update load_create_config change_swapsize timezone_setting - enable_ntp + setup_ntp "enable" locale_setting hostname_change - if is_pi; then memory_split; enable_rpi_audio; fi + memory_split + enable_rpi_audio basic_packages needed_packages bashrc_copy vimrc_copy firemotd_setup - # shellcheck disable=SC2154 - java_install_or_update "$java_opt" - openhab2_setup + java_install_or_update "${java_opt:-Zulu8-32}" + openhab2_setup "stable" vim_openhab_syntax nano_openhab_syntax multitail_openhab_scheme @@ -113,7 +113,6 @@ else apt_update whiptail_check load_create_config - ua-netinst_check openhabian_console_check openhabian_update_check while show_main_menu; do diff --git a/openhabian.conf.dist b/openhabian.conf.dist index 905ae1a81..b16c7229f 100644 --- a/openhabian.conf.dist +++ b/openhabian.conf.dist @@ -52,6 +52,6 @@ debugmode=off # 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/test.bash b/test.bash index 69e3560ec..1859217b1 100755 --- a/test.bash +++ b/test.bash @@ -34,7 +34,7 @@ if [[ $1 == "docker-full" ]]; then cond_redirect docker run --rm --name "unit-tests" -i openhabian/bats-openhabian bash -c 'bats --tap --recursive --filter "unit-." .' cond_redirect docker run --rm --name "installation-tests" -i openhabian/bats-openhabian bash -c 'bats --tap --recursive --filter "installation-." .' cond_redirect docker run --rm --name "destructive-tests" -i openhabian/bats-openhabian bash -c 'bats --tap --recursive --filter "destructive-." .' - cond_redirect echo_process "Test complete, please review result in terminal." + cond_redirect echo_process "Tests complete, please review results in terminal." exit 0 elif [[ $1 == "shellcheck" ]]; then shellcheck -x -s bash openhabian-setup.sh