Virtual Tiles@Home - Ubuntu-Source

From OpenStreetMap Wiki
Jump to navigation Jump to search

Discuss on this Talk page.

Source File

The one and only file needed to build VT@H-Ubuntu (beside a Ubuntu-Iso-CD).

You'll find the file in the VM at /usr/local/sbin/osm:

#!/bin/bash
# (c) 2008-2010 pjw
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# changelog Version 10.04
# - based on Ubuntu 10.04 LTS server optimiced for virtual machine
# - using now a partition whith 4 GB, no swap added
# - default swapfile 2048MB automatically createt on first boot
# - autostart using upstart
# - "sudo osm stop" waits up to 1 hour
# - "sudo osm stop-now" aborts after 10 seconds
# - MaxTilesetComplexity=5000000 removed (default is again 0)
# - directory of osm is in /home/openstreetmap/tilesAtHome
# - don't update/reinstall programs after publishing to public, security updates will still install automatically
# - http-proxy question
# - crontab not any more used
# - password entry not any more visible on typing
      
echo "Version 10.04 (02.05.2010)"

# osm
PRG=$(basename $0)

# User, if changped, also change writeInit()
TILES_USER="openstreetmap"
# Directory, if changed, also change getSVN() and writeInit()
TILES_HOME="/home/$TILES_USER/tilesAtHome"

PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin"
START="$TILES_HOME/start.sh"

if [ "$(id -u)" != "0" ]; then
  # sudo $(pwd)/$0 $@
  echo "Use: sudo $0 $@"
  exit
fi

# screen blank off
setterm -term linux -blank 0 -store

# network problem (workaround) (aptitude install dhcp3-client) 
LANG=C ifconfig | grep -v 127 | grep "inet addr" || dhclient

# needed programs
test -x /usr/bin/sudo  || { echo "installing sudo"; aptitude -y install sudo; }
test -x /sbin/rungetty || { echo "installing rungetty"; aptitude -y install rungetty; }

############
# tgc (tiles-gen-client) [start|stop|stop-now|restart]
tgc()
{
test -d $TILES_HOME && {
  test -f $TILES_HOME/authentication.conf && {
    test -f /etc/init.d/tiles-gen-client && {
      . $TILES_HOME/authentication.conf
      test "$UploadUsername" != "" && {
        /etc/init.d/tiles-gen-client $1
      }
    }
  }
}
}

############
# prepareAutostart
prepareAutostart()
{
  egrep -v -- "tail -f" /home/$TILES_USER/.bashrc >/tmp/bashrc
  printf "tty | grep tty1 >/dev/null && sudo /usr/local/sbin/$PRG auto\n" >> /tmp/bashrc
  mv /tmp/bashrc /home/$TILES_USER/.bashrc
  chown $TILES_USER.$TILES_USER /home/$TILES_USER/.bashrc
}

############
# autologin
autologin()
{
  # http://wiki.ubuntuusers.de/Autologin and https://help.ubuntu.com/community/SerialConsoleHowto
  # <<"AUTOLOG" no var expansion, <<AUTOLOG expands $...
  cat >/etc/init/tty1.conf <<AUTOLOG
# tty1 - getty
#
# This service maintains a getty on tty1 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]

respawn
exec /sbin/rungetty --autologin $TILES_USER tty1 
AUTOLOG

# .bashrc: tail (only if logged in tty1)
egrep -v "(tty1|ifconfig|tiles.log|osm)" /home/$TILES_USER/.bashrc >/tmp/bashrc
echo '
  tty | grep tty1 >/dev/null && LANG=C ifconfig | grep -v 127 | grep "inet addr"
  tty | grep tty1 >/dev/null && echo "To abort the output and enter commands (sudo osm ...) press {{key press|CTRL|C}}
  tty | grep tty1 >/dev/null && echo "To see the log again enter: tail -f /tmp/tiles.log"
  tty | grep tty1 >/dev/null && sleep 8
  tty | grep tty1 >/dev/null && tail -f /tmp/tiles.log' >>/tmp/bashrc
  mv /tmp/bashrc /home/$TILES_USER/.bashrc
  chown $TILES_USER.$TILES_USER /home/$TILES_USER/.bashrc
}

