]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
d6c54838ff0e0c594398064fd25652f081f2b536
[OpenSRF.git] / bin / osrf_ctl.sh.in
1 #!/bin/sh
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12  
13 # Strictness to avoid folly
14 set -e
15 set -u
16
17 prefix=@prefix@
18 exec_prefix=@exec_prefix@
19
20 OPT_ACTION=""
21 OPT_CONFIG=""
22 OPT_PID_DIR=""
23 OSRF_HOSTNAME=""
24
25 # ---------------------------------------------------------------------------
26 # Make sure we're running as the correct user
27 # ---------------------------------------------------------------------------
28 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
29
30
31 usage() {
32         cat << EOF
33
34 usage: $0 [OPTION]... -c <c_config> -a <action>
35
36 Mandatory parameters:
37   -a    action to perform
38
39 Optional parameters:";
40   -c    full path to C configuration file (opensrf_core.xml)
41   -d    store PID files in this directory
42   -l    accept 'localhost' as the fully-qualified domain name
43
44 Actions include:
45     start_router
46     stop_router
47     restart_router
48     start_perl
49     stop_perl
50     restart_perl
51     start_python
52     stop_python
53     restart_python
54     start_c
55     stop_c
56     restart_c
57     start_osrf
58     stop_osrf
59     restart_osrf
60     stop_all
61     start_all
62     restart_all
63
64 Examples:
65   $0 -a restart_all
66   $0 -l -c opensrf_core.xml -a restart_all
67
68 EOF
69 }
70
71 # ---------------------------------------------------------------------------
72 # Load the command line options and set the global vars
73 # ---------------------------------------------------------------------------
74 while getopts  "a:d:c:lh" flag; do
75         case $flag in   
76                 "a")            OPT_ACTION="$OPTARG";;
77                 "c")            OPT_CONFIG="$OPTARG";;
78                 "d")            OPT_PID_DIR="$OPTARG";;
79                 "l")            export OSRF_HOSTNAME="localhost";;
80                 "h"|*)  usage;;
81         esac;
82 done
83
84 OSRF_CONFIG="@bindir@/osrf_config"
85 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
86
87 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
88 if [ ! -r "$OPT_CONFIG" ]; then
89         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
90         exit 1;
91 fi;
92 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
93 [ -z "$OPT_ACTION" ] && usage;
94
95 PID_ROUTER="$OPT_PID_DIR/router.pid";
96 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
97 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
98
99
100 # ---------------------------------------------------------------------------
101 # Utility code for checking the PID files
102 # ---------------------------------------------------------------------------
103 do_action() {
104
105         action="$1"; 
106         pidfile="$2";
107         item="$3"; 
108
109         if [ $action = "start" ]; then
110
111                 if [ -e $pidfile ]; then
112                         pid=$(cat $pidfile);
113                         echo "$item already started : $pid";
114                         return 0;
115                 fi;
116                 echo "Starting $item";
117         fi;
118
119         if [ $action = "stop" ]; then
120
121                 if [ ! -e $pidfile ]; then
122                         echo "$item not running";
123                         return 0;
124                 fi;
125
126         while read pid; do
127             if [ -n "$pid" ]; then
128                 echo "Stopping $item process $pid..."
129                 kill -s INT $pid
130             fi;
131         done < $pidfile;
132                 rm -f $pidfile;
133
134         fi;
135
136         return 0;
137 }
138
139
140 # ---------------------------------------------------------------------------
141 # Start / Stop functions
142 # ---------------------------------------------------------------------------
143
144
145 start_router() {
146         do_action "start" $PID_ROUTER "OpenSRF Router";
147         opensrf_router $OPT_CONFIG routers
148     sleep 2; # give the router procs time to fork and appear in ps
149         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
150         echo $pid > $PID_ROUTER;
151         return 0;
152 }
153
154 stop_router() {
155         do_action "stop" $PID_ROUTER "OpenSRF Router";
156         return 0;
157 }
158
159 # The clean way to do this would be to invoke the "start_all" action
160 # on opensrf.py, but that seems to randomly fail for services. So we
161 # start each one separately from a bash script instead.
162 start_python() {
163     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
164     echo "Starting OpenSRF Python";
165     OPT_LOCAL=""
166     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
167     for service in `opensrf.py -a list_all $OPT_LOCAL`; do
168             opensrf.py -p $OPT_PID_DIR -d -a start -s $service $OPT_LOCAL
169     done
170     return 0;
171 }
172
173 stop_python() {
174     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
175     echo "Stopping OpenSRF Python";
176     OPT_LOCAL=""
177     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
178     opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL
179     sleep 1;
180     return 0;
181 }
182
183 start_perl() {
184     echo "Starting OpenSRF Perl";
185     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
186         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
187         return 0;
188 }
189
190 stop_perl() {
191     echo "Stopping OpenSRF Perl";
192     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
193         sleep 1;
194         return 0;
195 }
196
197 start_c() {
198         host=$OSRF_HOSTNAME
199         if [ "_$host" = "_" ]; then
200                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
201         fi;
202
203         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
204         opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
205         return 0;
206 }
207
208 stop_c() {
209         do_action "stop" $PID_OSRF_C "OpenSRF C";
210         sleep 1;
211         return 0;
212 }
213
214
215
216 # ---------------------------------------------------------------------------
217 # Do the requested action
218 # ---------------------------------------------------------------------------
219 case $OPT_ACTION in
220         "start_router") start_router;;
221         "stop_router") stop_router;;
222         "restart_router") stop_router; start_router;;
223         "start_perl") start_perl;;
224         "stop_perl") stop_perl;;
225         "restart_perl") stop_perl; start_perl;;
226         "start_python") start_python;;
227         "stop_python") stop_python;;
228         "restart_python") stop_python; start_python;;
229         "start_c") start_c;;
230         "stop_c") stop_c;;
231         "restart_c") stop_c; start_c;;
232         "start_osrf") start_perl; start_c; start_python;;
233         "stop_osrf") stop_python; stop_c; start_perl;;
234         "restart_osrf") stop_python; stop_c; stop_perl; start_perl; start_c; start_python;;
235         "stop_all") stop_python; stop_c; stop_perl; stop_router;;
236         "start_all") start_router; start_perl; start_c; start_python;;
237         "restart_all") stop_python; stop_c; stop_perl; stop_router; start_router; start_perl; start_c; start_python;;
238         *) usage;;
239 esac;
240
241
242