spaces
[myplc.git] / systemd / plc-ctl
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 source /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 function usage() {
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     plc_reload force
104 else
105     # Start or stop a particular step
106     steps=("$@")
107     nsteps=${#steps[@]}
108 fi
109
110 RETVAL=0
111
112 function start () {
113     for step in "${steps[@]}" ; do
114         if [ -x /etc/plc.d/$step ] ; then
115             /etc/plc.d/$step start
116             # Steps may alter the configuration, may need to regenerate
117             plc_reload
118         else
119             echo "PLC: $step: unrecognized step" >&4
120             exit 1
121         fi
122     done
123 }
124
125 function stop () {
126     for i in $(seq 1 $nsteps) ; do
127         step=${steps[$(($nsteps - $i))]}
128         if [ -x /etc/plc.d/$step ] ; then
129             /etc/plc.d/$step stop
130             # Steps may alter the configuration, may need to regenerate
131             plc_reload
132         else
133             echo "PLC: $step: unrecognized step" >&4
134             exit 1
135         fi
136     done
137 }
138
139 case "$command" in
140     start|stop)
141         $command
142         ;;
143
144     restart)
145         stop
146         start
147         ;;
148
149     reload)
150         plc_reload force
151         ;;
152
153     checkpoint)
154         cpfile=$1
155         if [ -z "$cpfile" ] ; then
156             echo "PLC: checkpoint requires a filename as an argument"
157             exit 1
158         fi
159         cpdir=$(mktemp -d tmp.XXXXXX)
160         cd $cpdir
161         mkdir -p ./etc/planetlab/
162         rsync -av /etc/planetlab/ ./etc/planetlab/
163         /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
164         tar cjf $cpfile etc
165         cd -
166         rm -rf $cpdir
167         ;;
168
169     restore)
170         cpfile=$1
171         cpdir=$(mktemp -d tmp.XXXXXX)
172         cd $cpdir
173         tar xjf $cpfile
174         /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
175         rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
176         rsync -av ./etc/planetlab/ /etc/planetlab/
177         cd -
178         rm -rf $cpdir
179         ;;
180
181     steps)
182         echo "${steps[@]}" >&4
183         ;;
184
185     # for backwards compatibility
186     mount|umount|mountstatus)
187         echo "${command} not used within native myplc environment"
188         ;;
189
190     *)
191         usage >&3
192         ;;
193 esac
194
195 exit $RETVAL