############
# installPrg
installPrg()
{
  aptitude -y update
  aptitude -y safe-upgrade
  aptitude -y install xmlstarlet subversion libgd-gd2-perl libmath-vec-perl inkscape pngcrush perlmagick zip \
              libwww-perl ttf-dejavu xfonts-thai libxml-writer-perl libset-object-perl libappconfig-perl \
              libxml-xpath-perl libxml-perl libxml-parser-perl libxml-sax-perl \
              pngnq optipng liberror-perl libipc-run-perl libclass-accessor-perl libmodule-pluggable-perl \
              joe acpi-support rungetty openssh-client openssh-server dhcp3-client unattended-upgrades
  aptitude -y clean
  aptitude -y autoclean
}

############
# getSNV 
getSVN()
{
cd $(echo $TILES_HOME | sed 's/tilesAtHome//;')
su "$TILES_USER" -c "svn co http://svn.openstreetmap.org/applications/rendering/tilesAtHome"
# tilesAtHome.conf: AutoResetInkscapePrefs=1 (-and MaxTilesetComplexity=5000000)
su "$TILES_USER" -c "egrep -v '^(AutoResetInkscapePrefs|MaxTilesetComplexityNOTANYMORE)' tilesAtHome/tilesAtHome.conf.linux >tilesAtHome/tilesAtHome.conf"
su "$TILES_USER" -c "echo 'AutoResetInkscapePrefs=1' >>tilesAtHome/tilesAtHome.conf"
su "$TILES_USER" -c "echo '#MaxTilesetComplexity=5000000' >>tilesAtHome/tilesAtHome.conf"
cd -
}

############
# setProxy
setProxy()
{
  printf "" >/tmp/proxy
  printf "HTTP-Proxy (Return for none):"
  read HttpProxy
  test "$HttpProxy" != "" && {
    export set http_proxy=$HttpProxy
    #printf "export set http_proxy=$HttpProxy" >>/home/$TILES_USER/.bashrc
    printf "export http_proxy=$HttpProxy\n" >/tmp/proxy;
    start=$(echo $HttpProxy | cut -d/ -f3)
    user=""
    pass=""
    host=""
    port=""
    if echo $start | grep "@" >/dev/null; then
      user=$(echo $start | cut -d@ -f1)
      if echo $user | grep ":" >/dev/null; then
        pass=$(echo $user | cut -d: -f2)
        user=$(echo $user | cut -d: -f1)
      fi
      s=$(echo $start | cut -d@ -f2)
    else
      s=$start
    fi
    if echo $s | grep ":" >/dev/null; then
      host=$(echo $s | cut -d: -f1)
      port=$(echo $s | cut -d: -f2)
    else
      host=$s
    fi
    echo "" >/tmp/proxy
    test "$host" != "" && echo "http-proxy-host=$host" >>/tmp/proxy
    test "$port" != "" && echo "http-proxy-port=$port" >>/tmp/proxy
    test "$user" != "" && echo "http-proxy-username=$user" >>/tmp/proxy
    test "$pass" != "" && echo "http-proxy-password=$pass" >>/tmp/proxy
  }
  egrep -v "^export http_proxy" /etc/environment >/tmp/x
  cat /tmp/proxy >>/tmp/x && 
    mv /tmp/x /etc/environment
  egrep -v "^http-proxy" /etc/subversion/servers >/tmp/x
  cat /tmp/proxy >>/tmp/x && 
    mv /tmp/x /etc/subversion/servers

}

############
# deleteUser
deleteUser()
{
egrep -v "\(UploadUsername|UploadPassword\)" $TILES_HOME/authentication.conf.example > $TILES_HOME/authentication.conf
}

