svc-pkg-mirror revision 3079
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# CDDL HEADER START
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# The contents of this file are subject to the terms of the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Common Development and Distribution License (the "License").
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# You may not use this file except in compliance with the License.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# or http://www.opensolaris.org/os/licensing.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# See the License for the specific language governing permissions
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# and limitations under the License.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# When distributing Covered Code, include this CDDL HEADER in each
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# If applicable, add the following below this CDDL HEADER, with the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# fields enclosed by brackets "[]" replaced with your own identifying
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# information: Portions Copyright [yyyy] [name of copyright owner]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# CDDL HEADER END
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# This is the method script for the svc:/application/pkg/mirror service
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# When called using the 'start' or 'stop' SMF method script, it adds
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# or removes a crontab entry for the user running the service, pkg5srv
4e2d9fe30bf8b692972a9654c60d2d90ed355815Stephen Gallagher# When called using the 'refresh' method, it runs pkgrecv(1) to update a
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# pkg(5) repository using configuration stored in the SMF instance.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# The following SMF properties are used to configure the service:
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# config/repository the local pkg5 repository we update.
f6cd1236c27817b97db002094b76648d92b55f82Jan Zeleny# config/ref_image the reference pkg5 image that contains
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# origin information that we should update
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# config/publishers a comma-separated list of the publishers
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# from ref_image that we pkgrecv from.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# config/crontab_period the first five fields of a crontab(4)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# entry, with the 3rd field allowing the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# special value 'random'.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# config/debug a boolean, 'true' or 'false'; whether
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# to log more output when debugging.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Load SMF constants and functions
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Since we deal with '*' values in crontab fields, we never want
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Multiple instances of this service should not point at the
e82832a64fd456d1541ce0ea3902bcfb05e69642Stephen Gallagher# same local repository, since they could step on each other's toes
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# during updates, so we check for this before enabling the service.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# check_duplicate_repos
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ALL_REPOS=$($SVCPROP -p config/repository "$SVCNAME:*" \
a4cce2c98eedecb5d3b47da62104634cae268434Stephen Gallagher REPOS=$($SVCPROP -p config/repository "$SVCNAME:*" \
f6cd1236c27817b97db002094b76648d92b55f82Jan Zeleny # if the unique list of repositories is not the same as the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # list of repositories, then we have duplicates.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# In order that all instances don't hit the remote origins on the same
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# day, when configured with a 'config/crontab_period' containing a
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# special value 'random' in the 'day of the month' field of the crontab
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# schedule, we randomize the day, choosing a value from 1-28, storing
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# that to the service config instead. We then print the crontab period.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# add_date_jitter
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher schedule=$($SVCPROP -p config/crontab_period $SMF_FMRI \
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # Validate the cron_period property value, checking that we have
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # exactly 5 fields, and that 'random' only appears in the 3rd
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # field. We leave other validation up to cron(1).
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher print "config/crontab_period property must contain 5 " \
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher $1 == "random" || $2 == "random" || $4 == "random" || \
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher $5 == "random" {
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher print "only field 3 can have the value random";
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher check_failure $? "invalid value for config/crontab_period." \
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher new_schedule=$(echo "$schedule" | $SED -e "s/random/$RAND/1")
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ "$new_schedule" != "$schedule" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # Save the schedule in the instance. Note that this
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # will not appear in the running instance until the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher new_schedule=$(echo $new_schedule| $SED -e 's/ /\\ /g')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher config/crontab_period = astring: "$new_schedule"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Add a crontab entry that does periodic pkgrecvs from a series of
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# remote pkg5 origins to a local repository. This is run as part of the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# SMF start method for this service. If the repository doesn't exist,
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# we create it. We also attempt to create a zfs dataset if the parent
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# directory for the repository is the leaf of a zfs dataset.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher check_failure $? "Two or more instances of $SVCNAME contain the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghersame 'config/repository' value, which is not supported." $SMF_FMRI exit
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher typeset -f schedule=$(add_date_jitter | $SED -e 's/\\//g')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher typeset repo=$($SVCPROP -p config/repository $SMF_FMRI)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher set -A publishers $($SVCPROP -p config/publishers $SMF_FMRI)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ ! -f $repo/pkg5.repository ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # We set canmount=noauto so that multiple bootable
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher check_failure $? "unable to create repository" \
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher set_default_publisher "$repo" ${publishers[0]}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Remove the crontab entry that was added by 'schedule_updates'. This is
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# run as part of the SMF stop method for this service.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Checks whether the given repository has a publisher/prefix set,
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# and if not, sets it to the given publisher.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# set_default_publisher <path to repo> <publisher>
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher DEFAULT=$($PKGREPO -s "$repo" get -H publisher/prefix | \
typeset SMF_FMRI=$1
if [ -f $lockfile ]; then
override, or check the SMF property 'config/crontab_period' to ensure
echo $$ > $lockfile
IFS=,
return $SMF_EXIT_FATAL
index=0
pub=${publishers[$i]}
$PKG -R $ref_image publisher -F tsv > /tmp/pkg.mirror.$$
configured for publisher $pub"
if [ $? -eq 0 ]; then
done < /tmp/pkg.mirror.$$
$RM /tmp/pkg.mirror.$$
http_proxy=${http_proxies[$i]}
https_proxy=${https_proxies[$i]}
check_failure $? \
if [ $? -ne 0 ]; then
EXIT=$?
return $EXIT
while read value; do
echo $value
typeset repo=$1
typeset origin=$2
typeset SMF_FMRI=$5
typeset cachedir=$6
export http_proxy=$http_proxy
export https_proxy=$https_proxy
>> $LOG
EXIT=$?
return $EXIT
echo "Problem mirroring repository for $SMF_FMRI"
echo "Problem mirroring repository for $SMF_FMRI"
RET=$?
echo "Mirror refresh failed: see $LOG for more detail."
exit $result