Patch from Kevin Beswick to use autotools for OpenSRF configuration and build
[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=`find $BASEDIR -name osrf_config`
91
92 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
93 if [ ! -r "$OPT_CONFIG" ]; then
94         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
95         exit 1;
96 fi;
97 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --sysconfdir`/run;
98 [ -z "$OPT_ACTION" ] && usage;
99
100 PID_ROUTER="$OPT_PID_DIR/router.pid";
101 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
102 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
103
104
105 # ---------------------------------------------------------------------------
106 # Utility code for checking the PID files
107 # ---------------------------------------------------------------------------
108 function do_action {
109
110         action="$1"; 
111         pidfile="$2";
112         item="$3"; 
113
114         if [ $action == "start" ]; then
115
116                 if [ -e $pidfile ]; then
117                         pid=$(cat $pidfile);
118                         echo "$item already started : $pid";
119                         return 0;
120                 fi;
121                 echo "Starting $item";
122         fi;
123
124         if [ $action == "stop" ]; then
125
126                 if [ ! -e $pidfile ]; then
127                         echo "$item not running";
128                         return 0;
129                 fi;
130
131         while read pid; do
132             echo "Stopping $item process $pid..."
133             kill -s INT $pid
134         done < $pidfile;
135                 rm -f $pidfile;
136
137         fi;
138
139         return 0;
140 }
141
142
143 # ---------------------------------------------------------------------------
144 # Start / Stop functions
145 # ---------------------------------------------------------------------------
146
147
148 function start_router {
149         do_action "start" $PID_ROUTER "OpenSRF Router";
150         opensrf_router $OPT_CONFIG routers
151         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
152         echo $pid > $PID_ROUTER;
153         return 0;
154 }
155
156 function stop_router {
157         do_action "stop" $PID_ROUTER "OpenSRF Router";
158         return 0;
159 }
160
161 function start_perl {
162         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
163         perl -MOpenSRF::System="$OPT_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
164         sleep 3;
165         pid=$(ps ax | grep "OpenSRF System" | grep -v grep | grep -v "System-C"| awk '{print $1}')
166         echo $pid > $PID_OSRF_PERL;
167         return 0;
168 }
169
170 function stop_perl {
171         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
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