Auto Update Script

From ubermix Wiki
Revision as of 20:22, 5 February 2013 by Cytochromec (talk | contribs)
Jump to: navigation, search

As of version 0.912 the ubermix has a network auto update script installed by default - prior versions include it as part of the default simple updates files (see about the default simple updates files on the key). This script offers the capability to apply updates to a any number of devices whenever they connect to the network. The functionality of the auto update script is quite simple and effective, as follows: when a network connection is established, the device retrieves another script file from a web server (any web server will do) and executes it. In that script, you can pull down and install files and updates and apply them to all of your devices. The script makes maintaining devices quite easy, so we would recommend configuring it for installations of any size, even if you don't know for sure that you will use it.

The default auto update script is in the user's home folder and is called ".network-autostart". Linux hides all files that begin with a ".", so you won't see it in any of the file browsing tools. The script doesn't do anything out of the box - it is essentially disabled. To edit it, open a terminal by pressing "ctrl-alt-t" and type "gedit .network-autostart". Your file will look something like the following:

#!/bin/bash
logger -p user.info "Network Autostart Script Starting"
if [ "`/sbin/ifconfig | grep -v lo | grep Link -A1 | grep inet `" ]; then
        logger -p user.info "An interface is up"
#       if [ -z `pgrep pidgin` ]; then
#               logger -p user.info "Loading Pidgin"
#               pidgin &
#       fi
#       sudo wget http://mirror.saugus.k12.ca.us/une1104/update.sh -O /root/update.sh
#       sudo chmod +x /root/update.sh
#       sudo /root/update.sh
fi

