diff --git a/emoncmsupdate b/emoncmsupdate index 1ccd884b..4b8bb5f8 100755 --- a/emoncmsupdate +++ b/emoncmsupdate @@ -1,83 +1,125 @@ #!/bin/bash echo -echo "=================================" +echo "===========================================" echo "Emoncms update started" -echo "Emoncms update script V1.2.0" -echo - +echo "Emoncms update script V1.2.1 (6th Feb 2019)" date - +echo "===========================================" echo -######################################################################################### -echo "#############################################################" +username="pi" +homedir="/home/$username" + +echo "username: $username" echo + +echo "Checking environment:" + +# Check that specified user directory exists +if [ -d $homedir ]; then + echo "- User directory $homedir found" +else + echo "- User directory $homedir not found (please ammend username)" + exit 0 +fi + +emonSD_pi_env=0 +# Check for pi user +pi=$(id -u pi) +if [ $pi ] && [ -d /home/pi ]; then + echo "- pi user and pi user directory found" + emonSD_pi_env=1 +else + echo "- could not find pi user or pi user directory" + emonSD_pi_env=0 +fi + # Check emonSD version image_version=$(ls /boot | grep emonSD) -echo "emonSD version: $image_version" -echo +if [ "$image_version" = "" ]; then + echo "- Could not find emonSD version file" + emonSD_pi_env=0 +else + echo "- emonSD version: $image_version" +fi + +if [ "$emonSD_pi_env" = "0" ]; then + echo "- Assuming non emonSD install" +fi + +# Check emoncms directory +if [ -d /var/www/emoncms ]; then + emoncms_dir="/var/www/emoncms" +else + if [ -d /var/www/html/emoncms ]; then + emoncms_dir="/var/www/html/emoncms" + else + echo "emoncms directory not found" + exit 0 + fi +fi +echo "- emoncms directory: $emoncms_dir" + +echo uid=`id -u` echo "EUID: $uid" if [ "$uid" = "0" ] ; then - # update is being ran mistakenly as root, switch to Pi user - echo "update running as root: switching to Pi user & restarting script" + # update is being ran mistakenly as root, switch to user + echo "update running as root: switching to $username user & restarting script" echo echo "**MANUAL SYSTEM REBOOT REQUIRED**" echo - echo "Please shutdown and reboot emonPi then run Update again" - su -c $0 pi + echo "Please reboot and run update again" + su -c $0 $username exit fi ######################################################################################### +if [ "$emonSD_pi_env" = "1" ]; then + + # Install sudoers entry to enable emoncms reboot button + if [ ! -f /etc/sudoers.d/emoncms-rebootbutton ]; then + # double check correct sudo syntax + sudo visudo -cf $homedir/emonpi/emoncms-rebootbutton && \ + sudo cp $homedir/emonpi/emoncms-rebootbutton /etc/sudoers.d/ + sudo chmod 0440 /etc/sudoers.d/emoncms-rebootbutton + echo + echo "Install emonPi Emoncms admin reboot button sudoers entry" + fi -# Install sudoers entry to enable emoncms reboot button - -if [ ! -f /etc/sudoers.d/emoncms-rebootbutton ]; then - # double check correct sudo syntax - sudo visudo -cf /home/pi/emonpi/emoncms-rebootbutton && \ - sudo cp /home/pi/emonpi/emoncms-rebootbutton /etc/sudoers.d/ - sudo chmod 0440 /etc/sudoers.d/emoncms-rebootbutton - echo - echo "Install emonPi Emoncms admin reboot button sudoers entry" -fi - -######################################################################################### - -# Install sudoers entry to enable toggling file-system RO/RW mount via Emoncms admin + ######################################################################################### + # Install sudoers entry to enable toggling file-system RO/RW mount via Emoncms admin -if [ ! -f /etc/sudoers.d/emoncms-filesystem ]; then - # double check correct sudo syntax - sudo visudo -cf /home/pi/emonpi/emoncms-filesystem && \ - sudo cp /home/pi/emonpi/emoncms-filesystem /etc/sudoers.d/ - sudo chmod 0440 /etc/sudoers.d/emoncms-filesystem - echo - echo "emonPi Emoncms admin file-system toggle buttons sudoers entry installed" + if [ ! -f /etc/sudoers.d/emoncms-filesystem ]; then + # double check correct sudo syntax + sudo visudo -cf $homedir/emonpi/emoncms-filesystem && \ + sudo cp $homedir/emonpi/emoncms-filesystem /etc/sudoers.d/ + sudo chmod 0440 /etc/sudoers.d/emoncms-filesystem + echo + echo "emonPi Emoncms admin file-system toggle buttons sudoers entry installed" + fi fi - ######################################################################################### - # Install sudoers entry to enable emonhub reboot button if [ ! -f /etc/sudoers.d/emonhub-sudoers ]; then # double check correct sudo syntax - sudo visudo -cf /home/pi/emonpi/emonhub-sudoers && \ - sudo cp /home/pi/emonpi/emonhub-sudoers /etc/sudoers.d/ + sudo visudo -cf $homedir/emonpi/emonhub-sudoers && \ + sudo cp $homedir/emonpi/emonhub-sudoers /etc/sudoers.d/ sudo chmod 0440 /etc/sudoers.d/emonhub-sudoers echo - echo "emonhub service constol sudoers entry installed" + echo "emonhub service control sudoers entry installed" fi ######################################################################################### - # Install sudoers entry for WiFI AP if [ ! -f /etc/sudoers.d/emoncms-setup-sudoers ]; then # double check correct sudo syntax - sudo visudo -cf /home/pi/emonpi/emoncms-setup/emoncms-setup-sudoers && \ - sudo cp /home/pi/emonpi/emoncms-setup/emoncms-setup-sudoers /etc/sudoers.d/ + sudo visudo -cf $homedir/emonpi/emoncms-setup/emoncms-setup-sudoers && \ + sudo cp $homedir/emonpi/emoncms-setup/emoncms-setup-sudoers /etc/sudoers.d/ sudo chmod 0440 /etc/sudoers.d/emoncms-setup-sudoers echo echo "Emoncms setup module sudoers entry installed" @@ -85,38 +127,45 @@ fi ######################################################################################### -# Setup user group to enable reading pi GPU temperature +# Setup user group to enable reading GPU temperature (pi only) #https://github.com/emoncms/emoncms/pull/869 - -sudo usermod -G video www-data - +if [ "$emonSD_pi_env" = "1" ]; then + sudo usermod -G video www-data +fi ######################################################################################### # Record current state of emoncms settings.php echo -current_settings_md5="$(/home/pi/emonpi/./md5sum.py /var/www/emoncms/settings.php)" +current_settings_md5="$($homedir/emonpi/./md5sum.py $emoncms_dir/settings.php)" echo "current settings.php md5: $current_settings_md5" -current_default_settings_md5="$(/home/pi/emonpi/md5sum.py /var/www/emoncms/default.emonpi.settings.php)" +current_default_settings_md5="$($homedir/emonpi/md5sum.py $emoncms_dir/default.emonpi.settings.php)" echo "Default settings.php md5: $current_default_settings_md5" ######################################################################################### # Pulling in latest Emoncms changes echo -echo "git pull /var/www/emoncms" -cd /var/www/emoncms -git branch -git status -git pull +echo "Checking status of $emoncms_dir git repository" +cd $emoncms_dir +branch=$(git branch | grep \* | cut -d ' ' -f2) +echo "- git branch: $branch" +changes=$(git diff-index --quiet HEAD --) +if $changes; then + echo "- no local changes" + echo "- running: git pull origin $branch" + echo + git pull origin $branch +else + echo "- changes" +fi ######################################################################################### # check to see if user has modifed settings.php and if update is need. Auto apply of possible echo -new_default_settings_md5="$(/home/pi/emonpi/md5sum.py /var/www/emoncms/default.emonpi.settings.php)" +new_default_settings_md5="$($homedir/emonpi/md5sum.py $emoncms_dir/default.emonpi.settings.php)" echo "NEW default settings.php md5: $new_default_settings_md5" - if [ "$current_default_settings_md5" == "$current_settings_md5" ]; then echo "settings.php has NOT been user modifed" settings_unmodified=true @@ -129,202 +178,250 @@ fi if [ "$new_default_settings_md5" != "$current_default_settings_md5" ]; then echo "Update required to settings.php..." if [ $settings_unmodified == true ]; then - sudo cp /var/www/emoncms/default.emonpi.settings.php /var/www/emoncms/settings.php + sudo cp $emoncms_dir/default.emonpi.settings.php $emoncms_dir/settings.php echo "settings.php autoupdated" else echo "**ERROR: unable to autoupdate settings.php since user changes are present, manual review required**" fi else - echo "settings.php not updated" -fi -######################################################################################### - -echo - -if [ -d /var/www/emoncms/Modules/nodes ]; then - echo "git pull /var/www/emoncms/Modules/nodes" - cd /var/www/emoncms/Modules/nodes - git status - git branch - git pull + echo "settings.php not updated" fi - -echo - -echo "git pull /var/www/emoncms/Modules/app" -cd /var/www/emoncms/Modules/app -git branch -git status -git pull -git checkout stable - echo - -echo "git pull /var/www/emoncms/Modules/config" -cd /var/www/emoncms/Modules/config -git branch -git status -git pull -git checkout stable - -echo - -echo "git pull /var/www/emoncms/Modules/wifi" -cd /var/www/emoncms/Modules/wifi -git branch -git status -git pull -git checkout stable - -echo "git pull /var/www/emoncms/Modules/dashboard" -cd /var/www/emoncms/Modules/dashboard -git status -git pull -git checkout stable - +######################################################################################### +echo "==========================================" +echo "UPDATING EMONCMS MODULES" +echo "==========================================" echo -if [ -d /var/www/emoncms/Modules/graph ]; then - echo "git pull /var/www/emoncms/Modules/graph" - cd /var/www/emoncms/Modules/graph - git branch - git status - git pull - git checkout stable -else - echo "Installing Emoncms graph module https://github.com/emoncms/graph" - cd /var/www/emoncms/Modules - git clone https://github.com/emoncms/graph -fi +# Update modules installed directly in the Modules folder +for M in $emoncms_dir/Modules/*; do + if [ -d "$M/.git" ]; then + echo "------------------------------------------" + echo "Updating $M module" + echo "------------------------------------------" + + branch=$(git -C $M branch | grep \* | cut -d ' ' -f2) + echo "- git branch: $branch" + tags=$(git -C $M describe --tags) + echo "- git tags: $tags" + + changes=$(git -C $M diff-index HEAD --) + if [ "$changes" = "" ]; then + echo "- no local changes" + echo "- running: git pull origin $branch" + echo + git -C $M pull origin $branch + else + echo "- git status:" + echo + git -C $M status + echo + fi + + echo + fi +done + +# Update modules installed in the home/user folder +for module in "postprocess" "sync" "backup"; do + + echo "------------------------------------------" + echo "Updating $module module" + echo "------------------------------------------" + if [ -d $homedir/$module ]; then + cd $homedir/$module + branch=$(git branch | grep \* | cut -d ' ' -f2) + echo "- git branch: $branch" + tags=$(git describe --tags) + echo "- git tags: $tags" + changes=$(git diff-index HEAD --) + if [ "$changes" = "" ]; then + echo "- no local changes" + echo "- running: git pull origin $branch" + echo + git pull origin $branch + # ln -sf $homedir/$module/$module-module $emoncms_dir/Modules/$module + else + echo "- git status:" + echo + git status + echo + fi + else + echo "$module module does not exist" + fi + echo +done +######################################################################################### +# Automatic installation of new modules if they dont already exist +echo "==========================================" +echo "AUTO INSTALL EMONCMS MODULES" +echo "==========================================" + +# Direct installation in Modules folder +for module in "graph" "device"; do + if ! [ -d $emoncms_dir/Modules/$module ]; then + echo "- Installing Emoncms $module module https://github.com/emoncms/$module" + cd $emoncms_dir/Modules + git clone https://github.com/emoncms/$module + cd $emoncms_dir/Modules/$module + if [ `git branch --list stable` ]; then + echo "-- git checkout stable" + git checkout stable + fi + else + echo "- $module module already installed" + fi +done +# Direct installation in Modules folder +for module in "postprocess" "sync" "backup"; do + if ! [ -d $homedir/$module ]; then + echo + echo "- Installing Emoncms $module module https://github.com/emoncms/$module" + cd $homedir + git clone https://github.com/emoncms/$module + ln -s $homedir/$module/$module-module $emoncms_dir/Modules + cd $homedir/$module/$module-module + if [ `git branch --list stable` ]; then + echo "-- git checkout stable" + git checkout stable + fi + echo + else + echo "- $module module already installed" + fi +done echo +######################################################################################### +# sudo service apache2 restart -if [ -d /home/pi/postprocess ]; then - echo "git pull /home/pi/postprocess" - cd /home/pi/postprocess - sudo chmod 777 .git -R - git checkout emonpi - git pull - ln -sf /home/pi/postprocess/postprocess-module /var/www/emoncms/Modules/postprocess -else - echo "git clone https://github.com/emoncms/postprocess" - cd /home/pi - git clone -b emonpi https://github.com/emoncms/postprocess - cd /home/pi/postprocess - git checkout emonpi -fi - -if [ -d /home/pi/sync ]; then - echo "git pull /home/pi/sync" - cd /home/pi/sync - git checkout master - git pull - ln -sf /home/pi/sync/sync-module /var/www/emoncms/Modules/sync -else - echo "git clone https://github.com/emoncms/sync" - cd /home/pi - git clone https://github.com/emoncms/sync - cd /home/pi/sync - git checkout master - ln -sf /home/pi/sync/sync-module /var/www/emoncms/Modules/sync -fi - -if [ -d /var/www/emoncms/Modules/device ]; then - echo "git pull /var/www/emoncms/Modules/device" - cd /var/www/emoncms/Modules/device - git checkout master - git pull -else - echo "git clone https://github.com/emoncms/device" - cd /var/www/emoncms/Modules - git clone https://github.com/emoncms/device - cd /var/www/emoncms/Modules/device - git checkout master -fi - - -if [ -d /home/pi/backup ]; then - echo "git pull /home/pi/backup" - cd /home/pi/backup - git branch - git status - git pull - git checkout stable -else - echo "Installing Emoncms backup module from https://github.com/emoncms/backup" - cd /home/pi - git clone https://github.com/emoncms/backup - cd /home/pi/backup - git checkout stable - ln -s /home/pi/backup/backup/ /var/www/emoncms/Modules/backup - echo "Restarting apache web-server" - sudo service apache2 restart -fi - -# Install emoncms-setup module (symlink from emonpi repo) -if [ -d /home/pi/emonpi/emoncms-setup ] && [ ! -d /var/www/emoncms/Modules/setup ]; then - echo "Installing emoncms/emonPi setup module: symlink from ~/emonpi/emoncms-setup" - ln -s /home/pi/emonpi/emoncms-setup /var/www/emoncms/Modules/setup -else - if [ ! -d /home/pi/emonpi/emoncms-setup ]; then - echo "Cannot find emoncms-setup module, please update ~/emonpi repo" +if [ "$emonSD_pi_env" = "1" ]; then + # Install emoncms-setup module (symlink from emonpi repo) + if [ -d $homedir/emonpi/emoncms-setup ] && [ ! -d $emoncms_dir/Modules/setup ]; then + echo "Installing emoncms/emonPi setup module: symlink from ~/emonpi/emoncms-setup" + ln -s $homedir/emonpi/emoncms-setup $emoncms_dir/Modules/setup + else + if [ ! -d $homedir/emonpi/emoncms-setup ]; then + echo "Cannot find emoncms-setup module, please update ~/emonpi repo" + fi fi + echo fi +################################################################################################ +# Removal of services +################################################################################################ +echo "==========================================" +echo "SERVICES" +echo "==========================================" +# 6th Feb 2019: mqtt_input renamed to emoncms_mqtt +for service in "mqtt_input"; do + + if [ -f /etc/init.d/$service ]; then + echo "removing initd $service service" + sudo /etc/init.d/$service stop + sudo rm /etc/init.d/$service + fi -echo + if [ -L /lib/systemd/system/$service.service ] || [ -f /lib/systemd/system/$service.service ]; then + echo "Removing $service.service" + sudo systemctl stop $service.service + sudo systemctl disable $service.service + sudo rm /lib/systemd/system/$service.service + sudo systemctl daemon-reload + fi + # if the service still exists in /etc then remove it + if [ -L /etc/systemd/system/$service.service ] || [ -f /etc/systemd/system/$service.service ]; then + echo "Removing $service.service from /etc/systemd/system" + sudo rm /etc/systemd/system/$service.service + fi + + # testing on emonpi the above attempts to stop did not work? + # make sure its dead!! + sudo pkill -f phpmqtt_input.php +done ################################################################################################ -if [ -d /etc/systemd ] && [ -f /var/www/emoncms/scripts/mqtt_input.service ]; then - if [ -f /etc/init.d/mqtt_input ]; then - echo "replacing initd mqtt_input with systemd mqtt input" - sudo /etc/init.d/mqtt_input stop - sudo rm /etc/init.d/mqtt_input - sudo cp /var/www/emoncms/scripts/mqtt_input.service /etc/systemd/system/mqtt_input.service - sudo systemctl daemon-reload - sudo systemctl enable mqtt_input.service - sudo systemctl start mqtt_input.service - else - echo "update mqtt_input systemd unit file" - sudo cp /var/www/emoncms/scripts/mqtt_input.service /etc/systemd/system/mqtt_input.service - sudo systemctl daemon-reload +# Installation or correction of services +################################################################################################ +for service in "emoncms_mqtt" "feedwriter" "service-runner"; do + + if [ -d /etc/systemd ] && [ -f $emoncms_dir/scripts/services/$service/$service.service ]; then + if [ -f /etc/init.d/$service ]; then + echo "removing initd $service service" + sudo /etc/init.d/$service stop + sudo rm /etc/init.d/$service fi -fi + + # service will be symlinked to /etc/systemd/system by "systemctl enable" + if [ -f /etc/systemd/system/$service.service ]; then + if ! [ -L /etc/systemd/system/$service.service ]; then + echo "Removing hard copy of $service.service from /etc/systemd/system (should be symlink)" + sudo systemctl stop $service.service + sudo systemctl disable $service.service + sudo rm /etc/systemd/system/$service.service + sudo systemctl daemon-reload + fi + fi + + if [ -f /lib/systemd/system/$service.service ]; then + if ! [ -L /lib/systemd/system/$service.service ]; then + echo "Removing hard copy of $service.service in /lib/systemd/system (should be symlink)" + sudo systemctl stop $service.service + sudo systemctl disable $service.service + sudo rm /lib/systemd/system/$service.service + sudo systemctl daemon-reload + fi + fi + + if [ ! -f /lib/systemd/system/$service.service ]; then + echo "Installing $service.service in /lib/systemd/system (creating symlink)" + sudo ln -s $emoncms_dir/scripts/services/$service/$service.service /lib/systemd/system + sudo systemctl enable $service.service + sudo systemctl start $service.service + else + echo "$service.service already installed" + fi + fi +done +echo "------------------------------------------" +################################################################################################ ## Don't overwrite settings unless required (un comment if required) # echo "Copy new default.emonpi.settings.php to settings.php & make backup old.emonpi.settings.php" -# sudo cp /var/www/emoncms/settings.php /var/www/emoncms/old.settings.php -# sudo cp /var/www/emoncms/default.emonpi.settings.php /var/www/emoncms/settings.php - -echo +# sudo cp $emoncms_dir/settings.php $emoncms_dir/old.settings.php +# sudo cp $emoncms_dir/default.emonpi.settings.php $emoncms_dir/settings.php +#if [ "$emonSD_pi_env" = "1" ]; then echo "Update Emoncms database" -php /home/pi/emonpi/emoncmsdbupdate.php - +php $homedir/emonpi/emoncmsdbupdate.php echo +#fi echo "Restarting Services..." -sudo /etc/init.d/emonhub restart -sudo /etc/init.d/feedwriter restart - -echo -if [ -f /etc/init.d/emoncms-nodes-service ]; then - sudo /etc/init.d/emoncms-nodes-service restart +if [ -d /lib/systemd/system ]; then + sudo systemctl daemon-reload fi +for service in "feedwriter" "mqtt_input" "emoncms_mqtt" "emoncms-nodes-service" "emonhub" "openhab"; do + if [ -f /lib/systemd/system/$service.service ]; then + echo "- sudo systemctl restart $service.service" + sudo systemctl restart $service.service + state=$(systemctl show $service | grep ActiveState) + echo "--- $state ---" + elif [ -f /etc/init.d/$service ]; then + echo "- sudo /etc/init.d/$service restart" + sudo /etc/init.d/$service restart + sudo /etc/init.d/$service status + fi +done echo -if [ -f /etc/init.d/openhab ]; then - sudo /etc/init.d/openhab restart +if [ "$emonSD_pi_env" = "1" ]; then + echo "set log rotate config owner to root" + sudo chown root:root /etc/logrotate.conf fi - echo -if [ -f /etc/init.d/mqtt_input ]; then - sudo /etc/init.d/mqtt_input start - sleep 1 - sudo /etc/init.d/mqtt_input restart -else - sudo systemctl restart mqtt_input.service -fi -echo -echo "set log rotate config owner to root" -sudo chown root:root /etc/logrotate.conf -echo "Restarting Services..." +echo "------------------------------------------" +echo "Emoncms update script complete" +echo "------------------------------------------" + diff --git a/emonhubupdate b/emonhubupdate index 3a8610e5..5ea71fb4 100755 --- a/emonhubupdate +++ b/emonhubupdate @@ -1,9 +1,55 @@ #!/bin/bash echo echo "=================================" -echo "EmonPi update started" +echo "emonHub update started" echo "=================================" +username="pi" +homedir="/home/$username" + +service="emonhub" +servicepath="$homedir/emonhub/service/emonhub.service" + +if [ -d /etc/systemd ] && [ -f $servicepath ]; then + if [ -f /etc/init.d/$service ]; then + echo "removing initd $service service" + sudo /etc/init.d/$service stop + sudo rm /etc/init.d/$service + fi + + # service will be symlinked to /etc/systemd/system by "systemctl enable" + if [ -f /etc/systemd/system/$service.service ]; then + if ! [ -L /etc/systemd/system/$service.service ]; then + echo "Removing hard copy of $service.service from /etc/systemd/system (should be symlink)" + sudo systemctl stop $service.service + sudo systemctl disable $service.service + sudo rm /etc/systemd/system/$service.service + sudo systemctl daemon-reload + fi + fi + + if [ -f /lib/systemd/system/$service.service ]; then + if ! [ -L /lib/systemd/system/$service.service ]; then + echo "Removing hard copy of $service.service in /lib/systemd/system (should be symlink)" + sudo systemctl stop $service.service + sudo systemctl disable $service.service + sudo rm /lib/systemd/system/$service.service + sudo systemctl daemon-reload + fi + fi + + if [ ! -f /lib/systemd/system/$service.service ]; then + echo "Installing $service.service in /lib/systemd/system (creating symlink)" + sudo ln -s $servicepath /lib/systemd/system + sudo systemctl enable $service.service + sudo systemctl start $service.service + else + echo "$service.service already installed" + fi +fi + +echo echo "Running emonhub automatic node addition script" echo "EUID: $EUID" -/home/pi/emonhub/conf/nodes/emonpi_auto_add_nodes.sh +$homedir/emonhub/conf/nodes/emonpi_auto_add_nodes.sh +