Skip to content

Commit

Permalink
Add support for FIND3
Browse files Browse the repository at this point in the history
Fixes openhab#632

Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Aug 2, 2020
1 parent 4c7415d commit 35b2ba1
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 176 deletions.
80 changes: 37 additions & 43 deletions build-image/first-boot.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# shellcheck source=/etc/openhabian.conf disable=SC1091

CONFIGFILE=/etc/openhabian.conf
Expand Down Expand Up @@ -33,7 +33,6 @@ if ! source "$CONFIGFILE"; then echo "FAILED (source config)"; fail_inprogress;
if ! source "/opt/openhabian/functions/helpers.bash"; then echo "FAILED (source helpers)"; fail_inprogress; fi
if source "/opt/openhabian/functions/openhabian.bash"; then echo "OK"; else echo "FAILED (source openhabian)"; fail_inprogress; fi


if [[ "${debugmode:-on}" == "on" ]]; then
unset SILENT
unset DEBUGMAX
Expand All @@ -45,9 +44,9 @@ fi

echo -n "$(timestamp) [openHABian] Starting webserver with installation log... "
if [[ -x $(command -v python3) ]]; then
bash /boot/webif.bash start
bash /boot/webserver.bash "start"
sleep 5
isWebRunning=$(ps -ef | pgrep python3)
isWebRunning="$(ps -ef | pgrep python3)"
if [[ -n $isWebRunning ]]; then echo "OK"; else echo "FAILED"; fi
else
echo "SKIPPED (Python not found)"
Expand Down Expand Up @@ -79,62 +78,56 @@ if [[ -z $wifi_ssid ]]; then
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 -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
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 |& grep -q "Interface doesn't support scanning"; then
ip link set wlan0 up
if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
echo "FAILED"
echo "$(timestamp) [openHABian] I was not able to turn on the WiFi - here is some more information:"
echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n"
rfkill list all
ifconfig
ip a
fail_inprogress
else
echo "OK"
fi
else
echo "OK"
fi
echo "OK"
else
echo -n "$(timestamp) [openHABian] Setting up Wi-Fi connection... "

# check the user input for the country code
# check: from the start of line, the uppercased input must be followed by a whitespace
if [[ -z $wifi_country ]]; then
wifi_country="US"
elif grep -q "^${wifi_country^^}\\s" /usr/share/zoneinfo/zone.tab; then
wifi_country="${wifi_country^^}"
wifiCountry="$wifi_country"
wifiSSID="$wifi_ssid"
wifiPassword="$wifi_psk"

# 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 "${wifi_country} is not a valid country code found in /usr/share/zoneinfo/zone.tab"
echo "Defaulting to US"
wifi_country="US"
echo "ERROR (${wifiCountry} is not a valid country code found in '/usr/share/zoneinfo/zone.tab' defaulting to US)"
wifiCountry="US"
fi
if ! wifiConfig="$(wpa_passphrase "${wifiSSID}" "${wifiPassword}")"; then echo "FAILED (wpa_passphrase)"; fail_inprogress; fi

echo -e "# config generated by openHABian first boot setup" > /etc/wpa_supplicant/wpa_supplicant.conf
echo -e "country=$wifi_country\\nctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\\nupdate_config=1" >> /etc/wpa_supplicant/wpa_supplicant.conf
# shellcheck disable=SC2154
if ! WNET=$(wpa_passphrase "${wifi_ssid}" "${wifi_psk}"); then
echo "FAILED"
echo "$WNET"
else
echo "# network config created by wpa_passphrase to ensure correct handling of special characters" >> /etc/wpa_supplicant/wpa_supplicant.conf
echo -e "${WNET//\}/\\tkey_mgmt=WPA-PSK\\n\}}" >> /etc/wpa_supplicant/wpa_supplicant.conf
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

sed -i "s/REGDOMAIN=.*/REGDOMAIN=${wifi_country}/g" /etc/default/crda
sed -i 's|REGDOMAIN=.*$|REGDOMAIN='"${wifiCountry}"'|g' /etc/default/crda

if is_pi; then
echo "OK, rebooting... "
reboot
else
wpa_cli reconfigure &> /dev/null
echo "OK"
fi
if is_pi; then
echo "OK (rebooting)"
reboot
else
wpa_cli reconfigure &> /dev/null
echo "OK"
fi
fi


echo -n "$(timestamp) [openHABian] Ensuring network connectivity... "
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
if grep -qs "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:"
iwlist wlan0 scanning | grep "ESSID" | sed 's/^\s*ESSID:/\t- /g'
echo "$(timestamp) [openHABian] Please try again with your correct SSID and password. The following Wi-Fi configuration was used:"
Expand All @@ -144,7 +137,6 @@ if tryUntil "ping -c1 www.example.com &> /dev/null || curl --silent --head http:
echo "$(timestamp) [openHABian] The public internet is not reachable. Please check your local network environment."
echo "$(timestamp) [openHABian] We will continue trying to get your system installed, but without proper Internet connectivity this is not guaranteed to work."
fi
#fail_inprogress
fi
echo "OK"

