Checkin of 0.2.98 upstream source
[time-slider.git] / lib / svc / method / time-slider-rsync
1 #!/bin/ksh
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 # Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24 # Use is subject to license terms.
25 # A rather simple SMF method script for
26 # svc:/application/time-slider:default
27 # It's sole purpose is to set the correct SMF property value of it's
28 # direct "auto-snapshot:<schedule>" dependencies: zfs/fs-name = '//'
29
30 . /lib/svc/share/smf_include.sh
31
32 RSYNC_PROG="/usr/lib/time-slider/plugins/rsync/rsync-backup"
33
34 # This function sets the appropriate svc configuration property for all
35 # dependent auto-snapshot:<schedule> instances if necessary
36 function update_service_props {
37         DEPENDENCIES=$(svcs -d  -H -o fmri $SMF_FMRI|grep auto-snapshot)
38         for dependency in ${DEPENDENCIES} ; do
39                 if [ "$(svcprop -p zfs/fs-name $dependency)" != "//" ] ; then
40                         svccfg -s $dependency setprop zfs/fs-name = astring: "//"
41                         svcadm refresh $dependency
42                 fi
43         done
44 }
45
46 # Given the exit status of a command, an integer, 0 if the command completed
47 # without errors. If the command exited with errors we degrade the
48 # state of this service into maintenance mode. If a 3rd argument is presented
49 # we don't degrade the service. We also log an error message as passed into
50 # this function.
51 #
52 function check_failure { # integer exit status, error message to display, be fatal
53
54         typeset RESULT=$1
55         typeset ERR_MSG=$2
56         typeset NON_FATAL=$3
57
58         if [ $RESULT -ne 0 ] ; then
59             print_log "Error: $ERR_MSG"
60             print_log "Moving service $SMF_FMRI to maintenance mode."
61             if [ -z "${NON_FATAL}" ] ; then
62                 print_log "Moving service $SMF_FMRI to maintenance mode."
63                 svcadm mark maintenance $SMF_FMRI
64             fi
65         fi
66
67 }
68
69 # A function we use to emit output. Right now, this goes to syslog via logger(1)
70 function print_log { # message to display
71         logger -t time-slider -p daemon.notice $*
72 }
73
74
75 function add_rsync_cronjob {
76         # Call every 30 minutes, 15 and 45 minutes after the hour.
77         SCHEDULE="15,45 * * * *"
78
79         # adding a cron job is essentially just looking for an existing entry,
80         # removing it, and appending a new one. Neato.
81         crontab -l | grep -v "${RSYNC_PROG} $SMF_FMRI" \
82             > /tmp/saved-crontab.$$
83
84         echo "${SCHEDULE} ${RSYNC_PROG} $SMF_FMRI" \
85             >> /tmp/saved-crontab.$$
86
87         crontab /tmp/saved-crontab.$$
88         check_failure $? "Unable to add cron job!"
89
90         rm /tmp/saved-crontab.$$
91         return 0
92 }
93
94 function remove_rsync_cronjob {
95
96         crontab -l | grep -v "${RSYNC_PROG} $SMF_FMRI$" \
97             > /tmp/saved-crontab.$$
98         crontab /tmp/saved-crontab.$$
99         check_failure $? "Unable to unschedule cron job for $SMF_FMRI"
100
101         rm /tmp/saved-crontab.$$
102
103         # finally, check our status before we return
104         STATE=$(svcprop -p restarter/state $SMF_FMRI)
105         if [ "${STATE}" == "maintenance" ] ; then
106             STATE=1
107         else
108             STATE=0
109         fi
110 }
111
112 case $SMF_METHOD in
113 "start")
114     #FIXME - This HAS to be == rsync-trigger program
115         PLUGIN_CMD="$(svcprop -p plugin/trigger_command $SMF_FMRI)"
116         if [ $? -ne 0 ] ; then
117                 echo "Unable to obtain plugin invocation command"
118                 exit $SMF_EXIT_ERR_CONFIG
119         fi
120
121         if [ ! -x $PLUGIN_CMD ] ; then
122             echo "\"$PLUGIN_CMD\" is not an executable path"
123             exit $SMF_EXIT_ERR_CONFIG
124         fi
125         add_rsync_cronjob
126         ;;
127 "stop")
128         remove_rsync_cronjob
129         ;;
130 *)
131         echo "Command line invocation of ${0} unsupported."
132         echo "This script is intended for smf(5) invocation only"
133         exit $SMF_EXIT_ERR_NOSMF
134         ;;
135 esac
136 exit $SMF_EXIT_OK