xref: /titanic_44/usr/src/tools/scripts/onu.sh (revision bb5c894bdad0d78b8fec5d050bda354c84f7c054)
1#!/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27PATH=/usr/bin:/usr/sbin
28export PATH
29
30DEFAULTONURI="http://ipkg.sfbay/on-nightly"
31DEFAULTONPUB="on-nightly"
32DEFAULTONEXTRAURI="http://ipkg.sfbay/on-extra"
33DEFAULTONEXTRAPUB="on-extra"
34
35usage()
36{
37	echo "usage: $0 [opts] [-s beName] -t beName"
38	echo "usage: $0 [opts] -r"
39	echo
40	echo "\t-d repodir : directory for repositories"
41	echo "\t-e uri : origin URI for extra repository"
42	echo "\t-E prefix : prefix for extra repository"
43	echo "\t-O : open mode, no extra repository will be used"
44	echo "\t-r : start repository servers only"
45	echo "\t-s : source BE to clone"
46	echo "\t-t : new BE name"
47	echo "\t-u uri : origin URI for redist repository"
48	echo "\t-U prefix:  prefix for redist repository"
49	echo "\t-v : verbose"
50	echo "\t-Z : skip updating zones"
51	echo
52	echo "Update to an ON build:"
53	echo "\tonu -t newbe -d /path/to/my/ws/packages/\`uname -p\`/nightly"
54	echo
55	echo "Update to the nightly build:"
56	echo "\tonu -t newbe"
57	echo
58	echo "Re-enable the publishers, and start any pkg.depotd servers"
59	echo "necessary in the current BE:"
60	echo "\tonu -r -d /path/to/my/ws/packages/\`uname -p\`/nightly"
61	exit 1
62}
63
64exit_error()
65{
66	echo $*
67	cleanup
68	exit 2
69}
70
71cleanup()
72{
73	[ $redistpid -gt 0 ] && kill $redistpid
74	[ $extrapid -gt 0 ] && kill $extrapid
75	[ -d /tmp/redist.$$ ] && /bin/rm -rf /tmp/redist.$$
76	[ -d /tmp/extra.$$ ] && /bin/rm -rf /tmp/extra.$$
77}
78
79do_cmd()
80{
81	[ $verbose -gt 0 ] && echo $*
82	$*
83	exit_code=$?
84	[ $exit_code -eq 0 ] && return
85	# pkg(1) returns 4 if "nothing to do", which is safe to ignore
86	[ $1 = "pkg" -a $exit_code -eq 4 ] && return
87	exit_error "$*" failed: exit code $exit_code
88}
89
90configure_publishers()
91{
92	root=$1
93
94	do_cmd pkg -R $root set-publisher --no-refresh --non-sticky opensolaris.org
95	do_cmd pkg -R $root set-publisher -e --no-refresh -P -O $uri $redistpub
96	[ $open -eq 0 ] && {
97		do_cmd pkg -R $root set-publisher -e \
98		    --no-refresh -O $extrauri $extrapub
99	}
100	do_cmd pkg -R $root refresh --full
101}
102
103#
104# If we're working from a repodir, disable the new publishers in the new
105# BE; they won't work without further configuration, in which case the
106# -r option should be used.
107#
108unconfigure_publishers()
109{
110	root=$1
111
112	if [ -n "$repodir" ]; then
113		do_cmd pkg -R $root set-publisher -P opensolaris.org
114		do_cmd pkg -R $root set-publisher -d $redistpub
115		[ $open -eq 0 ] && {
116			do_cmd pkg -R $root set-publisher -d $extrapub
117		}
118	fi
119}
120
121update()
122{
123	root=$1
124
125	pkg -R $root list entire > /dev/null 2>&1
126	[ $? -eq 0 ] && do_cmd pkg -R $root uninstall entire
127
128	configure_publishers $root
129
130	do_cmd pkg -R $root image-update
131
132	unconfigure_publishers $root
133}
134
135update_zone()
136{
137	zone=$1
138
139	name=`echo $zone | cut -d: -f 2`
140	if [ $name = "global" ]; then
141		return
142	fi
143
144	brand=`echo $zone | cut -d: -f 6`
145	if [ $brand != "ipkg" ]; then
146		return
147	fi
148
149	if [ "$zone_warned" = 0 ]; then
150		echo "WARNING: Use of onu(1) will prevent use of zone attach in the new BE" >&2
151		echo "See onu(1)" >&2
152		zone_warned=1
153	fi
154
155	state=`echo $zone | cut -d: -f 3`
156
157	case "$state" in
158	configured|incomplete)
159		return
160		;;
161	esac
162
163	zoneroot=`echo $zone | cut -d: -f 4`
164
165	echo "Updating zone $name"
166	update $zoneroot/root
167}
168
169sourcebe=""
170targetbe=""
171uri=""
172extrauri=""
173repodir=""
174verbose=0
175open=0
176redistpid=0
177extrapid=0
178redistport=13000
179extraport=13001
180no_zones=0
181zone_warned=0
182reposonly=0
183
184trap cleanup 1 2 3 15
185
186while getopts :d:E:e:Ors:t:U:u:vZ i ; do
187	case $i in
188	d)
189		repodir=$OPTARG
190		;;
191	E)
192		extrapub=$OPTARG
193		;;
194	e)
195		extrauri=$OPTARG
196		;;
197	O)
198		open=1
199		;;
200	r)
201		reposonly=1
202		;;
203	s)
204		sourcebe=$OPTARG
205		;;
206	t)
207		targetbe=$OPTARG
208		;;
209	U)
210		redistpub=$OPTARG
211		;;
212	u)
213		uri=$OPTARG
214		;;
215	v)
216		verbose=1
217		;;
218	Z)
219		no_zones=1
220		;;
221	*)
222		usage
223	esac
224done
225shift `expr $OPTIND - 1`
226
227[ -n "$1" ] && usage
228
229if [ "$reposonly" -eq 1 ]; then
230	[ -n "$sourcebe" ] && usage
231	[ -n "$targetbe" ] && usage
232	[ "$no_zones" -eq 1 ] && usage
233else
234	[ -z "$targetbe" ] && usage
235fi
236[ -z "$uri" ] && uri=$ONURI
237[ -z "$uri" ] && uri=$DEFAULTONURI
238[ -z "$redistpub" ] && redistpub=$ONPUB
239[ -z "$redistpub" ] && redistpub=$DEFAULTONPUB
240[ -z "$extrauri" ] && extrauri=$ONEXTRAURI
241[ -z "$extrauri" ] && extrauri=$DEFAULTONEXTRAURI
242[ -z "$extrapub" ] && extrapub=$ONEXTRAPUB
243[ -z "$extrapub" ] && extrapub=$DEFAULTONEXTRAPUB
244
245if [ -n "$repodir" ]; then
246	redistdir=$repodir/repo.redist
247	[ -d $redistdir ] || exit_error "$redistdir not found"
248	redistpub=$(python2.6 <<# EOF
249		import ConfigParser
250		p = ConfigParser.SafeConfigParser()
251		p.read("$redistdir/cfg_cache")
252		pp = p.get("publisher", "prefix")
253		print "%s" % pp
254		EOF)
255	[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $redistdir -p $redistport"
256	ARGS="--readonly --writable-root"
257	mkdir /tmp/redist.$$
258	/usr/lib/pkg.depotd -d $redistdir -p $redistport $ARGS /tmp/redist.$$ >/dev/null &
259	redistpid=$!
260	uri="http://localhost:$redistport/"
261	if [ $open -eq 0 ]; then
262		extradir=$repodir/repo.extra
263		[ -d $extradir ] || exit_error "$extradir not found"
264		extrapub=$(python2.6 <<# EOF
265			import ConfigParser
266			p = ConfigParser.SafeConfigParser()
267			p.read("$extradir/cfg_cache")
268			pp = p.get("publisher", "prefix")
269			print "%s" % pp
270			EOF)
271		[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $extradir -p $extraport"
272		mkdir /tmp/extra.$$
273		/usr/lib/pkg.depotd -d $extradir -p $extraport $ARGS /tmp/extra.$$ >/dev/null &
274		extrapid=$!
275		extrauri="http://localhost:$extraport/"
276	fi
277fi
278
279if [ "$reposonly" -eq 1 ]; then
280	configure_publishers /
281	if [ "$redistpid" -ne 0 ]; then
282		echo "$redistpub pkg.depotd running with pid $redistpid"
283	fi
284	if [ "$extrapid" -ne 0 ]; then
285		echo "$extrapub pkg.depotd running with pid $extrapid"
286	fi
287	exit 0
288fi
289
290createargs=""
291[ -n "$sourcebe" ] && createargs="-e $sourcebe"
292
293# ksh seems to have its own mktemp with slightly different semantics
294tmpdir=`/usr/bin/mktemp -d /tmp/onu.XXXXXX`
295[ -z "$tmpdir" ] && exit_error "mktemp failed"
296
297do_cmd beadm create $createargs $targetbe
298do_cmd beadm mount $targetbe $tmpdir
299update $tmpdir
300do_cmd beadm activate $targetbe
301
302if [ "$no_zones" != 1 ]; then
303	for zone in `do_cmd zoneadm -R $tmpdir list -cip`; do
304		update_zone $zone
305	done
306fi
307
308cleanup
309exit 0
310