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