home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / etc / cron.daily / apt < prev    next >
Encoding:
Text File  |  2009-04-17  |  8.5 KB  |  286 lines

  1. #!/bin/sh
  2. #
  3.  
  4. #set -e
  5. #
  6. # This file understands the following apt configuration variables:
  7. #
  8. #  "APT::Periodic::Update-Package-Lists=1"
  9. #  - Do "apt-get update" automatically every n-days (0=disable)
  10. #    
  11. #  "APT::Periodic::Download-Upgradeable-Packages=0",
  12. #  - Do "apt-get upgrade --download-only" every n-days (0=disable)
  13. #  "APT::Periodic::AutocleanInterval"
  14. #  - Do "apt-get autoclean" every n-days (0=disable)
  15. #
  16. #  "APT::Periodic::Unattended-Upgrade"
  17. #  - Run the "unattended-upgrade" security upgrade script 
  18. #    every n-days (0=disabled)
  19. #    Requires the package "unattended-upgrades" and will write
  20. #    a log in /var/log/unattended-upgrades
  21. #  "APT::Archives::MaxAge",
  22. #  - Set maximum allowed age of a cache package file. If a cache 
  23. #    package file is older it is deleted (0=disable)
  24. #
  25. #  "APT::Archives::MaxSize",
  26. #  - Set maximum size of the cache in MB (0=disable). If the cache
  27. #    is bigger, cached package files are deleted until the size
  28. #    requirement is met (the biggest packages will be deleted 
  29. #    first).
  30. #
  31. #  "APT::Archives::MinAge"
  32. #  - Set minimum age of a package file. If a file is younger it
  33. #    will not be deleted (0=disable). Usefull to prevent races 
  34. #    and to keep backups of the packages for emergency.
  35.  
  36. check_stamp()
  37. {
  38.     stamp="$1"
  39.     interval="$2"
  40.  
  41.     if [ $interval -eq 0 ]; then
  42.         return 1
  43.     fi
  44.  
  45.     if [ ! -f $stamp ]; then
  46.         return 0
  47.     fi
  48.  
  49.     # compare midnight today to midnight the day the stamp was updated
  50.     stamp_file="$stamp"
  51.     stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
  52.     if [ "$?" != "0" ]; then
  53.         # Due to some timezones returning 'invalid date' for midnight on
  54.         # certain dates (eg America/Sao_Paulo), if date returns with error
  55.         # remove the stamp file and return 0. See coreutils bug:
  56.         # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
  57.         rm -f "$stamp_file"
  58.         return 0
  59.     fi
  60.  
  61.     now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
  62.     if [ "$?" != "0" ]; then
  63.         # As above, due to some timezones returning 'invalid date' for midnight
  64.         # on certain dates (eg America/Sao_Paulo), if date returns with error
  65.         # return 0.
  66.         return 0
  67.     fi
  68.  
  69.     delta=$(($now-$stamp))
  70.  
  71.     # intervall is in days,
  72.     interval=$(($interval*60*60*24))
  73.     #echo "stampfile: $1"
  74.     #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
  75.  
  76.     if [ $delta -ge $interval ]; then
  77.         return 0
  78.     fi
  79.  
  80.     return 1
  81. }
  82.  
  83. update_stamp()
  84. {
  85.     stamp="$1"
  86.  
  87.     touch $stamp
  88. }
  89.  
  90.  
  91.  
  92. # we check here if autoclean was enough sizewise
  93. check_size_constraints()
  94. {
  95.     # min-age in days
  96.     MaxAge=0
  97.     MinAge=2
  98.     MaxSize=0
  99.     CacheDir="var/cache/apt"
  100.     CacheArchive="archives/"
  101.     eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  102.     eval $(apt-config shell MinAge APT::Archives::MinAge)
  103.     eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  104.     eval $(apt-config shell Dir Dir)
  105.     eval $(apt-config shell CacheDir Dir::Cache)
  106.     eval $(apt-config shell CacheArchive Dir::Cache::archives)
  107.  
  108.     # sanity check
  109.     if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  110.     echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  111.     exit
  112.     fi
  113.     
  114.     Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  115.  
  116.     # check age
  117.     if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  118.     find $Cache -name "*.deb"  \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  119.     elif [ ! $MaxAge -eq 0 ]; then
  120.     find $Cache -name "*.deb"  -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  121.     fi
  122.     
  123.     # check size
  124.     if [ ! $MaxSize -eq 0 ]; then
  125.     # maxSize is in MB
  126.     MaxSize=$(($MaxSize*1024))
  127.  
  128.     #get current time
  129.     now=$(date --date=$(date --iso-8601) +%s)
  130.     MinAge=$(($MinAge*24*60*60))
  131.  
  132.     # reverse-sort by mtime
  133.     for file in $(ls -rt $Cache/*.deb 2>/dev/null); do 
  134.         du=$(du -s $Cache)
  135.         size=${du%%/*}
  136.         # check if the cache is small enough
  137.         if [ $size -lt $MaxSize ]; then
  138.         break
  139.         fi
  140.  
  141.         # check for MinAge of the file
  142.         if [ ! $MinAge -eq 0 ]; then 
  143.         # check both ctime and mtime 
  144.         mtime=$(stat -c %Y $file)
  145.         ctime=$(stat -c %Z $file)
  146.         if [ $mtime -gt $ctime ]; then
  147.             delta=$(($now-$mtime))
  148.         else
  149.             delta=$(($now-$ctime))
  150.         fi
  151.         #echo "$file ($delta), $MinAge"
  152.         if [ $delta -le $MinAge ]; then
  153.             #echo "Skiping $file (delta=$delta)"
  154.             break
  155.         fi
  156.         fi
  157.  
  158.         # delete oldest file
  159.         rm -f $file
  160.     done
  161.     fi
  162. }
  163.  
  164. # sleep for a random interval of time (default 30min)
  165. # (some code taken from cron-apt, thanks)
  166. random_sleep()
  167. {
  168.     RandomSleep=1800
  169.     eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  170.     if [ $RandomSleep -eq 0 ]; then
  171.     return
  172.     fi
  173.     if [ -z "$RANDOM" ] ; then
  174.         # A fix for shells that do not have this bash feature.
  175.     RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  176.     fi
  177.     TIME=$(($RANDOM % $RandomSleep))
  178.     sleep $TIME
  179. }
  180.  
  181. # main
  182.  
  183. if ! which apt-config >/dev/null; then
  184.     exit 0
  185. fi
  186.  
  187. UpdateInterval=0
  188. DownloadUpgradeableInterval=0
  189. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  190. AutocleanInterval=$DownloadUpgradeableInterval
  191. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  192. UnattendedUpgradeInterval=0
  193. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  194.  
  195. # check if we actually have to do anything
  196. if [ $UpdateInterval -eq 0 ] &&
  197.    [ $DownloadUpgradeableInterval -eq 0 ] &&
  198.    [ $UnattendedUpgradeInterval -eq 0 ] &&
  199.    [ $AutocleanInterval -eq 0 ]; then
  200.     exit 0
  201. fi
  202.  
  203. # laptop check, on_ac_power returns:
  204. #       0 (true)    System is on mains power
  205. #       1 (false)   System is not on mains power
  206. #       255 (false) Power status could not be determined
  207. # Desktop systems always return 255 it seems
  208. if which on_ac_power >/dev/null; then
  209.     on_ac_power
  210.     if [ $? -eq 1 ]; then
  211.     exit 0
  212.     fi
  213. fi
  214.  
  215. # sleep random amount of time to avoid hitting the 
  216. # mirrors at the same time
  217. random_sleep
  218.  
  219. # check if we can access the cache
  220. if ! apt-get check -q -q 2>/dev/null; then
  221.     # wait random amount of time before retrying
  222.     random_sleep
  223.     # check again
  224.     if ! apt-get check -q -q 2>/dev/null; then
  225.        echo "$0: could not lock the APT cache while performing daily cron job. "
  226.        echo "Is another package manager working?"
  227.        exit 1
  228.     fi
  229. fi
  230.  
  231. # set the proxy based on the admin users gconf settings
  232. admin_user=$(getent group admin|cut -d: -f4|cut -d, -f1)
  233. if [ -n "$admin_user" ] && [ -x /usr/bin/sudo ] && [ -z "$http_proxy" ] && [ -x /usr/bin/gconftool ]; then
  234.     use=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/use_http_proxy 2>/dev/null)
  235.     host=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/host 2>/dev/null)
  236.     port=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/port 2>/dev/null)
  237.     if [ "$use" = "true" ] && [ -n "$host" ] && [ -n "$port" ]; then
  238.         export http_proxy="http://$host:$port/"
  239.     fi
  240. fi
  241.  
  242. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  243. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  244.     # check for a new archive signing key (against the master keyring)
  245.     apt-key net-update
  246.     # now run the update
  247.     if apt-get -qq update -o APT::Update::Auth-Failure::="cp /usr/share/apt/apt-auth-failure.note /var/lib/update-notifier/user.d/" 2>/dev/null; then 
  248.     # Could possible test access to '/var/run/dbus/system_bus_socket' has well,
  249.     # but I'm not sure how stable the internal pipe location is defined as
  250.     # being;  so for the moment just 2>/dev/null . --sladen 2007-09-27
  251.     if which dbus-send >/dev/null; then
  252.         dbus-send --system / app.apt.dbus.updated boolean:true 2>/dev/null || true
  253.     fi
  254.     # now run apt-xapian-index if it is installed to ensure the index
  255.     # is up-to-date
  256.     if [ -x /usr/sbin/update-apt-xapian-index ]; then
  257.         ionice -c3 update-apt-xapian-index -q
  258.     fi
  259.         update_stamp $UPDATE_STAMP
  260.     fi
  261. fi
  262.  
  263. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  264. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  265.     apt-get -qq -d dist-upgrade 2>/dev/null
  266.     update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  267. fi
  268.  
  269. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  270. if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  271.     unattended-upgrade
  272.     update_stamp $UPGRADE_STAMP
  273. fi
  274.  
  275. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  276. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  277.     apt-get -qq autoclean
  278.     update_stamp $AUTOCLEAN_STAMP
  279. fi
  280.  
  281. # check cache size 
  282. check_size_constraints
  283.