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

Last change on this file since 265 was 265, checked in by Rick van der Zwet, 14 years ago

Buildin check to ensure I am using the latest version.

  • Property svn:executable set to *
File size: 5.4 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
126if [ -z "$NEWPORTS" ]; then
127 echo "# No ports to update"
128 echo "# All done, goodbye"
129 exit 0
130fi
131
132#
133# Update ports
134if [ -z "$BATCH" ]; then
135 # Make sure we have read the (new) UPDATING announcements
136 # Use ls to display the last motification time of the directories in
137 # /var/db/pkg and assume that is the last time the user read the
138 # UPDATING file
139 LAST_UPDATED=`ls -rtlD '%Y%m%d' /var/db/pkg/ | awk 'END{print $6}'`
140
141 # Find new entries
142 TMPFILE=`mktemp -t $(basename $0).XXX`
143 awk -F: -v last=$LAST_UPDATED '{if (match($0,"^[0-9]+:$") && $1 < last) {exit}; print $0}' /usr/ports/UPDATING > $TMPFILE
144
145 echo "# Show new /usr/ports/UPDATING entries (starting from $LAST_UPDATED) ..."
146 if [`cat $TMPFILE | wc -l` -eq 7 ]; then
147 echo "## No new entries found"
148 else
149 cat $TMPFILE
150 fi
151 rm $TMPFILE
152
153 # agree to the terms of the game
154 echo "# Ports pending update:"
155 echo_list "# - " $NEWPORTS
156 ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
157
158 #
159 # Stop daemons
160 DAEMONS_STOPPED=$FALSE
161 if [ -n "$RC_FILES" ]; then
162 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
163 if [ -n "$RUNNING_DAEMONS" ]; then
164 echo "# Running daemons:"; echo_list "# - " $RUNNING_DAEMONS
165 ask_question "# Do you like to stop running daemons" "yes" && {
166 for RC_FILE in $RUNNING_DAEMONS; do
167 $RC_FILE stop
168 done;
169 DAEMONS_STOPPED=$TRUE
170 }
171 fi
172 fi
173fi
174echo "# Using update tool '$USE_PORTMGNT"
175case $USE_PORTMGNT in
176 portupgrade)
177 portupgrade -r -mBATCH=yes -a $NEWPORTS
178 ;;
179 portmaster)
180 portmaster -d --no-confirm --no-term-title --no-index-fetch -mBATCH=yes -a $NEWPORTS
181 ;;
182 *)
183 echo "# ERROR: Failed to find a port management tool"
184 exit 1
185 ;;
186esac
187
188#
189# (Re)start daemons. Assume we only like to start the just stopped ones.
190if [ -n "$RC_FILES" ]; then
191 echo "# Affected daemons:"; echo_list "# - " $RC_FILES
192 if [ -n "$RUNNING_DAEMONS" ]; then
193 if [ $DAEMONS_STOPPED -eq $TRUE ]; then
194 echo "# Enabled daemons: "; echo_list "# - " $RUNNING_DAEMONS
195 ask_question "# Do you like to start your daemons" "yes" && {
196 for RC_FILE in $RUNNING_DAEMONS; do
197 $RC_FILE start
198 done
199 }
200 else
201 echo "# Running daemons: "; echo_list "# - " $RUNNING_DAEMONS
202 ask_question "# Do you like to restart your daemons" "yes" && {
203 for RC_FILE in $RUNNING_DAEMONS; do
204 $RC_FILE restart
205 done
206 }
207 fi
208 fi
209fi
210
211echo "# All done, goodbye"
Note: See TracBrowser for help on using the repository browser.