source: src-sh/pc-updatemanager/pc-updatemanager @ 5b01725

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 5b01725 was 5b01725, checked in by Kris Moore <kris@…>, 17 months ago

Add new branches / chbranch functionality to pc-updatemanager.

This will allow users to see what branches are available, I.E. 9.1-RELEASE, 9-TRUEOS, etc and upgrade their system to them.

  • Property mode set to 100755
File size: 15.7 KB
Line 
1#!/bin/sh
2# Script which performs updating services
3############################################################################
4
5# Source our functions
6. /usr/local/share/pcbsd/scripts/functions.sh
7
8# Set the program location
9PROGDIR="/usr/local/share/pcbsd/pc-updatemanager" ; export PROGDIR
10
11# Start by sourcing /etc/profile
12# This grabs any HTTP_ / FTP_ PROXY variables
13. /etc/profile
14
15PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
16export PCBSD_ETCCONF
17
18# Set the pub openssl key
19SKEY="${PROGDIR}/conf/security.key" ; export SKEY
20
21# Directory to store downloaded updates
22DOWNLOADDIR="/usr/local/tmp"
23
24# Get the system version we are checking for updates to
25SYSVER="`uname -r | cut -d '-' -f 1-2`" ; export SYSVER
26
27# Get the system type
28SYSTYPE="`pbreg get /PC-BSD/SysType`" ; export SYSTYPE
29
30# Set the config location
31UPDATECONF="${PROGDIR}/conf/sysupdate.conf"
32
33# Set the system arch type
34ARCH=`uname -m`
35
36# Default pcbsd.conf file
37PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
38
39# Patchset Data
40PATCHSERVER="`cat ${UPDATECONF} | grep '^UPDATESERVER:' | cut -d ' ' -f 2`"
41PATCHSET="`cat ${UPDATECONF} | grep '^PATCHSET:' | cut -d ' ' -f 2`"
42PATCHFILE="${PATCHSET}.upd"
43PATCHURL="${PATCHSERVER}/${PATCHFILE}"
44PATCHTMPDIR=`mktemp -d /tmp/.sysupdateXXXXXX`
45PATCHTMPFILE="${PATCHTMPDIR}/sysupdate-${SYSVER}.upd"
46
47MUSTAGEDIR="${DOWNLOADDIR}/update-stagedir"
48
49# Enable ftp passive mode for file transfers
50FTP_PASSIVE_MODE="YES" ; export FTP_PASSIVE_MODE
51
52if [ ! -d "${DOWNLOADDIR}" ]; then mkdir -p ${DOWNLOADDIR}; fi
53
54
55# Trigger File for Tray Application
56TRIGGERFILE="/tmp/.sysupdatetraytrigger"
57
58DBDIR="/var/db/pc-updatemanager"
59INSDIR="${DBDIR}/installed"
60IGNDIR="${DBDIR}/ignored"
61
62
63######################################################################
64# Done with config values
65######################################################################
66
67show_usage() {
68        echo "$0 - Usage
69----
70  branches              - List available system branches
71  chbranch <tag>        - Change to new system branch
72  check                 - Check for system updates
73  install <tag>,<tag2>  - Install system updates
74  pkgcheck              - Check for updates to packages
75  pkgupdate             - Install packages updates
76"       
77
78        exit 1
79}
80
81# Run-command, don't halt if command exits with non-0
82rc_nohalt()
83{
84  CMD="$1"
85 
86  if [ -z "${CMD}" ] ; then
87    exit_err "Error: missing argument in rc_nohalt()"
88  fi
89
90  ${CMD}
91}
92
93# Run-command, halt if command exits with non-0
94rc_halt()
95{
96  CMD="$@"
97
98  if [ -z "${CMD}" ] ; then
99    exit_err "Error: missing argument in rc_halt()"
100  fi
101
102  ${CMD}
103  STATUS=$?
104  if [ ${STATUS} -ne 0 ] ; then
105    exit_err "Error ${STATUS}: ${CMD}"
106  fi
107}
108
109get_update_file() {
110
111  # Now fetch the update file
112  get_file "${PATCHURL}" "${PATCHTMPFILE}" 1 >/dev/null 2>/dev/null
113  if [ $? -ne 0 ] ; then
114     rm ${PATCHTMPFILE} 2>/dev/null
115     is_net_up
116     if [ $? -eq 0 ] ; then
117       echo "No updates available for $SYSVER!" ; exit 0
118     else
119       exit_err "Could not contact update server!"
120     fi
121  fi
122
123  # Now fetch the update file signature
124  get_file "${PATCHURL}.sha1" "${PATCHTMPFILE}.sha1" 1 >/dev/null 2>/dev/null
125  if [ $? -ne 0 ] ; then
126     rm ${PATCHTMPFILE}.sha1 2>/dev/null
127     is_net_up
128     if [ $? -eq 0 ] ; then
129       echo "No updates available for $SYSVER!" ; exit 0
130     else
131       exit_err "Could not contact update server!"
132     fi
133  fi
134
135  # Lets verify the signature of the file
136  openssl dgst -sha1 -verify ${SKEY} \
137        -signature ${PATCHTMPFILE}.sha1 \
138        ${PATCHTMPFILE} >/dev/null 2>/dev/null
139  if [ $? -ne 0 ] ; then
140     rm ${PATCHTMPFILE} 2>/dev/null
141     rm ${PATCHTMPFILE}.sha1 2>/dev/null
142     exit_err "Update file failed verification..."
143  fi
144
145  # Done with the signature we can discard
146  rm ${PATCHTMPFILE}.sha1
147
148}
149
150do_branch() {
151  up="$1"
152
153  # Setup our variable to catch if we have a bad branch
154  BRANCHFOUND="0" 
155
156  while read line
157  do
158     echo $line | grep -q "^BRANCHES:"
159     if [ $? -eq 0 ] ; then
160        for i in `echo $line | cut -d ':' -f 2 | sed 's|,| |g'`
161        do
162          if [ "$i" != "$up" ] ; then continue ;fi
163          if [ "`uname -r | cut -d '-' -f 1-2`" = "$up" ] ; then
164             echo "You are already on this branch!"
165             BRANCHFOUND="1"
166             continue;
167          fi
168          start_major_fbsd_update "fbsd-$up"
169          BRANCHFOUND="1"
170          break
171        done
172     fi
173  done < ${PATCHTMPFILE}
174
175  # If no patches
176  if [ "${BRANCHFOUND}" = "0" ]; then 
177     # No available updates
178     echo "Unknown branch ${1}"
179  fi
180
181  rm ${PATCHTMPFILE}
182
183};
184
185list_branches() {
186  # Setup our variable, we have no branches so far
187  BRANCHFOUND="0" 
188
189  while read line
190  do
191     echo $line | grep -q "^BRANCHES:"
192     if [ $? -eq 0 ] ; then
193        echo "Available branches:"
194        echo "----------------------------------"
195        echo $line | cut -d ':' -f 2 | sed 's|,|\
196|g'
197        echo ""
198        echo "To change branches run: ${0} chbranch <branch>"
199        BRANCHFOUND="1"
200        continue
201     fi
202  done < ${PATCHTMPFILE}
203
204  # If no patches
205  if [ "${BRANCHFOUND}" = "0" ]; then 
206     # No available updates
207     echo "No branches currently available!"
208  fi
209
210  rm ${PATCHTMPFILE}
211
212};
213
214do_check() {
215  # Setup our variable, we have no patches so far
216  PATCHFOUND="0" 
217
218  while read line
219  do
220     echo $line | grep -q "^MU:"
221     if [ $? -eq 0 ] ; then
222        parse_mu_update_line "$line"
223        continue
224     fi
225     echo $line | grep -q "^SA:"
226     if [ $? -eq 0 ] ; then
227        parse_sa_update_line "$line"
228        continue
229     fi
230  done < ${PATCHTMPFILE}
231
232  # If no patches
233  if [ "${PATCHFOUND}" = "0" ]; then 
234     # No available updates
235     echo "Your system is up to date!"
236  fi
237
238  rm ${PATCHTMPFILE}
239
240};
241
242parse_mu_update_line()
243{
244   local line="$1"
245
246   # Check if this is a major update we can install
247   local sysUpOVer=`echo $line | cut -d ':' -f 2`
248   local sysUpNVer=`echo $line | cut -d ':' -f 3`
249
250   # Is this an update for our version?
251   if [ "$SYSVER" != "$sysUpOVer" ] ; then return; fi
252
253   if [ $PATCHFOUND -eq 0 ] ; then
254      echo ""
255      echo "The following updates are available:"
256      echo "------------------------------------"
257   fi
258   echo "NAME: System Update to ${sysUpNVer}" 
259   echo "TYPE: SYSUPDATE" 
260   echo "TAG: fbsd-${sysUpNVer}"
261   echo "VERSION: ${sysUpNVer}" 
262   echo ""
263   echo "To install: \"pc-updatemanager install fbsd-${sysUpNVer}\""
264   echo ""
265   echo ""
266
267   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
268}
269
270parse_sa_update_line()
271{
272   local line="$1"
273
274   # Check if this is an update we can install
275   local saVer=`echo $line | cut -d ':' -f 2`
276   local saArch=`echo $line | cut -d ':' -f 3`
277   local saType=`echo $line | cut -d ':' -f 4`
278   local saIdent="`echo $line | cut -d ':' -f 5`"
279
280   # For this version?
281   if [ "$SYSVER" != "$saVer" ] ; then return ; fi
282
283   # For this system arch?
284   if [ "$ARCH" != "$saArch" -a "$saArch" != "noarch" ] ; then return ; fi
285
286   # For this system type?
287   if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then return ; fi
288
289   # Already installed?
290   if [ -e "${INSDIR}/${SYSVER}/$saIdent" ] ; then return ; fi
291
292   if [ $PATCHFOUND -eq 0 ] ; then
293      echo ""
294      echo "The following updates are available:"
295      echo "------------------------------------"
296   fi
297   local saDesc="`echo $line | cut -d ':' -f 6`"
298   local saSize=`echo $line | cut -d ':' -f 8`
299   local saDetail="http://trac.pcbsd.org`echo $line | cut -d ':' -f 10`"
300   local saDate="`echo $line | cut -d ':' -f 11`"
301
302   echo "NAME: ${saDesc}" 
303   echo "TYPE: PATCH" 
304   echo "TAG: ${saIdent}"
305   echo "DETAILS: ${saDetail}"
306   echo "DATE: ${saDate}"
307   echo "SIZE: ${saSize}Mb" 
308   echo " "
309   echo "To install: \"pc-updatemanager install ${saIdent}\""
310   echo " "
311
312   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
313}
314
315start_pcbsd_patch() {
316  local up="$1"
317  local saIdent=""
318  local saVer=""
319  local saArch=""
320  local saPlat=""
321  local saUrl=""
322  local saDesc=""
323  local saCsum=""
324  local patchLine=""
325  if [ -e "${INSDIR}/${SYSVER}/${up}" ]; then
326     rm ${PATCHTMPFILE}
327     exit_err "Patch $up already installed!"
328  fi
329
330  while read line
331  do
332    echo $line | grep -q "^SA:"
333    if [ $? -ne 0 ] ; then continue; fi
334    saIdent="`echo $line | cut -d ':' -f 5`"
335    # Look for the right stand-alone update
336    if [ "$saIdent" != "$up" ] ; then continue ; fi
337    saVer=`echo $line | cut -d ':' -f 2`
338    saArch=`echo $line | cut -d ':' -f 3`
339    saType=`echo $line | cut -d ':' -f 4`
340    # Does this update apply to this system arch?
341    if [ "$saArch" != "$ARCH" -a "$saArch" != "noarch" ] ; then
342       rm ${PATCHTMPFILE}
343       exit_err "Patch $up is not for this system arch type!"
344    fi
345    # For this version?
346    if [ "$saVer" != "$SYSVER" ] ; then
347       rm ${PATCHTMPFILE}
348       exit_err "Patch $up is not for this system version!"
349    fi
350
351    # For this system type?
352    if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then
353       rm ${PATCHTMPFILE}
354       exit_err "Patch $up is not for this system type!"
355    fi
356
357    # Got here? We have a patch ready to go
358    patchLine="$line"
359    break
360  done < ${PATCHTMPFILE}
361
362  # Did we find the patch?
363  if [ -z "$patchLine" ] ; then
364     rm ${PATCHTMPFILE}
365     exit_err "Patch $up not found!"
366  fi
367
368  # Get the patch details
369  saVer=`echo $patchLine | cut -d ':' -f 2`
370  saArch=`echo $patchLine | cut -d ':' -f 3`
371  saPlat=`echo $patchLine | cut -d ':' -f 4`
372  saIdent="`echo $patchLine | cut -d ':' -f 5`"
373  saDesc="`echo $patchLine | cut -d ':' -f 6`"
374  saUrl="`echo $patchLine | cut -d ':' -f 7`"
375  saCsum="`echo $patchLine | cut -d ':' -f 9`"
376  local FILENAME="`basename $saUrl`"
377
378  # Start downloading the patch
379  touch ${TRIGGERFILE}
380  echo "DOWNLOADING: ${saIdent}"
381  echo "DOWNLOADING: ${saIdent}" >${TRIGGERFILE}
382
383  # Get the file
384  get_file_from_mirrors "/${saUrl}" "${DOWNLOADDIR}/${FILENAME}"
385  if [ $? -ne 0 ] ; then
386     rm ${PATCHTMPFILE}
387     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
388     exit_err "Failed to download: ${saIdent}"
389  fi
390
391  # Check the sha256 checksum
392  if [ "$saCsum" != "`sha256 -q ${DOWNLOADDIR}/${FILENAME} 2>/dev/null`" ]
393  then
394     # Download MD5 doesn't match! Delete the file
395     rm ${PATCHTMPFILE}
396     rm ${DOWNLOADDIR}/${FILENAME}
397     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
398     exit_err "Failed to download: ${saIdent}"
399  else
400     echo "DOWNLOADFINISHED: ${saIdent}"
401     echo "DOWNLOADFINISHED: ${saIdent}" >${TRIGGERFILE}
402  fi
403
404  echo "INSTALLING: ${saIdent}" >${TRIGGERFILE}
405  sleep 1
406
407  PATCHTMPDIR="`mktemp -d ${DOWNLOADDIR}/patchInstallXXXXX`"
408  rc_halt "tar xvJf ${DOWNLOADDIR}/${FILENAME} -C ${PATCHTMPDIR}" 2>/dev/null
409  PATCHDIR="$PATCHTMPDIR" ; export PATCHDIR
410  cd ${PATCHTMPDIR}
411  sh update.sh
412  if [ $? -eq 0 ]; then
413     touch "${INSDIR}/${SYSVER}/${saIdent}"
414     echo "INSTALLFINISHED: ${saIdent}"
415     echo "INSTALLFINISHED: ${saIdent}" >${TRIGGERFILE}
416  else
417     rm ${PATCHTMPFILE}
418     rm -rf ${PATCHTMPDIR}
419     rm ${DOWNLOADDIR}/${FILENAME}
420     echo "INSTALLFAILED: ${saIdent}" >${TRIGGERFILE}
421     exit_err "INSTALLFAILED: ${saIdent}"
422  fi
423
424  rc_halt "rm -rf ${PATCHTMPDIR}"
425  rc_halt "rm ${DOWNLOADDIR}/${FILENAME}"
426}
427
428rollback_update()
429{
430  cp /usr/local/etc/pkg.conf.preUpgrade /usr/local/etc/pkg.conf
431  if [ "$PKGROLLBACK" = "YES" ] ; then
432
433  fi
434}
435
436start_major_fbsd_update() {
437  local rel=`echo $1 | sed 's|fbsd-||g'`
438  PKGROLLBACK="NO"
439
440  # Lets backup the old pkg.conf file
441  cp /usr/local/etc/pkg.conf /usr/local/etc/pkg.conf.preUpgrade
442
443  # Set the new pkg.conf file to the new repo
444  local arch=`uname -m`
445  echo "packagesite: http://ftp.pcbsd.org/pub/mirror/packages/$rel/$arch" >/usr/local/etc/pkg.conf
446  echo "PUBKEY: /usr/local/etc/pkg-pubkey.cert" >>/usr/local/etc/pkg.conf
447  echo "PKG_CACHEDIR: /usr/local/tmp" >>/usr/local/etc/pkg.conf
448
449  # Lets backup old pkgng
450  oPkg=`pkg info pkg | awk '{print $1}'`
451  pkg create -o /usr/local/tmp/ ${oPkg}
452  if [ -e "/usr/local/tmp/oldPkg.txz" ] ; then rm /usr/local/tmp/oldPkg.txz; fi
453  mv /usr/local/tmp/${oPkg}.txz /usr/local/tmp/oldPkg.txz
454
455  # Lets download the new pkgng for this release
456  if [ -e "/usr/local/tmp/pkg.txz" ] ; then rm /usr/local/tmp/pkg.txz; fi
457  pc-pkg getfile -o /usr/local/tmp/pkg.txz /packages/$rel/$arch/Latest/pkg.txz
458  if [ $? -ne 0 ] ; then
459     rollback_update
460     exit_err "Failed downloading pkgng update..."
461  fi
462
463  # Lets install pkgng update
464  PKGROLLBACK="YES"
465  echo "Installing pkgng update..."
466  pkg-static add /usr/local/tmp/pkg.txz
467
468  # Lets pre-download all packages
469  download_cache_packages "pkg-static upgrade -fn"
470  if [ $? -ne 0 ] ; then
471     rollback_update
472     exit_err "Failed downloading packages for update..."
473  fi
474
475  # Download update files from freebsd-update
476  freebsd-update --non-interactive -r "$rel" upgrade
477  if [ $? -ne 0 ] ; then
478     rollback_update
479     exit_err "Failed running \"freebsd-update -r $rel upgrade\"..."
480  fi
481
482  # Now install freebsd-update files
483  freebsd-update --non-interactive install
484  if [ $? -ne 0 ] ; then
485     rollback_update
486     exit_err "Failed running \"freebsd-update install\"..."
487  fi
488
489  # Now its time to force a pkg-update of all packages to their new FreeBSD compiled versions
490  pc-updatemanager pkgupdate -f
491
492  echo "Major update finished!" 
493  echo "Please reboot the system for changes to take effect." 
494  exit 0
495}
496
497do_install() {
498        # Make sure we are root and have specified updates to install
499        if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
500        if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
501
502        # Begin to update the selected items
503        for up in `echo $1 | sed 's|,| |g'`
504        do
505          # If this is a major update, start it up!
506          echo $up | grep -q "^fbsd-" 
507          if [ $? -eq 0 ] ; then
508            start_major_fbsd_update "$up"
509            exit 0
510          elif [ "$up" == "pkg-updates" ] ; then
511            # Check if this is a pkg-update request or regular patch
512            start_pkg_updates
513            continue
514          else
515            # Doing regular pcbsd patch
516            start_pcbsd_patch "${up}"
517            continue
518          fi
519
520        done 
521
522        # All Finished!
523        rm ${PATCHTMPFILE}
524        exit 0
525}
526
527is_net_up() {
528        ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
529        return $?
530}
531
532checkup_pkgs() {
533
534  # Checkout the installed pkgs and compare to master list
535  rc_halt "pkg-static update"
536  pkg-static upgrade -n >/tmp/.pkgData.$$
537  grep -q "Upgrading" /tmp/.pkgData.$$
538  if [ $? -ne 0 ] ; then
539     echo "All packages are up to date!"
540     rm /tmp/.pkgData.$$
541     return 0
542  fi
543
544  # Display the pkg update data
545  cat /tmp/.pkgData.$$
546  rm /tmp/.pkgData.$$
547  echo ""
548  echo "To start the upgrade run \"${0} pkgupdate\""
549  return 0
550}
551
552update_pkgs()
553{
554  if [ -n "$1" -a "$1" != "-f" ] ; then
555     exit_err "Invalid option $1 passed"
556  fi
557
558  # Lets pre-download all the packages
559  download_cache_packages "pkg-static upgrade -U -n $1"
560
561  check_pkg_conflicts "upgrade -U -F -y $1"
562  if [ $? -ne 0 ] ; then
563     exit 1
564  fi
565
566  # Start the package update!
567  pkg-static upgrade -U -y ${1}
568  result=$?
569
570  # Lets now re-extract our overlay data to grab any adjusted port files
571  pc-extractoverlay ports >/dev/null 2>/dev/null
572
573  # Now re-extract overlay files to ensure we do file fixing / massaging
574  if [ "$SYSTYPE" = "PCBSD" ]; then
575    pc-extractoverlay desktop
576  fi
577  if [ "$SYSTYPE" = "TRUEOS" ]; then
578    pc-extractoverlay server
579  fi
580
581  if [ $result -ne 0 ] ; then
582     echo "Failed updating..."
583  fi
584  exit $result
585}
586
587# Check if we have an update folder for the version we are on
588if [ ! -d "${AVAILDIR}/${SYSVER}" ] ; then mkdir -p ${AVAILDIR}/${SYSVER} ; fi
589
590if [ "`id -u`" = "0" ] ; then
591  # Make the installed directory for this version
592  if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
593
594  # Make the ignore directory for this version
595  if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
596fi
597
598case $1 in
599           check) get_update_file
600                  do_check ;;
601        branches) get_update_file
602                  list_branches ;;
603        chbranch) get_update_file
604                  do_branch "${2}" ;;
605        pkgcheck) checkup_pkgs ;;
606       pkgupdate) update_pkgs "$2" ;;
607         install) get_update_file
608                  do_install "${2}" ;;
609        *) show_usage ;;
610esac
611
612exit 0
Note: See TracBrowser for help on using the repository browser.