############
# defaultUser
defaultUser()
{
deleteUser
echo 'UploadUsername=virtual.tiles@gmail.com' >> $TILES_HOME/authentication.conf
echo 'UploadPassword=ubuntu12' >> $TILES_HOME/authentication.conf
}

############
# setUser
setUser()
{  
  echo "Enter UploadUsername (Return for Default User):"
  read UploadUsername
  if test "$UploadUsername" != ""; then
    # if user interrupt, echo on before exit
    trap "stty echo ; exit" 1 2 15
    # echo off
    stty -echo
    # read password
    read -p "Enter UploadPassword:" UploadPassword; echo
    # echo on
    stty echo
    # switch trap off, normal handling
    trap "stty echo ; exit" 1 2 15
    deleteUser
    echo 'UploadUsername="'$UploadUsername'"' >> $TILES_HOME/authentication.conf
    echo 'UploadPassword="'$UploadPassword'"' >> $TILES_HOME/authentication.conf
  else
    defaultUser
  fi
  chown $TILES_USER.users $TILES_HOME/authentication.conf
}

############
# swapfile [sizeMB]
swapfile()
{
echo "Create swapfile, size $1 MB, please wait..."
swapoff /var/swapfile >/dev/null 2>&1
dd if=/dev/zero of=/var/swapfile bs=1024 count=$(($1 * 1024))
echo "Activate swapfile."
mkswap /var/swapfile
swapon /var/swapfile
grep -v swapfile /etc/fstab >/tmp/fstab
echo "/var/swapfile   none   swap   sw   0   0" >>/tmp/fstab
mv /tmp/fstab /etc/fstab
}

############
# compactVDI
compactVDI()
{
aptitude clean
aptitude autoclean
# remove any personal connections
rm -r /root/.ssh /home/$TILES_USER/.ssh $TILES_HOME /etc/init.d/tiles-gen-client 2>/dev/null
update-rc.d tiles-gen-client remove
test -f /var/swapfile &&
{
  swapoff /var/swapfile
  rm -f /var/swapfile
}
grep -v swapfile /etc/fstab >/tmp/fstab
mv /tmp/fstab /etc/fstab
su "$TILES_USER" -c "history -c"
history -c
test $(df -l /dev/sda1 | tail -1 | tr -s ' ' | cut -d' ' -f3) -gt 500000 && {
  echo "After shutdown, you can compact the VDI by using: VBoxManage modifyhd osm.vdi --compact"
  echo "Please wait, I write $(df -lh / | tail -1 | tr -s ' ' | cut -d' ' -f4)B data..."
  dd if=/dev/zero of=junk
  sync
  rm junk
}
}

