svc-pkg-mirror revision 3018
290N/A#!/usr/bin/ksh -p
290N/A#
290N/A# CDDL HEADER START
290N/A#
290N/A# The contents of this file are subject to the terms of the
290N/A# Common Development and Distribution License (the "License").
290N/A# You may not use this file except in compliance with the License.
290N/A#
290N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
290N/A# or http://www.opensolaris.org/os/licensing.
290N/A# See the License for the specific language governing permissions
290N/A# and limitations under the License.
290N/A#
290N/A# When distributing Covered Code, include this CDDL HEADER in each
290N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
290N/A# If applicable, add the following below this CDDL HEADER, with the
290N/A# fields enclosed by brackets "[]" replaced with your own identifying
290N/A# information: Portions Copyright [yyyy] [name of copyright owner]
290N/A#
290N/A# CDDL HEADER END
290N/A#
290N/A# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
290N/A#
290N/A
290N/A#
290N/A# This is the method script for the svc:/application/pkg/mirror service
290N/A#
290N/A# When called using the 'start' or 'stop' SMF method script, it adds
290N/A# or removes a crontab entry for the user running the service, pkg5srv
290N/A# by default.
290N/A#
290N/A
290N/A#
290N/A# When called using the 'refresh' method, it runs pkgrecv(1) to update a
290N/A# pkg(5) repository using configuration stored in the SMF instance.
290N/A#
290N/A# The following SMF properties are used to configure the service:
290N/A#
290N/A# config/repository the local pkg5 repository we update.
290N/A#
290N/A# config/ref_image the reference pkg5 image that contains
290N/A# origin information that we should update
290N/A# from.
290N/A#
290N/A# config/publishers a comma-separated list of the publishers
290N/A# from ref_image that we pkgrecv from.
290N/A#
290N/A# config/crontab_period the first five fields of a crontab(4)
290N/A# entry, with the 3rd field allowing the
290N/A# special value 'random'.
290N/A#
290N/A# config/debug a boolean, 'true' or 'false'; whether
290N/A# to log more output when debugging.
290N/A#
290N/A
290N/A# Load SMF constants and functions
290N/A. /lib/svc/share/smf_include.sh
290N/A. /lib/svc/share/fs_include.sh
290N/A. /lib/svc/share/pkg5_include.sh
290N/A
290N/AAWK=/usr/bin/awk
290N/ACAT=/usr/bin/cat
290N/ADATE=/usr/bin/date
290N/AGREP=/usr/bin/grep
290N/APKG=/usr/bin/pkg
290N/APKGRECV=/usr/bin/pkgrecv
290N/APKGREPO=/usr/bin/pkgrepo
290N/APYTHON=/usr/bin/python
290N/ARM=/usr/bin/rm
290N/ASED=/usr/bin/sed
290N/ASORT=/usr/bin/sort
290N/ASVCCFG=/usr/sbin/svccfg
290N/ASVCPROP=/usr/bin/svcprop
290N/AWC=/usr/bin/wc
290N/AZFS=/usr/sbin/zfs
290N/A
290N/ASVCNAME=svc:/application/pkg/mirror
290N/A
290N/A#
290N/A# Since we deal with '*' values in crontab fields, we never want
290N/A# globbing.
290N/A#
290N/Aset -o noglob
290N/A
290N/A#
290N/A# Multiple instances of this service should not point at the
290N/A# same local repository, since they could step on each other's toes
290N/A# during updates, so we check for this before enabling the service.
290N/A#
290N/A# Usage:
290N/A# check_duplicate_repos
290N/A#
290N/Afunction check_duplicate_repos {
290N/A
290N/A ALL_REPOS=$($SVCPROP -p config/repository "$SVCNAME:*" \
290N/A | $AWK '{print $NF}' | $SORT | $WC -l)
290N/A REPOS=$($SVCPROP -p config/repository "$SVCNAME:*" \
290N/A | $AWK '{print $NF}' | $SORT -u | $WC -l)
290N/A #
290N/A # if the unique list of repositories is not the same as the
290N/A # list of repositories, then we have duplicates.
290N/A #
290N/A if [ "$ALL_REPOS" != "$REPOS" ]; then
290N/A return 1
290N/A fi
290N/A return 0
290N/A}
290N/A
290N/A#
290N/A# In order that all instances don't hit the remote origins on the same
290N/A# day, when configured with a 'config/crontab_period' containing a
290N/A# special value 'random' in the 'day of the month' field of the crontab
290N/A# schedule, we randomize the day, choosing a value from 1-28, storing
290N/A# that to the service config instead. We then print the crontab period.
290N/A#
290N/A# Usage:
290N/A# add_date_jitter
290N/A#
290N/Afunction add_date_jitter {
290N/A
290N/A schedule=$($SVCPROP -p config/crontab_period $SMF_FMRI \
290N/A | $SED -e 's/\\//g')
290N/A
290N/A #
290N/A # Validate the cron_period property value, checking that we have
290N/A # exactly 5 fields, and that 'random' only appears in the 3rd
290N/A # field. We leave other validation up to cron(1).
290N/A #
290N/A echo "$schedule" | $AWK '
290N/A NF != 5 {
290N/A print "config/crontab_period property must contain 5 " \
290N/A "values.";
290N/A exit 1
290N/A }
290N/A $1 == "random" || $2 == "random" || $4 == "random" || \
290N/A $5 == "random" {
290N/A print "only field 3 can have the value random";
290N/A exit 1
290N/A }'
290N/A
290N/A check_failure $? "invalid value for config/crontab_period." \
290N/A $SMF_FMRI exit
290N/A
290N/A RAND=$(( ($RANDOM % 27) + 1 ))
290N/A new_schedule=$(echo "$schedule" | $SED -e "s/random/$RAND/1")
290N/A if [ "$new_schedule" != "$schedule" ]; then
290N/A #
290N/A # Save the schedule in the instance. Note that this
290N/A # will not appear in the running instance until the
290N/A # refresh method has fired.
290N/A #
290N/A new_schedule=$(echo $new_schedule| $SED -e 's/ /\\ /g')
290N/A $SVCCFG -s $SMF_FMRI setprop \
290N/A config/crontab_period = astring: "$new_schedule"
290N/A fi
290N/A print $new_schedule
290N/A}
290N/A
290N/A#
290N/A# Add a crontab entry that does periodic pkgrecvs from a series of
290N/A# remote pkg5 origins to a local repository. This is run as part of the
290N/A# SMF start method for this service. If the repository doesn't exist,
290N/A# we create it. We also attempt to create a zfs dataset if the parent
290N/A# directory for the repository is the leaf of a zfs dataset.
290N/A#
290N/Afunction smf_schedule_updates {
290N/A
290N/A check_duplicate_repos
290N/A check_failure $? "Two or more instances of $SVCNAME contain the
290N/Asame 'config/repository' value, which is not supported." $SMF_FMRI exit
290N/A typeset -f schedule=$(add_date_jitter | $SED -e 's/\\//g')
290N/A typeset repo=$($SVCPROP -p config/repository $SMF_FMRI)
290N/A
290N/A SAVED_IFS="$IFS"
290N/A IFS=,
290N/A set -A publishers $($SVCPROP -p config/publishers $SMF_FMRI)
290N/A IFS="$SAVED_IFS"
290N/A
290N/A if [ ! -f $repo/pkg5.repository ]; then
290N/A repo_parent=$(dirname $repo)
290N/A repo_base=$(basename $repo)
290N/A readmnttab "$repo_parent" < /etc/mnttab
290N/A if [ "$fstype" = "zfs" ]; then
290N/A #
290N/A # $special gets set by readmnttab in
290N/A # /lib/svc/share/fs_include.sh
290N/A #
290N/A DS="$special/$repo_base"
290N/A
290N/A #
290N/A # We set canmount=noauto so that multiple bootable
290N/A # rpools can coexist on the same system.
290N/A #
290N/A $ZFS create -o canmount=noauto "$DS"
290N/A check_failure $? \
290N/A "unable to create zfs dataset $DS" \
290N/A $SMF_FMRI degrade
290N/A $ZFS mount "$DS"
290N/A check_failure $? \
290N/A "unable to mount zfs dataset $DS" \
290N/A $SMF_FMRI degrade
290N/A fi
290N/A $PKGREPO create "$repo"
290N/A check_failure $? "unable to create repository" \
290N/A $SMF_FMRI degrade
290N/A fi
290N/A set_default_publisher "$repo" ${publishers[0]}
290N/A add_cronjob $SMF_FMRI "$schedule" \
290N/A "/usr/sbin/svcadm refresh $SMF_FMRI"
290N/A}
290N/A
290N/A#
290N/A# Remove the crontab entry that was added by 'schedule_updates'. This is
290N/A# run as part of the SMF stop method for this service.
290N/A#
290N/Afunction smf_unschedule_updates {
290N/A
290N/A remove_cronjob $SMF_FMRI \
290N/A "/usr/sbin/svcadm refresh $SMF_FMRI"
290N/A}
290N/A
290N/A#
290N/A# Checks whether the given repository has a publisher/prefix set,
290N/A# and if not, sets it to the given publisher.
290N/A#
290N/A# Usage:
290N/A# set_default_publisher <path to repo> <publisher>
290N/A#
290N/Afunction set_default_publisher {
290N/A typeset repo="$1"
290N/A typeset pub=$2
290N/A
290N/A DEFAULT=$($PKGREPO -s "$repo" get -H publisher/prefix | \
290N/A $AWK '{print $NF}')
290N/A if [ "$DEFAULT" = '""' ]; then
290N/A $PKGREPO -s "$repo" set publisher/prefix=$pub
290N/A fi
290N/A}
290N/A
290N/A#
290N/A# Intended to be called as part of a cron job firing, this calls
290N/A# 'pkgrecv_from_origin' for each publisher configured in the SMF
290N/A# instance.
290N/A#
290N/A# Usage:
290N/A# update_repository <smf fmri>
290N/A#
290N/Afunction update_repository {
290N/A
290N/A typeset SMF_FMRI=$1
290N/A typeset instance=$(echo $SMF_FMRI | $AWK -F: '{print $NF}')
290N/A typeset lockfile=/var/log/pkg/mirror/mirror.$instance.lock
290N/A
290N/A if [ -f $lockfile ]; then
290N/A pid=$(<$lockfile)
290N/A check_failure 1 "A mirror operation was already running
290N/A under process $pid when the cron job fired. Remove $lockfile to
290N/A override, or check the SMF property 'config/crontab_period' to ensure
290N/A cron schedules don't overlap." $SMF_FMRI degrade
290N/A return 1
290N/A fi
290N/A # write our pid into the lock file
290N/A echo $$ > $lockfile
290N/A check_failure $? "unable to create lockfile" $SMF_FMRI degrade
290N/A
290N/A typeset repo=$($SVCPROP -p config/repository $SMF_FMRI \
290N/A | $SED -e 's/\\//g')
290N/A typeset cachedir=$($SVCPROP -p config/cache_dir $SMF_FMRI \
290N/A | $SED -e 's/\\//g')
290N/A typeset ref_image=$($SVCPROP -p config/ref_image $SMF_FMRI\
290N/A | $SED -e 's/\\//g')
290N/A
290N/A SAVED_IFS="$IFS"
290N/A IFS=,
290N/A set -A publishers $($SVCPROP -p config/publishers $SMF_FMRI)
290N/A IFS="$SAVED_IFS"
290N/A if [ -z "$publishers" ]; then
290N/A echo "ERROR: no publishers found in 'config/publishers'"
290N/A return $SMF_EXIT_FATAL
290N/A fi
290N/A
290N/A set -A origins ""
290N/A set -A ssl_keys ""
290N/A set -A ssl_certs ""
290N/A set -A http_proxies ""
290N/A set -A https_proxies ""
290N/A
290N/A #
290N/A # Gather the details we need to connect to the origins
290N/A # we want to pkgrecv from.
290N/A #
290N/A i=0
290N/A index=0
290N/A while [ $i -lt ${#publishers[@]} ]; do
290N/A pub=${publishers[$i]}
290N/A sslkey=$($PKG -R $ref_image publisher $pub \
290N/A | $GREP 'SSL Key:' \
290N/A | $GREP -v None | $SED -e 's/.* //g')
sslcert=$($PKG -R $ref_image publisher $pub \
| $GREP 'SSL Cert:' \
| $GREP -v None | $SED -e 's/.* //g')
$PKG -R $ref_image publisher -F tsv > /tmp/pkg.mirror.$$
#
# this function depends on the output of
# 'pkg publisher -F tsv'. It really ought to use
# 'pkg publisher -o' option when that's available.
#
while read publisher sticky syspub enabled ptype status \
uri proxy ; do
if [ "$pub" != "$publisher" ]; then
continue
fi
if [ -z "$uri" ]; then
echo "WARNING: no URI \
configured for publisher $pub"
continue
fi
origins[$index]=$uri
echo $uri | $GREP -q https://
if [ $? -eq 0 ]; then
ssl_keys[$index]=$sslkey
ssl_certs[$index]=$sslcert
else
ssl_keys[$index]=''
ssl_certs[$index]=''
fi
if [ "$proxy" = "-" ]; then
proxy=''
fi
https_proxies[$index]=$proxy
http_proxy[$index]=$proxy
index=$(( $index + 1 ))
done < /tmp/pkg.mirror.$$
$RM /tmp/pkg.mirror.$$
i=$(( $i + 1 ))
done
# Iterate over all configured origins
i=0
while [ $i -lt ${#origins[@]} ]; do
origin=${origins[$i]}
key=${ssl_keys[$i]}
cert=${ssl_certs[$i]}
http_proxy=${http_proxies[$i]}
https_proxy=${https_proxies[$i]}
pkgrecv_from_origin "$repo" "$origin" "$key" \
"$cert" $SMF_FMRI "$cachedir" "$http_proxy" \
"$https_proxy"
check_failure $? \
"unable to update repository $repo" $SMF_FMRI \
degrade
if [ $? -ne 0 ]; then
$RM $lockfile
return 1
fi
i=$(( $i + 1 ))
done
EXIT=$?
$RM $lockfile
return $EXIT
}
#
# When retrieving values from SMF, we can get the string '""'
# (two quotes) returned. For our purposes, this is equivalent to the
# null string, so we normalize it to ''. This function reads from stdin.
#
function reduce_null_str {
while read value; do
if [ "$value" = '""' ]; then
echo ''
else
echo $value
fi
done
}
#
# Perform a pkgrecv from the given origin to the given repository.
# We assume that the repository exists.
#
# Usage:
# pkgrecv_from_origin <repo> <origin> <key path> <cert path> <FMRI>
# <cache dir> <http_proxy> <https_proxy>
#
function pkgrecv_from_origin {
typeset repo=$1
typeset origin=$2
typeset key=$(echo $3 | reduce_null_str)
typeset cert=$(echo $4 | reduce_null_str)
typeset SMF_FMRI=$5
typeset cachedir=$6
typeset http_proxy=$(echo $7 | reduce_null_str)
typeset https_proxy=$(echo $8 | reduce_null_str)
typeset instance=$(echo $SMF_FMRI | $AWK -F: '{print $NF}')
typeset debug_flag=$($SVCPROP -p config/debug $SMF_FMRI)
typeset LOG=/var/log/pkg/mirror/mirror.$instance.log
export http_proxy=$http_proxy
export https_proxy=$https_proxy
TSTAMP=$($DATE +%Y%m%dT%H%M%SZ)
echo "$TSTAMP: $SMF_FMRI updates to $repo from $origin :" \
>> $LOG
if [ -n "$key" ] && [ -n "$cert" ]; then
key="--key $key"
cert="--cert $cert"
fi
# show the command we're running
if [ "$debug_flag" = "true" ] ; then
echo $PKGRECV -s $origin -c "$cachedir"/$instance \
-d "$repo" -m all-timestamps $key $cert '*'
fi
$PKGRECV -s $origin -c "$cachedir"/$instance -d "$repo" \
-m all-timestamps $key $cert '*' > $LOG.tmp 2>&1
EXIT=$?
if [ "$debug_flag" = "true" ]; then
$CAT $LOG.tmp >> $LOG
elif [ $EXIT -ne 0 ]; then
#
# in the case of errors, getting the full pkgrecv output
# can be helpful.
#
$CAT $LOG.tmp >> $LOG
else
# otherwise, we only log messages containing pkg5 FMRIs
$GREP 'pkg:/' $LOG.tmp >> $LOG
# we only destroy the cache if a pkgrecv was successful
$RM -rf "$cachedir"/$instance
fi
$PKGREPO -s "$repo" refresh
$RM $LOG.tmp
return $EXIT
}
# $1 start | stop | an FMRI containing configuration
case "$1" in
'start')
smf_schedule_updates
if [ $? -eq 0 ]; then
result=$SMF_EXIT_OK
else
echo "Problem mirroring repository for $SMF_FMRI"
result=$SMF_EXIT_ERR_FATAL
fi
;;
'stop')
smf_unschedule_updates
if [ $? -eq 0 ]; then
result=$SMF_EXIT_OK
else
echo "Problem mirroring repository for $SMF_FMRI"
result=$SMF_EXIT_ERR_FATAL
fi
;;
#
# A note on logging.
#
# The following log files are created while this service is running:
#
# /var/log/pkg/mirror/mirror.<instance>.log
# This is the top-level log file for the service. This log
# shows a summary of each pkgrecv, listing a timestamp and the
# packages that were received during that run of the cron job.
#
# /var/log/pkg/mirror/mirror.<instance>.run.<pid>
# This is a temporary log file, which should contain very little
# output - it exists to capture all other output from the service
# If 'config/debug' is set, then this file will also include the
# full pkgrecv(1) command that is executed.
#
# /var/log/pkg/mirror/mirror.<instance>.log.tmp
# Another temporary log file, which captures the complete output
# of each pkgrecv command as it runs. At the end of the pkgrecv
# process, we extract a summary and append it to
# mirror.<instance>.log. If 'config/debug' is set, the contents
# of this log are appended to mirror.<instance>.log. If any errors
# were encountered while running pkgrecv, the contents of this log
# are appended to mirror.<instance>.log.
#
'refresh')
typeset instance=$(echo $SMF_FMRI | $AWK -F: '{print $NF}')
typeset LOG=/var/log/pkg/mirror/mirror.$instance.log
typeset debug_flag=$($SVCPROP -p config/debug $SMF_FMRI)
# Most output should get captured by update_repository, but we
# capture any remaining output.
update_repository $SMF_FMRI > $LOG.run.$$ 2>&1
RET=$?
if [ -s $LOG.run.$$ ]; then
cat $LOG.run.$$ >> $LOG
fi
if [ "$debug_flag" = "false" ]; then
$RM $LOG.run.$$
fi
if [ $RET -eq 0 ]; then
result=$SMF_EXIT_OK
else
echo "Mirror refresh failed: see $LOG for more detail."
# try to remove the cron job so we don't keep failing
smf_unschedule_updates
result=$SMF_EXIT_ERR_FATAL
fi
;;
esac
exit $result