]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh
Tinkering with macros.
[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 --localstatedir`/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     echo "Starting OpenSRF Perl";
163     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
164         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
165         return 0;
166 }
167
168 function stop_perl {
169     echo "Stopping OpenSRF Perl";
170     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
171         sleep 1;
172         return 0;
173 }
174
175 function start_c {
176         host=$OSRF_HOSTNAME
177         if [ "_$host" == "_" ]; then
178                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
179         fi;
180
181         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
182         opensrf-c $host $OPT_CONFIG opensrf;
183         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
184         echo $pid > "$PID_OSRF_C";
185         return 0;
186 }
187
188 function stop_c {
189         do_action "stop" $PID_OSRF_C "OpenSRF C";
190         sleep 1;
191         return 0;
192 }
193
194
195
196 # ---------------------------------------------------------------------------
197 # Do the requested action
198 # ---------------------------------------------------------------------------
199 case $OPT_ACTION in
200         "start_router") start_router;;
201         "stop_router") stop_router;;
202         "restart_router") stop_router; start_router;;
203         "start_perl") start_perl;;
204         "stop_perl") stop_perl;;
205         "restart_perl") stop_perl; start_perl;;
206         "start_c") start_c;;
207         "stop_c") stop_c;;
208         "restart_c") stop_c; start_c;;
209         "start_osrf") start_perl; start_c;;
210         "stop_osrf") stop_perl; stop_c;;
211         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
212         "stop_all") stop_c; stop_perl; stop_router;;
213         "start_all") start_router; start_perl; start_c;;
214         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
215         *) usage;;
216 esac;
217
218
219