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

Last change on this file since 332 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
Line 
1#!/bin/sh
2# FreeBSD system port upgrade script
3#
4# Licence: BSDLike - http://rickvanderzwet.nl/LICENSE
5# Rick van der Zwet <info@rickvanderzwet.nl>
6
7. /etc/rc.subr
8
9# Pointers to avoid confusion co-respond with logical boolean style values.
10# WARNING do not compare with exit codes!
11FALSE=0
12TRUE=1
13
14ask_question() {
15 # Will return 1 if yes, return 0 if no
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
24echo_list() {
25 PREFIX=$1
26 shift
27 for ENTRY in $*; do
28 echo "$PREFIX $ENTRY"
29 done
30}
31
32
33#
34# SuperCow power only
35if [ `id -u` -ne 0 ]; then
36 echo "Sorry root only" 1>&2
37 exit 128
38fi
39
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
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"
66 exit 1
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
92fi
93
94
95
96
97
98DIRNAME=`dirname $0`
99BATCH=${BATCH-""}
100
101# Get list of rc.d marked for updates
102RC_FILES=`$DIRNAME/port-list.sh`
103RUNNING_DAEMONS=""
104
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
111# Update port tree
112echo "# Updating portstree ..."
113portsnap update > /dev/null
114
115# Update ports listing
116echo "# Checking ports which needs updating ..."
117NEWPORTS=`pkg_version -qol '<'`
118
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
126
127if [ -z "$NEWPORTS" ]; then
128 echo "# No ports to update"
129 echo "# All done, goodbye"
130 exit 0
131fi
132
133# Check for broken pkgdb, else we have trouble during upgrades
134if [ $USE_PORTMGNT = "portupgrade" ]; then
135 pkgdb -a || exit 1
136fi
137
138#
139# Update ports
140if [ -z "$BATCH" ]; then
141 # Make sure we have read the (new) UPDATING announcements
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}'`
146
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
160 echo "# Ports pending update:"
161 echo_list "# - " $NEWPORTS
162 ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
163
164 #
165 # Stop daemons
166 DAEMONS_STOPPED=$FALSE
167 if [ -n "$RC_FILES" ]; then
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
178 fi
179fi
180echo "# Using update tool '$USE_PORTMGNT"
181case $USE_PORTMGNT in
182 portupgrade)
183 portupgrade -r -mBATCH=yes -a $NEWPORTS
184 ;;
185 portmaster)
186 portmaster -d --no-confirm --no-term-title --no-index-fetch -mBATCH=yes -a $NEWPORTS
187 ;;
188 *)
189 echo "# ERROR: Failed to find a port management tool"
190 exit 1
191 ;;
192esac
193
194echo "# Ports updated:"
195echo_list "# - " $NEWPORTS
196#
197# (Re)start daemons. Assume we only like to start the just stopped ones.
198if [ -n "$RC_FILES" ]; then
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
217fi
218
219echo "# All done, goodbye"
Note: See TracBrowser for help on using the repository browser.