[84] | 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 |
|
---|
[182] | 7 | . /etc/rc.subr
|
---|
| 8 |
|
---|
[193] | 9 | # Pointers to avoid confusion corresponts with logical boolean style values.
|
---|
| 10 | # WARNING do not conpare with exit codes!
|
---|
| 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 |
|
---|
| 32 | #
|
---|
| 33 | # SuperCow power only
|
---|
[84] | 34 | if [ `id -u` -ne 0 ]; then
|
---|
| 35 | echo "Sorry root only" 1>&2
|
---|
| 36 | exit 128
|
---|
| 37 | fi
|
---|
| 38 |
|
---|
[193] | 39 |
|
---|
| 40 | #
|
---|
| 41 | # Check which ports-mgnt tool to use to upgrade outdated ports
|
---|
| 42 | #
|
---|
| 43 | FORCE_PORTUPGRADE=${FORCE_PORTUPGRADE:-$FALSE}
|
---|
| 44 | FORCE_PORTMASTER=${FORCE_PORTMASTER:-$FALSE}
|
---|
| 45 |
|
---|
| 46 | PORTUPGRADE_INSTALLED=$FALSE
|
---|
| 47 | PORTMASTER_INSTALLED=$FALSE
|
---|
| 48 | USE_PORTMGNT="none"
|
---|
| 49 | if [ -n "`whereis -qb portupgrade`" ]; then
|
---|
| 50 | PORTUPGRADE_INSTALLED=$TRUE
|
---|
| 51 | fi
|
---|
| 52 | if [ -n "`whereis -qb portmaster`" ]; then
|
---|
| 53 | PORTMASTER_INSTALLED=$TRUE
|
---|
| 54 | fi
|
---|
| 55 |
|
---|
| 56 | if [ $FORCE_PORTMASTER -eq $TRUE ]; then
|
---|
| 57 | if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
|
---|
| 58 | echo "# ERROR: FORCE_PORTMASTER set but binary not found"
|
---|
[192] | 59 | exit 1
|
---|
[193] | 60 | fi
|
---|
| 61 | USE_PORTMGNT="portmaster"
|
---|
| 62 | elif [ $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"
|
---|
| 68 | else
|
---|
| 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
|
---|
[192] | 85 | fi
|
---|
| 86 |
|
---|
[193] | 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 |
|
---|
[119] | 91 | DIRNAME=`dirname $0`
|
---|
[182] | 92 | BATCH=${BATCH-""}
|
---|
[84] | 93 |
|
---|
[185] | 94 | # Get list of rc.d marked for updates
|
---|
| 95 | RC_FILES=`$DIRNAME/port-list.sh`
|
---|
[193] | 96 | RUNNING_DAEMONS=""
|
---|
[119] | 97 |
|
---|
[193] | 98 | for RC_FILE in $RC_FILES; do
|
---|
| 99 | $RC_FILE onestatus | grep -q 'is running as' && {
|
---|
| 100 | RUNNING_DAEMONS="$RUNNING_DAEMONS $RC_FILE"
|
---|
| 101 | }
|
---|
| 102 | done
|
---|
| 103 |
|
---|
[84] | 104 | # Update port tree
|
---|
[185] | 105 | echo "# Updating portstree ..."
|
---|
| 106 | portsnap update > /dev/null
|
---|
[84] | 107 |
|
---|
[182] | 108 | # Update ports listing
|
---|
| 109 | echo "# Checking ports which needs updating ..."
|
---|
[193] | 110 | NEWPORTS=`pkg_version -qol '<'`
|
---|
[182] | 111 |
|
---|
[193] | 112 | # Check for potentional trouble.
|
---|
| 113 | # XXX: Weird -q does not strip at '!' check
|
---|
| 114 | BROKENPORTS=`pkg_version -qol '!' | awk '{print $1}'`
|
---|
| 115 | if [ -n "$BROKENPORTS" ]; then
|
---|
| 116 | echo "# WARNING comparion failed, fix by hand"; echo_list "# - " $BROKENPORTS
|
---|
| 117 | fi
|
---|
| 118 |
|
---|
[182] | 119 | if [ -z "$NEWPORTS" ]; then
|
---|
[193] | 120 | echo "# No ports to update"
|
---|
| 121 | echo "# All done, goodbye"
|
---|
[182] | 122 | exit 0
|
---|
| 123 | fi
|
---|
| 124 |
|
---|
[185] | 125 | #
|
---|
| 126 | # Update ports
|
---|
[182] | 127 | if [ -z "$BATCH" ]; then
|
---|
| 128 | # Make sure we have read the (new) UPDATING announcements
|
---|
[185] | 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}'`
|
---|
[182] | 133 |
|
---|
[185] | 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
|
---|
[193] | 147 | echo "# Ports pending update:"
|
---|
| 148 | echo_list "# - " $NEWPORTS
|
---|
| 149 | ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
|
---|
[182] | 150 |
|
---|
[185] | 151 | #
|
---|
| 152 | # Stop daemons
|
---|
[193] | 153 | DAEMONS_STOPPED=$FALSE
|
---|
[190] | 154 | if [ -n "$RC_FILES" ]; then
|
---|
[193] | 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
|
---|
[190] | 165 | fi
|
---|
[182] | 166 | fi
|
---|
[193] | 167 | echo "# Using update tool '$USE_PORTMGNT"
|
---|
| 168 | case $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 | ;;
|
---|
| 179 | esac
|
---|
[182] | 180 |
|
---|
[185] | 181 | #
|
---|
[193] | 182 | # (Re)start daemons. Assume we only like to start the just stopped ones.
|
---|
[190] | 183 | if [ -n "$RC_FILES" ]; then
|
---|
[193] | 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
|
---|
[190] | 202 | fi
|
---|
[193] | 203 |
|
---|
| 204 | echo "# All done, goodbye"
|
---|