Note that lines that start with a hash mark (#) are disabled. Toward the top of the file are some examples of starting programs when an interface comes up. Near the end there is a line that begins with "wget", which contains the web address for the script file. Change this address to point to one of your web servers and remove the hash marks from the start of the the "wget" line and the two lines after it. You don't have to actually have a script on your web server to make this change, the script won't do anything if it can't download one.

Save the changes, and your device will be ready to self-update. Below is a sample script to install on the web server (for the devices to download):

#!/bin/bash

# Variables Section
#
# Set the WEBSERVER to the url location of the we server storing your updates.
# DO NOT use a trailing slash

WEBSERVER="http://webserver.yourdistrict.k12.ca.us/ubermix-updates"
REBOOT=false

# Functions Section
#
# Contains functions used by the script to handle updates. Do not delete.

standardInstall()
{
  # Performs a standard download and install of a file. Extracts any .tar.gz
  # file, making the assumption that the archive was prepared to be installed
  # from the root (/). Automatically installs any .deb using dpkg.
  # All files to download must have an accompanying md5 checksum file with
  # the same name+.md5 on the web server. To create this file, simply change
  # to the file's directory on the web server and do:
  #
  #	md5sum [filename] >[filename.md5]
  #
  # Function expects either 2 or 3 parameters in the following order:
  # 	1. name of file
  #	2. name of checkfile
  #	3. (optional) destination directory
  #
  # There can be no spaces in any parameter that are not escaped with a \
  # The purpose of the checkfile is to simply create a file in /root/ that
  # is used as a flag to indicate that the update has been installed (so that
  # already installed files don't get reinstalled every time. 
  #
  # Function returns 0 on successful install, 1 for errors or already installed

  FILE=$1
  CKFILE=$2
  RESULT=0
  DESTDIR="/root/"
  if [ -n $3 ]; then
  	if [ -d $3 ]; then
		DESTDIR=$3
	fi
  fi
  if [ ! -e "/root/.$CKFILE" ]; then
	wget "$WEBSERVER/$FILE" -O "$DESTDIR/$FILE"
	wget "$WEBSERVER/$FILE.md5" -O "/root/$FILE.md5"
	if [ -s "$DESTDIR/$FILE" ]; then
		if [ "$(md5sum $DESTDIR/$FILE | cut -d\  -f1)" = "$(cat /root/$FILE.md5 | cut -d\  -f1)" ]; then
			if `echo $1 | grep -q tar.gz`; then
				tar -xzvf $DESTDIR/$FILE -C / >>/tmp/update.log
				rm -f $DESTDIR/$FILE
			fi
			if `echo $1 | grep -q tgz`; then
				tar -xzvf $DESTDIR/$FILE -C / >>/tmp/update.log
				rm -f $DESTDIR/$FILE
			fi
			if `echo $1 | grep -q .deb`; then
				dpkg -i $DESTDIR/$FILE  >>/tmp/update.log
			fi
			touch /root/.$CKFILE
			echo "$CKFILE update applied" >>/tmp/update.log
			rm -f /root/$FILE.md5
	  	else
	    		RESULT=1
	  	fi
	else
	  RESULT=1
	fi
  else
  	RESULT=1
  fi
  return $RESULT
}


# Main Updates Section.
#
# Add your updates here. See notes for standardInstall function above for
# details on how to use it


#Example
#standardInstall showid.tar.gz showid_update



if $REBOOT; then 
	xmessage "An important update has occured. Your netbook is restarting." & >>/tmp/update.log
	sleep 5
	reboot
fi

This script automates much of the update process. So let's take a look at a few examples:

Installing printers

Add_Printers_Via_Network_Script

Simply copying a single file

Copying a single is the easiest thing to do with the update script. Let's say we want to copy the file "textbook.pdf" to the users's "Documents" folder on all of our ubermix devices. To make this happen, do the following:

  1. Copy the "textbook.pdf" file to the root of your home folder on an ubermix device.
  2. Open a terminal and type "md5sum textbook.pdf >textbook.pdf.md5" (no quotes).
  3. Copy both of these files to the web server you are using for updates.
  4. Add "standardInstall textbook.pdf textbook1 /home/user/Documents" (no quotes) above the "if $REBOOT" line in the update script. What we are telling the script is the name of the file (textbook.pdf), what we want to call our update (textbook1) - which the script uses to prevent an update from applying over and over again on every start, and the place we want it to copy the file to (/home/user/Documents).

That's it! The file will automatically copy to every device. A few things to keep in mind:

  1. No spaces are allowed in file names, so be sure to replace any spaces in your names with dashes or underscores. If you want spaces, use the archive example below.
  2. You must enter the full path of the destination for your file. That means if you want to put it somewhere in the user's home folder, say the "Documents" folder, you must precede the folder name with "/home/user/" as in the example above.


Copying an archive of files to an ubermix device

If you want to copy a bunch of files - including files with spaces in their names - to an ubermix device, you can create an archive containing all of the files in one shot for easy distribution. Let's say we want to copy a series of files in a folder called "Textbooks" to the user's home folder. To do this:

  1. Create a folder called "Textbooks" in the user's home folder on a master ubermix device.
  2. Copy all of the files you want to include to the "Textbooks" folder.
  3. Open a terminal and type "tar -czvf textbooks.tar.gz /home/user/Textbooks". This tells our device to create an archive called "textbooks.tar.gz" and include everything in the path "/home/user/Textbooks" to the archive.
  4. Type "md5sum textbooks.tar.gz >textbooks.tar.gz.md5" (no quotes).
  5. Copy both of these files to the web server you are using for updates.
  6. Add "standardInstall textbooks.tar.gz textbooks" (no quotes) above the "if $REBOOT" line in the update script. What we are telling the script is the name of the file (textbooks.tar.gz) and what we want to call our update (textbooks) - which the script uses to prevent an update from applying over and over again on every start.

The script will automatically download and extract the archive. A few things to keep in mind:

  1. No spaces are allowed in the file name for the archive (textbooks.tar.gz above), but you can have spaces in the file names inside the archive
  2. You must enter the full path of the target files for the tar command in step 3 above (note the "/home/user/" prefix) - do not use relative paths.


Installing an Ubuntu deb package

Pre-built packages are even easier to install. Let's say you have a package named "cool-program_1.0.deb":

  1. Copy the "cool-program_1.0.deb" file to the root of your home folder on an ubermix device.
  2. Open a terminal and type "md5sum cool-program_1.0.deb >cool-program_1.0.deb.md5" (no quotes).
  3. Copy both of these files to the web server you are using for updates.
  4. Add "standardInstall cool-program_1.0.deb cool-program" (no quotes) above the "if $REBOOT" line in the update script. What we are telling the script is the name of the file (cool-program_1.0.deb) and what we want to call our update (cool-program) - which the script uses to prevent an update from applying over and over again on every start.

That's it. The package will download and install automatically.