#!/bin/bash ##################################################################################################### # DinGo script "ftp-upgrade.sh" # # Applies to DinGo Pi # # This script should be placed in /opt/GoIoT/DinGo/bin # # The script must be made executable like this: # chmod +x /opt/GoIoT/DinGo/bin/ftp-upgrade.sh # # The purpose of this script, is to install or upgrade the DINGO-stack, when the network band-width # is limited or expensive. # # Note: The DINGO-stack has some dependencies that should be manually solved. This script will not try # to install these dependencies. The dependencies are these: sqlite3, libsystemd-dev, libusb-1.0-0, # libusb-1.0-0-dev, openssl, libc-bin, ntpdate and wiringpi # # This script takes one parameter. If it equals "install", then the script will try to install # the DINGO-stack. If the parameter is empty or something else, then the DINGO-stack will be upgraded. # If using install-mode, when the DINGO-stack is already installed, then the script will switch # into upgrade-mode automatically. # # When installing or upgrading, this script will download files from a remote site. # The remote directory structure starts with a platform e.g. RPI for Raspberry Pi. It then has a # directory called "rootfs", which contains the files that should be copied onto the local filesystem. # Under the "rootfs", we have two directories: one called "configfiles", which contains files that # should only be copied once to the filesystem, and one called "exec", which basicly contains all # the other files. # # Note: During each upgrade, a backup will be created of the DINGO database. # # Usage: This script should be called when installing or upgrading the DINGO-stack. # # Debug: # # Written by Danjal S. Adlersson @ Go-IoT # Created 12. Mar. 2018 # Copyright (c) Go-IoT ##################################################################################################### # Note: We are using wget to retrieve files from a remote site. This retrieval will not preserve # permissions or symbolic links. rsync is an alternative, if we want to work with permissions. # Causes the shell to exit if any subcommand or pipeline returns a non-zero status. set -e # FTP settings for the remote downloading username="dingo" password="dingdong" server="82.221.35.195" #username="ftp" #password="ftp" #server="192.168.1.22" platform="RPI" # Color settings for echo messages blue='\e[1;36m' # Blue color green='\e[1;32m' # Green color yellow='\e[1;33m' # Yellow color red='\e[1;31m' # Red color NC='\e[0m' # No color # Function definitinos. These functions are called in the script function GetFiles { # wget # -m (mirror) uses recursion and timestamping. It will not delete any files. # -nv turns off verboseness, without being quit. # -nH does not create host directories. # --cut-dirs ignores a number of parent remote directories. echo -e "${blue}GoIoT: Get executables and other...${NC}" # etc cd /etc sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/rootfs/exec/etc # lib # opt cd /opt sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/rootfs/exec/opt # usr cd /usr sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/rootfs/exec/usr # var cd /var sudo wget -m -nv -nH --cut-dirs=4 ftp://$username:$password@$server/$platform/rootfs/exec/var } function GetConfigFiles { # This should only be done once! # Unless the files do not exists. echo -e "${blue}GoIoT: Get configuration files...${NC}" cd /opt/GoIoT/DinGo/bin/ddns if [ ! -f "rn_ddns.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ddns/rn_ddns.conf fi cd /opt/GoIoT/DinGo/bin/dhcp if [ ! -f "rn_dhcp.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/dhcp/rn_dhcp.conf fi cd /opt/GoIoT/DinGo/bin/modem if [ ! -f "rn_ppp0.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/modem/rn_ppp0.conf fi if [ ! -f "rn_ppp0_state" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/modem/rn_ppp0_state fi if [ ! -f "rn_ppp0_signalstrength" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/modem/rn_ppp0_signalstrength fi if [ ! -f "rn_ppp0_signalstrengthinterval" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/modem/rn_ppp0_signalstrengthinterval fi cd /opt/GoIoT/DinGo/bin/network if [ ! -f "rn_network.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/network/rn_network.conf fi cd /opt/GoIoT/DinGo/bin/ntp if [ ! -f "rn_ntp.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ntp/rn_ntp.conf fi cd /opt/GoIoT/DinGo/bin/portforward if [ ! -f "rn_portforward.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/portforward/rn_portforward.conf fi cd /opt/GoIoT/DinGo/bin/ssl if [ ! -f "DinGoCA.key" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/DinGoCA.key fi if [ ! -f "DinGoCA.pem" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/DinGoCA.pem fi if [ ! -f "customDev.crt" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/customDev.crt fi if [ ! -f "customDev.key" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/customDev.key fi if [ ! -f "dingoserverDomain.crt" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverDomain.crt fi if [ ! -f "dingoserverDomain.csr" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverDomain.csr fi if [ ! -f "dingoserverDomain.key" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverDomain.key fi if [ ! -f "dingoserverIP.crt" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverIP.crt fi if [ ! -f "dingoserverIP.csr" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverIP.csr fi if [ ! -f "dingoserverIP.key" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/dingoserverIP.key fi if [ ! -f "openssl.cnf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/ssl/openssl.cnf fi cd /opt/GoIoT/DinGo/bin/wifi if [ ! -f "rn_hostap.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/bin/wifi/rn_hostap.conf fi cd /opt/GoIoT/DinGo/database if [ ! -f "DinGo.db" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/GoIoT/DinGo/database/DinGo.db fi cd /opt/owfs if [ ! -f "owfs.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/opt/owfs/owfs.conf fi cd /etc/ld.so.conf.d if [ ! -f "dingo.so.conf" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/etc/ld.so.conf.d/dingo.so.conf fi cd /etc/profile.d if [ ! -f "dingo.sh" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/etc/profile.d/dingo.sh fi cd /etc/udev/rules.d if [ ! -f "99-usb-serial.rules" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/etc/udev/rules.d/99-usb-serial.rules fi cd /var/www/DingoWSClient if [ ! -f "DinGoCA.pem" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/var/www/DingoWSClient/DinGoCA.pem fi cd /lib/systemd/system if [ ! -f "dingo.service" ]; then sudo wget -nv ftp://$username:$password@$server/$platform/rootfs/configfiles/lib/systemd/system/dingo.service fi } function SetPermissions { echo -e "${blue}GoIoT: Set permissions...${NC}" # Executables sudo chmod +x /etc/init.d/dingosrv sudo chmod +x /usr/local/bin/mbus-* sudo chmod +x /var/www/DingoWSClient/favicon.ico sudo chmod +x /opt/GoIoT/apt/dingo-stack_1.2017.8.28-1_armhf/etc/init.d/owserver sudo chmod +x /opt/GoIoT/DinGo/bin/dingo-* sudo chmod +x /opt/GoIoT/DinGo/bin/setupUSBdev sudo chmod +x /opt/GoIoT/DinGo/bin/i2c-tester sudo chmod +x /opt/GoIoT/DinGo/bin/mbpoll sudo chmod +x /opt/GoIoT/DinGo/bin/pingo-passthrough-srv sudo chmod +x /opt/GoIoT/DinGo/bin/pingo-tester sudo chmod +x /opt/GoIoT/DinGo/bin/modem/rn_ppp0_state sudo chmod +x /opt/GoIoT/DinGo/bin/routing/rn_clear_iptables find /opt/GoIoT/DinGo/bin/ -name '*.sh' -type f | xargs chmod +x chmod +x /opt/GoIoT/DinGo/bin/install sudo chmod +x /opt/modbus/libmodbus.so.5.1.0 sudo chmod +x /opt/owfs/bin/* sudo chmod +x /opt/owfs/lib/libow-2.9.so.9.0.0 sudo chmod +x /opt/owfs/lib/libowcapi-2.9.so.9.0.0 sudo chmod +x /opt/owfs/lib/libowcapi.la sudo chmod +x /opt/owfs/lib/libow.la sudo chmod +x /opt/owfs/lib/libownet-2.9.so.9.0.0 sudo chmod +x /opt/owfs/lib/libownet.la sudo chmod +x /opt/poco/* # Symbolic links cd /usr/local/lib sudo ln -sfn libmbus.so.0.0.8 libmbus.so.0 sudo ln -sfn libmbus.so.0 libmbus.so cd /opt/modbus sudo ln -sfn libmodbus.so.5.1.0 libmodbus.so.5 sudo ln -sfn libmodbus.so.5.1.0 libmodbus.so sudo ln -sfn libmodbus.lai libmodbus.la cd /opt/owfs/lib sudo ln -sfn libow-2.9.so.9.0.0 libow-2.9.so.9 sudo ln -sfn libowcapi-2.9.so.9.0.0 libowcapi-2.9.so.9 sudo ln -sfn libowcapi-2.9.so.9.0.0 libowcapi.so sudo ln -sfn libownet-2.9.so.9.0.0 libownet-2.9.so.9 sudo ln -sfn libownet-2.9.so.9.0.0 libownet.so sudo ln -sfn libow-2.9.so.9.0.0 libow.so } function PostScript { echo -e "${blue}GoIoT: Run post-scripts...${NC}" # First update ldconfig ldconfig # Then enable the dingo-stack service. But what if it has been disabled on purpose when upgrading. sudo systemctl enable dingo.service # Start the dingo-stack service after install or upgrade. sudo systemctl start dingo.service # Add DINGO related exports to rc.local grep -q -F '[ -f /etc/profile.d/dingo.sh ] && . /etc/profile.d/dingo.sh' /etc/rc.local || sed -i -e '$i \[ -f /etc/profile.d/dingo.sh ] && . /etc/profile.d/dingo.sh\n' /etc/rc.local # Add SSL certificate generator to rc.local grep -q -F '$DINGO_SCRIPTS/ssl/rn_auto_create_self_signed_for_ip.sh' /etc/rc.local || sed -i -e '$i \$DINGO_SCRIPTS/ssl/rn_auto_create_self_signed_for_ip.sh\n' /etc/rc.local # We are now going to copy some files to etc and overwrite them. cp -p /opt/GoIoT/apt/dingo-stack_1.2017.8.28-1_armhf/etc/init.d/owserver /etc/init.d/owserver } # MAIN SCRIPT STARTS HERE # Set default mode, install or upgrade mode="upgrade" # Check "mode" parameter if [ "$1" = "" ]; then # Parameter is empty, so we use default value echo -e "${blue}GoIoT: No parameter. Using default mode.${NC}" else if [ "$1" = "install" ]; then mode=$1 # Check if DINGO-stack is already installed if [ -f "/opt/GoIoT/DinGo/bin/dingo-stack" ]; then # File exists - so this is a upgrade echo -e "${blue}GoIoT: Switching to upgrade mode.${NC}" mode="upgrade" fi fi fi # -- INSTALL --- if [ "$mode" = "install" ]; then echo -e "${blue}GoIoT: Start installing...${NC}" # 1. RUN PRE-SCRIPTS # When we install for the first time, we need to take backup of # certain files before we copy our files to the rootfs. echo -e "${blue}GoIoT: Run pre-scripts...${NC}" if [ ! -d /opt/GoIoT/backup ]; then mkdir -p /opt/GoIoT/backup fi if [ -f /etc/init.d/owserver ]; then cp -p /etc/init.d/owserver /opt/GoIoT/backup/owserver.`date +%Y%m%d%H%M%S` fi # 2. GET EXECUTABLES AND OTHER GetFiles # 3. GET CONFIGURATION FILES GetConfigFiles # 4. SET PERMISSIONS ON EXECUTABLES SetPermissions # 5. RUN POST-SCRIPTS PostScript echo -e "${blue}GoIoT: Install finished.${NC}" exit 0 fi # -- UPGRADE -- if [ "$mode" = "upgrade" ]; then echo -e "${blue}GoIoT: Start upgrading...${NC}" # 1. RUN PRE-SCRIPTS echo -e "${blue}GoIoT: Run pre-scripts...${NC}" # Stop the dingo-stack service before upgrading sudo systemctl stop dingo.service # Take backup of the database if [ -f /opt/GoIoT/DinGo/database/DinGo.db ]; then cp -p /opt/GoIoT/DinGo/database/DinGo.db /opt/GoIoT/DinGo/database/DinGo`date +%Y%m%d%H%M%S`.db fi # 2. GET EXECUTABLES AND OTHER GetFiles # 3. GET CONFIGURATION FILES GetConfigFiles # 4. SET PERMISSIONS ON EXECUTABLES SetPermissions # 5. RUN POST-SCRIPTS PostScript echo -e "${blue}GoIoT: Finished upgrading.${NC}" exit 0 fi