Expand Down Expand Up @@ -176,7 +168,7 @@ else
fi
fi

if [[ -x $(command -v python3) ]]; then bash /boot/webif.bash reinsure_running; fi
if [[ -x $(command -v python3) ]]; then bash /boot/webserver.bash "reinsure_running"; fi

if ! dpkg -s 'git' &> /dev/null; then
echo -n "$(timestamp) [openHABian] Installing git package... "
Expand Down Expand Up @@ -214,7 +206,9 @@ echo "$(timestamp) [openHABian] First time setup successfully finished. Rebootin
echo "$(timestamp) [openHABian] After rebooting the openHAB dashboard will be available at: http://${HOSTNAME:-openhab}:8080"
echo "$(timestamp) [openHABian] After rebooting to gain access to a console, simply reconnect using ssh."
sleep 12
if [[ -x $(command -v python3) ]]; then bash /boot/webif.bash inst_done; fi
if [[ -x $(command -v python3) ]]; then bash /boot/webserver.bash "inst_done"; fi
sleep 12
if [[ -x $(command -v python3) ]]; then bash /boot/webserver.bash "cleanup"; fi

if running_in_docker; then
PID="/var/lib/openhab2/tmp/karaf.pid"
Expand Down
65 changes: 0 additions & 65 deletions build-image/webif.bash

This file was deleted.

35 changes: 35 additions & 0 deletions build-image/webserver.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

## start: Starts a minimalistic web server that shows the status of the
## openHABian installation.
## reinsure_running: Checks if webserver is running.
## inst_done: Create finish message and link to http://${HOSTNAME:-openhab}:8080.
## cleanup: Stops the webserver and removes all no longer needed files.

port="80"
isWebRunning="$(ps -ef | pgrep python3)"

if [[ "$1" = "start" ]]; then
mkdir -p /tmp/webserver
ln -sf /boot/first-boot.log /tmp/webserver/first-boot.txt
cp /opt/openhabian/includes/install-log.html /tmp/webserver/index.html
(cd /tmp/webserver || exit 1; python3 -m http.server "$port" &> /dev/null &)
fi

if [[ $1 == "reinsure_running" ]]; then
if [[ -z $isWebRunning ]]; then
(cd /tmp/webserver || exit 1; python3 -m http.server "$port" &> /dev/null &)
fi
fi

if [[ $1 == "inst_done" ]]; then
sed 's|%HOSTNAME|'"${HOSTNAME:-openhab}"'|g' /opt/openhabian/includes/install-complete.html > /tmp/webserver/index.html
fi

if [[ $1 == "cleanup" ]]; then
if [[ -n $isWebRunning ]]; then
kill "$isWebRunning" &> /dev/null
fi
rm -rf /tmp/webserver &> /dev/null
rm -f /boot/webserver.bash &> /dev/null
fi
6 changes: 3 additions & 3 deletions build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ elif [ "$1" == "rpi64" ]; then
elif [ "$1" == "local-test" ]; then
echo_process "Preparing local system for installation"
cp ./build-image/first-boot.bash /boot/first-boot.bash
cp ./build-image/webif.bash /boot/webif.bash
cp ./build-image/webserver.bash /boot/webserver.bash
cp ./build-image/openhabian.conf /boot/openhabian.conf
cp ./build-image/openhabian-installer.service /etc/systemd/system/
ln -sf /etc/systemd/system/openhabian-installer.service /etc/systemd/system/multi-user.target.wants/openhabian-installer.service
Expand All @@ -204,7 +204,7 @@ elif [ "$1" == "local-test" ]; then
sed -i 's|! openhabian_update &> /dev/null|true|' /boot/first-boot.bash
fi
chmod +x /boot/first-boot.bash
chmod +x /boot/webif.bash
chmod +x /boot/webserver.bash
echo_process "Local system ready for installation test. Run 'systemctl start openhabian-installer' or reboot to initiate!"
exit 0
else
Expand Down Expand Up @@ -344,7 +344,7 @@ if [[ $hw_platform == "pi-raspios32" ]] || [[ $hw_platform == "pi-raspios64beta"
cp $sourcefolder/first-boot.bash $buildfolder/boot/first-boot.bash
touch $buildfolder/boot/first-boot.log
unix2dos -q -n $sourcefolder/openhabian.${hw_platform}.conf $buildfolder/boot/openhabian.conf
cp $sourcefolder/webif.bash $buildfolder/boot/webif.bash
cp $sourcefolder/webserver.bash $buildfolder/boot/webserver.bash

# Injecting development git repo if clone_string is set and watermark build
if [[ -n "${clone_string+x}" ]]; then
Expand Down
Loading

0 comments on commit 35b2ba1

Please sign in to comment.