1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
# This is the method script for the svc:/application/pkg/mirror service
#
# When called using the 'start' or 'stop' SMF method script, it adds
# or removes a crontab entry for the user running the service, pkg5srv
# by default.
#
#
# When called using the 'refresh' method, it runs pkgrecv(1) to update a
# pkg(7) repository using configuration stored in the SMF instance.
#
# The following SMF properties are used to configure the service:
#
# config/repository the local pkg5 repository we update.
#
# origin information that we should update
# from.
#
# config/publishers a comma-separated list of the publishers
# from ref_image that we pkgrecv from.
#
# config/crontab_period the first five fields of a crontab(4)
# entry, with the 3rd field allowing the
# special value 'random'.
#
# to log more output when debugging.
#
# Load SMF constants and functions
#
# Since we deal with '*' values in crontab fields, we never want
# globbing.
#
set -o noglob
#
# Multiple instances of this service should not point at the
# same local repository, since they could step on each other's toes
# during updates, so we check for this before enabling the service.
#
# Usage:
# check_duplicate_repos
#
#
# if the unique list of repositories is not the same as the
# list of repositories, then we have duplicates.
#
return 1
fi
return 0
}
#
# In order that all instances don't hit the remote origins on the same
# day, when configured with a 'config/crontab_period' containing a
# special value 'random' in the 'day of the month' field of the crontab
# schedule, we randomize the day, choosing a value from 1-28, storing
# that to the service config instead. We then print the crontab period.
#
# Usage:
# add_date_jitter
#
#
# Validate the cron_period property value, checking that we have
# exactly 5 fields, and that 'random' only appears in the 3rd
# field. We leave other validation up to cron(1).
#
NF != 5 {
print "config/crontab_period property must contain 5 " \
"values.";
exit 1
}
$1 == "random" || $2 == "random" || $4 == "random" || \
$5 == "random" {
print "only field 3 can have the value random";
exit 1
}'
$SMF_FMRI exit
#
# Save the schedule in the instance. Note that this
# will not appear in the running instance until the
# refresh method has fired.
#
fi
print $new_schedule
}
#
# Add a crontab entry that does periodic pkgrecvs from a series of
# remote pkg5 origins to a local repository. This is run as part of the
# SMF start method for this service. If the repository doesn't exist,
# we create it. We also attempt to create a zfs dataset if the parent
# directory for the repository is the leaf of a zfs dataset.
#
check_failure $? "Two or more instances of $SVCNAME contain the
same 'config/repository' value, which is not supported." $SMF_FMRI exit
typeset repo=$($SVCPROP -p config/repository $SMF_FMRI)
IFS=,
set -A publishers $($SVCPROP -p config/publishers $SMF_FMRI)
if [ ! -f $repo/pkg5.repository ]; then
#
# $special gets set by readmnttab in
# /lib/svc/share/fs_include.sh
#
#
# We set canmount=noauto so that multiple bootable
# rpools can coexist on the same system.
#
check_failure $? \
check_failure $? \
fi
fi
}
#
# Remove the crontab entry that was added by 'schedule_updates'. This is
# run as part of the SMF stop method for this service.
#
}
#
# and if not, sets it to the given publisher.
#
# Usage:
# set_default_publisher <path to repo> <publisher>
#
typeset repo="$1"
typeset pub=$2
fi
}
#
# Intended to be called as part of a cron job firing, this calls
# 'pkgrecv_from_origin' for each publisher configured in the SMF
# instance.
#
# Usage:
# update_repository <smf fmri>
#
typeset SMF_FMRI=$1
if [ -f $lockfile ]; then
check_failure 1 "A mirror operation was already running
override, or check the SMF property 'config/crontab_period' to ensure
return 1
fi
# write our pid into the lock file
echo $$ > $lockfile
typeset repo=$($SVCPROP -p config/repository $SMF_FMRI \
IFS=,
set -A publishers $($SVCPROP -p config/publishers $SMF_FMRI)
echo "ERROR: no publishers found in 'config/publishers'"
return $SMF_EXIT_FATAL
fi
set -A origins ""
set -A ssl_keys ""
set -A ssl_certs ""
set -A http_proxies ""
set -A https_proxies ""
set -A clones ""
set -A pubs ""
#
# Gather the details we need to connect to the origins
# we want to pkgrecv from.
#
i=0
index=0
while [ $i -lt ${#publishers[@]} ]; do
pub=${publishers[$i]}
sslkey=$($PKG -R $ref_image publisher $pub \
sslcert=$($PKG -R $ref_image publisher $pub \
$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.
#
continue
fi
echo "WARNING: no URI \
configured for publisher $pub"
continue
fi
if [ $? -eq 0 ]; then
else
fi
proxy=''
fi
done < /tmp/pkg.mirror.$$
$RM /tmp/pkg.mirror.$$
i=$(( $i + 1 ))
# If only one origin for this publisher
clones[$first_index]="true"
fi
done
# Iterate over all configured origins
i=0
while [ $i -lt ${#origins[@]} ]; do
http_proxy=${http_proxies[$i]}
https_proxy=${https_proxies[$i]}
"$https_proxy" "$clone" "$pub"
check_failure $? \
if [ $? -ne 0 ]; then
return 1
fi
i=$(( $i + 1 ))
done
EXIT=$?
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.
#
while read value; do
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>
#
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 clone=$9
typeset publisher=${10}
typeset debug_flag=$($SVCPROP -p config/debug $SMF_FMRI)
export http_proxy=$http_proxy
export https_proxy=$https_proxy
>> $LOG
fi
set -f
else
fi
# show the command we're running
echo $cmd
fi
set +f
EXIT=$?
#
# in the case of errors, getting the full pkgrecv output
# can be helpful.
#
else
# otherwise, we only log messages containing pkg5 FMRIs
# we only destroy the cache if a pkgrecv was successful
fi
return $EXIT
}
# $1 start | stop | an FMRI containing configuration
case "$1" in
'start')
if [ $? -eq 0 ]; then
else
echo "Problem mirroring repository for $SMF_FMRI"
fi
;;
'stop')
if [ $? -eq 0 ]; then
else
echo "Problem mirroring repository for $SMF_FMRI"
fi
;;
#
# A note on logging.
#
# The following log files are created while this service is running:
#
# 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.
#
# This is a temporary log file, which should contain very little
# output - it exists to capture all other output from the service
# full pkgrecv(1) command that is executed.
#
# 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
# 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 debug_flag=$($SVCPROP -p config/debug $SMF_FMRI)
# Most output should get captured by update_repository, but we
# capture any remaining output.
RET=$?
fi
if [ "$debug_flag" = "false" ]; then
fi
else
echo "Mirror refresh failed: see $LOG for more detail."
# try to remove the cron job so we don't keep failing
fi
;;
esac
exit $result