changeset 448:fdb30affab1c prerelease

Make checkupdate checks that the zip file in updates/ is valid for an update and make update actually performs an updatescp bbb:Bela/Makefile ./
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 19 Jun 2016 18:55:06 +0100
parents 86591d203c78
children 5e79364c9b1d 7b63e3295343
files Makefile
diffstat 1 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Jun 19 16:33:05 2016 +0100
+++ b/Makefile	Sun Jun 19 18:55:06 2016 +0100
@@ -8,7 +8,7 @@
 
 .DEFAULT_GOAL := Bela
 
-NO_PROJECT_TARGETS=help coreclean distclean stop nostartup idestart idestop idestartup idenostartup connect ideconnect
+NO_PROJECT_TARGETS=help coreclean distclean stop nostartup idestart idestop idestartup idenostartup connect ideconnect update checkupdate
 NO_PROJECT_TARGETS_MESSAGE=PROJECT or EXAMPLE should be set for all targets except: $(NO_PROJECT_TARGETS)
 # list of targets that automatically activate the QUIET=true flag
 QUIET_TARGETS=runide
@@ -305,4 +305,68 @@
 ideconnect: ## Brings in the foreground the IDE that currently is running in a screen (if any), can detach with ctrl-a ctrl-d.
 	@screen -r -S $(BELA_IDE_SCREEN_NAME)
 
