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

Last change on this file since 261 was 193, checked in by Rick van der Zwet, 14 years ago
  • Support both portmaster and portupgrade.
  • Only manage daemons which are running.
  • If not stopped, ask user to restart.
  • Pretty formatting of lists
  • Property svn:executable set to *
File size: 5.2 KB
Line 
1#!/bin/sh
2# FreeBSD system upgade 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 corresponts with logical boolean style values.
10# WARNING do not conpare 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# SuperCow power only
34if [ `id -u` -ne 0 ]; then
35 echo "Sorry root only" 1>&2
36 exit 128
37fi
38
39
40#
41# Check which ports-mgnt tool to use to upgrade outdated ports
42#
43FORCE_PORTUPGRADE=${FORCE_PORTUPGRADE:-$FALSE}
44FORCE_PORTMASTER=${FORCE_PORTMASTER:-$FALSE}
45
46PORTUPGRADE_INSTALLED=$FALSE
47PORTMASTER_INSTALLED=$FALSE
48USE_PORTMGNT="none"
49if [ -n "`whereis -qb portupgrade`" ]; then
50 PORTUPGRADE_INSTALLED=$TRUE
51fi
52if [ -n "`whereis -qb portmaster`" ]; then
53 PORTMASTER_INSTALLED=$TRUE
54fi
55
56if [ $FORCE_PORTMASTER -eq $TRUE ]; then
57 if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
58 echo "# ERROR: FORCE_PORTMASTER set but binary not found"
59 exit 1
60 fi
61 USE_PORTMGNT="portmaster"
62elif [ $FORCE_PORTUPGRADE -eq $TRUE ]; then
63 if [ $PORTUPGRADE_INSTALLED -eq $FALSE ]; then
64 echo "# ERROR: FORCE_PORTUPGRADE set but binary not found"
65 exit 1
66 fi
67 USE_PORTMGNT="portupgrade"
68else
69 if [ $PORTUPGRADE_INSTALLED -eq $TRUE ]; then
70 if [ $PORTMASTER_INSTALLED -eq $TRUE ]; then
71 echo "# ERROR: both portmaster and portupgrade are installed"
72 echo "# Pick your favorite by setting either FORCE_PORTMASTER=1 or FORCE_PORTUPGRADE=1"
73 exit 1
74 fi
75 USE_PORTMGNT="portupgrade"
76 else
77 if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
78 echo "# WARNING: ports-mgnt/portupgrade not found!"
79 echo "# WARNING: ports-mgnt/portmaster not found!"
80 echo "# ERROR : Not port upgrade tool installed"
81 exit 1
82 fi
83 USE_PORTMGNT="portmaster"
84 fi
85fi
86
87
88
89
90
91DIRNAME=`dirname $0`
92BATCH=${BATCH-""}
93
94# Get list of rc.d marked for updates
95RC_FILES=`$DIRNAME/port-list.sh`
96RUNNING_DAEMONS=""
97
98for RC_FILE in $RC_FILES; do
99 $RC_FILE onestatus | grep -q 'is running as' && {
100 RUNNING_DAEMONS="$RUNNING_DAEMONS $RC_FILE"
101 }
102done
103
104# Update port tree
105echo "# Updating portstree ..."
106portsnap update > /dev/null
107
108# Update ports listing
109echo "# Checking ports which needs updating ..."
110NEWPORTS=`pkg_version -qol '<'`
111
112# Check for potentional trouble.
113# XXX: Weird -q does not strip at '!' check
114BROKENPORTS=`pkg_version -qol '!' | awk '{print $1}'`
115if [ -n "$BROKENPORTS" ]; then
116 echo "# WARNING comparion failed, fix by hand"; echo_list "# - " $BROKENPORTS
117fi
118
119if [ -z "$NEWPORTS" ]; then
120 echo "# No ports to update"
121 echo "# All done, goodbye"
122 exit 0
123fi
124
125#
126# Update ports
127if [ -z "$BATCH" ]; then
128 # Make sure we have read the (new) UPDATING announcements
129 # Use ls to display the last motification time of the directories in
130 # /var/db/pkg and assume that is the last time the user read the
131 # UPDATING file
132 LAST_UPDATED=`ls -rtlD '%Y%m%d' /var/db/pkg/ | awk 'END{print $6}'`
133
134 # Find new entries
135 TMPFILE=`mktemp -t $(basename $0).XXX`
136 awk -F: -v last=$LAST_UPDATED '{if (match($0,"^[0-9]+:$") && $1 < last) {exit}; print $0}' /usr/ports/UPDATING > $TMPFILE
137
138 echo "# Show new /usr/ports/UPDATING entries (starting from $LAST_UPDATED) ..."
139 if [`cat $TMPFILE | wc -l` -eq 7 ]; then
140 echo "## No new entries found"
141 else
142 cat $TMPFILE
143 fi
144 rm $TMPFILE
145
146 # agree to the terms of the game
147 echo "# Ports pending update:"
148 echo_list "# - " $NEWPORTS
149 ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
150
151 #
152 # Stop daemons
153 DAEMONS_STOPPED=$FALSE
154 if [ -n "$RC_FILES" ]; then
155 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
156 if [ -n "$RUNNING_DAEMONS" ]; then
157 echo "# Running daemons:"; echo_list "# - " $RUNNING_DAEMONS
158 ask_question "# Do you like to stop running daemons" "yes" && {
159 for RC_FILE in $RUNNING_DAEMONS; do
160 $RC_FILE stop
161 done;
162 DAEMONS_STOPPED=$TRUE
163 }
164 fi
165 fi
166fi
167echo "# Using update tool '$USE_PORTMGNT"
168case $USE_PORTMGNT in
169 portupgrade)
170 portupgrade -r -mBATCH=yes -a $NEWPORTS
171 ;;
172 portmaster)
173 portmaster -d --no-confirm --no-term-title --no-index-fetch -mBATCH=yes -a $NEWPORTS
174 ;;
175 *)
176 echo "# ERROR: Failed to find a port management tool"
177 exit 1
178 ;;
179esac
180
181#
182# (Re)start daemons. Assume we only like to start the just stopped ones.
183if [ -n "$RC_FILES" ]; then
184 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
185 if [ -n "$RUNNING_DAEMONS" ]; then
186 if [ $DAEMONS_STOPPED -eq $TRUE ]; then
187 echo "# Enabled daemons: "; echo_list "# - " $RUNNING_DAEMONS
188 ask_question "# Do you like to start your daemons" "yes" && {
189 for RC_FILE in $RUNNING_DAEMONS; do
190 $RC_FILE start
191 done
192 }
193 else
194 echo "# Running daemons: "; echo_list "# - " $RUNNING_DAEMONS
195 ask_question "# Do you like to restart your daemons" "yes" && {
196 for RC_FILE in $RUNNING_DAEMONS; do
197 $RC_FILE restart
198 done
199 }
200 fi
201 fi
202fi
203
204echo "# All done, goodbye"
Note: See TracBrowser for help on using the repository browser.