#!/bin/bash # # plc Manages all PLC services on this machine # # chkconfig: 2345 60 40 # # description: Manages all PLC services on this machine # # Source function library and configuration # plc_reload is defined here . /etc/plc.d/functions # Verbosity verbose=0 # All steps should be idempotent. This means that you should be able # to run them multiple times without depending on anything previously # being run. The idea is that when the configuration changes, "service # plc restart" is called, all dependencies are fixed up, and # everything just works. ### NOTE. # we want the resulting myplc to be able to easily skip # some steps. e.g. the packages step takes ages if you install # all rpms under the repository. # We skip steps whose name contains a dot (.) or a tilde (~) # this way the operations would just rename a step name e.g. # cd /etc/plc.d # mv packages packages.hide # # The drawback is, this stuff does not survive an rpm update # but that's maybe a good thing, that all is done at first start ### # do not consider files that contain '.', '~' or 'functions' in the name steps=($( for step in /etc/plc.d/* ; do stepname=$(basename $step) plainstepname=$(echo $stepname | sed -e 's,\.,,' -e 's,~,,' -e 's,functions,,' ) if [ -f $step -a -x $step -a "$stepname" = "$plainstepname" ] ; then priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step) echo $priority $stepname fi done | sort -n | cut -d' ' -f2 )) nsteps=${#steps[@]} usage() { echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..." echo " -v Be verbose" echo " -h This message" echo echo "Commands:" echo " start Start all PLC subsystems" echo " stop Stop all PLC subsystems" echo " reload Regenerate configuration files" echo " restart Restart all PLC subsystems" echo " checkpoint filename : Checkpoint the current state of MyPLC to filename" echo " restore filename : Restore MyPLC state from filename" echo " steps Displays ordered list of subsystems" echo echo "Steps:" for step in "${steps[@]}" ; do if [ -x /etc/plc.d/$step ] ; then echo " $(basename $step)" fi done exit 1 } # Get options while getopts "vh" opt ; do case $opt in v) verbose=1 ;; h|*) usage ;; esac done # Redirect stdout and stderr of each step to /var/log/boot.log if [ $verbose -eq 0 ] ; then touch /var/log/boot.log chmod 600 /var/log/boot.log exec 1>>/var/log/boot.log exec 2>>/var/log/boot.log fi # Get command shift $(($OPTIND - 1)) if [ -z "$1" ] ; then usage >&3 fi command=$1 # Get step(s) shift 1 if [ -z "$1" ] ; then # Start or stop everything. Regenerate configuration first. plc_reload force else # Start or stop a particular step steps=("$@") nsteps=${#steps[@]} fi RETVAL=0 start () { for step in "${steps[@]}" ; do if [ -x /etc/plc.d/$step ] ; then /etc/plc.d/$step start # Steps may alter the configuration, may need to regenerate plc_reload else echo "PLC: $step: unrecognized step" >&4 exit 1 fi done } stop () { for i in $(seq 1 $nsteps) ; do step=${steps[$(($nsteps - $i))]} if [ -x /etc/plc.d/$step ] ; then /etc/plc.d/$step stop # Steps may alter the configuration, may need to regenerate plc_reload else echo "PLC: $step: unrecognized step" >&4 exit 1 fi done } case "$command" in start|stop) $command ;; restart) stop start ;; reload) plc_reload force ;; checkpoint) cpfile=$1 if [ -z "$cpfile" ] ; then echo "PLC: checkpoint requires a filename as an argument" exit 1 fi cpdir=$(mktemp -d tmp.XXXXXX) cd $cpdir mkdir -p ./etc/planetlab/ rsync -av /etc/planetlab/ ./etc/planetlab/ /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint tar cjf $cpfile etc cd - rm -rf $cpdir ;; restore) cpfile=$1 cpdir=$(mktemp -d tmp.XXXXXX) cd $cpdir tar xjf $cpfile /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint rsync -av ./etc/planetlab/ /etc/planetlab/ cd - rm -rf $cpdir ;; steps) echo "${steps[@]}" >&4 ;; # for backwards compatibility mount|umount|mountstatus) echo "${command} not used within native myplc environment" ;; *) usage >&3 ;; esac exit $RETVAL