onu.sh revision 11838:32bb5d254240
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 2010 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28PATH=/usr/bin:/usr/sbin
29export PATH
30
31DEFAULTONURI="http://ipkg.sfbay/on-nightly"
32DEFAULTONPUB="on-nightly"
33DEFAULTONEXTRAURI="http://ipkg.sfbay/on-extra"
34DEFAULTONEXTRAPUB="on-extra"
35
36usage()
37{
38	echo "usage: $0 [-e URI [-E pub]] [-O] [-s beName] -t beName [-u URI [-U pub]] [-v]"
39	echo "usage: $0 [ -d dir ][ -O ][ -s beName ] -t beName [ -v ]"
40	echo "Use -s to specify the source BE to clone (default is the active BE)."
41	echo "Use -t to specify the target BE name."
42	echo "Use -u to specify the origin URI for the publisher packaging repository"
43	echo "\t(default is \$ONURI if set in the environment, otherwise"
44	echo "\t$DEFAULTONURI )."
45	echo "Use -U to specify the publisher prefix for the packaging repository"
46	echo "\t(default is \$ONPUB if set in the environment, otherwise"
47	echo "\t$DEFAULTONPUB )."
48	echo "Use -e to specify the origin URI for the extra repository (default is"
49	echo "\t\$ONEXTRAURI if set in the environment, otherwise"
50	echo "\t$DEFAULTONEXTRAURI )."
51	echo "Use -E to specify the publisher prefix for the extra repository (default"
52	echo "\tis \$ONEXTRAPUB if set in the environment, otherwise"
53	echo "\t$DEFAULTONEXTRAPUB )."
54	echo "Use -d to specify a directory containing redist and extra directories."
55	echo "\tNote that -d overrides -u, -U, -e and -E."
56	echo "Use -O for open mode, meaning that no extra repository will be used."
57	echo "Use -v for verbose mode."
58	exit 1
59}
60
61exit_error()
62{
63	echo $*
64	cleanup
65	exit 2
66}
67
68do_cmd()
69{
70	[ $verbose -gt 0 ] && echo $*
71	$*
72	exit_code=$?
73	[ $exit_code -eq 0 ] && return
74	# pkg(1) returns 4 if "nothing to do", which is safe to ignore
75	[ $1 = "pkg" -a $exit_code -eq 4 ] && return
76	exit_error "$*" failed: exit code $exit_code
77}
78
79sourcebe=""
80targetbe=""
81uri=""
82extrauri=""
83repodir=""
84verbose=0
85open=0
86redistpid=0
87extrapid=0
88redistport=13000
89extraport=13001
90
91cleanup()
92{
93	[ $redistpid -gt 0 ] && kill $redistpid
94	[ $extrapid -gt 0 ] && kill $extrapid
95	[ -d /tmp/redist.$$ ] && /bin/rm -rf /tmp/redist.$$
96	[ -d /tmp/extra.$$ ] && /bin/rm -rf /tmp/extra.$$
97}
98
99trap cleanup 1 2 3 15
100
101while getopts :d:E:e:Os:t:U:u:v i ; do
102	case $i in
103	d)
104		repodir=$OPTARG
105		;;
106	E)
107		extrapub=$OPTARG
108		;;
109	e)
110		extrauri=$OPTARG
111		;;
112	O)
113		open=1
114		;;
115	s)
116		sourcebe=$OPTARG
117		;;
118	t)
119		targetbe=$OPTARG
120		;;
121	U)
122		redistpub=$OPTARG
123		;;
124	u)
125		uri=$OPTARG
126		;;
127	v)
128		verbose=1
129		;;
130	*)
131		usage
132	esac
133done
134shift `expr $OPTIND - 1`
135
136[ -z "$targetbe" ] && usage
137[ -z "$uri" ] && uri=$ONURI
138[ -z "$uri" ] && uri=$DEFAULTONURI
139[ -z "$redistpub" ] && redistpub=$ONPUB
140[ -z "$redistpub" ] && redistpub=$DEFAULTONPUB
141[ -z "$extrauri" ] && extrauri=$ONEXTRAURI
142[ -z "$extrauri" ] && extrauri=$DEFAULTONEXTRAURI
143[ -z "$extrapub" ] && extrapub=$ONEXTRAPUB
144[ -z "$extrapub" ] && extrapub=$DEFAULTONEXTRAPUB
145
146if [ -n "$repodir" ]; then
147	redistdir=$repodir/repo.redist
148	[ -d $redistdir ] || exit_error "$redistdir not found"
149	redistpub=$(python2.6 <<# EOF
150		import ConfigParser
151		p = ConfigParser.SafeConfigParser()
152		p.read("$redistdir/cfg_cache")
153		pp = p.get("publisher", "prefix")
154		print "%s" % pp
155		EOF)
156	[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $redistdir -p $redistport"
157	ARGS="--readonly --writable-root"
158	mkdir /tmp/redist.$$
159	/usr/lib/pkg.depotd -d $redistdir -p $redistport $ARGS /tmp/redist.$$ >/dev/null &
160	redistpid=$!
161	uri="http://localhost:$redistport/"
162	if [ $open -eq 0 ]; then
163		extradir=$repodir/repo.extra
164		[ -d $extradir ] || exit_error "$extradir not found"
165		extrapub=$(python2.6 <<# EOF
166			import ConfigParser
167			p = ConfigParser.SafeConfigParser()
168			p.read("$extradir/cfg_cache")
169			pp = p.get("publisher", "prefix")
170			print "%s" % pp
171			EOF)
172		[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $extradir -p $extraport"
173		mkdir /tmp/extra.$$
174		/usr/lib/pkg.depotd -d $extradir -p $extraport $ARGS /tmp/extra.$$ >/dev/null &
175		extrapid=$!
176		extrauri="http://localhost:$extraport/"
177	fi
178fi
179
180createargs=""
181[ -n "$sourcebe" ] && createargs="-e $sourcebe"
182
183# ksh seems to have its own mktemp with slightly different semantics
184tmpdir=`/usr/bin/mktemp -d /tmp/onu.XXXXXX`
185[ -z "$tmpdir" ] && exit_error "mktemp failed"
186
187do_cmd beadm create $createargs $targetbe
188do_cmd beadm mount $targetbe $tmpdir
189do_cmd pkg -R $tmpdir set-publisher --non-sticky opensolaris.org
190pkg -R $tmpdir list entire > /dev/null 2>&1
191[ $? -eq 0 ] && do_cmd pkg -R $tmpdir uninstall entire
192do_cmd pkg -R $tmpdir set-publisher -P -O $uri $redistpub
193[ $open -eq 0 ] && do_cmd pkg -R $tmpdir set-publisher -O $extrauri $extrapub
194do_cmd pkg -R $tmpdir refresh --full
195do_cmd pkg -R $tmpdir image-update
196do_cmd beadm activate $targetbe
197
198cleanup
199
200exit 0
201