source: src-sh/pc-updatemanager/pc-updatemanager @ f68f02e

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

When we list branches, put a * on the current one

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