/
sync-ports
executable file
·105 lines (96 loc) · 3.82 KB
/
sync-ports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
# Copyright (c) 2014-2016 Reed A. Cartwright <reed@cartwrig.ht>
# Copyright (c) 2014-2015 Alberto Villa <avilla@FreeBSD.org>
# Copyright (c) 2015 Mike Clarke <jmc-freebsd2@milibyte.co.uk>
#
# This script determines the revision number used to build FreeBSD packages
# and syncs a local ports directory to match it.
#
# USAGE: sync-ports [name or abs_path]
#
# REQUIREMENTS: textproc/jq, ports-mgmt/poudriere (optional)
#
# If the argument is not an absolute path, the script will treat it as
# the name of a poudriere-managed ports directory. If poudriere is
# not installed, it will use /usr/ports as the ports directory.
#
# If the argument is not specified, it defaults to "default".
#
# The script uses uname to choose which pkg jail to sync with.
# You can specify PKG_SERVER, PKG_JAIL, and/or PKG_URL in the
# environment to override the default options.
# Set the path
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
# Determine PKG_JAIL
case `uname -r` in
*CURRENT) : ${PKG_JAIL:=head-`uname -m`-default} ;;
10.*) : ${PKG_JAIL:=101`uname -m`-default} ;;
9.*) : ${PKG_JAIL:=93`uname -m`-default} ;;
8.*) : ${PKG_JAIL:=84`uname -m`-default} ;;
*) : ${PKG_JAIL:=`uname -r | cut -d- -f1 | tr -d .``uname -m`-default} ;;
esac
# Determine PKG_SERVER
if [ -z "${PKG_URL}" -a -z "${PKG_SERVER}" ]; then
case "${PKG_JAIL}" in
93i386-default) PKG_SERVER=beefy1.isc.freebsd.org ;;
101i386-quarterly) PKG_SERVER=beefy1.isc.freebsd.org ;;
93amd64-default) PKG_SERVER=beefy2.isc.freebsd.org ;;
101amd64-quarterly) PKG_SERVER=beefy2.isc.freebsd.org ;;
head-i386-default) PKG_SERVER=beefy3.isc.freebsd.org ;;
93i386-quarterly) PKG_SERVER=beefy3.isc.freebsd.org ;;
head-amd64-default) PKG_SERVER=beefy4.isc.freebsd.org ;;
93amd64-quarterly) PKG_SERVER=beefy4.isc.freebsd.org ;;
101i386-default) PKG_SERVER=beefy5.nyi.freebsd.org ;;
84i386-default) PKG_SERVER=beefy5.nyi.freebsd.org ;;
101amd64-default) PKG_SERVER=beefy6.nyi.freebsd.org ;;
84amd64-default) PKG_SERVER=beefy6.nyi.freebsd.org ;;
*) >&2 echo "[sync-ports] ERROR: Unable to determine package server for ${PKG_JAIL}."
exit 1
;;
esac
fi
# Build PKG_URL
: ${PKG_URL:=http://${PKG_SERVER}/data/${PKG_JAIL}/.data.json}
# If the argument is not an absolute path, use poudriere to resolve it.
# We test for absolute path by seeing if it begins with a slash.
PORTSTREE="${1:-default}"
if [ "${PORTSTREE#/}" != "${PORTSTREE}" ]; then
PORTSDIR="${PORTSTREE}"
elif which poudriere >/dev/null 2>&1 ; then
PORTSDIR=`poudriere ports -ql | awk -v PT="${PORTSTREE}" '$1 == PT { print $3 }'`
else
PORTSTREE="/usr/ports"
PORTSDIR="${PORTSTREE}"
fi
# Check if the directory exists
if [ ! -d "${PORTSDIR}" ]; then
>&2 echo "ERROR: Unable to resolve ports tree '${PORTSTREE}' to a directory."
exit 1
fi
# Fetch data from server
JSON=`fetch -qo - "${PKG_URL}"`
if [ $? -ne 0 ]; then
>&2 echo "[sync-ports] ERROR: Unable to fetch data from package server."
exit 1
fi
# Parse revision information from server (exclude incomplete builds)
REV=`printf %s "${JSON}" | jq -r '[.builds[] | select(.status? == "stopped:done:") | .buildname] | max'`
# Check revision information
if expr "${REV}" : '^[[:digit:]][[:digit:]]*$' >/dev/null; then
# Skip update if revisions are in sync
CURREV=`svnlite info "${PORTSDIR}" | awk -F': ' '$1 == "Revision" {print $2; exit}'`
if [ -z "${CURREV}" ]; then
>&2 echo "[sync-ports] ERROR: Unable to get current revision for ports tree '${PORTSTREE}'."
exit 1
fi
if [ "${CURREV}" -ne "${REV}" ]; then
echo "[sync-ports] Updating ports tree '${PORTSTREE}' from revision ${CURREV} to ${REV}"
svnlite up -q -r "${REV}" "${PORTSDIR}"
else
echo "[sync-ports] Ports tree '${PORTSTREE}' is up to date at revision ${CURREV}"
fi
exit
else
>& echo "[sync-ports] ERROR: Unable to determine revision number for latest packages."
exit 1
fi