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