Blame
Date:
Wed Apr 29 20:51:15 2020 UTC
Message:
add all_mirror : FIXME try to figure out how to print 1 mirror per line
001
2015-05-06
f
#!/bin/ksh
002
2015-05-06
f
#
003
2015-05-06
f
########################################################################
004
2015-05-06
f
#
005
2020-04-25
f
# snaps.sh - Check if you can upgrade your OpenBSD OS base to
006
2020-04-25
f
# last -current snapshot
007
2015-05-06
f
#
008
2020-04-25
f
# Copyright (c) 2017-2020, Fred Galusik <f AT galusik.fr>
009
2015-05-06
f
# All rights reserved.
010
2015-05-06
f
#
011
2015-05-06
f
# Redistribution and use in source and binary forms, with or without
012
2015-05-06
f
# modification, are permitted provided that the following conditions are
013
2015-05-06
f
# met:
014
2015-05-06
f
#
015
2015-05-06
f
# 1. Redistributions of source code must retain the above copyright
016
2015-05-06
f
# notice, this list of conditions and the following disclaimer.
017
2015-05-06
f
#
018
2015-05-06
f
# 2. Redistributions in binary form must reproduce the above copyright
019
2015-05-06
f
# notice, this list of conditions and the following disclaimer in the
020
2015-05-06
f
# documentation and/or other materials provided with the distribution.
021
2015-05-06
f
#
022
2015-05-06
f
# 3. Neither the name of the copyright holder nor the names of its
023
2015-05-06
f
# contributors may be used to endorse or promote products derived from
024
2015-05-06
f
# this software without specific prior written permission.
025
2015-05-06
f
#
026
2015-05-06
f
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027
2015-05-06
f
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028
2015-05-06
f
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029
2015-05-06
f
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
030
2015-05-06
f
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
031
2015-05-06
f
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032
2015-05-06
f
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
033
2015-05-06
f
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
034
2015-05-06
f
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
035
2015-05-06
f
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
036
2015-05-06
f
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037
2015-05-06
f
#
038
2015-05-06
f
########################################################################
039
2015-05-06
f
040
2015-05-06
f
###
041
2015-05-06
f
### VARS
042
2015-05-06
f
###
043
2015-05-06
f
044
2017-08-25
dev1
VERSION=1.3
045
2015-05-09
f
046
2020-04-29
f
# choose your MIRROR
047
2020-04-29
f
if [ ! "${MIRROR}" ]; then
048
2017-03-11
f
MIRROR=$(grep -v "^#" /etc/installurl)
049
2015-05-17
f
fi
050
2020-04-27
f
051
2020-04-29
f
# curl is mandatory
052
2020-04-29
f
CURL=/usr/local/bin/curl
053
2020-04-29
f
if [[ ! -e ${CURL} ]]; then
054
2020-04-29
f
echo "{CURL} is requested to run this program. Aborting..."
055
2020-04-29
f
exit 1
056
2020-04-29
f
fi
057
2020-04-29
f
058
2020-04-29
f
#
059
2020-04-27
f
CURRENTSYS=$(sed 1q /var/run/dmesg.boot)
060
2017-03-06
dev1
ARCH=$(uname -m)
061
2020-04-29
f
BASE=/snapshots/${ARCH}/
062
2020-04-29
f
PACKAGES=/snapshots/packages/${ARCH}/
063
2020-04-29
f
064
2017-03-06
dev1
MIRRORB=${MIRROR}/snapshots/${ARCH}/
065
2017-03-06
dev1
MIRRORPKG=${MIRROR}/snapshots/packages/${ARCH}/
066
2020-04-29
f
HTTPSLIST=https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/www/httpslist
067
2015-05-17
f
068
2015-05-06
f
SHAF=SHA256
069
2015-05-06
f
070
2020-04-25
f
GET='ftp -n -m -C'
071
2020-04-25
f
072
2015-05-06
f
LOGF=/var/log/snaps.log
073
2015-05-17
f
NOW=$(date "+ %Y-%m-%d %R")
074
2015-05-06
f
075
2016-09-11
f
REMOTESNAPS='https://framagit.org/fredg/snaps/raw/master/snaps'
076
2016-09-11
f
LASTSNAPS=/tmp/snaps
077
2016-10-15
dev1
078
2015-05-06
f
###
079
2015-05-06
f
###
080
2015-06-20
f
###
081
2015-05-06
f
082
2020-04-27
f
083
2020-04-27
f
usage() {
084
2020-04-27
f
echo "${0##*/} $VERSION"
085
2020-04-27
f
echo "Usage: ${0##*/} [-scpluh]"
086
2020-04-27
f
echo ""
087
2020-04-27
f
echo " -s check \$MIRROR freshness & choose to sysupgrade or not"
088
2020-04-27
f
echo " -c check BASE and PACKAGES build date from \$MIRROR"
089
2020-04-27
f
echo " -p update port tree to -current"
090
2020-04-27
f
echo " -l list date and \$MIRROR of the 3 last upgrade with this script"
091
2020-04-27
f
echo " -u upgrade snaps to last version"
092
2020-04-27
f
echo " -h print this help and exit"
093
2020-04-27
f
echo ""
094
2020-04-27
f
echo "MIRROR is based upon /etc/installurl and set to: $MIRROR"
095
2020-04-27
f
echo ""
096
2020-04-27
f
echo "You can set it manually:"
097
2020-04-27
f
echo "\$ MIRROR=http://mirrors.ircam.fr/pub/OpenBSD snaps -c"
098
2020-04-27
f
echo ""
099
2020-04-27
f
}
100
2020-04-27
f
101
2020-04-27
f
102
2020-04-27
f
s_root() {
103
2020-04-27
f
if [[ $(id -u) != "0" ]]; then
104
2020-04-27
f
echo "WARNING: You need to be root to do this!" 1>&2
105
2020-04-27
f
exit 1
106
2020-04-27
f
fi
107
2020-04-27
f
}
108
2020-04-27
f
109
2020-04-27
f
110
2016-09-11
f
toupgrade() {
111
2020-04-27
f
echo "Checking snaps release..."
112
2020-04-27
f
if [[ -f "$LASTSNAPS" ]]; then
113
2016-09-11
dev1
rm -f $LASTSNAPS
114
2016-09-11
dev1
fi
115
2020-04-25
f
if $GET -V -o "$LASTSNAPS" "$REMOTESNAPS"; then
116
2020-04-25
f
REMOTEVERSION=$(awk -F '=' '/^VERSION=/ { print $2 }' "$LASTSNAPS")
117
2016-09-11
dev1
if [ "$VERSION" != "$REMOTEVERSION" ]; then
118
2020-04-27
f
echo "WARNING: snaps is not up-to-date !"
119
2016-09-11
dev1
echo "Last version: $REMOTEVERSION"
120
2016-09-11
f
echo "To upgrade, run 'snaps -u'"
121
2016-09-11
dev1
else
122
2020-04-27
f
echo "GOOD: snaps is up-to-date."
123
2016-09-11
f
fi
124
2016-10-23
dev1
else
125
2020-04-27
f
echo "WARNING: Remote snaps not reachable ! Check the connection."
126
2016-09-11
f
fi
127
2020-04-27
f
echo ""
128
2016-09-11
f
}
129
2016-09-11
f
130
2020-04-27
f
131
2016-09-11
f
upgrade() {
132
2020-04-27
f
SELFPATH=$(dirname "$(readlink -f -- "$0")")
133
2020-04-27
f
echo "Downloading last snaps version..."
134
2016-09-11
f
$GET -o "$SELFPATH" "$REMOTESNAPS"
135
2016-09-11
f
}
136
2016-09-11
f
137
2016-09-11
f
138
2015-05-09
f
get_log() {
139
2020-04-27
f
if [[ -f "$LOGF" ]]; then
140
2015-05-06
f
tail -n 3 $LOGF
141
2015-05-06
f
else
142
2020-04-27
f
echo "No $LOGF file. Seems you have not yet played with snaps."
143
2015-05-09
f
exit 1
144
2015-05-06
f
fi
145
2015-05-06
f
}
146
2015-05-06
f
147
2015-05-17
f
148
2016-06-11
f
s_upports() {
149
2020-04-27
f
echo "Updating Ports tree..."
150
2016-06-11
f
cd /usr/ports || exit 1
151
2016-06-11
f
cvs up -Pd || exit 1
152
2020-04-27
f
echo "New Ports tree fetched!"
153
2016-06-11
f
}
154
2016-06-11
f
155
2020-04-27
f
156
2020-04-27
f
s_cur() {
157
2020-04-27
f
echo "=== LOCAL BASE BUILD DATE ==="
158
2020-04-27
f
echo "${CURRENTSYS}"
159
2020-04-27
f
}
160
2020-04-27
f
161
2020-04-27
f
162
2016-10-15
dev1
s_when() {
163
2020-04-29
f
CURLB="$(${CURL} -sI "${MIRRORB}${SHAF}" | grep Last-Modified)"
164
2020-04-29
f
CURLP="$(${CURL} -sI "${MIRRORPKG}${SHAF}" | grep Last-Modified)"
165
2020-04-29
f
echo "=== ONLINE BASE AND PACKAGES BUILD DATE ==="
166
2020-04-29
f
echo "From ${MIRROR}"
167
2020-04-29
f
echo "Base (${ARCH}) => ${CURLB}"
168
2020-04-29
f
echo "Packages (${ARCH}) => ${CURLP}"
169
2016-10-15
dev1
}
170
2016-10-15
dev1
171
2020-04-27
f
172
2020-04-27
f
s_sysup () {
173
2020-04-27
f
echo ""
174
2020-04-27
f
echo -n "Do you want to run 'sysupgrade -s' ? (y/n): "
175
2020-04-27
f
read -r _c
176
2020-04-27
f
case ${_c} in
177
2020-04-27
f
y|Y)
178
2020-04-27
f
;;
179
2020-04-27
f
*)
180
2020-04-27
f
echo "Aborting..."
181
2020-04-27
f
exit 1
182
2020-04-27
f
;;
183
2020-04-27
f
esac
184
2020-04-27
f
echo "${NOW} FROM ${MIRRORB}" >> ${LOGF}
185
2020-04-27
f
sysupgrade -s
186
2020-04-27
f
}
187
2020-04-27
f
188
2020-04-27
f
189
2020-04-29
f
all_mirrors () {
190
2020-04-29
f
MIRRORLIST=$(${CURL} ${HTTPSLIST} | awk '{print $1}')
191
2020-04-29
f
for _m in ${MIRRORLIST}
192
2020-04-29
f
do
193
2020-04-29
f
CB="$(${CURL} -sI "${_m}${BASE}${SHAF}" | grep Last-Modified |\
194
2020-04-29
f
sed 's/Last-Modified: //')"
195
2020-04-29
f
CP="$(${CURL} -sI "${_m}${PACKAGES}${SHAF}" | grep Last-Modified |\
196
2020-04-29
f
sed 's/Last-Modified: //')"
197
2020-04-29
f
# only print mirror which give an answer
198
2020-04-29
f
# FIXME try to figure out how to print 1 mirror per line
199
2020-04-29
f
if [ "${CB}" ]; then
200
2020-04-29
f
echo "${_m}"
201
2020-04-29
f
echo "${CB}"
202
2020-04-29
f
echo "${CP}"
203
2020-04-29
f
echo ""
204
2020-04-29
f
fi
205
2020-04-29
f
done
206
2020-04-29
f
}
207
2020-04-29
f
208
2020-04-29
f
209
2015-05-06
f
##
210
2015-05-06
f
## Run
211
2015-05-06
f
##
212
2016-10-15
dev1
213
2015-05-06
f
case $1 in
214
2020-04-27
f
-s)
215
2015-05-09
f
s_root
216
2016-10-15
dev1
toupgrade
217
2020-04-27
f
s_cur
218
2020-04-27
f
echo ""
219
2016-10-15
dev1
s_when
220
2020-04-27
f
s_sysup
221
2015-05-06
f
;;
222
2020-04-27
f
-c)
223
2020-04-27
f
s_when
224
2020-04-27
f
;;
225
2020-04-29
f
-a)
226
2020-04-29
f
all_mirrors
227
2020-04-29
f
;;
228
2016-06-11
f
-p)
229
2016-06-11
f
s_upports
230
2016-06-11
f
;;
231
2015-05-06
f
-l)
232
2015-05-30
f
get_log
233
2015-05-06
f
;;
234
2016-09-11
f
-u)
235
2016-09-11
f
upgrade
236
2016-09-11
f
;;
237
2015-05-06
f
-h|*)
238
2015-05-06
f
usage
239
2015-05-06
f
exit 1
240
2015-05-06
f
;;
241
2015-05-06
f
esac
242
2020-04-27
f
Frédéric Galusik