[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] | 11 | FALSE=0
|
---|
| 12 | TRUE=1
|
---|
| 13 |
|
---|
[185] | 14 | ask_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] | 24 | echo_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] | 35 | if [ `id -u` -ne 0 ]; then
|
---|
| 36 | echo "Sorry root only" 1>&2
|
---|
| 37 | exit 128
|
---|
| 38 | fi
|
---|
| 39 |
|
---|
[265] | 40 | # Make sure we always using the latest version
|
---|
| 41 | CHANGES=`svn up --non-interactive $(dirname $0) | wc -l`
|
---|
| 42 | if [ $CHANGES -gt 1 ]; then
|
---|
| 43 | sh $0
|
---|
| 44 | exit 0
|
---|
| 45 | fi
|
---|
[193] | 46 |
|
---|
| 47 | #
|
---|
| 48 | # Check which ports-mgnt tool to use to upgrade outdated ports
|
---|
| 49 | #
|
---|
| 50 | FORCE_PORTUPGRADE=${FORCE_PORTUPGRADE:-$FALSE}
|
---|
| 51 | FORCE_PORTMASTER=${FORCE_PORTMASTER:-$FALSE}
|
---|
| 52 |
|
---|
| 53 | PORTUPGRADE_INSTALLED=$FALSE
|
---|
| 54 | PORTMASTER_INSTALLED=$FALSE
|
---|
| 55 | USE_PORTMGNT="none"
|
---|
| 56 | if [ -n "`whereis -qb portupgrade`" ]; then
|
---|
| 57 | PORTUPGRADE_INSTALLED=$TRUE
|
---|
| 58 | fi
|
---|
| 59 | if [ -n "`whereis -qb portmaster`" ]; then
|
---|
| 60 | PORTMASTER_INSTALLED=$TRUE
|
---|
| 61 | fi
|
---|
| 62 |
|
---|
| 63 | if [ $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"
|
---|
| 69 | elif [ $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"
|
---|
| 75 | else
|
---|
| 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] | 92 | fi
|
---|
| 93 |
|
---|
[193] | 94 |
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 |
|
---|
[119] | 98 | DIRNAME=`dirname $0`
|
---|
[182] | 99 | BATCH=${BATCH-""}
|
---|
[84] | 100 |
|
---|
[185] | 101 | # Get list of rc.d marked for updates
|
---|
| 102 | RC_FILES=`$DIRNAME/port-list.sh`
|
---|
[193] | 103 | RUNNING_DAEMONS=""
|
---|
[119] | 104 |
|
---|
[193] | 105 | for RC_FILE in $RC_FILES; do
|
---|
| 106 | $RC_FILE onestatus | grep -q 'is running as' && {
|
---|
| 107 | RUNNING_DAEMONS="$RUNNING_DAEMONS $RC_FILE"
|
---|
| 108 | }
|
---|
| 109 | done
|
---|
| 110 |
|
---|
[84] | 111 | # Update port tree
|
---|
[185] | 112 | echo "# Updating portstree ..."
|
---|
| 113 | portsnap update > /dev/null
|
---|
[84] | 114 |
|
---|
[182] | 115 | # Update ports listing
|
---|
| 116 | echo "# Checking ports which needs updating ..."
|
---|
[193] | 117 | NEWPORTS=`pkg_version -qol '<'`
|
---|
[182] | 118 |
|
---|
[193] | 119 | # Check for potentional trouble.
|
---|
| 120 | # XXX: Weird -q does not strip at '!' check
|
---|
| 121 | BROKENPORTS=`pkg_version -qol '!' | awk '{print $1}'`
|
---|
| 122 | if [ -n "$BROKENPORTS" ]; then
|
---|
| 123 | echo "# WARNING comparion failed, fix by hand"; echo_list "# - " $BROKENPORTS
|
---|
| 124 | fi
|
---|
| 125 |
|
---|
[266] | 126 |
|
---|
[182] | 127 | if [ -z "$NEWPORTS" ]; then
|
---|
[193] | 128 | echo "# No ports to update"
|
---|
| 129 | echo "# All done, goodbye"
|
---|
[182] | 130 | exit 0
|
---|
| 131 | fi
|
---|
| 132 |
|
---|
[266] | 133 | # Check for broken pkgdb, else we have trouble during upgrades
|
---|
| 134 | if [ $USE_PORTMGNT = "portupgrade" ]; then
|
---|
| 135 | pkgdb -a || exit 1
|
---|
| 136 | fi
|
---|
| 137 |
|
---|
[185] | 138 | #
|
---|
| 139 | # Update ports
|
---|
[182] | 140 | if [ -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] | 179 | fi
|
---|
[193] | 180 | echo "# Using update tool '$USE_PORTMGNT"
|
---|
| 181 | case $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 | ;;
|
---|
| 192 | esac
|
---|
[182] | 193 |
|
---|
[185] | 194 | #
|
---|
[193] | 195 | # (Re)start daemons. Assume we only like to start the just stopped ones.
|
---|
[190] | 196 | if [ -n "$RC_FILES" ]; then
|
---|
[193] | 197 | echo "# Affected daemons:"; echo_list "# - " $RC_FILES
|
---|
| 198 | if [ -n "$RUNNING_DAEMONS" ]; then
|
---|
| 199 | if [ $DAEMONS_STOPPED -eq $TRUE ]; then
|
---|
| 200 | echo "# Enabled daemons: "; echo_list "# - " $RUNNING_DAEMONS
|
---|
| 201 | ask_question "# Do you like to start your daemons" "yes" && {
|
---|
| 202 | for RC_FILE in $RUNNING_DAEMONS; do
|
---|
| 203 | $RC_FILE start
|
---|
| 204 | done
|
---|
| 205 | }
|
---|
| 206 | else
|
---|
| 207 | echo "# Running daemons: "; echo_list "# - " $RUNNING_DAEMONS
|
---|
| 208 | ask_question "# Do you like to restart your daemons" "yes" && {
|
---|
| 209 | for RC_FILE in $RUNNING_DAEMONS; do
|
---|
| 210 | $RC_FILE restart
|
---|
| 211 | done
|
---|
| 212 | }
|
---|
| 213 | fi
|
---|
| 214 | fi
|
---|
[190] | 215 | fi
|
---|
[193] | 216 |
|
---|
| 217 | echo "# All done, goodbye"
|
---|