Imported Upstream version 3.3.2
[debian/amanda] / packaging / common / common_functions.sh
1 #!/bin/sh
2 # Common Functions
3
4 # Required variables:
5 # LOGFILE
6 # SYSCONFDIR
7 # os
8
9 logger() {
10         # A non-annoying way to log stuff
11         # ${@} is all the parameters, also known as the message.  Quoting the input
12         # preserves whitespace.
13         msg="`date +'%b %e %Y %T'`: ${@}"
14         echo "${msg}" >> ${LOGFILE}
15 }
16
17 log_output_of() {
18         # A non-annoying way to log output of commands
19         # ${@} is all the parameters supplied to the function.  just execute it,
20         # and capture the output in a variable.  then log that.
21         output=`"${@}" 2>&1`
22         ret=$?
23         if [ -n "${output}" ] ; then
24                 logger "${1}: ${output}"
25         fi
26         return ${ret}
27 }
28
29 check_superserver() {
30     # Check for the superserver $1 for the config $2
31     case $1 in
32         xinetd) check_xinetd $2; return $?;;
33         inetd) check_inetd $2; return $?;;
34         launchd) check_launchd $2; return $?;;
35         smf) check_smf $2; return $?;;
36     esac
37 }
38
39 check_xinetd() {
40         # Checks for an xinetd install and a config name passed as the first
41         # parameter.
42         # Returns:
43         #        0 if the file exists,
44         #        1 if it does not,
45         #        2 if xinetd.d/ does not exist or is a file
46
47         if [ -d ${SYSCONFDIR}/xinetd.d ] ; then
48                 if [ -f ${SYSCONFDIR}/xinetd.d/${1} ] ; then
49                         logger "Found existing xinetd config: ${1}"
50                         return 0
51                 else
52                         return 1
53                 fi
54         else
55                 # No xinetd installation.
56                 return 2
57         fi
58 }
59
60 check_inetd() {
61     case $os in
62         SunOS) inetd_conf=${SYSCONFDIR}/inet/inetd.conf ;;
63         *) inetd_conf=${SYSCONFDIR}/inetd.conf ;;
64     esac
65     if [ -e ${inetd_conf} ] ; then
66         if grep "${1}" ${inetd_conf} > /dev/null ; then
67             logger "Found existing inetd config: ${1}"
68             return 0
69         else
70             return 1
71         fi
72     else
73         # No inetd installation.
74             return 2
75     fi
76 }
77
78 check_launchd() {
79     # TODO: refactor OS X scripts.
80     :
81 }
82
83 check_smf() {
84     # Only for solaris! This check only notices if an amanda service is active,
85     # it does not notice server vs client entries.
86     log_output_of svcs -H "*amanda*" || { \
87         logger "No amanda service found."; return 1; }
88 }
89
90 check_superserver_running() {
91     # Check for the given superserver, $1, in the output of ps -ef, or on
92     # mac/bsd ps ax.
93     # Return codes:
94     #  0: $1 is running
95     #  1: $1 is not running
96     #  2: $1 is not valid for this system
97     case $1 in
98         # Linux or Solaris.  This works despite sol10 using SMF.
99         inetd) ps_flags='-e';;
100         xinetd) ps_flags='-e';;
101         # Mac OS X
102         launchd) ps_flags='aux';;
103         *) echo "Bad superserver."; return 2 ;;
104     esac
105     if [ "$1" = "launchd" ] && [ `uname` != 'Darwin' ]; then
106         echo "Only darwin uses launchd"
107         return 2
108     fi
109     if [ "$1" = "xinetd" ] && [ "$os" = 'SunOS' ] && \
110        [ `uname -r` = "5.10" ]; then
111         echo "Solaris 10 does not use xinetd."
112         return 2
113     fi
114     # Search for $1, 
115     PROC=`ps ${ps_flags} | grep -v 'grep'| grep "${1}"`
116     if [ x"${PROC}" != x ]; then
117         return 0
118     else
119         return 1
120     fi
121 }
122
123 backup_xinetd() {
124     log_output_of mv ${SYSCONFDIR}/xinetd.d/${1} ${AMANDAHOMEDIR}/example/xinetd.${1}.orig || \
125         { logger "WARNING:  Could not back up existing xinetd configuration '${1}'";
126         return 1; }
127     logger "Old xinetd config for '${1}' backed up to '${AMANDAHOMEDIR}/example/xinetd.${1}.orig'"
128 }
129
130 backup_inetd() {
131     case $os in
132         SunOS) inetd_conf=${SYSCONFDIR}/inet/inetd.conf ;;
133         *) inetd_conf=${SYSCONFDIR}/inetd.conf ;;
134     esac
135     # Backs up any amanda configuration it finds
136     log_output_of sed -n "/^amanda .* amandad/w ${AMANDAHOMEDIR}/example/inetd.orig" ${inetd_conf} || \
137         { logger "WARNING: could not write ${AMANDAHOMEDIR}/example/inetd.orig";
138         return 1; }
139     log_output_of sed "/^amanda .* amandad/d" ${inetd_conf} > \
140         ${inetd_conf}.tmp || \
141         { logger "WARNING: could not write ${inetd_conf}.tmp";
142         return 1; }
143     log_output_of mv ${inetd_conf}.tmp ${inetd_conf} || \
144         { logger "WARNING: could not overwrite ${inetd_conf}, old config not removed.";
145         return 1; }
146     logger "Old inetd config for amanda backed up to ${AMANDAHOMEDIR}/example/inetd.orig"
147 }
148
149 backup_smf() {
150     # Solaris only. I *think* this should be consistent across all smf installs
151     svccfg -s *amanda* > ${AMANDAHOMEDIR}/example/amanda_smf.xml.orig || {\
152         logger "Warning: export of existing amanda service failed.";
153         return 1; }
154
155     log_output_of inetadm -d *amanda* || { \
156         # Not critical, we don't need to return if this fails.
157         logger "Warning: disabling existing amanda service failed."; }
158
159     log_output_of svccfg delete -f *amanda* || { \
160         logger "Error: removing the old amanda service failed.";
161         return 1; }
162 }
163
164 install_xinetd() {
165     log_output_of install -m 0644 ${AMANDAHOMEDIR}/example/xinetd.${1} ${SYSCONFDIR}/xinetd.d/${1} || \
166         { logger "WARNING:  Could not install xinetd configuration '${1}'" ; return 1; }                
167     logger "Installed xinetd config for ${1}."
168 }
169
170 install_inetd() {
171     case $os in
172         SunOS) inetd_conf=${SYSCONFDIR}/inet/inetd.conf ;;
173         *) inetd_conf=${SYSCONFDIR}/inetd.conf ;;
174     esac
175     # This one is hard to log because we're just appending.
176     logger "Appending ${AMANDAHOMEDIR}/example/inetd.conf.${1} to ${inetd_conf}"
177     cat ${AMANDAHOMEDIR}/example/inetd.conf.${1} >> ${inetd_conf}
178 }
179
180 install_smf() {
181     # First parameter should be the name of the service to install
182     # (amandaserver, or amandaclient).
183     ver=`uname -r`
184     case $ver in
185       5.10)
186         # Use inetadm and svcadm.
187         log_output_of ${basedir}/usr/sbin/inetconv -f -i ${AMANDAHOMEDIR}/example/inetd.conf.${1} || { \
188             logger "Warning: Failed to create Amanda SMF manifest. Check the system log.";
189             return 1; }
190         log_output_of ${basedir}/usr/sbin/inetadm -e svc:/network/amanda/tcp || { \
191             logger "Warning: Failed to enable Amanda service. See system log for more information.";
192             return 1; }
193         log_output_of ${basedir}/usr/sbin/svcadm restart network/amanda/tcp || { \
194             logger "Warning: Failed to restart Amanda service.  See system log for details.";
195             return 1; }
196       ;;
197
198       5.8|5.9)
199         logger "Solaris 8 and 9 use inetd, not SMF tools."
200         return 1
201       ;;
202       
203       *)
204         # I don't know what to do...
205         logger "ERROR: Unsupported and untested version of Solaris: $ver"
206         return 1
207       ;;
208     esac
209 }
210
211 reload_xinetd() {
212     # Default action is to try reload.
213     if [ "x$1" = "x" ]; then
214         action="reload"
215     elif [ "$1" = "reload" ] || [ "$1" = "restart" ]; then
216         action="$1"
217     else
218         logger "WARNING: bad argument to reload_xinetd: $1"
219         return 1
220     fi
221     if [ "$action" = "reload" ] ; then
222         logger "Reloading xinetd configuration..." 
223         log_output_of ${SYSCONFDIR}/init.d/xinetd $action # Don't exit!
224         if [ $? -ne 0 ] ; then
225             logger "xinetd reload failed.  Attempting restart..."
226             log_output_of ${SYSCONFDIR}/init.d/xinetd restart || \
227                 { logger "WARNING:  restart failed." ; return 1; }
228         fi
229     else
230         # Must be restart...
231         logger "Restarting xinetd."
232         log_output_of ${SYSCONFDIR}/init.d/xinetd $1 || \
233             { logger "WARNING:  ${1} failed." ; return 1; }
234     fi
235 }
236
237 reload_inetd() {
238     # Default action is to try reload.
239     if [ "x$1" = "x" ]; then
240         action="reload"
241     elif [ "$1" = "reload" ] || [ "$1" = "restart" ]; then
242         action="$1"
243     else
244         logger "WARNING: bad argument to reload_inetd: $1"
245         return 1
246     fi
247     if [ "$1" = "reload" ] ; then
248         logger "Reloading inetd configuration..."
249         log_output_of ${SYSCONFDIR}/init.d/inetd $1 # Don't exit!
250         if [ $? -ne 0 ] ; then
251             logger "inetd reload failed.  Attempting restart..."
252             log_output_of ${SYSCONFDIR}/init.d/inetd restart || \
253                     { logger "WARNING:  restart failed." ; return 1; }
254         fi
255     else
256         # Must be restart...
257         logger "Restarting inetd."
258         log_output_of ${SYSCONFDIR}/init.d/inetd $1 || \
259                 { logger "WARNING:  ${1} failed." ; return 1; }
260     fi
261 }
262 # End Common functions