try finding osrf_config in the users PATH first to prevent the use of 'find' when...
[OpenSRF.git] / bin / osrf_ctl.sh
1 #!/bin/bash
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
14 OPT_ACTION=""
15 OPT_CONFIG=""
16 OPT_PID_DIR=""
17
18 # ---------------------------------------------------------------------------
19 # Make sure we're running as the correct user
20 # ---------------------------------------------------------------------------
21 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
22
23
24 function usage {
25         echo "";
26         echo "usage: $0 [OPTION]... -c <c_config> -a <action>";
27         echo "";
28         echo "Mandatory parameters:";
29         echo -e "  -a\t\taction to perform";
30         echo "";
31         echo "Optional parameters:";
32         echo -e "  -c\t\tfull path to C configuration file (opensrf_core.xml)";
33         echo -e "  -d\t\tstore PID files in this directory";
34         echo -e "  -l\t\taccept 'localhost' as the fully-qualified domain name";
35         echo "";
36         echo "Actions include:";
37         echo -e "\tstart_router"
38         echo -e "\tstop_router"
39         echo -e "\trestart_router"
40         echo -e "\tstart_perl"
41         echo -e "\tstop_perl"
42         echo -e "\trestart_perl"
43         echo -e "\tstart_c"
44         echo -e "\tstop_c"
45         echo -e "\trestart_c"
46         echo -e "\tstart_osrf"
47         echo -e "\tstop_osrf"
48         echo -e "\trestart_osrf"
49         echo -e "\tstop_all" 
50         echo -e "\tstart_all"
51         echo -e "\trestart_all"
52         echo "";
53         echo "Examples:";
54         echo "  $0 -a restart_all";
55         echo "  $0 -l -c opensrf_core.xml -a restart_all";
56         echo "";
57         exit;
58 }
59
60 # Get root directory of this script
61 function basepath {
62         BASEDIR=""
63         script_path="$1"
64         IFS="/"
65         for p in $script_path
66         do
67                 if [ -z "$BASEDIR" ] && [ -n "$p" ]; then
68                         BASEDIR="$p"
69                 fi;
70         done
71         BASEDIR="/$BASEDIR"
72         IFS=
73 }
74
75 basepath $0
76
77 # ---------------------------------------------------------------------------
78 # Load the command line options and set the global vars
79 # ---------------------------------------------------------------------------
80 while getopts  "a:d:c:lh" flag; do
81         case $flag in   
82                 "a")            OPT_ACTION="$OPTARG";;
83                 "c")            OPT_CONFIG="$OPTARG";;
84                 "d")            OPT_PID_DIR="$OPTARG";;
85                 "l")            export OSRF_HOSTNAME="localhost";;
86                 "h"|*)  usage;;
87         esac;
88 done
89
90 OSRF_CONFIG=`which osrf_config`
91 [ -z "$OSRF_CONFIG" ] && OSRF_CONFIG=`find $BASEDIR -name osrf_config`
92
93 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
94 if [ ! -r "$OPT_CONFIG" ]; then
95         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
96         exit 1;
97 fi;
98 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run;
99 [ -z "$OPT_ACTION" ] && usage;
100
101 PID_ROUTER="$OPT_PID_DIR/router.pid";
102 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
103 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
104
105
106 # ---------------------------------------------------------------------------
107 # Utility code for checking the PID files
108 # ---------------------------------------------------------------------------
109 function do_action {
110
111         action="$1"; 
112         pidfile="$2";
113         item="$3"; 
114
115         if [ $action == "start" ]; then
116
117                 if [ -e $pidfile ]; then
118                         pid=$(cat $pidfile);
119                         echo "$item already started : $pid";
120                         return 0;
121                 fi;
122                 echo "Starting $item";
123         fi;
124
125         if [ $action == "stop" ]; then
126
127                 if [ ! -e $pidfile ]; then
128                         echo "$item not running";
129                         return 0;
130                 fi;
131
132         while read pid; do
133             echo "Stopping $item process $pid..."
134             kill -s INT $pid
135         done < $pidfile;
136                 rm -f $pidfile;
137
138         fi;
139
140         return 0;
141 }
142
143
144 # ---------------------------------------------------------------------------
145 # Start / Stop functions
146 # ---------------------------------------------------------------------------
147
148
149 function start_router {
150         do_action "start" $PID_ROUTER "OpenSRF Router";
151         opensrf_router $OPT_CONFIG routers
152         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
153         echo $pid > $PID_ROUTER;
154         return 0;
155 }
156
157 function stop_router {
158         do_action "stop" $PID_ROUTER "OpenSRF Router";
159         return 0;
160 }
161
162 function start_perl {
163     echo "Starting OpenSRF Perl";
164     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
165         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
166         return 0;
167 }
168
169 function stop_perl {
170     echo "Stopping OpenSRF Perl";
171     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
172         sleep 1;
173         return 0;
174 }
175
176 function start_c {
177         host=$OSRF_HOSTNAME
178         if [ "_$host" == "_" ]; then
179                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
180         fi;
181
182         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
183         opensrf-c $host $OPT_CONFIG opensrf;
184         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
185         echo $pid > "$PID_OSRF_C";
186         return 0;
187 }
188
189 function stop_c {
190         do_action "stop" $PID_OSRF_C "OpenSRF C";
191         sleep 1;
192         return 0;
193 }
194
195
196
197 # ---------------------------------------------------------------------------
198 # Do the requested action
199 # ---------------------------------------------------------------------------
200 case $OPT_ACTION in
201         "start_router") start_router;;
202         "stop_router") stop_router;;
203         "restart_router") stop_router; start_router;;
204         "start_perl") start_perl;;
205         "stop_perl") stop_perl;;
206         "restart_perl") stop_perl; start_perl;;
207         "start_c") start_c;;
208         "stop_c") stop_c;;
209         "restart_c") stop_c; start_c;;
210         "start_osrf") start_perl; start_c;;
211         "stop_osrf") stop_perl; stop_c;;
212         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
213         "stop_all") stop_c; stop_perl; stop_router;;
214         "start_all") start_router; start_perl; start_c;;
215         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
216         *) usage;;
217 esac;
218
219
220