-.PHONY: all clean distclean help projectclean nostartup startup startuploop debug run runfg runscreen runscreenfg runscreenfifo stop idestart idestop idestartup idenostartup ideconnect connect
+BELA_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
+UPDATES_DIR?=/root/Bela/updates
+UPDATE_SOURCE_DIR?=/tmp/belaUpdate
+UPDATE_REQUIRED_PATHS?=scripts include core scripts/update_board 
+UPDATE_BELA_TEMP?=/tmp/belaTemp
+UPDATE_BELA_BACKUP?=/tmp/belaBak
+UPDATE_BELA_MV_BACKUP?=/tmp/belaMvBak
+
+updateclean: ## Cleans the $(UPDATES_DIR) folder
+	@[ -n $(UPDATE_DIR) ] && rm -rf $(UPDATE_DIR) && mkdir -p $(UPDATE_DIR)
+
+checkupdate: ## Unzips the zip file in $(UPDATES_DIR) and checks that it contains a valid
+	# Check that exactly one zip file exists
+	@cd $(UPDATES_DIR) && COUNT=`ls -l *.zip | wc -l` && [ $$COUNT -eq 1 ] && rm -rf `ls | grep -v "\.zip$$"`
+	@#TODO: heuristics on available space. Use unzip -l and df
+	@echo uncompressed size: `unzip -l \`ls $(UPDATES_DIR)/*.zip\` | tail -n1 | awk '{print $$1}'`
+	# Delete and re-create the temp directory (first, make sure it is not empty!!)
+	@[ -n $(UPDATE_SOURCE_DIR) ] && rm -rf $(UPDATE_SOURCE_DIR) && mkdir -p $(UPDATE_SOURCE_DIR)
+	# Unzip the contents to the temp folder
+	@cd $(UPDATE_SOURCE_DIR) && unzip -qq $(UPDATES_DIR)/*zip
+	# Strip the top-level folder ( if there is only one )
+	@DIR=`ls -d $(UPDATE_SOURCE_DIR)` && COUNT=`echo $$DIR | wc -l` &&\
+	  [ $$COUNT -eq 1 ] && mv $(UPDATE_SOURCE_DIR)/* /tmp/supertemp && rm -rf $(UPDATE_SOURCE_DIR) && mv /tmp/supertemp $(UPDATE_SOURCE_DIR)
+	# Now actually check if some key-files and folders are there
+	@cd $(UPDATE_SOURCE_DIR) && FAIL=0 && for path in $(UPDATE_REQUIRED_PATHS); do `ls $$path >/dev/null 2>&1` || { FAIL=1; break; }; done;\
+	  [ $$FAIL -eq 0 ] || { echo "$$path was not found in the zip archive. Maybe it is corrupted?"; exit 1; }
+UPDATE_LOG?=~/update.log
+LOG=>> $(UPDATE_LOG) 2>&1
+update: ## Installs the update from $(UPDATES_DIR)
+update: stop
+	# Truncate the log file
+	@echo > $(UPDATE_LOG)
+	# Re-perform the check, just in case ...	
+	# Now actually check if some key-files and folders are there
+	@cd $(UPDATE_SOURCE_DIR) && FAIL=0 && for path in $(UPDATE_REQUIRED_PATHS); do `ls $$path >/dev/null 2>&1` || { FAIL=1; break; }; done;\
+	  [ $$FAIL -eq 0 ] || { echo "$$path was not found in the zip archive. Maybe it is corrupted?"; exit 1; }
+	# Duplicate the Bela folder $(BELA_DIR) to $(UPDATE_BELA_TEMP)
+	@[ -n $(UPDATE_BELA_TEMP) ] && mkdir -p $(UPDATE_BELA_TEMP)
+	@#TODO: this would allow to trim trailing slashes in case we want to be safer: a="`pwd`/" ; target=${a%/} ; echo $target
+	@rsync -a --delete-before $(BELA_DIR)/ $(UPDATE_BELA_TEMP)
+	# Also back it up in $(UPDATE_BELA_BACKUP). 
+	@[ -n $(UPDATE_BELA_BACKUP) ] && mkdir -p $(UPDATE_BELA_BACKUP)
+	@rsync -a $(BELA_DIR)/ $(UPDATE_BELA_BACKUP)
+	@#TODO: add command line switch for update_board which avoids useless stuff (e.g.: setting the date, start/stop the IDE)
+	@cd $(UPDATE_SOURCE_DIR)/scripts && BBB_ADDRESS=root@127.0.0.1 BBB_BELA_HOME=$(UPDATE_BELA_TEMP) ./update_board -y 
+	# If everything went ok, we now have the updated version of $(BELA_DIR) in $(UPDATE_BELA_TEMP) and a backup of $(BELA_DIR) in $(UPDATE_BELA_BACKUP)
+	# So let's operate the magic swap. $(BELA_DIR) is moved to $(UPDATE_BELA_MV_BACKUP) and $(UPDATE_BELA_TEMP) is moved to $(BELA_DIR).
+	# If something goes wrong at thie stage, you can always find your old $(BELA_DIR) folder at $(UPDATE_BELA_BACKUP)
+	# The fun part is that this Makefile is moved as well...
+	# We are about to kill the IDE, so just in case you are running this from within the IDE, we run the remainder of this update in a screen.
+	# Output will be logged to $(UPDATE_LOG)
+	@screen -S update-Bela -d -m bash -c '\
+	  echo so let us just make a backup of the Makefile we are working on TODO: remove this one $(LOG);\
+	  cp $(BELA_DIR)/Makefile /tmp/ $(LOG) &&\
+	  [ -n $(UPDATE_BELA_MV_BACKUP) ] $(LOG) && rm -rf $(UPDATE_BELA_MV_BACKUP) $(LOG) &&\
+	  echo Kill the IDE $(LOG) && \
+	  $(MAKE) --no-print-directory idestop $(LOG) &&\
+	  mv $(BELA_DIR) $(UPDATE_BELA_MV_BACKUP) $(LOG) && mv $(UPDATE_BELA_TEMP) $(BELA_DIR) $(LOG) &&\
+	  echo Hope we are still alive here $(LOG) &&\
+	  echo Restart the IDE $(LOG) &&\
+	  make --no-print-directory -C $(BELA_DIR) idestart $(LOG) &&\
+	  echo Update succesful $(LOG); \
+	  ' $(LOG)
+
+.PHONY: all clean distclean help projectclean nostartup startup startuploop debug run runfg runscreen runscreenfg runscreenfifo stop idestart idestop idestartup idenostartup ideconnect connect update checkupdate