#!/bin/bash
###################################################################################
#
# ctlrc.sh
#
# Script to add/remove init scripts and rc*.d links for programs using ctl scripts.
#
# Relies on etc/ctlrc.conf for configuration
#
# @(#)$Id$
# $URL$
###################################################################################
#Setup our base
BASEDIR=/sites/utils/local
# Load TAPS globals
source $BASEDIR/lib/loadglobals.sh
[ $? -ne 0 ] && exit 1
TYPE=""
INSTANCE=""
ACTION_ADD=0
ACTION_RM=0
ACTION_LIST=0
AUTO=0
VERBOSE=0
STARTDIR="rc3.d"
STOPDIRS="rc0.d rc1.d rc2.d"
CONF="$BASEDIR/etc/ctlrc.conf"
ETC="/etc"
##
# Usage
###
usage() {
$ECHO "Usage: $0 -a -i <instance> -t <type> -[yv]"
$ECHO " $0 -r -i <instance> -t <type> -[yv]"
$ECHO " $0 -l"
$ECHO " -a Add init script and links"
$ECHO " -r Remove init script and links"
$ECHO " -t Type of init script (use -l to find available types)"
$ECHO " -l List available types"
$ECHO " -i Instance name"
$ECHO " -y Run without confirmation prompt"
$ECHO " -v Verbose output"
exit $1
}
##
# Get Types
###
getTypes() {
types=`$GREP -v ^# $CONF | $AWK -F: '{print $1}'`
$ECHO $types
}
##
# Confirm
###
confirm() {
action=$1
if [ $AUTO -ne 0 ]; then
return 0
fi
$ECHO "Will $action script $initFile"
$ECHO "Will $action link $initSFile"
for dir in $STOPDIRS; do
$ECHO "Will $action link ${ETC}/${dir}/${initKFile}"
done
$PRINTF "Do you wish to continue? [yN] "
read confirm
if [ "$confirm" != "y" -a "$confirm" != "Y" ]; then
return 1
else
return 0
fi
}
##
# Add All
###
addAll() {
confirm "add" || return 1
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating init script $initFile";
fi
$CAT >$initFile 2>&1 <<EOF
#!/bin/sh
# This script is autogenerated by /sites/utils/local/bin/ctlrc
INSTANCE=$INSTANCE
CTL=$ctlScript
if [ ! -x "\$CTL" ]; then
$ECHO "Unable to locate \$CTL"
exit 2
fi
exec \$CTL \$INSTANCE \$1
EOF
[ $? -ne 0 ] && return 1
if [ $VERBOSE -eq 1 ]; then
$ECHO "Setting execute permissions on $initFile"
fi
$CHMOD 755 $initFile
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating symlink to $initSFile"
fi
$LN -s $initFile $initSFile
[ $? -ne 0 ] && return 1
for dir in $STOPDIRS; do
t=${ETC}/${dir}/${initKFile}
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating symlink to $t"
fi
$LN -s $initFile $t
[ $? -ne 0 ] && return 1
done
return 0
}
##
# Remove All
###
removeAll() {
confirm "remove" || return 1
if [ -f $initFile ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $initFile"
fi
$RM -f $initFile || return 1
fi
if [ -h $initSFile ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $initSFile"
fi
$RM -f $initSFile || return 1
fi
for dir in $STOPDIRS; do
t="${ETC}/${dir}/${initKFile}"
if [ -h $t ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $t"
fi
$RM -f $t || return 1
fi
done
return 0
}
##
# Check Arguments
###
checkArgs() {
if [ $ACTION_LIST -eq 1 ]; then
return 0
elif [ $ACTION_ADD -eq 1 -a $ACTION_RM -eq 1 ]; then
$ECHO "Option -a and -r are not compatible"
return 1
elif [ -z "$TYPE" ]; then
$ECHO "You must provide a type"
return 1
elif (! $GREP ^$TYPE $CONF >/dev/null 2>&1); then
$ECHO "No such type $TYPE"
return 1
elif [ "$instanceRequired" -eq 1 -a ! "$INSTANCE" ]; then
$ECHO "You must provide an instance name"
return 1
fi
return 0
}
### Main ###
# Check command line options
[ $# -eq 0 ] && usage 1
while getopts t:i:arlyvh op; do
case $op in
t) TYPE=$OPTARG
;;
i) INSTANCE=$OPTARG
;;
a) ACTION_ADD=1
;;
r) ACTION_RM=1
;;
l) ACTION_LIST=1
;;
y) AUTO=1
;;
v) VERBOSE=1
;;
h) usage 0
;;
*) usage 1
;;
esac
done
instanceRequired=`$GREP ^$TYPE $CONF | $CUT -d: -f2`
checkArgs || usage 1
baseName=`$GREP ^$TYPE $CONF | $CUT -d : -f 3`
priority=`$GREP ^$TYPE $CONF | $CUT -d : -f 4`
ctlScript=`$GREP ^$TYPE $CONF | $CUT -d : -f 5`
if [ -z "$INSTANCE" ]; then
initFile=${ETC}/init.d/${baseName}
initSFile=${ETC}/${STARTDIR}/S${priority}${baseName}
initKFile=K${priority}${baseName}
else
initFile=${ETC}/init.d/${baseName}_${INSTANCE}
initSFile=${ETC}/${STARTDIR}/S${priority}${baseName}_${INSTANCE}
initKFile=K${priority}${baseName}_${INSTANCE}
fi
if [ $ACTION_LIST -eq 1 ]; then
getTypes
exit $?
elif [ $ACTION_ADD -eq 1 ]; then
addAll
exit $?
elif [ $ACTION_RM -eq 1 ]; then
removeAll
exit $?
fi
###################################################################################
#
# ctlrc.sh
#
# Script to add/remove init scripts and rc*.d links for programs using ctl scripts.
#
# Relies on etc/ctlrc.conf for configuration
#
# @(#)$Id$
# $URL$
###################################################################################
#Setup our base
BASEDIR=/sites/utils/local
# Load TAPS globals
source $BASEDIR/lib/loadglobals.sh
[ $? -ne 0 ] && exit 1
TYPE=""
INSTANCE=""
ACTION_ADD=0
ACTION_RM=0
ACTION_LIST=0
AUTO=0
VERBOSE=0
STARTDIR="rc3.d"
STOPDIRS="rc0.d rc1.d rc2.d"
CONF="$BASEDIR/etc/ctlrc.conf"
ETC="/etc"
##
# Usage
###
usage() {
$ECHO "Usage: $0 -a -i <instance> -t <type> -[yv]"
$ECHO " $0 -r -i <instance> -t <type> -[yv]"
$ECHO " $0 -l"
$ECHO " -a Add init script and links"
$ECHO " -r Remove init script and links"
$ECHO " -t Type of init script (use -l to find available types)"
$ECHO " -l List available types"
$ECHO " -i Instance name"
$ECHO " -y Run without confirmation prompt"
$ECHO " -v Verbose output"
exit $1
}
##
# Get Types
###
getTypes() {
types=`$GREP -v ^# $CONF | $AWK -F: '{print $1}'`
$ECHO $types
}
##
# Confirm
###
confirm() {
action=$1
if [ $AUTO -ne 0 ]; then
return 0
fi
$ECHO "Will $action script $initFile"
$ECHO "Will $action link $initSFile"
for dir in $STOPDIRS; do
$ECHO "Will $action link ${ETC}/${dir}/${initKFile}"
done
$PRINTF "Do you wish to continue? [yN] "
read confirm
if [ "$confirm" != "y" -a "$confirm" != "Y" ]; then
return 1
else
return 0
fi
}
##
# Add All
###
addAll() {
confirm "add" || return 1
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating init script $initFile";
fi
$CAT >$initFile 2>&1 <<EOF
#!/bin/sh
# This script is autogenerated by /sites/utils/local/bin/ctlrc
INSTANCE=$INSTANCE
CTL=$ctlScript
if [ ! -x "\$CTL" ]; then
$ECHO "Unable to locate \$CTL"
exit 2
fi
exec \$CTL \$INSTANCE \$1
EOF
[ $? -ne 0 ] && return 1
if [ $VERBOSE -eq 1 ]; then
$ECHO "Setting execute permissions on $initFile"
fi
$CHMOD 755 $initFile
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating symlink to $initSFile"
fi
$LN -s $initFile $initSFile
[ $? -ne 0 ] && return 1
for dir in $STOPDIRS; do
t=${ETC}/${dir}/${initKFile}
if [ $VERBOSE -eq 1 ]; then
$ECHO "Creating symlink to $t"
fi
$LN -s $initFile $t
[ $? -ne 0 ] && return 1
done
return 0
}
##
# Remove All
###
removeAll() {
confirm "remove" || return 1
if [ -f $initFile ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $initFile"
fi
$RM -f $initFile || return 1
fi
if [ -h $initSFile ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $initSFile"
fi
$RM -f $initSFile || return 1
fi
for dir in $STOPDIRS; do
t="${ETC}/${dir}/${initKFile}"
if [ -h $t ]; then
if [ $VERBOSE -eq 1 ]; then
$ECHO "Removing $t"
fi
$RM -f $t || return 1
fi
done
return 0
}
##
# Check Arguments
###
checkArgs() {
if [ $ACTION_LIST -eq 1 ]; then
return 0
elif [ $ACTION_ADD -eq 1 -a $ACTION_RM -eq 1 ]; then
$ECHO "Option -a and -r are not compatible"
return 1
elif [ -z "$TYPE" ]; then
$ECHO "You must provide a type"
return 1
elif (! $GREP ^$TYPE $CONF >/dev/null 2>&1); then
$ECHO "No such type $TYPE"
return 1
elif [ "$instanceRequired" -eq 1 -a ! "$INSTANCE" ]; then
$ECHO "You must provide an instance name"
return 1
fi
return 0
}
### Main ###
# Check command line options
[ $# -eq 0 ] && usage 1
while getopts t:i:arlyvh op; do
case $op in
t) TYPE=$OPTARG
;;
i) INSTANCE=$OPTARG
;;
a) ACTION_ADD=1
;;
r) ACTION_RM=1
;;
l) ACTION_LIST=1
;;
y) AUTO=1
;;
v) VERBOSE=1
;;
h) usage 0
;;
*) usage 1
;;
esac
done
instanceRequired=`$GREP ^$TYPE $CONF | $CUT -d: -f2`
checkArgs || usage 1
baseName=`$GREP ^$TYPE $CONF | $CUT -d : -f 3`
priority=`$GREP ^$TYPE $CONF | $CUT -d : -f 4`
ctlScript=`$GREP ^$TYPE $CONF | $CUT -d : -f 5`
if [ -z "$INSTANCE" ]; then
initFile=${ETC}/init.d/${baseName}
initSFile=${ETC}/${STARTDIR}/S${priority}${baseName}
initKFile=K${priority}${baseName}
else
initFile=${ETC}/init.d/${baseName}_${INSTANCE}
initSFile=${ETC}/${STARTDIR}/S${priority}${baseName}_${INSTANCE}
initKFile=K${priority}${baseName}_${INSTANCE}
fi
if [ $ACTION_LIST -eq 1 ]; then
getTypes
exit $?
elif [ $ACTION_ADD -eq 1 ]; then
addAll
exit $?
elif [ $ACTION_RM -eq 1 ]; then
removeAll
exit $?
fi
No comments:
Post a Comment