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