openpkg/rc.func

changeset 13
cb59d6afeb61
child 427
71503088f51b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/openpkg/rc.func	Tue Jan 06 23:40:39 2009 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +##
     1.5 +##  @l_prefix@/etc/rc.func -- Run-Command Helper Functions
     1.6 +##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
     1.7 +##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
     1.8 +##
     1.9 +##  Permission to use, copy, modify, and distribute this software for
    1.10 +##  any purpose with or without fee is hereby granted, provided that
    1.11 +##  the above copyright notice and this permission notice appear in all
    1.12 +##  copies.
    1.13 +##
    1.14 +##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    1.15 +##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    1.16 +##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.17 +##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    1.18 +##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.19 +##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.20 +##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    1.21 +##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.22 +##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.23 +##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    1.24 +##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.25 +##  SUCH DAMAGE.
    1.26 +##
    1.27 +
    1.28 +##
    1.29 +##  NOTICE: This script is a collection of reusable shell functions
    1.30 +##  running under GNU Bash during the execution of OpenPKG run-command
    1.31 +##  sections.
    1.32 +##
    1.33 +
    1.34 +#
    1.35 +#   rcMsg (display message)
    1.36 +#
    1.37 +#   Usage:       rcMsg [-e] [-w] <message>
    1.38 +#   Example:     rcMsg -e "invalid command line"
    1.39 +#   Description: display a regular/warning/error message.
    1.40 +#
    1.41 +rcMsg () {
    1.42 +    local prefix="rc:"
    1.43 +    while [ $# -gt 0 ]; do
    1.44 +        case $1 in
    1.45 +            -e ) prefix="${prefix}ERROR:";   shift ;;
    1.46 +            -w ) prefix="${prefix}WARNING:"; shift ;;
    1.47 +            *  ) break ;;
    1.48 +        esac
    1.49 +    done
    1.50 +    echo "${prefix} $*"
    1.51 +    return 0
    1.52 +}
    1.53 +
    1.54 +#
    1.55 +#   rcPath (manipulate colon-separated PATH-style variable)
    1.56 +#
    1.57 +#   Usage:       rcPath [-a] [-r] [-p] [-e] <var> <dir> [<dir> ...]
    1.58 +#   Example:     rcPath -a -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
    1.59 +#   Description: removes (-r) or adds (-a) by appending or prepending
    1.60 +#                (-p) one or more directories <dir> (optionally have
    1.61 +#                to be existing if -e is given) to a colon-separated
    1.62 +#                PATH-style variable <var>. In case a directory already
    1.63 +#                exists, it is first removed.
    1.64 +#
    1.65 +rcPath () {
    1.66 +    local mode=""
    1.67 +    local prepend=0
    1.68 +    local exists=0
    1.69 +    while [ $# -gt 0 ]; do
    1.70 +        case $1 in
    1.71 +            -a ) mode="add";    shift ;;
    1.72 +            -r ) mode="remove"; shift ;;
    1.73 +            -p ) prepend=1;     shift ;;
    1.74 +            -e ) exists=1;      shift ;;
    1.75 +            *  ) break                ;;
    1.76 +        esac
    1.77 +    done
    1.78 +    local var="$1"
    1.79 +    shift
    1.80 +    if [ ".${mode}" = .add ]; then
    1.81 +        local edit_del=""
    1.82 +        local edit_add=""
    1.83 +        local dir
    1.84 +        for dir in "$@"; do
    1.85 +            if [ ".${exists}" = .1 ] && [ ! -d "${dir}" ]; then
    1.86 +                continue
    1.87 +            fi
    1.88 +            edit_del="${edit_del} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
    1.89 +            edit_del="${edit_del} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
    1.90 +            if [ ".${prepend}" = .0 ]; then
    1.91 +                edit_add="${edit_add} -e 's;\$;:${dir};'"
    1.92 +            else
    1.93 +                edit_add="-e 's;^;${dir}:;' ${edit_add}"
    1.94 +            fi
    1.95 +        done
    1.96 +        if [ ".${edit_del}${edit_add}" != . ]; then
    1.97 +            eval "${var}=\`echo \"\$${var}\" | sed ${edit_del} ${edit_add}\`"
    1.98 +        fi
    1.99 +        return 0
   1.100 +    elif [ ".${mode}" = .remove ]; then
   1.101 +        local edit=""
   1.102 +        local dir
   1.103 +        for dir in "$@"; do
   1.104 +            edit="${edit} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
   1.105 +            edit="${edit} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
   1.106 +        done
   1.107 +        eval "${var}=\`echo \"\$${var}\" | sed ${edit}\`"
   1.108 +        return 0
   1.109 +    else
   1.110 +        rcMsg -e "rcPath: neither add (-a) nor remove (-r) operation specified"
   1.111 +        return 1
   1.112 +    fi
   1.113 +}
   1.114 +
   1.115 +#
   1.116 +#   rcTmp (temporary file handling)
   1.117 +#
   1.118 +#   Usage:       rcTmp [-i] [-f [-n <name>]] [-k]
   1.119 +#   Example:     rcTmp -i; tmpfile=`rcTmp -f -n tmp`; ...; rcTmp -k
   1.120 +#   Description: ???
   1.121 +#
   1.122 +rcTmp () {
   1.123 +    local mode=""
   1.124 +    local name=""
   1.125 +    while [ $# -gt 0 ]; do
   1.126 +        case $1 in
   1.127 +            -i ) mode="init"; shift        ;;
   1.128 +            -f ) mode="file"; shift        ;;
   1.129 +            -k ) mode="kill"; shift        ;;
   1.130 +            -n ) name="$2";   shift; shift ;;
   1.131 +            *  ) break                     ;;
   1.132 +        esac
   1.133 +    done
   1.134 +    if [ ".${mode}" = .init ]; then
   1.135 +        if [ ".${RC_TMPDIR}" = . ]; then
   1.136 +            local i=0
   1.137 +            while [ ${i} -lt 10 ]; do
   1.138 +               RC_TMPDIR="@l_prefix@/RPM/TMP/rc-`date '+%Y%m%d%H%M%S'`-$$"
   1.139 +               (umask 022; mkdir ${RC_TMPDIR} >/dev/null 2>&1) && break
   1.140 +               i=$((${i} + 1))
   1.141 +               sleep 1
   1.142 +            done
   1.143 +            if [ ${i} -eq 10 ]; then
   1.144 +                rcMsg -e "rcTmp: unable to establish secure temporary directory" 1>&2
   1.145 +                return 1
   1.146 +            fi
   1.147 +            declare -r RC_TMPDIR
   1.148 +        fi
   1.149 +        return 0
   1.150 +    elif [ ".${mode}" = .file ]; then
   1.151 +        echo "${RC_TMPDIR}/${name:-tmp}"
   1.152 +        return 0
   1.153 +    elif [ ".${mode}" = .kill ]; then
   1.154 +        if [ ".${RC_TMPDIR}" = . ]; then
   1.155 +            rcMsg -e "rcTmp: no secure temporary directory known"
   1.156 +            return 1
   1.157 +        else
   1.158 +            rm -rf ${RC_TMPDIR}
   1.159 +            return 0
   1.160 +        fi
   1.161 +    else
   1.162 +        rcMsg -e "rcTmp: neither init (-i), file (-f) nor kill (-k) operation specified"
   1.163 +        return 1
   1.164 +    fi
   1.165 +}
   1.166 +
   1.167 +#
   1.168 +#   rcService (check for service status enable/active/usable)
   1.169 +#
   1.170 +#   Usage:       rcService <pkg> <service> <val>
   1.171 +#   Example:     if rcService openssh enable yes; then ...
   1.172 +#   Description: check <service> of package <pkg> against value <val>.
   1.173 +#                <service> has to be one of "enable", "active" or "usable".
   1.174 +#                <val> has to be either "no", "yes", or "unknown".
   1.175 +#
   1.176 +rcService () {
   1.177 +    local pkg="`echo ${1} | sed -e 's;-;_;g'`"
   1.178 +    local var="${pkg}_${2}"
   1.179 +    local chk="${3}"
   1.180 +    eval "local val=\$${var}"
   1.181 +    if [ ".${val}" = . ]; then
   1.182 +        eval `@l_prefix@/bin/openpkg rc 2>/dev/null --silent ${1} status || true`
   1.183 +        eval "local val=\$${var}"
   1.184 +    fi
   1.185 +    if [ ".${val}" = ".${chk}" ]; then
   1.186 +        return 0
   1.187 +    else
   1.188 +        return 1
   1.189 +    fi
   1.190 +}
   1.191 +
   1.192 +#
   1.193 +#   rcVarIsYes (check variable for positive value)
   1.194 +#
   1.195 +#   Usage:       rcVarIsYes <var>
   1.196 +#   Example:     if rcVarIsYes foo; then ...
   1.197 +#   Description: check whether a variable <var> contains a positive
   1.198 +#                value, i.e., the values "yes", "true", "on" or "1" in
   1.199 +#                arbitrary lower or upper case.
   1.200 +#
   1.201 +rcVarIsYes () {
   1.202 +    local var="${1}"
   1.203 +    eval "local val=\"\$${var}\""
   1.204 +    case "${val}" in
   1.205 +        [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 )
   1.206 +            return 0
   1.207 +            ;;
   1.208 +        * )
   1.209 +            return 1
   1.210 +            ;;
   1.211 +    esac
   1.212 +}
   1.213 +

mercurial