Setting tag myplc-5.1-5
[myplc.git] / plc.init
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 60 40
6 #
7 # description:  Manages all PLC services on this machine
8 #
9
10 # Source function library and configuration
11 # plc_reload is defined here
12 . /etc/plc.d/functions
13
14 # Verbosity
15 verbose=0
16
17 # All steps should be idempotent. This means that you should be able
18 # to run them multiple times without depending on anything previously
19 # being run. The idea is that when the configuration changes, "service
20 # plc restart" is called, all dependencies are fixed up, and
21 # everything just works.
22
23 ### NOTE.
24 # we want the resulting myplc to be able to easily skip
25 # some steps. e.g. the packages step takes ages if you install
26 # all rpms under the repository.
27 # We skip steps whose name contains a dot (.) or a tilde (~)
28 # this way the operations would just rename a step name e.g.
29 # cd /etc/plc.d
30 # mv packages packages.hide
31 #
32 # The drawback is, this stuff does not survive an rpm update
33 # but that's maybe a good thing, that all is done at first start
34 ###
35
36 # do not consider files that contain '.', '~' or 'functions' in the name
37 steps=($(
38 for step in /etc/plc.d/* ; do
39     stepname=$(basename $step)
40     plainstepname=$(echo $stepname | sed -e 's,\.,,' -e 's,~,,' -e 's,functions,,' )
41     if [ -f $step -a -x $step -a "$stepname" = "$plainstepname" ] ; then
42         priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step)
43         echo $priority $stepname
44     fi
45 done | sort -n | cut -d' ' -f2
46 ))
47 nsteps=${#steps[@]}
48
49 usage()
50 {
51     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
52     echo "      -v              Be verbose"
53     echo "      -h              This message"
54     echo
55     echo "Commands:"
56     echo "      start           Start all PLC subsystems"
57     echo "      stop            Stop all PLC subsystems"
58     echo "      reload          Regenerate configuration files"
59     echo "      restart         Restart all PLC subsystems"
60     echo "      checkpoint filename : Checkpoint the current state of MyPLC to filename"
61     echo "      restore filename : Restore MyPLC state from filename"
62     echo "      steps           Displays ordered list of subsystems"
63     echo
64     echo "Steps:"
65     for step in "${steps[@]}" ; do
66         if [ -x /etc/plc.d/$step ] ; then
67             echo "      $(basename $step)"
68         fi
69     done
70     exit 1
71 }
72
73 # Get options
74 while getopts "vh" opt ; do
75     case $opt in
76         v)
77             verbose=1
78             ;;
79         h|*)
80             usage
81             ;;
82     esac
83 done
84
85 # Redirect stdout and stderr of each step to /var/log/boot.log
86 if [ $verbose -eq 0 ] ; then
87     touch /var/log/boot.log
88     chmod 600 /var/log/boot.log
89     exec 1>>/var/log/boot.log
90     exec 2>>/var/log/boot.log
91 fi
92
93 # Get command
94 shift $(($OPTIND - 1))
95 if [ -z "$1" ] ; then
96     usage >&3
97 fi
98 command=$1
99
100 # Get step(s)
101 shift 1
102 if [ -z "$1" ] ; then
103     # Start or stop everything. Regenerate configuration first.
104     plc_reload force
105 else
106     # Start or stop a particular step
107     steps=("$@")
108     nsteps=${#steps[@]}
109 fi
110
111 RETVAL=0
112
113 start ()
114 {
115     for step in "${steps[@]}" ; do
116         if [ -x /etc/plc.d/$step ] ; then
117             /etc/plc.d/$step start
118             # Steps may alter the configuration, may need to regenerate
119             plc_reload
120         else
121             echo "PLC: $step: unrecognized step" >&4
122             exit 1
123         fi
124     done
125 }
126
127 stop ()
128 {
129     for i in $(seq 1 $nsteps) ; do
130         step=${steps[$(($nsteps - $i))]}
131         if [ -x /etc/plc.d/$step ] ; then
132             /etc/plc.d/$step stop
133             # Steps may alter the configuration, may need to regenerate
134             plc_reload
135         else
136             echo "PLC: $step: unrecognized step" >&4
137             exit 1
138         fi
139     done
140 }
141
142 case "$command" in
143     start|stop)
144         $command
145         ;;
146
147     restart)
148         stop
149         start
150         ;;
151
152     reload)
153         plc_reload force
154         ;;
155
156     checkpoint)
157         cpfile=$1
158         if [ -z "$cpfile" ] ; then
159             echo "PLC: checkpoint requires a filename as an argument"
160             exit 1
161         fi 
162         cpdir=$(mktemp -d tmp.XXXXXX)
163         cd $cpdir
164         mkdir -p ./etc/planetlab/
165         rsync -av /etc/planetlab/ ./etc/planetlab/
166         /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
167         tar cjf $cpfile etc
168         cd -
169         rm -rf $cpdir
170         ;;
171
172     restore)
173         cpfile=$1
174         cpdir=$(mktemp -d tmp.XXXXXX)
175         cd $cpdir
176         tar xjf $cpfile
177         /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
178         rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
179         rsync -av ./etc/planetlab/ /etc/planetlab/
180         cd -
181         rm -rf $cpdir
182         ;;
183
184     steps)
185         echo "${steps[@]}" >&4
186         ;;
187
188     # for backwards compatibility
189     mount|umount|mountstatus)
190         echo "${command} not used within native myplc environment"
191         ;;
192
193     *)
194         usage >&3
195         ;;
196 esac
197
198 exit $RETVAL