]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
Cruft removal; there should be no outwardly visible effects.
[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_c
52     stop_c
53     restart_c
54     start_osrf
55     stop_osrf
56     restart_osrf
57     stop_all
58     start_all
59     restart_all
60
61 Examples:
62   $0 -a restart_all
63   $0 -l -c opensrf_core.xml -a restart_all
64
65 EOF
66 }
67
68 # ---------------------------------------------------------------------------
69 # Load the command line options and set the global vars
70 # ---------------------------------------------------------------------------
71 while getopts  "a:d:c:lh" flag; do
72         case $flag in   
73                 "a")            OPT_ACTION="$OPTARG";;
74                 "c")            OPT_CONFIG="$OPTARG";;
75                 "d")            OPT_PID_DIR="$OPTARG";;
76                 "l")            export OSRF_HOSTNAME="localhost";;
77                 "h"|*)  usage;;
78         esac;
79 done
80
81 OSRF_CONFIG="@bindir@/osrf_config"
82 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
83
84 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
85 if [ ! -r "$OPT_CONFIG" ]; then
86         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
87         exit 1;
88 fi;
89 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run;
90 [ -z "$OPT_ACTION" ] && usage;
91
92 PID_ROUTER="$OPT_PID_DIR/router.pid";
93 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
94 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
95
96
97 # ---------------------------------------------------------------------------
98 # Utility code for checking the PID files
99 # ---------------------------------------------------------------------------
100 do_action() {
101
102         action="$1"; 
103         pidfile="$2";
104         item="$3"; 
105
106         if [ $action = "start" ]; then
107
108                 if [ -e $pidfile ]; then
109                         pid=$(cat $pidfile);
110                         echo "$item already started : $pid";
111                         return 0;
112                 fi;
113                 echo "Starting $item";
114         fi;
115
116         if [ $action = "stop" ]; then
117
118                 if [ ! -e $pidfile ]; then
119                         echo "$item not running";
120                         return 0;
121                 fi;
122
123         while read pid; do
124             if [ -n "$pid" ]; then
125                 echo "Stopping $item process $pid..."
126                 kill -s INT $pid
127             fi;
128         done < $pidfile;
129                 rm -f $pidfile;
130
131         fi;
132
133         return 0;
134 }
135
136
137 # ---------------------------------------------------------------------------
138 # Start / Stop functions
139 # ---------------------------------------------------------------------------
140
141
142 start_router() {
143         do_action "start" $PID_ROUTER "OpenSRF Router";
144         opensrf_router $OPT_CONFIG routers
145     sleep 2; # give the router procs time to fork and appear in ps
146         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
147         echo $pid > $PID_ROUTER;
148         return 0;
149 }
150
151 stop_router() {
152         do_action "stop" $PID_ROUTER "OpenSRF Router";
153         return 0;
154 }
155
156 start_perl() {
157     echo "Starting OpenSRF Perl";
158     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
159         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
160         return 0;
161 }
162
163 stop_perl() {
164     echo "Stopping OpenSRF Perl";
165     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
166         sleep 1;
167         return 0;
168 }
169
170 start_c() {
171         host=$OSRF_HOSTNAME
172         if [ "_$host" = "_" ]; then
173                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
174         fi;
175
176         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
177         opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
178         return 0;
179 }
180
181 stop_c() {
182         do_action "stop" $PID_OSRF_C "OpenSRF C";
183         sleep 1;
184         return 0;
185 }
186
187
188
189 # ---------------------------------------------------------------------------
190 # Do the requested action
191 # ---------------------------------------------------------------------------
192 case $OPT_ACTION in
193         "start_router") start_router;;
194         "stop_router") stop_router;;
195         "restart_router") stop_router; start_router;;
196         "start_perl") start_perl;;
197         "stop_perl") stop_perl;;
198         "restart_perl") stop_perl; start_perl;;
199         "start_c") start_c;;
200         "stop_c") stop_c;;
201         "restart_c") stop_c; start_c;;
202         "start_osrf") start_perl; start_c;;
203         "stop_osrf") stop_perl; stop_c;;
204         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
205         "stop_all") stop_c; stop_perl; stop_router;;
206         "start_all") start_router; start_perl; start_c;;
207         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
208         *) usage;;
209 esac;
210
211
212