Initial commit: Sale packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:49 +02:00
commit 14e3d26998
6469 changed files with 2479670 additions and 0 deletions

View file

@ -0,0 +1,152 @@
#!/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_server_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_server_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_server_wifi "Connecting to ${ESSID}"
sudo service hostapd stop
sudo killall nginx
current_iotbox_version=$(cat "/var/odoo/iotbox_version")
# Above this version we need the NetworkManager
required_version="23.09"
if [[ "$current_iotbox_version" > "$required_version" ]]; then
logger -t posbox_connect_to_server_wifi "USING NETWORK MANAGER"
# Necessary when comming from the access point
sudo ip address del 10.11.12.1/24 dev wlan0
sudo nmcli g reload
if [ -z "${PASSWORD}" ] ; then
sudo nmcli d wifi connect "${ESSID}"
else
sudo nmcli d wifi connect "${ESSID}" password "${PASSWORD}"
fi
sudo service nginx restart
else
logger -t posbox_connect_to_server_wifi "USING WPA_SUPPLICANT"
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
fi
# 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_server_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_server_wifi "Restarting odoo"
fi
if [ ${WIFI_WAS_LOST} -eq 0 ] ; then
touch "${LOST_WIFI_FILE}"
fi
logger -t posbox_connect_to_server_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}" &

View file

@ -0,0 +1,109 @@
#!/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
current_iotbox_version=$(cat "/var/odoo/iotbox_version")
# Above this version we need the NetworkManager
required_version="23.09"
if [[ "$current_iotbox_version" > "$required_version" ]]; then
logger -t posbox_connect_to_wifi "USING NETWORK MANAGER"
# Necessary when comming from the access point
sudo ip address del 10.11.12.1/24 dev wlan0
sudo nmcli g reload
if [ -z "${PASSWORD}" ] ; then
sudo nmcli d wifi connect "${ESSID}"
else
sudo nmcli d wifi connect "${ESSID}" password "${PASSWORD}"
fi
sudo service nginx restart
else
logger -t posbox_connect_to_wifi "USING WPA_SUPPLICANT"
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
fi
# 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}" &

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,36 @@
#!/usr/bin/env bash
ODOO_CONF=$(</home/pi/odoo.conf)
migrate_setting() {
TARGET_FILE="$1"
CONF_KEY="$2"
APPEND="$3"
if [ ! -f "$TARGET_FILE" ] || [ -n "$APPEND" ]
then
REGEX="$CONF_KEY = ([^[:space:]]+)"
if [[ "$ODOO_CONF" =~ $REGEX ]]
then
VALUE=${BASH_REMATCH[1]}
echo "$VALUE" >> "$TARGET_FILE"
SETTINGS_MIGRATED='true'
fi
fi
}
migrate_setting '/home/pi/odoo-remote-server.conf' 'remote_server'
migrate_setting '/home/pi/token' 'token'
migrate_setting '/home/pi/odoo-db-uuid.conf' 'db_uuid'
migrate_setting '/home/pi/odoo-enterprise-code.conf' 'enterprise_code'
migrate_setting '/home/pi/odoo-subject.conf' 'subject'
if [ ! -f '/home/pi/wifi_network.txt' ]
then
migrate_setting '/home/pi/wifi_network.txt' 'wifi_ssid'
migrate_setting '/home/pi/wifi_network.txt' 'wifi_password' 'true'
fi
if [ -n "$SETTINGS_MIGRATED" ]
then
cp '/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/odoo.conf' '/home/pi/odoo.conf'
fi

View file

@ -0,0 +1,10 @@
[options]
data_dir = /var/run/odoo
log_handler = :WARNING
log_level = warn
logfile = /var/log/odoo/odoo-server.log
pidfile = /var/run/odoo/odoo.pid
limit_time_cpu = 600
limit_time_real = 1200
max_cron_threads = 0
server_wide_modules=hw_drivers,hw_escpos,hw_posbox_homepage,web

View file

@ -0,0 +1,24 @@
#!/usr/bin/env bash
sudo mount -o remount,rw /
sudo service odoo 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
echo "addons/iot_base" >> .git/info/sparse-checkout
git fetch "${localremote}" "${localbranch}" --depth=1
git reset "${localremote}"/"${localbranch}" --hard
git clean -dfx
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 mount -o remount,ro /
sudo mount -o remount,rw /root_bypass_ramdisks/etc/cups
(sleep 5 && sudo service odoo restart) &

View file

@ -0,0 +1,34 @@
#!/usr/bin/env bash
# Write the server configuration
function connect () {
HOSTS=/root_bypass_ramdisks/etc/hosts
HOST_FILE=/root_bypass_ramdisks/etc/hostname
HOSTNAME="$(hostname)"
IOT_NAME="${1}"
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 [ "${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
}
connect "${1}"

View file

@ -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

View file

@ -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/
}

View file

@ -0,0 +1,70 @@
#!/usr/bin/env bash
FORCE_HOST_AP="${1}"
WIFI_NETWORK_FILE="/home/pi/wifi_network.txt"
COUNTER=0
# we need to wait to receive an ip address from the dhcp before enable the access point.
# only if only if no configuration file for the wifi networks is recorded
if ! [ -f "${WIFI_NETWORK_FILE}" ] && [ -z "${FORCE_HOST_AP}" ] ; then
while [ "$(hostname -I)" = '' ] && [ "$COUNTER" -le 10 ]; do sleep 2;((COUNTER++)); done
fi
# Do we have to use the NetworkManager ?
current_iotbox_version=$(cat "/var/odoo/iotbox_version")
required_version="23.11"
if [[ "$current_iotbox_version" < "$required_version" ]]; then
logger -t wireless_ap "USING WPA_SUPPLICANT REMOVING NETWORK MANAGER SERVICE"
sudo service NetworkManager stop
fi
WIRED_IP=$(hostname -I)
if [ "$WIRED_IP" ]; then
printf "My IP address is %s\n" "$WIRED_IP"
fi
# by default wlan0 is soft blocked.
# we need unblock all radio devices
rfkill unblock all
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 odoo restart
fi
# wired
else
killall nginx
service nginx restart
service dnsmasq stop
service odoo restart
fi