source: freebsd-mgnt/system-update.sh@ 316

Last change on this file since 316 was 306, checked in by Rick van der Zwet, 13 years ago

Be more verbose when it comes of making a result file.

  • Property svn:executable set to *
File size: 5.5 KB
RevLine 
[84]1#!/bin/sh
[265]2# FreeBSD system port upgrade script
[84]3#
4# Licence: BSDLike - http://rickvanderzwet.nl/LICENSE
5# Rick van der Zwet <info@rickvanderzwet.nl>
6
[182]7. /etc/rc.subr
8
[265]9# Pointers to avoid confusion co-respond with logical boolean style values.
10# WARNING do not compare with exit codes!
[193]11FALSE=0
12TRUE=1
13
[185]14ask_question() {
[191]15 # Will return 1 if yes, return 0 if no
[185]16 QUESTION=$1
17 DEFAULT=$2
18 echo -n "$QUESTION? [$DEFAULT]: "; read INPUT
19 INPUT=${INPUT:-"${DEFAULT}"}
20 checkyesno INPUT || return 1 && return 0
21
22}
23
[193]24echo_list() {
25 PREFIX=$1
26 shift
27 for ENTRY in $*; do
28 echo "$PREFIX $ENTRY"
29 done
30}
31
[265]32
[193]33#
34# SuperCow power only
[84]35if [ `id -u` -ne 0 ]; then
36 echo "Sorry root only" 1>&2
37 exit 128
38fi
39
[265]40# Make sure we always using the latest version
41CHANGES=`svn up --non-interactive $(dirname $0) | wc -l`
42if [ $CHANGES -gt 1 ]; then
43 sh $0
44 exit 0
45fi
[193]46
47#
48# Check which ports-mgnt tool to use to upgrade outdated ports
49#
50FORCE_PORTUPGRADE=${FORCE_PORTUPGRADE:-$FALSE}
51FORCE_PORTMASTER=${FORCE_PORTMASTER:-$FALSE}
52
53PORTUPGRADE_INSTALLED=$FALSE
54PORTMASTER_INSTALLED=$FALSE
55USE_PORTMGNT="none"
56if [ -n "`whereis -qb portupgrade`" ]; then
57 PORTUPGRADE_INSTALLED=$TRUE
58fi
59if [ -n "`whereis -qb portmaster`" ]; then
60 PORTMASTER_INSTALLED=$TRUE
61fi
62
63if [ $FORCE_PORTMASTER -eq $TRUE ]; then
64 if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
65 echo "# ERROR: FORCE_PORTMASTER set but binary not found"
[192]66 exit 1
[193]67 fi
68 USE_PORTMGNT="portmaster"
69elif [ $FORCE_PORTUPGRADE -eq $TRUE ]; then
70 if [ $PORTUPGRADE_INSTALLED -eq $FALSE ]; then
71 echo "# ERROR: FORCE_PORTUPGRADE set but binary not found"
72 exit 1
73 fi
74 USE_PORTMGNT="portupgrade"
75else
76 if [ $PORTUPGRADE_INSTALLED -eq $TRUE ]; then
77 if [ $PORTMASTER_INSTALLED -eq $TRUE ]; then
78 echo "# ERROR: both portmaster and portupgrade are installed"
79 echo "# Pick your favorite by setting either FORCE_PORTMASTER=1 or FORCE_PORTUPGRADE=1"
80 exit 1
81 fi
82 USE_PORTMGNT="portupgrade"
83 else
84 if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
85 echo "# WARNING: ports-mgnt/portupgrade not found!"
86 echo "# WARNING: ports-mgnt/portmaster not found!"
87 echo "# ERROR : Not port upgrade tool installed"
88 exit 1
89 fi
90 USE_PORTMGNT="portmaster"
91 fi
[192]92fi
93
[193]94
95
96
97
[119]98DIRNAME=`dirname $0`
[182]99BATCH=${BATCH-""}
[84]100
[185]101# Get list of rc.d marked for updates
102RC_FILES=`$DIRNAME/port-list.sh`
[193]103RUNNING_DAEMONS=""
[119]104
[193]105for RC_FILE in $RC_FILES; do
106 $RC_FILE onestatus | grep -q 'is running as' && {
107 RUNNING_DAEMONS="$RUNNING_DAEMONS $RC_FILE"
108 }
109done
110
[84]111# Update port tree
[185]112echo "# Updating portstree ..."
113portsnap update > /dev/null
[84]114
[182]115# Update ports listing
116echo "# Checking ports which needs updating ..."
[193]117NEWPORTS=`pkg_version -qol '<'`
[182]118
[193]119# Check for potentional trouble.
120# XXX: Weird -q does not strip at '!' check
121BROKENPORTS=`pkg_version -qol '!' | awk '{print $1}'`
122if [ -n "$BROKENPORTS" ]; then
123 echo "# WARNING comparion failed, fix by hand"; echo_list "# - " $BROKENPORTS
124fi
125
[266]126
[182]127if [ -z "$NEWPORTS" ]; then
[193]128 echo "# No ports to update"
129 echo "# All done, goodbye"
[182]130 exit 0
131fi
132
[266]133# Check for broken pkgdb, else we have trouble during upgrades
134if [ $USE_PORTMGNT = "portupgrade" ]; then
135 pkgdb -a || exit 1
136fi
137
[185]138#
139# Update ports
[182]140if [ -z "$BATCH" ]; then
141 # Make sure we have read the (new) UPDATING announcements
[185]142 # Use ls to display the last motification time of the directories in
143 # /var/db/pkg and assume that is the last time the user read the
144 # UPDATING file
145 LAST_UPDATED=`ls -rtlD '%Y%m%d' /var/db/pkg/ | awk 'END{print $6}'`
[182]146
[185]147 # Find new entries
148 TMPFILE=`mktemp -t $(basename $0).XXX`
149 awk -F: -v last=$LAST_UPDATED '{if (match($0,"^[0-9]+:$") && $1 < last) {exit}; print $0}' /usr/ports/UPDATING > $TMPFILE
150
151 echo "# Show new /usr/ports/UPDATING entries (starting from $LAST_UPDATED) ..."
152 if [`cat $TMPFILE | wc -l` -eq 7 ]; then
153 echo "## No new entries found"
154 else
155 cat $TMPFILE
156 fi
157 rm $TMPFILE
158
159 # agree to the terms of the game
[193]160 echo "# Ports pending update:"
161 echo_list "# - " $NEWPORTS
162 ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
[182]163
[185]164 #
165 # Stop daemons
[193]166 DAEMONS_STOPPED=$FALSE
[190]167 if [ -n "$RC_FILES" ]; then
[193]168 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
169 if [ -n "$RUNNING_DAEMONS" ]; then
170 echo "# Running daemons:"; echo_list "# - " $RUNNING_DAEMONS
171 ask_question "# Do you like to stop running daemons" "yes" && {
172 for RC_FILE in $RUNNING_DAEMONS; do
173 $RC_FILE stop
174 done;
175 DAEMONS_STOPPED=$TRUE
176 }
177 fi
[190]178 fi
[182]179fi
[193]180echo "# Using update tool '$USE_PORTMGNT"
181case $USE_PORTMGNT in
182 portupgrade)
183 portupgrade -r -mBATCH=yes -a $NEWPORTS
184 ;;
185 portmaster)
[266]186 portmaster -d --no-confirm --no-term-title --no-index-fetch -mBATCH=yes -a $NEWPORTS
[193]187 ;;
188 *)
189 echo "# ERROR: Failed to find a port management tool"
190 exit 1
191 ;;
192esac
[182]193
[306]194echo "# Ports updated:"
195echo_list "# - " $NEWPORTS
[185]196#
[193]197# (Re)start daemons. Assume we only like to start the just stopped ones.
[190]198if [ -n "$RC_FILES" ]; then
[193]199 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
200 if [ -n "$RUNNING_DAEMONS" ]; then
201 if [ $DAEMONS_STOPPED -eq $TRUE ]; then
202 echo "# Enabled daemons: "; echo_list "# - " $RUNNING_DAEMONS
203 ask_question "# Do you like to start your daemons" "yes" && {
204 for RC_FILE in $RUNNING_DAEMONS; do
205 $RC_FILE start
206 done
207 }
208 else
209 echo "# Running daemons: "; echo_list "# - " $RUNNING_DAEMONS
210 ask_question "# Do you like to restart your daemons" "yes" && {
211 for RC_FILE in $RUNNING_DAEMONS; do
212 $RC_FILE restart
213 done
214 }
215 fi
216 fi
[190]217fi
[193]218
219echo "# All done, goodbye"
Note: See TracBrowser for help on using the repository browser.