#!/bin/bash -u #Firefox startup wrapper # #This script makes it easy maintain many firefox profiles at once # #The idea being only one instance of firefox can run on any given set of #machines at any given point in time. Thus one profile can exist for many #machines at once. This script is a wrapper to handle this. # #When ran, it inspects all the firefox profiles its knows about to decide which #one is the most up-to-date. If the local profile is the most up-to-date it #just hands off to the real firefox, if not it connects to the most up-to-date #hosts and kills the remote firefox sesion. It then pulls the remote profile #to the local one useing rsync. After this it hands everything off to the real #firefox. # #Ultimatly, when ran the final outcome of the script is to allways run firefox #via: #/usr/bin/firefox which is itself a self contained wrapper. The only time #firefox is not run is when there is a remote host with a newer profile AND #firefox is allready running locally. # #Requirements and Asumptions #-rysnc, to transfare files (over ssh) #-ntp'ed hosts, so time stamps are accurate #-ssh access from all hosts to all hosts, get info, kill firefox and transfare #-gmessage, for warnings # #To install: put this script on all machines in the path, adjust hosts= #run this script with setup as first paraeter, this till connect to all hosts #and gives them an old time stamp and give the current hosts a new stamp. ie #the current host is the uptodate one #list of sort hosts where firefox profiles resides. (must match ~/.ssh/config) hosts="diamond d8s7c1j onyx" #find out what hosts are up newhosts="" for host in $hosts; do ssh $host true test $? -eq 0 && newhosts="$newhosts $host" done hosts=$newhosts #this hostname hostname=`cat /proc/sys/kernel/hostname | awk -F. '{print $1}'` #where thr profile lives profile="$HOME/.mozilla/." #display an message using X11 message() { gmessage -center --name "Firefox Wraper" -title "Firefox Wraper" \ -wrap "$1" } #updates local profile from $1 host updatefrom() { host=$1 #Kill remote firefox ssh $host pkill -TERM -u $USER firefox-bin &> /dev/null ssh $host pkill -TERM -u $USER sunbird-bin &> /dev/null ssh $host pkill -KILL -u $USER firefox-bin &> /dev/null ssh $host pkill -KILL -u $USER sunbird-bin &> /dev/null ssh $host pgrep -u $USER firefox-bin &> /dev/null test $? -ne 1 && message "Failed to kill Firefox" && exit 1 ssh $host pgrep -u $USER sunbird-bin &> /dev/null test $? -ne 1 && message "Failed to kill Sunbird" && exit 1 #Copy remote profile to local one rsync --archive --checksum --compress --delete --progress \ $host:$profile $profile #Timestamp profile, this local profile is now definitive (ie uptodate) echo "`date +%Y%m%d%H%M%S` $hostname" > $profile/.datestamp } if [ $# -gt 1 ]; then if [ "$1" == "setup" ]; then shift for host in $hosts; do ssh $host "echo \"00000000000000 $host\" > $profile/.datestamp" done echo "`date +%Y%m%d%H%M%S` $hostname" > $profile/.datestamp fi fi #find most uptodata host uptodatehost=$( for host in $hosts; do ssh $host cat $profile/.datestamp 2> /dev/null done | sort -n | tail -1 | awk '{print $2}' ) #if the most uptodate is us if [ "$uptodatehost" == "$hostname" ]; then #we are up to date uptodate=true else #we are not up to date uptodate=false fi #lets see if firefox is runnign locally pgrep -u $USER firefox-bin &> /dev/null if [ $? -eq 1 ]; then running=false else running=true fi if [ "$uptodate" == "true" -a "$running" == "true" ]; then message "uptodate=$uptodate, running=$running\nnew window" fi if [ "$uptodate" == "true" -a "$running" == "false" ]; then message "uptodate=$uptodate, running=$running\nstarting firefox" fi if [ "$uptodate" == "false" -a "$running" == "true" ]; then message "uptodate=$uptodate, running=$running\nconfused, exiting" exit fi if [ "$uptodate" == "false" -a "$running" == "false" ]; then message "uptodate=$uptodate, running=$running\nupdating profile from $uptodatehost" updatefrom $uptodatehost fi /usr/bin/firefox $@ &