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

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

allow batch mode edits...

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