############
# writeInit
writeInit()
{
cat >/etc/init.d/tiles-gen-client <<"CLIENT"
#!/bin/sh
### BEGIN INIT INFO
# Provides:          tiles-gen-client
# Required-Start:    $remote_fs $syslog $all 
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start OSM at boot time
# Description:       Start OSM
### END INIT INFO
#
# tilesGen@home client software init script.
# feel free to change the script.
LOG=/tmp/tiles.log
printf "$* started\n" >>$LOG

# import lsb functions for init.d scripts
. /lib/lsb/init-functions

printf "test network\n" >>$LOG

# network problem (workaround) (aptitude install dhcp3-client) 
LANG=C ifconfig | grep -v 127 | grep "inet addr" || dhclient

PIDFILE=/var/run/tilesGen.pid
# username for tiles process
TILES_USER=openstreetmap
# tilesAtHome directory
TILES_HOME="/home/$TILES_USER/tilesAtHome"
PERL="/usr/bin/perl"
# niceness level for client software
NICELEVEL=19

# timeout to wait for the client to finish on shutdown
TIMEOUT=3600
TIMEOUT_NOW=10
# name of start script
START=start.sh

# overwrite defaults
test -f /etc/default/osm && . /etc/default/osm

chmod 666 $LOG # for user openstreetmap
chown $TILES_USER.$TILES_USER $LOG


printf "init done.\n" >>$LOG

printf "start main()\n" >>$LOG

test -f $TILES_HOME/authentication.conf && . $TILES_HOME/authentication.conf
test "$UploadUsername" = "" && {
  log_action_cont_msg "UploadUsername in file $TILES_HOME/authentication.conf missing. "
  log_end_msg 0
  exit
}
        
      
case "$1" in
 start)
  printf "START\n" >>$LOG
  printf "setterm\n" >>$LOG
  setterm -term linux -blank 0 -store >> $LOG 2>&1
  printf "chmod tty1\n" >>$LOG
  # for user openstreetmap
  chmod 666 /dev/tty1 
  ls -l /dev/tty1  >>$LOG
  cd "$TILES_HOME" >>$LOG 2>&1
  log_begin_msg "Starting tilesGen@home client software..."
  printf "start daemon\n" >>$LOG
  start-stop-daemon --start --quiet --pidfile "$PIDFILE" --nicelevel "$NICELEVEL" --background --make-pidfile --chdir "$TILES_HOME" --chuid "$TILES_USER" --exec "/bin/sh" -- $START >/dev/null 2>&1
  printf "done.\n" >>$LOG
  log_end_msg 0
 ;;
 stop)
  printf "STOP\n" >>$LOG
  log_begin_msg "Stopping tilesGen@home client software (force after $TIMEOUT seconds)..."
  cd "$TILES_HOME"
  touch stopfile.txt
  chown $TILES_USER.$TILES_USER stopfile.txt
  # we have to wait for $PERL because perl was exec'ed by /bin/sh.
  start-stop-daemon --stop --quiet --chuid "$TILES_USER" --signal 0 -R $TIMEOUT --pidfile "$PIDFILE" --exec "$PERL"
  # sometimes a process is still running, make sure all is stoped
  sleep 3
  killall perl inkscape pngcrush pngnq >/dev/null 2>&1
  sleep 3
  killall -9 perl inkscape pngcrush pngnq >/dev/null 2>&1
  log_end_msg 0
 ;;
 stop-now)
  printf "STOP\n" >>$LOG
  log_begin_msg "Stopping tilesGen@home client software (force after $TIMEOUT_NOW seconds)..."
  cd "$TILES_HOME"
  touch stopfile.txt
  chown $TILES_USER.$TILES_USER stopfile.txt
  # we have to wait for $PERL because perl was exec'ed by /bin/sh.
  start-stop-daemon --stop --quiet --chuid "$TILES_USER" --signal 0 -R $TIMEOUT_NOW --pidfile "$PIDFILE" --exec "$PERL"
  # sometimes a process is still running, make sure all is stoped
  sleep 3
  killall perl inkscape pngcrush pngnq >/dev/null 2>&1
  sleep 3
  killall -9 perl inkscape pngcrush pngnq >/dev/null 2>&1
  log_end_msg 0
 ;;
 restart)
  $0 stop
  exec $0 start
 ;;
 *)
  log_action_msg "Usage: /etc/init.d/tiles-gen-client {start|stop|restart}"
  exit 1
 ;;
esac
CLIENT
chmod 755 /etc/init.d/tiles-gen-client
}

############
# writeStart
writeStart()
{
cat >$START <<"START"
#!/bin/sh
PERL=/usr/bin/perl
LOG=/tmp/tiles.log # must be writeable by the t@h user

# workarounds for inkscape problems when started from user environment
export LANG=C
unset DISPLAY

# set ulimits here to keep your system from thrashing
# must be writeable by the t@h user
rm -f stopfile.txt > /dev/null 2>&1

svn up >> $LOG 2>&1
exec $PERL tilesGen.pl loop >> $LOG 2>&1
START
chown $TILES_USER.$TILES_USER $START
chmod 755 $START
}

