openpkg/rc

changeset 428
f880f219c566
parent 427
71503088f51b
child 469
65414bdd541c
     1.1 --- a/openpkg/rc	Tue Jul 31 12:12:54 2012 +0200
     1.2 +++ b/openpkg/rc	Tue Jul 31 12:23:42 2012 +0200
     1.3 @@ -1,15 +1,14 @@
     1.4  #!@l_prefix@/lib/openpkg/bash --noprofile
     1.5  ##
     1.6 -##  rc -- OpenPKG Runcommand Processor
     1.7 -##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
     1.8 -##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
     1.9 +##  rc -- OpenPKG Run-Command Processor
    1.10 +##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
    1.11  ##
    1.12 -##  Permission to use, copy, modify, and distribute this software for
    1.13 -##  any purpose with or without fee is hereby granted, provided that
    1.14 -##  the above copyright notice and this permission notice appear in all
    1.15 -##  copies.
    1.16 +##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
    1.17 +##  All rights reserved. Licenses which grant limited permission to use,
    1.18 +##  copy, modify and distribute this software are available from the
    1.19 +##  OpenPKG GmbH.
    1.20  ##
    1.21 -##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    1.22 +##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
    1.23  ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    1.24  ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.25  ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    1.26 @@ -29,8 +28,8 @@
    1.27  
    1.28  #   program name, version and date
    1.29  progname="rc"
    1.30 -progvers="1.2.0"
    1.31 -progdate="28-Jul-2003"
    1.32 +progvers="1.2.1"
    1.33 +progdate="2007-12-27"
    1.34  
    1.35  #   path to OpenPKG instance
    1.36  prefix="@l_prefix@"
    1.37 @@ -49,6 +48,42 @@
    1.38  "
    1.39  
    1.40  ##
    1.41 +##  temporary file handling
    1.42 +##
    1.43 +
    1.44 +#   establish secure temporary directory
    1.45 +i=0
    1.46 +while [ $i -lt 10 ]; do
    1.47 +   tmpdir="/tmp/rc-`date '+%Y%m%d%H%M%S'`-$$"
    1.48 +   (umask 022; mkdir $tmpdir >/dev/null 2>&1) && break
    1.49 +   i=$(($i + 1))
    1.50 +   sleep 1
    1.51 +done
    1.52 +if [ $i -eq 10 ]; then
    1.53 +    echo "openpkg:rc:ERROR: unable to establish secure temporary directory" 1>&2
    1.54 +    exit 1
    1.55 +fi
    1.56 +declare -r tmpdir
    1.57 +cleanup () {
    1.58 +    if [ ".$tmpdir" != . ]; then
    1.59 +        if [ -d $tmpdir ]; then
    1.60 +            rm -rf $tmpdir >/dev/null 2>&1 || true
    1.61 +        fi
    1.62 +    fi
    1.63 +}
    1.64 +trap "cleanup; trap - EXIT INT ABRT QUIT TERM" EXIT INT ABRT QUIT TERM
    1.65 +
    1.66 +#   determine reasonable temporary files
    1.67 +tmpfile="$tmpdir/rc.tmp"
    1.68 +outfile="$tmpdir/rc.out"
    1.69 +errfile="$tmpdir/rc.err"
    1.70 +allfile="$tmpdir/rc.all"
    1.71 +deffile="$tmpdir/rc.def"
    1.72 +
    1.73 +#   initialize files
    1.74 +cp /dev/null $deffile
    1.75 +
    1.76 +##
    1.77  ##  command line option parsing
    1.78  ##
    1.79  
    1.80 @@ -65,12 +100,7 @@
    1.81  
    1.82  #   iterate over argument line
    1.83  while [ $# -gt 0 ]; do
    1.84 -    opt=$1
    1.85 -    case $opt in
    1.86 -        -*=*) arg=${opt/-*=/} ;;
    1.87 -           *) arg='' ;;
    1.88 -    esac
    1.89 -    case $opt in
    1.90 +    case "$1" in
    1.91          -s|--silent  ) silent=1      ;;
    1.92          -v|--verbose ) verbose=1     ;;
    1.93          -d|--debug   ) debug=1       ;;
    1.94 @@ -80,8 +110,12 @@
    1.95          -e|--eval    ) eval=1        ;;
    1.96          -c|--config  ) config=1      ;;
    1.97          -q|--query   ) query=1       ;;
    1.98 -        -*           ) help="Invalid option \`$opt'"; break ;;
    1.99 -        *            ) break         ;;
   1.100 +        -D|--define  ) echo "@$2" | \
   1.101 +                       sed -e "s;';\\\\';g" | \
   1.102 +                       sed -e "s;^@\\([a-z][a-zA-Z0-9_]*\\)=\\(.*\\)\$;\1='\2';" \
   1.103 +                           -e "s;^@.*;;" >>$deffile; shift ;;
   1.104 +        -*           ) help="Invalid option \`$1'"; break ;;
   1.105 +        *            ) break ;;
   1.106      esac
   1.107      shift
   1.108  done
   1.109 @@ -92,7 +126,7 @@
   1.110          echo "$progname:ERROR: $help" 1>&2
   1.111      fi
   1.112      echo "Usage: $progname [-s|--silent] [-v|--verbose] [-d|--debug] [-k|--keep] [-h|--help]" 1>&2
   1.113 -    echo "       [-p|--print] [-e|--eval] [-c|--config] [-q|--query]" 1>&2
   1.114 +    echo "       [-p|--print] [-e|--eval] [-c|--config] [-q|--query] [-D|--define <name>=<value>]" 1>&2
   1.115      echo "       <package> <command> [<command> ...]"  1>&2
   1.116      if [ ".$help" != ".Usage" ]; then
   1.117          exit 1
   1.118 @@ -117,30 +151,11 @@
   1.119  
   1.120  #   extend run-time environment with local OpenPKG tools (shtool, rpmtool, etc)
   1.121  PATH_ORIG="$PATH"
   1.122 +PATH="$prefix/lib/openpkg/fallback:$PATH"
   1.123  PATH="$prefix/bin:$PATH"
   1.124  PATH="$prefix/sbin:$PATH"
   1.125  PATH="$prefix/lib/openpkg:$PATH"
   1.126 -
   1.127 -#   establish secure temporary directory
   1.128 -i=0
   1.129 -while [ $i -lt 10 ]; do
   1.130 -   tmpdir="/tmp/rc-`date '+%Y%m%d%H%M%S'`-$$"
   1.131 -   (umask 022; mkdir $tmpdir >/dev/null 2>&1) && break
   1.132 -   i=$(($i + 1))
   1.133 -   sleep 1
   1.134 -done
   1.135 -if [ $i -eq 10 ]; then
   1.136 -    echo "openpkg:rc:ERROR: unable to establish secure temporary directory" 1>&2
   1.137 -    exit 1
   1.138 -fi
   1.139 -declare -r tmpdir
   1.140 -trap "trap - EXIT INT ABRT QUIT TERM; rm -rf $tmpdir >/dev/null 2>&1 || true" EXIT INT ABRT QUIT TERM
   1.141 -
   1.142 -#   determine reasonable temporary files
   1.143 -tmpfile="$tmpdir/rc.tmp"
   1.144 -outfile="$tmpdir/rc.out"
   1.145 -errfile="$tmpdir/rc.err"
   1.146 -allfile="$tmpdir/rc.all"
   1.147 +PATH="$prefix/lib/openpkg/override:$PATH"
   1.148  
   1.149  #   handle --query option
   1.150  if [ ".$query" = .1 ]; then
   1.151 @@ -149,6 +164,7 @@
   1.152      touch $tmpfile
   1.153      sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.154      echo ". $rcconf" >>$tmpfile
   1.155 +    echo ". $deffile" >>$tmpfile
   1.156      scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   1.157      for s_name in $scripts; do
   1.158          sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.159 @@ -157,6 +173,7 @@
   1.160  
   1.161      #   apply override values to get effective values
   1.162      . $rcconf
   1.163 +    . $deffile
   1.164  
   1.165      #   display variable value
   1.166      for var in $*; do
   1.167 @@ -174,6 +191,7 @@
   1.168      touch $tmpfile
   1.169      sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.170      echo ". $rcconf" >>$tmpfile
   1.171 +    echo ". $deffile" >>$tmpfile
   1.172      scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   1.173      for s_name in $scripts; do
   1.174          sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.175 @@ -192,6 +210,7 @@
   1.176  
   1.177      #   apply override values to get effective values
   1.178      . $rcconf
   1.179 +    . $deffile
   1.180  
   1.181      #   determine how to print in bold mode in case output
   1.182      #   is connected to a terminal device
   1.183 @@ -237,20 +256,48 @@
   1.184  scripts="${1/*rc./}"
   1.185  shift
   1.186  isall=0
   1.187 +if [ ".$scripts" = ".cron" ]; then
   1.188 +    #   "cron" is an "all" with a random delay (to ensure that multiple
   1.189 +    #   OpenPKG instances do not all startup at exactly the same time)
   1.190 +    #   plus a mutex lock to ensure that multiple OpenPKG cron tasks of
   1.191 +    #   the same OpenPKG instance do not stumble over each other).
   1.192 +
   1.193 +    #   determine delay range and timeout
   1.194 +    case "${1-quarterly}" in
   1.195 +        monthly     ) delay=1800; timeout=28800 ;; # 30m /  8h
   1.196 +        weekly      ) delay=1800; timeout=14400 ;; # 30m /  4h
   1.197 +        daily       ) delay=900;  timeout=7200  ;; # 15m /  2h
   1.198 +        hourly      ) delay=600;  timeout=3600  ;; #  5m /  1h
   1.199 +        quarterly|* ) delay=30;   timeout=900   ;; # 30s / 15m
   1.200 +    esac
   1.201 +
   1.202 +    #   apply random run-time delay
   1.203 +    #   (hint: $RANDOM is a random value 0..32767)
   1.204 +    sleep $(( ($RANDOM * $delay) / 32767 ))
   1.205 +
   1.206 +    #   wrap ourself for mutual exclusion run-time
   1.207 +    #   and then perform the "all" command
   1.208 +    cleanup
   1.209 +    exec $prefix/lib/openpkg/mutex \
   1.210 +        -t $timeout $prefix/RPM/TMP/openpkg-rc-cron.mutex \
   1.211 +        sh -c "exec $0 all $*" || exit $?
   1.212 +fi
   1.213  if [ ".$scripts" = ".all" ]; then
   1.214      isall=1
   1.215      . $rcconf
   1.216 -    if [ ".$openpkg_runall" != . ]; then
   1.217 -        #   backward compatibility only
   1.218 -        echo "openpkg:rc:WARNING: variable \"openpkg_runall\" was renamed to \"openpkg_rc_all\"." 1>&2
   1.219 -        echo "openpkg:rc:WARNING: value of deprecated variable \"openpkg_runall\" taken over for compatibility." 1>&2
   1.220 -        echo "openpkg:rc:WARNING: please update your local configuration in \"$rcconf\"." 1>&2
   1.221 -        openpkg_rc_all="$openpkg_runall"
   1.222 -    fi
   1.223 +    . $deffile
   1.224      case "$openpkg_rc_all" in
   1.225          [Nn][Oo] | [Ff][Aa][Ll][Ss][Ee] | [Oo][Ff][Ff] | 0 ) exit 0 ;;
   1.226      esac
   1.227      scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;"`
   1.228 +
   1.229 +    #   the "all" target is usually called from system startup scripts,
   1.230 +    #   and we really want to ensure that no potentially corrupt
   1.231 +    #   (because of an unclean shutdown of the system) RPM DB journal
   1.232 +    #   files are staying around and preventing the startup of the
   1.233 +    #   OpenPKG instance. When called manually it also doesn't hurt to
   1.234 +    #   cleanup.
   1.235 +    rm -f $prefix/RPM/DB/__db.* >/dev/null 2>&1 || true
   1.236  else
   1.237      if [ ! -f "$rcdir/rc.$scripts" ]; then
   1.238          echo "openpkg:rc:ERROR: package \"$scripts\" not found" 1>&2
   1.239 @@ -269,6 +316,9 @@
   1.240      exit 1
   1.241  fi
   1.242  
   1.243 +#   just call OpenPKG RPM to let it once perform the run-time integrity checks
   1.244 +$prefix/bin/openpkg rpm -q openpkg >/dev/null || exit $?
   1.245 +
   1.246  #   iterate over the specified commands
   1.247  rv=0
   1.248  cmds="$*"
   1.249 @@ -397,6 +447,7 @@
   1.250          #   rc.conf to override the default of $openpkg_rc_def, too.
   1.251          sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.252          echo ". $rcconf" >>$tmpfile
   1.253 +        echo ". $deffile" >>$tmpfile
   1.254          l_scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   1.255          for l_name in $l_scripts; do
   1.256              sed <$rcdir/rc.$l_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.257 @@ -404,6 +455,7 @@
   1.258  
   1.259          #   generate: inclusion of the application of override variables
   1.260          echo ". $rcconf" >>$tmpfile
   1.261 +        echo ". $deffile" >>$tmpfile
   1.262  
   1.263          #   for --eval redirect stderr and stdout (but remember stdout)
   1.264          #   (let stderr pass unfiltered in case of debug mode)
   1.265 @@ -492,6 +544,7 @@
   1.266              #   rc.conf to override the default of $openpkg_rc_def, too.
   1.267              sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.268              echo ". $rcconf" >>$tmpfile
   1.269 +            echo ". $deffile" >>$tmpfile
   1.270              l_scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   1.271              for l_name in $l_scripts; do
   1.272                  sed <$rcdir/rc.$l_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   1.273 @@ -499,6 +552,7 @@
   1.274  
   1.275              #   generate: inclusion of the application of override variables
   1.276              echo ". $rcconf" >>$tmpfile
   1.277 +            echo ". $deffile" >>$tmpfile
   1.278  
   1.279              #   generate: %common section and particular command section
   1.280              sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%common/d" -e '/^%.*/,$d'

mercurial