comparison sbin/fpkg @ 128:3dcae0e91769

Move all admin scripts into the "sbin" folder
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 17 Oct 2019 09:09:13 +0200
parents bin/fpkg@2d531cbd0feb
children 4aeff9d5275d
comparison
equal deleted inserted replaced
127:37737f225887 128:3dcae0e91769
1 #!/bin/sh
2 # -*- indent-tabs-mode: nil; -*-
3 : 'A pkg frontend for common operations that also operates in all
4 running jails.
5
6 :Author: Franz Glasner
7 :Copyright: (c) 2019 Franz Glasner.
8 All rights reserved.
9 :License: BSD 3-Clause "New" or "Revised" License.
10 See LICENSE for details.
11 If you cannot find LICENSE see
12 <https://opensource.org/licenses/BSD-3-Clause>
13 :ID: @(#)@@PKGORIGIN@@ $HGid$
14
15 '
16
17 VERSION="@@VERSION@@"
18
19 USAGE='
20 USAGE: fpkg [ OPTIONS] COMMAND [ COMMAND-OPTIONS ]
21
22 OPTIONS:
23
24 -V Print the program name and version number to stdout and exit
25
26 -h Print this help message to stdout and exit
27
28 COMMANDS:
29
30 audit
31
32 `pkg audit` on the local host and all running visible jails
33
34 update
35
36 `pkg update` on the local host and all running visible jails
37
38 upgrade
39
40 `pkg upgrade` on the local host and all running visible jails
41
42 upgrade-check
43
44 `pkg upgrade -n` on the local host and all running visible jails
45
46 check-fast-track
47 Check packages installed from the LocalBSDPorts repository against
48 the repositories `FreeBSD` and `LocalBSDPorts` on the local host
49 and all visible jails
50
51
52 ENVIRONMENT:
53
54 FPKG_AUDIT_FLAGS
55 Additional flags given to `pkg audit`
56 (Default: -Fr)
57
58 FPKG_UPDATE_FLAGS
59 Additional flags given to `pkg update`
60 (Default: empty)
61
62 FPKG_UPGRADE_FLAGS
63 Additional flags given to `pkg upgrade` and `pkg upgrade -n`
64 (Default: empty)
65
66 FPKG_SIGN
67 Marker for the begin of an output group (local host or jail)
68 (Default: "===> ")
69
70 FPKG_SKIPSIGN
71 Marker for the begin of a skipped output group
72 (Default: "----> ")
73
74 All other environment variables that affect `pkg` are effective also.
75 '
76
77 #
78 # Configuration directory
79 #
80 : ${CONFIGDIR:=@@ETCDIR@@}
81
82 test -r "${CONFIGDIR}/tools.conf" && . "${CONFIGDIR}/tools.conf"
83
84 : ${FPKG_AUDIT_FLAGS:=-Fr}
85 : ${FPKG_UPDATE_FLAGS:=}
86 : ${FPKG_UPGRADE_FLAGS:=}
87 : ${FPKG_SIGN:='===> '}
88 : ${FPKG_SKIPSIGN:='----> '}
89
90 #
91 # The official FreeBSD binary repository
92 #
93 : ${FREEBSD_REPO:=FreeBSD}
94
95 #
96 # Local repository with ports with default OPTIONS (i.e. unchanged)
97 # but newer than the packages in the "FreeBSD" repository.
98 # Some sort of a fast-track repository.
99 #
100 : ${LOCALBSDPORTS_REPO:=LocalBSDPorts}
101
102
103 has_same_userland_version() {
104 : 'Check whether the jail `_jail` has the same FreeBSD userland version
105 as the host the the current process runs.
106
107 Args:
108 _jail: the running jail to check for
109
110 Returns:
111 0 if the userland versions match, 1 otherwise
112
113 '
114 local _jail _host_version _jail_version
115
116 _jail="$1"
117
118 _host_version=$(/bin/freebsd-version -u) || exit 1
119 _jail_version=$(jexec -l "${_jail}" /bin/freebsd-version -u) || exit 1
120 if [ "${_host_version%%-*}" = "${_jail_version%%-*}" ]; then
121 return 0
122 fi
123 return 1
124 }
125
126
127 command_audit() {
128 : 'Do a local `pkg audit -Fr` and also for all running jails
129
130 '
131 echo "${FPKG_SIGN}LOCALHOST"
132 pkg audit ${FPKG_AUDIT_FLAGS}
133 for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
134 echo ""
135 echo "${FPKG_SIGN}JAIL: ${_j}"
136 if has_same_userland_version "${_j}"; then
137 pkg -j "${_j}" audit ${FPKG_AUDIT_FLAGS}
138 else
139 echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
140 fi
141 done
142 }
143
144
145 command_update() {
146 : 'Do a local `pkg update` and also for all running jails
147
148 '
149 echo "${FPKG_SIGN}LOCALHOST"
150 pkg update ${FPKG_UPDATE_FLAGS}
151 for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
152 echo ""
153 echo "${FPKG_SIGN}JAIL: ${_j}"
154 if has_same_userland_version "${_j}"; then
155 pkg -j "${_j}" update ${FPKG_UPDATE_FLAGS}
156 else
157 echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
158 fi
159 done
160 }
161
162
163 command_upgrade() {
164 : 'Do a local `pkg upgrade` and also for all running jails
165
166 '
167 echo "${FPKG_SIGN}LOCALHOST"
168 pkg upgrade ${FPKG_UPGRADE_FLAGS}
169 for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
170 echo ""
171 echo "${FPKG_SIGN}JAIL: ${_j}"
172 if has_same_userland_version "${_j}"; then
173 pkg -j "${_j}" upgrade ${FPKG_UPGRADE_FLAGS}
174 else
175 echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
176 fi
177 done
178 }
179
180
181 command_upgrade_check() {
182 : 'Do a local `pkg upgrade -n` and also for all running jails
183
184 '
185 echo "${FPKG_SIGN}LOCALHOST"
186 pkg upgrade -n ${FPKG_UPGRADE_FLAGS}
187 for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
188 echo ""
189 echo "${FPKG_SIGN}JAIL: ${_j}"
190 if has_same_userland_version "${_j}"; then
191 pkg -j "${_j}" upgrade -n ${FPKG_UPGRADE_FLAGS}
192 else
193 echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
194 fi
195 done
196 }
197
198
199 command_check_fasttrack() {
200 : 'Check the fast-track repository versions against the canonical
201 FreeBSD repository versions.
202
203 Input (Globals):
204 FREEBSD_REPO: the (canonical) FreeBSD repository name
205 LOCALBSDPORTS_REPO: the fast-track repository name
206
207 '
208 local _name local _repo _j
209
210 echo "${FPKG_SIGN}LOCALHOST"
211 pkg query '%n %R' |
212 while read _name _repo; do
213 if [ "${_repo}" = "${LOCALBSDPORTS_REPO}" ]; then
214 echo " ${_name}"
215 printf " %-15s : %s\n" "${LOCALBSDPORTS_REPO}" "$(pkg version -U -r ${LOCALBSDPORTS_REPO} -n ${_name} -v)"
216 printf " %-15s : %s\n" "${FREEBSD_REPO}" "$(pkg version -U -r ${FREEBSD_REPO} -n ${_name} -v)"
217 fi
218 done
219 for _j in $(jls -N | awk '{if(NR>1)print $1}' | sort); do
220 echo ""
221 echo "${FPKG_SIGN}JAIL: ${_j}"
222 if has_same_userland_version "${_j}"; then
223 pkg -j "${_j}" query '%n %R' |
224 while read _name _repo; do
225 if [ "${_repo}" = "${LOCALBSDPORTS_REPO}" ]; then
226 echo " ${_name}"
227 printf " %s-15s : %s\n" "${LOCALBSDPORTS_REPO}" "$(pkg -j ${_j} version -U -r ${LOCALBSDPORTS_REPO} -n ${_name} -v)"
228 printf " %-15s : %s\n" "${FREEBSD_REPO}" "$(pkg -j ${_j} version -U -r ${FREEBSD_REPO} -n ${_name} -v)"
229 fi
230 done
231 else
232 echo "${FPKG_SKIPSIGN}SKIPPED because of different userland"
233 fi
234 done
235 }
236
237
238 #
239 # Global option handling
240 #
241 while getopts "Vh" _opt ; do
242 case ${_opt} in
243 V)
244 echo "fpkg v${VERSION} (rv:@@HGREVISION@@)"
245 exit 0
246 ;;
247 h)
248 echo "${USAGE}"
249 exit 0
250 ;;
251 \?)
252 exit 2;
253 ;;
254 *)
255 echo "ERROR: option handling failed" >&2
256 exit 2
257 ;;
258 esac
259 done
260
261 #
262 # Reset the Shell's option handling system to prepare for handling
263 # command-local options.
264 #
265 shift $((OPTIND-1))
266 OPTIND=1
267
268 command="$1"
269 shift
270
271 test -n "$command" || { echo "ERROR: no command given" >&2; exit 2; }
272
273 case "${command}" in
274 audit)
275 command_audit "$@"
276 ;;
277 update)
278 command_update "$@"
279 ;;
280 upgrade)
281 command_upgrade "$@"
282 ;;
283 upgrade-check|upgrade_check)
284 command_upgrade_check "$@"
285 ;;
286 check-fast-track|check-fasttrack|check_fast_track|check_fasttrack)
287 command_check_fasttrack "$@"
288 ;;
289 *)
290 echo "ERROR: unknown command \`${command}'" >&2
291 exit 2;
292 ;;
293 esac