############
# main()

case "$1" in 
	install-local) # install on a real machine (user $TILES_USER must exist)
		 printf "\n\n\nInstall OSM (local)\n===========\n\n"
		 printf "complete log is in /var/log/install-osm.log\n\n"
		 printf "Install Global Packages\n"
		 installPrg
		 printf "add user $TILES_USER\n"
		 useradd $TILES_USER
		 printf "Install OSM Packages (SVN)\n"
		 getSVN 
		 printf "Write start.sh script.\n"
		 writeStart
		 printf "Set up a user (change later with: sudo $PRG setuser\n" 
		 setUser
		 # install swap, if none defined
		 printf "install swap, if none defined\n" 
		 grep swap /etc/fstab >/dev/null || swapfile 2048
		 printf "Write /etc/init.d/tiles-gen-client script\n" 
		 writeInit
		 test -f /etc/default/osm || {
		   echo "TILES_USER=$TILES_USER" >>/etc/default/osm
		   echo "TIMEOUT=120" >>/etc/default/osm
		   echo "NICELEVEL=19" >>/etc/default/osm
		   
		 }
		 update-rc.d tiles-gen-client defaults 90 20 >/dev/null 2>&1
		 ;;
	install) # just first time used (bevore published to the public)
		 tgc stop-now
		 printf "\n\n\nInstall OSM\n===========\n\n"
		 printf "complete log is in /var/log/install-osm.log\n\n"
		 printf "Install Global Packages\n"
		 installPrg
		 # sudo without a password (for openstreetmap)
		 echo '%admin	ALL=(ALL)	NOPASSWD: ALL' >>/etc/sudoers
		 compactVDI
		 autologin
		 prepareAutostart
		 shutdown -h now
		 exit
		   ;;
	compact) tgc stop-now
		 compactVDI
		 autologin
		 prepareAutostart
		 shutdown -h now
		 exit
		   ;;
	setuser) tgc stop-now
		 setUser
		 tgc start
		 tty | grep tty1 >/dev/null && tail -f /tmp/tiles.log
		   ;;
 	swap)    if test "$2" != ""; then 
		   swapfile $2
		 else
		   swapfile 2048
		 fi
 		   ;;
        proxy)	 setProxy
		   ;;
	auto)	 # it's called at first boot (first time published to public)
		 tgc stop-now
		 # wait for login prompt
		 for i in 1 2 3 4 5 6 7 8; do printf "\n"; sleep 1; done
		 printf "\n\n\nInstall OSM\n===========\n\n"
		 printf "complete log is in /var/log/install-osm.log\n\n"
                 # proxy set
                 setProxy
		 # don't install all programs again after publishing to public
                 #printf "Install Global Packages\n"
		 #installPrg
		 printf "Install OSM Packages (SVN)\n"
		 getSVN 
		 printf "Write start.sh script.\n"
		 writeStart
		 printf "Set up a user (change later with: sudo $PRG setuser\n" 
		 setUser
		 printf "Write /etc/init.d/tiles-gen-client script\n" 
		 writeInit
		 update-rc.d tiles-gen-client defaults 90 20 >/dev/null 2>&1
		 # install swap, if none defined
		 printf "install swap-file, if no swap-file is defined\n" 
		 grep "swapfile" /etc/fstab >/dev/null || swapfile 2048
		 # config unattended upgrades
		 sudo dpkg-reconfigure -plow unattended-upgrades
                 # remove autostart entry
                 autologin
		 # start
		 printf "Start OSM\n\n" 
		 tgc start
		 exec bash /home/$TILES_USER/.bashrc
		   ;;
	start|stop|restart|stop-now)
		 tgc $1
		   ;;
	*)	 echo "Use: $0 [start | stop | stop-now | restart | compact | setuser | swap sizeInMB | proxy]"
		   ;;
esac