diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/point_of_sale/tools/posbox/configuration | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/point_of_sale/tools/posbox/configuration')
11 files changed, 757 insertions, 0 deletions
diff --git a/addons/point_of_sale/tools/posbox/configuration/connect_to_server.sh b/addons/point_of_sale/tools/posbox/configuration/connect_to_server.sh new file mode 100755 index 00000000..3c6e8be5 --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/connect_to_server.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# Write the server configuration +function connect () { + SERVER="${1}" + CURRENT_SERVER_FILE=/home/pi/odoo-remote-server.conf + TOKEN_FILE=/home/pi/token + TOKEN="${3}" + REBOOT="${4}" + HOSTS=/root_bypass_ramdisks/etc/hosts + HOST_FILE=/root_bypass_ramdisks/etc/hostname + HOSTNAME="$(hostname)" + IOT_NAME="${2}" + IOT_NAME="${IOT_NAME//[^[:ascii:]]/}" + IOT_NAME="${IOT_NAME//[^a-zA-Z0-9-]/}" + if [ -z "$IOT_NAME" ] + then + IOT_NAME="${HOSTNAME}" + fi + sudo mount -o remount,rw / + sudo mount -o remount,rw /root_bypass_ramdisks + if [ ! -z "${1}" ] + then + echo "${SERVER}" > ${CURRENT_SERVER_FILE} + echo "${TOKEN}" > ${TOKEN_FILE} + fi + if [ "${IOT_NAME}" != "${HOSTNAME}" ] + then + sudo sed -i "s/${HOSTNAME}/${IOT_NAME}/g" ${HOSTS} + echo "${IOT_NAME}" > /tmp/hostname + sudo cp /tmp/hostname "${HOST_FILE}" + + echo "interface=wlan0" > /root_bypass_ramdisks/etc/hostapd/hostapd.conf + echo "ssid=${IOT_NAME}" >> /root_bypass_ramdisks/etc/hostapd/hostapd.conf + echo "channel=1" >> /root_bypass_ramdisks/etc/hostapd/hostapd.conf + + sudo hostname "${IOT_NAME}" + sudo reboot + fi + sudo mount -o remount,ro / + sudo mount -o remount,ro /root_bypass_ramdisks + if [ "$REBOOT" == 'reboot' ] + then + sudo service odoo restart + fi +} + +connect "${1}" "${2}" "${3}" "${4}"
\ No newline at end of file diff --git a/addons/point_of_sale/tools/posbox/configuration/connect_to_server_wifi.sh b/addons/point_of_sale/tools/posbox/configuration/connect_to_server_wifi.sh new file mode 100755 index 00000000..12c74f61 --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/connect_to_server_wifi.sh @@ -0,0 +1,133 @@ +#!/usr/bin/env bash + +# Write the server configuration +# call with ESSID and optionally a password +# when called without an ESSID, it will attempt +# to reconnect to a previously chosen network +function connect () { + SERVER="${1}" + CURRENT_SERVER_FILE=/home/pi/odoo-remote-server.conf + HOSTS=/root_bypass_ramdisks/etc/hosts + HOST_FILE=/root_bypass_ramdisks/etc/hostname + HOSTNAME="$(hostname)" + TOKEN_FILE=/home/pi/token + TOKEN="${3}" + IOT_NAME="${2}" + IOT_NAME="${IOT_NAME//[^[:ascii:]]/}" + IOT_NAME="${IOT_NAME//[^a-zA-Z0-9-]/}" + if [ -z "$IOT_NAME" ] + then + IOT_NAME="${HOSTNAME}" + fi + sudo mount -o remount,rw / + sudo mount -o remount,rw /root_bypass_ramdisks + if [ ! -z "${1}" ] + then + echo "${SERVER}" > ${CURRENT_SERVER_FILE} + echo "${TOKEN}" > ${TOKEN_FILE} + fi + if [ "${IOT_NAME}" != "${HOSTNAME}" ] + then + sudo sed -i "s/${HOSTNAME}/${IOT_NAME}/g" ${HOSTS} + echo "${IOT_NAME}" > /tmp/hostname + sudo cp /tmp/hostname "${HOST_FILE}" + + echo "interface=wlan0" > /root_bypass_ramdisks/etc/hostapd/hostapd.conf + echo "ssid=${IOT_NAME}" >> /root_bypass_ramdisks/etc/hostapd/hostapd.conf + echo "channel=1" >> /root_bypass_ramdisks/etc/hostapd/hostapd.conf + + sudo hostname "${IOT_NAME}" + fi + sudo mount -o remount,ro / + sudo mount -o remount,ro /root_bypass_ramdisks + + WPA_PASS_FILE="/tmp/wpa_pass.txt" + PERSISTENT_WIFI_NETWORK_FILE="/home/pi/wifi_network.txt" + CURRENT_WIFI_NETWORK_FILE="/tmp/current_wifi_network.txt" # used to repair connection when we lose it + LOST_WIFI_FILE="/tmp/lost_wifi.txt" + ESSID="${4}" + PASSWORD="${5}" + PERSIST="${6}" + NO_AP="${7}" + + sleep 3 + + sudo pkill -f keep_wifi_alive.sh + WIFI_WAS_LOST=$? + + # make network choice persistent + if [ -n "${ESSID}" ] ; then + if [ -n "${PERSIST}" ] ; then + logger -t posbox_connect_to_wifi "Making network selection permanent" + sudo mount -o remount,rw / + echo "${ESSID}" > ${PERSISTENT_WIFI_NETWORK_FILE} + echo "${PASSWORD}" >> ${PERSISTENT_WIFI_NETWORK_FILE} + sudo mount -o remount,ro / + fi + else + logger -t posbox_connect_to_wifi "Reading configuration from ${PERSISTENT_WIFI_NETWORK_FILE}" + ESSID=$(head -n 1 "${PERSISTENT_WIFI_NETWORK_FILE}" | tr -d '\n') + PASSWORD=$(tail -n 1 "${PERSISTENT_WIFI_NETWORK_FILE}" | tr -d '\n') + fi + + echo "${ESSID}" > ${CURRENT_WIFI_NETWORK_FILE} + echo "${PASSWORD}" >> ${CURRENT_WIFI_NETWORK_FILE} + + logger -t posbox_connect_to_wifi "Connecting to ${ESSID}" + sudo service hostapd stop + sudo killall nginx + sudo service nginx restart + sudo service dnsmasq stop + + sudo pkill wpa_supplicant + sudo ifconfig wlan0 down + sudo ifconfig wlan0 0.0.0.0 # this is how you clear the interface's configuration + sudo ifconfig wlan0 up + + if [ -z "${PASSWORD}" ] ; then + sudo iwconfig wlan0 essid "${ESSID}" + else + # Necessary in stretch: https://www.raspberrypi.org/forums/viewtopic.php?t=196927 + sudo cp /etc/wpa_supplicant/wpa_supplicant.conf "${WPA_PASS_FILE}" + sudo chmod 777 "${WPA_PASS_FILE}" + sudo wpa_passphrase "${ESSID}" "${PASSWORD}" >> "${WPA_PASS_FILE}" + sudo wpa_supplicant -B -i wlan0 -c "${WPA_PASS_FILE}" + fi + + sudo systemctl daemon-reload + sudo service dhcpcd restart + + # give dhcp some time + timeout 30 sh -c 'until ifconfig wlan0 | grep "inet " ; do sleep 0.1 ; done' + TIMEOUT_RETURN=$? + + + if [ ${TIMEOUT_RETURN} -eq 124 ] && [ -z "${NO_AP}" ] ; then + logger -t posbox_connect_to_wifi "Failed to connect, forcing Posbox AP" + sudo /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh "force" & + else + if [ ${TIMEOUT_RETURN} -ne 124 ] ; then + rm -f "${LOST_WIFI_FILE}" + fi + + if [ ! -f "${LOST_WIFI_FILE}" ] ; then + logger -t posbox_connect_to_wifi "Restarting odoo" + fi + + if [ ${WIFI_WAS_LOST} -eq 0 ] ; then + touch "${LOST_WIFI_FILE}" + fi + + logger -t posbox_connect_to_wifi "Starting wifi keep alive script" + /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh & + fi + + if [ "${IOT_NAME}" != "${HOSTNAME}" ] + then + sudo reboot + else + sudo service odoo restart + fi +} + +connect "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" & diff --git a/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh b/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh new file mode 100755 index 00000000..c843d605 --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash + +# call with ESSID and optionally a password +# when called without an ESSID, it will attempt +# to reconnect to a previously chosen network +function connect () { + WPA_PASS_FILE="/tmp/wpa_pass.txt" + PERSISTENT_WIFI_NETWORK_FILE="/home/pi/wifi_network.txt" + CURRENT_WIFI_NETWORK_FILE="/tmp/current_wifi_network.txt" # used to repair connection when we lose it + LOST_WIFI_FILE="/tmp/lost_wifi.txt" + ESSID="${1}" + PASSWORD="${2}" + PERSIST="${3}" + NO_AP="${4}" + + sleep 3 + + sudo pkill -f keep_wifi_alive.sh + WIFI_WAS_LOST=$? + + # make network choice persistent + if [ -n "${ESSID}" ] ; then + if [ -n "${PERSIST}" ] ; then + logger -t posbox_connect_to_wifi "Making network selection permanent" + sudo mount -o remount,rw / + echo "${ESSID}" > ${PERSISTENT_WIFI_NETWORK_FILE} + echo "${PASSWORD}" >> ${PERSISTENT_WIFI_NETWORK_FILE} + sudo mount -o remount,ro / + fi + else + logger -t posbox_connect_to_wifi "Reading configuration from ${PERSISTENT_WIFI_NETWORK_FILE}" + ESSID=$(head -n 1 "${PERSISTENT_WIFI_NETWORK_FILE}" | tr -d '\n') + PASSWORD=$(tail -n 1 "${PERSISTENT_WIFI_NETWORK_FILE}" | tr -d '\n') + fi + + echo "${ESSID}" > ${CURRENT_WIFI_NETWORK_FILE} + echo "${PASSWORD}" >> ${CURRENT_WIFI_NETWORK_FILE} + + logger -t posbox_connect_to_wifi "Connecting to ${ESSID}" + sudo service hostapd stop + sudo killall nginx + sudo service nginx restart + sudo service dnsmasq stop + + sudo pkill wpa_supplicant + sudo ifconfig wlan0 down + sudo ifconfig wlan0 0.0.0.0 # this is how you clear the interface's configuration + sudo ifconfig wlan0 up + + if [ -z "${PASSWORD}" ] ; then + sudo iwconfig wlan0 essid "${ESSID}" + else + # Necessary in stretch: https://www.raspberrypi.org/forums/viewtopic.php?t=196927 + sudo cp /etc/wpa_supplicant/wpa_supplicant.conf "${WPA_PASS_FILE}" + sudo chmod 777 "${WPA_PASS_FILE}" + sudo wpa_passphrase "${ESSID}" "${PASSWORD}" >> "${WPA_PASS_FILE}" + sudo wpa_supplicant -B -i wlan0 -c "${WPA_PASS_FILE}" + fi + + sudo systemctl daemon-reload + sudo service dhcpcd restart + + # give dhcp some time + timeout 30 sh -c 'until ifconfig wlan0 | grep "inet " ; do sleep 0.1 ; done' + TIMEOUT_RETURN=$? + + + if [ ${TIMEOUT_RETURN} -eq 124 ] && [ -z "${NO_AP}" ] ; then + logger -t posbox_connect_to_wifi "Failed to connect, forcing Posbox AP" + sudo /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh "force" & + else + if [ ${TIMEOUT_RETURN} -ne 124 ] ; then + rm -f "${LOST_WIFI_FILE}" + fi + + if [ ! -f "${LOST_WIFI_FILE}" ] ; then + logger -t posbox_connect_to_wifi "Restarting odoo" + sudo service odoo restart + fi + + if [ ${WIFI_WAS_LOST} -eq 0 ] ; then + touch "${LOST_WIFI_FILE}" + fi + + logger -t posbox_connect_to_wifi "Starting wifi keep alive script" + /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh & + fi +} + +connect "${1}" "${2}" "${3}" "${4}" & diff --git a/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh b/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh new file mode 100755 index 00000000..5c8a2749 --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/keep_wifi_alive.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +CURRENT_WIFI_NETWORK_FILE="/tmp/current_wifi_network.txt" +while true ; do + if [ -z "$(cat <(ifconfig eth0) <(ifconfig wlan0) | grep "inet ";)" ] ; then + ESSID=$(head -n 1 "${CURRENT_WIFI_NETWORK_FILE}" | tr -d '\n') + PASSWORD=$(tail -n 1 "${CURRENT_WIFI_NETWORK_FILE}" | tr -d '\n') + + logger -t posbox_keep_wifi_alive "Lost wifi, trying to reconnect to ${ESSID}" + + sudo /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh "${ESSID}" "${PASSWORD}" "" "NO_AP" + + sleep 30 + fi + + sleep 2 +done diff --git a/addons/point_of_sale/tools/posbox/configuration/led_status.sh b/addons/point_of_sale/tools/posbox/configuration/led_status.sh new file mode 100755 index 00000000..8d272d7d --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/led_status.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +set_brightness() { + echo "${1}" > /sys/class/leds/led0/brightness +} + +check_status_loop() { + while true ; do + if wget --quiet localhost:8069/hw_proxy/hello -O /dev/null ; then + set_brightness 255 + else + set_brightness 0 + fi + sleep 5 + done +} + +echo none > /sys/class/leds/led0/trigger +check_status_loop diff --git a/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf b/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf new file mode 100644 index 00000000..a64a6f6b --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf @@ -0,0 +1,97 @@ +user www-data; +worker_processes auto; +pid /run/nginx.pid; +include /etc/nginx/modules-enabled/*.conf; + +events { + worker_connections 768; + # multi_accept on; +} + +http { + + ## + # Basic Settings + ## + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + # server_tokens off; + + # server_names_hash_bucket_size 64; + # server_name_in_redirect off; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + ## + # SSL Settings + ## + + ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE + ssl_prefer_server_ciphers on; + + ## + # Logging Settings + ## + + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + ## + # Gzip Settings + ## + + gzip on; + gzip_disable "msie6"; + + # gzip_vary on; + # gzip_proxied any; + # gzip_comp_level 6; + # gzip_buffers 16 8k; + # gzip_http_version 1.1; + # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; + + ## + # Virtual Host Configs + ## + + include /etc/nginx/conf.d/*.conf; + #include /etc/nginx/sites-enabled/*; + server { + listen 80 default_server; + + location / { + # Every OS request specific address and .txt file to test if hotspot are connected to internet however login splash page don't display + # this line respond true for every request and send a login splash page to client + # it work for every OS + return 301 $scheme://10.11.12.1:8069; + } + +} +} + + +#mail { +# # See sample authentication script at: +# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript +# +# # auth_http localhost/auth.php; +# # pop3_capabilities "TOP" "USER"; +# # imap_capabilities "IMAP4rev1" "UIDPLUS"; +# +# server { +# listen localhost:110; +# protocol pop3; +# proxy on; +# } +# +# server { +# listen localhost:143; +# protocol imap; +# proxy on; +# } +#} diff --git a/addons/point_of_sale/tools/posbox/configuration/odoo.conf b/addons/point_of_sale/tools/posbox/configuration/odoo.conf new file mode 100644 index 00000000..e274a0fe --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/odoo.conf @@ -0,0 +1,7 @@ +[options] +data_dir = /var/run/odoo +log_level = error +logfile = /var/log/odoo/odoo-server.log +pidfile = /var/run/odoo/odoo.pid +limit_time_cpu = 600 +limit_time_real = 1200 diff --git a/addons/point_of_sale/tools/posbox/configuration/posbox_update.sh b/addons/point_of_sale/tools/posbox/configuration/posbox_update.sh new file mode 100755 index 00000000..2bfb9f3a --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/posbox_update.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +sudo mount -o remount,rw / + +sudo service led-status stop + +cd /home/pi/odoo +localbranch=$(git symbolic-ref -q --short HEAD) +localremote=$(git config branch.$localbranch.remote) + +echo "addons/point_of_sale/tools/posbox/overwrite_after_init/home/pi/odoo" >> .git/info/sparse-checkout + +git fetch "${localremote}" "${localbranch}" --depth=1 +git reset "${localremote}"/"${localbranch}" --hard + +git clean -df +cp -a /home/pi/odoo/addons/point_of_sale/tools/posbox/overwrite_after_init/home/pi/odoo/* /home/pi/odoo/ +rm -r /home/pi/odoo/addons/point_of_sale/tools/posbox/overwrite_after_init + +sudo find / -type f -name "*.iotpatch" 2> /dev/null | while read iotpatch; do + DIR=$(dirname "${iotpatch}") + BASE=$(basename "${iotpatch%.iotpatch}") + sudo find "${DIR}" -type f -name "${BASE}" ! -name "*.iotpatch" | while read file; do + sudo patch -f "${file}" < "${iotpatch}" + done +done + +sudo mount -o remount,ro / +sudo mount -o remount,rw /root_bypass_ramdisks/etc/cups + +sudo service led-status start + +(sleep 5 && sudo service odoo restart) & diff --git a/addons/point_of_sale/tools/posbox/configuration/setup_ramdisks.sh b/addons/point_of_sale/tools/posbox/configuration/setup_ramdisks.sh new file mode 100755 index 00000000..01470fff --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/setup_ramdisks.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail +# set -o xtrace + +create_ramdisk () { + ORIGINAL="${1}" + RAMDISK="${ORIGINAL}_ram" + SIZE="${2}" + echo "Creating ramdisk for ${1} of size ${SIZE}..." + + mount -t tmpfs -o size="${SIZE}" tmpfs "${RAMDISK}" + rsync -a --exclude="swap" --exclude="apt" --exclude="dpkg" --exclude=".mozilla" "${ORIGINAL}/" "${RAMDISK}/" + mount --bind "${RAMDISK}" "${ORIGINAL}" +} + +echo "Creating ramdisks..." +create_ramdisk "/var" "192M" +create_ramdisk "/etc" "16M" +create_ramdisk "/tmp" "64M" + +# bind mount / so that we can get to the real /var and /etc +mount --bind / /root_bypass_ramdisks + +# allow to cups server to save configuration file of printers +mount --bind /root_bypass_ramdisks/etc/cups /root_bypass_ramdisks/etc/cups +mount -o remount,rw /root_bypass_ramdisks/etc/cups /root_bypass_ramdisks/etc/cups diff --git a/addons/point_of_sale/tools/posbox/configuration/upgrade.sh b/addons/point_of_sale/tools/posbox/configuration/upgrade.sh new file mode 100755 index 00000000..ed3fd11a --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/upgrade.sh @@ -0,0 +1,232 @@ +#!/usr/bin/env bash + +file_exists () { + [[ -f $1 ]]; +} + +create_partition () { + mount -o remount,rw / + + if [ -f start_upgrade ] + then + echo "Error_Upgrade_Already_Started" + exit 0 + fi + + touch start_upgrade + + echo "Fdisking" + + PARTITION=$(lsblk | awk 'NR==2 {print $1}') + PARTITION="/dev/${PARTITION}" + SECTORS_SIZE=$(fdisk -l "${PARTITION}" | awk 'NR==1 {print $7}') + + if [ "${SECTORS_SIZE}" -lt 15583488 ] # self-flash not permited if SD size < 16gb + then + rm start_upgrade + echo "Error_Card_Size" + exit 0 + fi + + PART_ODOO_ROOT=$(fdisk -l | tail -n 1 | awk '{print $1}') + START_OF_ODOO_ROOT_PARTITION=$(fdisk -l | tail -n 1 | awk '{print $2}') + END_OF_ODOO_ROOT_PARTITION=$((START_OF_ODOO_ROOT_PARTITION + 11714061)) # sectors to have a partition of ~5.6Go + START_OF_UPGRADE_ROOT_PARTITION=$((END_OF_ODOO_ROOT_PARTITION + 1)) # sectors to have a partition of ~7.0Go + (echo 'p'; # print + echo 'd'; # delete partition + echo '2'; # number 2 + echo 'n'; # create new partition + echo 'p'; # primary + echo '2'; # number 2 + echo "${START_OF_ODOO_ROOT_PARTITION}"; # starting at previous offset + echo "${END_OF_ODOO_ROOT_PARTITION}"; # ending at ~9.9Go + echo 'n'; # create new partition + echo 'p'; # primary + echo '3'; # number 3 + echo "${START_OF_UPGRADE_ROOT_PARTITION}"; # starting at previous offset + echo ''; # ending at default (fdisk should propose max) ~7.0Go + echo 'p'; # print + echo 'w') |fdisk "${PARTITION}" # write and quit + + PART_RASPIOS_ROOT=$(sudo fdisk -l | tail -n 1 | awk '{print $1}') + sleep 5 + + # Clean partition + mount -o remount,rw / + partprobe # apply changes to partitions + resize2fs "${PART_ODOO_ROOT}" + mkfs.ext4 -Fv "${PART_RASPIOS_ROOT}" # change file sytstem + + echo "end fdisking" +} + +download_raspios () { + if [ ! -f *raspios*.img ] ; then + # download latest Raspios image and check integrity + LATEST_RASPIOS=$(curl -LIsw %{url_effective} http://downloads.raspberrypi.org/raspios_lite_armhf_latest | tail -n 1) + wget -c "${LATEST_RASPIOS}" + RASPIOS=$(echo *raspios*.zip) + wget -c "${LATEST_RASPIOS}".sha256 + CHECK=$(sha256sum -c "${RASPIOS}".sha256) + if [ "${CHECK}" != "${RASPIOS}: OK" ] + then + # Checksum is not correct so clean and reset self-flashing + mount -o remount,rw / + # Clean raspios img + rm "${RASPIOS}" "${RASPIOS}".sha256 + + rm start_upgrade + echo "Error_Raspios_Download" + exit 0 + fi + unzip "${RASPIOS}" + fi + + echo "end dowloading raspios" +} + +copy_raspios () { + umount -v /boot + + # mapper raspios + PART_RASPIOS_ROOT=$(fdisk -l | tail -n 1 | awk '{print $1}') + PART_ODOO_ROOT=$(fdisk -l | tail -n 2 | awk 'NR==1 {print $1}') + PART_BOOT=$(fdisk -l | tail -n 3 | awk 'NR==1 {print $1}') + RASPIOS=$(echo *raspios*.img) + LOOP_RASPIOS=$(kpartx -avs "${RASPIOS}") + LOOP_RASPIOS_ROOT=$(echo "${LOOP_RASPIOS}" | tail -n 1 | awk '{print $3}') + LOOP_RASPIOS_ROOT="/dev/mapper/${LOOP_RASPIOS_ROOT}" + LOOP_BOOT=$(echo "${LOOP_RASPIOS}" | tail -n 2 | awk 'NR==1 {print $3}') + LOOP_BOOT="/dev/mapper/${LOOP_BOOT}" + + mount -o remount,rw / + # copy raspios + dd if="${LOOP_RASPIOS_ROOT}" of="${PART_RASPIOS_ROOT}" bs=4M status=progress + e2fsck -fv "${PART_RASPIOS_ROOT}" # resize2fs requires clean fs + + # Modify startup + mkdir -v raspios + mount -v "${PART_RASPIOS_ROOT}" raspios + resize2fs "${PART_RASPIOS_ROOT}" + chroot raspios/ /bin/bash -c "sudo apt-get -y update" + chroot raspios/ /bin/bash -c "sudo apt-get -y install kpartx" + PATH_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + cp -v "${PATH_DIR}"/upgrade.sh raspios/home/pi/ + NBR_LIGNE=$(sed -n -e '$=' raspios/etc/rc.local) + sed -ie "${NBR_LIGNE}"'i\. /home/pi/upgrade.sh; copy_iot' raspios/etc/rc.local + cp -v /etc/fstab raspios/etc/fstab + sed -ie "s/$(echo ${PART_ODOO_ROOT} | sed -e 's/\//\\\//g')/$(echo ${PART_RASPIOS_ROOT} | sed -e 's/\//\\\//g')/g" raspios/etc/fstab + mkdir raspios/home/pi/config + find /home/pi -maxdepth 1 -type f ! -name ".*" -exec cp {} raspios/home/pi/config/ \; + + # download latest IoT Box image and check integrity + wget -c 'https://nightly.odoo.com/master/iotbox/iotbox-latest.zip' -O raspios/iotbox-latest.zip + wget -c 'https://nightly.odoo.com/master/iotbox/SHA1SUMS.txt' -O raspios/SHA1SUMS.txt + cd raspios/ + CHECK=$(sha1sum -c --ignore-missing SHA1SUMS.txt) + cd .. + + umount -v raspios + if [ "${CHECK}" != "iotbox-latest.zip: OK" ] + then + # Checksum is not correct so clean and reset self-flashing + rm start_upgrade + echo "Error_Iotbox_Download" + exit 0 + fi + + # copy boot + mkfs.ext4 -Fv "${PART_BOOT}" # format /boot file sytstem + e2fsck -fv "${PART_BOOT}" # clean /boot fs + dd if="${LOOP_BOOT}" of="${PART_BOOT}" bs=4M status=progress + + # Modify boot file + mkdir -v boot + mount -v "${PART_BOOT}" boot + PART_IOT_BOOT_ID=$(grep -oP '(?<=root=).*(?=rootfstype)' boot/cmdline.txt) + sed -ie "s/$(echo ${PART_IOT_BOOT_ID} | sed -e 's/\//\\\//g')/$(echo ${PART_RASPIOS_ROOT} | sed -e 's/\//\\\//g')/g" boot/cmdline.txt + umount -v boot + + kpartx -dv "${RASPIOS}" + rm -v "${RASPIOS}" + + reboot +} + +copy_iot () { + mount -o remount,rw / + + PART_IOTBOX_ROOT=$(fdisk -l | tail -n 2 | awk 'NR==1 {print $1}') + PART_BOOT=$(fdisk -l | tail -n 3 | awk 'NR==1 {print $1}') + + # unzip latest IoT Box image + unzip iotbox-latest.zip + rm -v iotbox-latest.zip + IOTBOX=$(echo *iotbox*.img) + + # mapper IoTBox + LOOP_IOTBOX=$(kpartx -avs "${IOTBOX}") + LOOP_IOTBOX_ROOT=$(echo "${LOOP_IOTBOX}" | tail -n 1 | awk '{print $3}') + LOOP_IOTBOX_ROOT="/dev/mapper/${LOOP_IOTBOX_ROOT}" + LOOP_BOOT=$(echo "${LOOP_IOTBOX}" | tail -n 2 | awk 'NR==1 {print $3}') + LOOP_BOOT="/dev/mapper/${LOOP_BOOT}" + + umount -v /boot + sleep 5 + + echo "----------------------------------" + echo "Flash in progress - Please wait..." + echo "----------------------------------" + # copy new IoT Box + dd if="${LOOP_IOTBOX_ROOT}" of="${PART_IOTBOX_ROOT}" bs=4M status=progress + # copy boot of new IoT Box + dd if="${LOOP_BOOT}" of="${PART_BOOT}" bs=4M status=progress + + mount -v "${PART_BOOT}" /boot + + # Modify boot file + PART_BOOT_ID=$(grep -oP '(?<=root=).*(?=rootfstype)' /boot/cmdline.txt) + sed -ie "s/$(echo ${PART_BOOT_ID} | sed -e 's/\//\\\//g')/$(echo ${PART_IOTBOX_ROOT} | sed -e 's/\//\\\//g')/g" /boot/cmdline.txt + sed -i 's| init=/usr/lib/raspi-config/init_resize.sh||' /boot/cmdline.txt + + # Modify startup + mkdir -v odoo + mount -v "${PART_IOTBOX_ROOT}" odoo + cp -v /home/pi/upgrade.sh odoo/home/pi/ + NBR_LIGNE=$(sed -n -e '$=' odoo/etc/rc.local) + sed -ie "${NBR_LIGNE}"'i\. /home/pi/upgrade.sh; clean_local' odoo/etc/rc.local + find /home/pi/config -maxdepth 1 -type f ! -name ".*" -exec cp {} odoo/home/pi/ \; + + reboot +} + +cleanup () { + # clean partitions + PART_RASPIOS_ROOT=$(fdisk -l | tail -n 1 | awk '{print $1}') + mkfs.ext4 -Fv "${PART_RASPIOS_ROOT}" # format file sytstem + wipefs -a "${PART_RASPIOS_ROOT}" + + PARTITION=$(echo "${PART_RASPIOS_ROOT}" | sed 's/..$//') + + (echo 'p'; # print + echo 'd'; # delete partition + echo '3'; # number 3 + echo 'p'; # print + echo 'w') |fdisk "${PARTITION}" # write and quit + + echo "end cleanup" +} + +clean_local () { + mount -o remount,rw / + mount -o remount,rw /root_bypass_ramdisks/ + cleanup + NBR_LIGNE=$(sed -n -e '$=' /root_bypass_ramdisks/etc/rc.local) + DEL_LIGNE=$((NBR_LIGNE - 1)) + sed -i "${DEL_LIGNE}"'d' /root_bypass_ramdisks/etc/rc.local + + rm /home/pi/upgrade.sh + + mount -o remount,ro / + mount -o remount,ro /root_bypass_ramdisks/ +} diff --git a/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh b/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh new file mode 100755 index 00000000..00d9814f --- /dev/null +++ b/addons/point_of_sale/tools/posbox/configuration/wireless_ap.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +FORCE_HOST_AP="${1}" +WIRED_IP=$(python3 -c "import netifaces as ni; print(ni.ifaddresses('eth0').get(ni.AF_INET) and ni.ifaddresses('eth0')[ni.AF_INET][0]['addr'] or '')") +WIFI_NETWORK_FILE="/home/pi/wifi_network.txt" + + +ifconfig wlan0 down +ifconfig wlan0 up + +# wait for wlan0 to come up +sleep 5 + +# if there is no wired ip, attempt to start an AP through wireless interface +if [ -z "${WIRED_IP}" ] ; then + logger -t posbox_wireless_ap "No wired IP" + + if [ -f "${WIFI_NETWORK_FILE}" ] && [ -z "${FORCE_HOST_AP}" ] ; then + logger -t posbox_wireless_ap "Loading persistently saved setting" + /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh & + else + logger -t posbox_wireless_ap "Starting AP" + + SSID=$(grep -oP '(?<=ssid=).*' /etc/hostapd/hostapd.conf) + + if [ "${SSID}" = "IoTBox" ] + then + # override SSID to get a unique SSID + MAC=$(ip link show wlan0 | tail -n 1 | awk '{print $2}' | sed 's/\://g') + NEWSSID="${SSID}-${MAC}" + sed -ie "s/$(echo ${SSID})/$(echo ${NEWSSID})/g" /etc/hostapd/hostapd.conf + fi + + service hostapd restart + + ip addr add 10.11.12.1/24 dev wlan0 + + service dnsmasq restart + + service nginx stop + # We start nginx in another configuration than the default one with https + # as it needs to do redirect instead in case the IoT Box acts as an ap + nginx -c /home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/nginx_ap.conf + + service odoo restart + fi +# wired +else + killall nginx + service nginx restart + service dnsmasq stop + service odoo restart +fi |
