svc-pkg-mirror revision 2867
2867N/A# The contents of this file are subject to the terms of the 2867N/A# Common Development and Distribution License (the "License"). 2867N/A# You may not use this file except in compliance with the License. 2867N/A# See the License for the specific language governing permissions 2867N/A# and limitations under the License. 2867N/A# When distributing Covered Code, include this CDDL HEADER in each 2867N/A# If applicable, add the following below this CDDL HEADER, with the 2867N/A# fields enclosed by brackets "[]" replaced with your own identifying 2867N/A# information: Portions Copyright [yyyy] [name of copyright owner] 2867N/A# Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 2867N/A# When called using the 'start' or 'stop' SMF method script, it adds 2867N/A# or removes a crontab entry for the user running the service, pkg5srv 2867N/A# When called using the 'refresh' method, it runs pkgrecv(1) to update a 2867N/A# pkg(5) repository using configuration stored in the SMF instance. 2867N/A# The following SMF properties are used to configure the service: 2867N/A# origin information that we should update 2867N/A# from ref_image that we pkgrecv from. 2867N/A# entry, with the 3rd field allowing the 2867N/A# to log more output when debugging. 2867N/A# Load SMF constants and functions 2867N/A# Since we deal with '*' values in crontab fields, we never want 2867N/A# Multiple instances of this service should not point at the 2867N/A# same local repository, since they could step on each other's toes 2867N/A# during updates, so we check for this before enabling the service. 2867N/A # if the unique list of repositories is not the same as the 2867N/A # list of repositories, then we have duplicates. 2867N/A# In order that all instances don't hit the remote origins on the same 2867N/A# special value 'random' in the 'day of the month' field of the crontab 2867N/A# schedule, we randomize the day, choosing a value from 1-28, storing 2867N/A# that to the service config instead. We then print the crontab period. 2867N/A # Validate the cron_period property value, checking that we have 2867N/A # exactly 5 fields, and that 'random' only appears in the 3rd 2867N/A # field. We leave other validation up to cron(1). 2867N/A $1 == "random" || $2 == "random" || $4 == "random" || \ 2867N/A print "only field 3 can have the value random"; 2867N/A # Save the schedule in the instance. Note that this 2867N/A # will not appear in the running instance until the 2867N/A # refresh method has fired. 2867N/A# Add a crontab entry that does periodic pkgrecvs from a series of 2867N/A# remote pkg5 origins to a local repository. This is run as part of the 2867N/A# SMF start method for this service. If the repository doesn't exist, 2867N/A# we create it. We also attempt to create a zfs dataset if the parent 2867N/A# directory for the repository is the leaf of a zfs dataset. 2867N/A # $special gets set by readmnttab in 2867N/A# Remove the crontab entry that was added by 'schedule_updates'. This is 2867N/A# run as part of the SMF stop method for this service. 2867N/A# Intended to be called as part of a cron job firing, this calls 2867N/A# 'pkgrecv_from_origin' for each publisher configured in the SMF 2867N/A# update_repository <smf fmri> 2867N/A # write our pid into the lock file 2867N/A # Gather the details we need to connect to the origins 2867N/A while [ $i -lt ${#publishers[@]} ]; do 2867N/A # this function depends on the output of 2867N/A # 'pkg publisher -F tsv'. It really ought to use 2867N/A # 'pkg publisher -o' option when that's available. 2867N/A # Iterate over all configured origins 2867N/A# When retrieving values from SMF, we can get the string '""' 2867N/A# (two quotes) returned. For our purposes, this is equivalent to the 2867N/A# null string, so we normalize it to ''. This function reads from stdin. 2867N/A# Perform a pkgrecv from the given origin to the given repository. 2867N/A# We assume that the repository exists. 2867N/A# pkgrecv_from_origin <repo> <origin> <key path> <cert path> <FMRI> 2867N/A# <cache dir> <http_proxy> <https_proxy> 2867N/A # show the command we're running 2867N/A # in the case of errors, getting the full pkgrecv output 2867N/A # otherwise, we only log messages containing pkg5 FMRIs 2867N/A# $1 start | stop | an FMRI containing configuration 2867N/A# The following log files are created while this service is running: 2867N/A# This is the top-level log file for the service. This log 2867N/A# shows a summary of each pkgrecv, listing a timestamp and the 2867N/A# packages that were received during that run of the cron job. 2867N/A# This is a temporary log file, which should contain very little 2867N/A# output - it exists to capture all other output from the service 2867N/A# full pkgrecv(1) command that is executed. 2867N/A# Another temporary log file, which captures the complete output 2867N/A# of each pkgrecv command as it runs. At the end of the pkgrecv 2867N/A# process, we extract a summary and append it to 2867N/A# of this log are appended to mirror.<instance>.log. If any errors 2867N/A# were encountered while running pkgrecv, the contents of this log 2867N/A# are appended to mirror.<instance>.log. 2867N/A # Most output should get captured by update_repository, but we 2867N/A # capture any remaining output. 2867N/A echo "Mirror refresh failed: see $LOG for more detail." 2867N/A # try to remove the cron job so we don't keep failing