Patch from Scott McKellar:
[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 -e "  -c\t\tfull path to C configuration file (opensrf_core.xml)";
20         echo "";
21         echo "Optional parameters:";
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 -c opensrf_core.xml -a restart_all";
44         echo "  $0 -l -c opensrf_core.xml -a restart_all";
45         echo "";
46         exit;
47 }
48
49
50 # ---------------------------------------------------------------------------
51 # Load the command line options and set the global vars
52 # ---------------------------------------------------------------------------
53 while getopts  "c:a:d:lh" flag; do
54         case $flag in   
55                 "a")            OPT_ACTION="$OPTARG";;
56                 "c")            OPT_CONFIG="$OPTARG";;
57                 "d")            OPT_PID_DIR="$OPTARG";;
58                 "l")            export OSRF_HOSTNAME="localhost";;
59                 "h"|*)  usage;;
60         esac;
61 done
62
63
64 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
65 [ -z "$OPT_ACTION" ] && usage;
66
67 PID_ROUTER="$OPT_PID_DIR/router.pid";
68 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
69 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
70
71
72 # ---------------------------------------------------------------------------
73 # Utility code for checking the PID files
74 # ---------------------------------------------------------------------------
75 function do_action {
76
77         action="$1"; 
78         pidfile="$2";
79         item="$3"; 
80
81         if [ $action == "start" ]; then
82
83                 if [ -e $pidfile ]; then
84                         pid=$(cat $pidfile);
85                         echo "$item already started : $pid";
86                         return 0;
87                 fi;
88                 echo "Starting $item";
89         fi;
90
91         if [ $action == "stop" ]; then
92
93                 if [ ! -e $pidfile ]; then
94                         echo "$item not running";
95                         return 0;
96                 fi;
97
98                 pid=$(cat $pidfile);
99                 echo "Stopping $item : $pid";
100                 kill -s INT $pid 2> /dev/null;
101         sleep 1;
102                 kill -9 $pid 2> /dev/null;
103                 rm -f $pidfile;
104
105         fi;
106
107         return 0;
108 }
109
110
111 # ---------------------------------------------------------------------------
112 # Start / Stop functions
113 # ---------------------------------------------------------------------------
114
115
116 function start_router {
117         do_action "start" $PID_ROUTER "OpenSRF Router";
118         opensrf_router $OPT_CONFIG router
119         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
120         echo $pid > $PID_ROUTER;
121         return 0;
122 }
123
124 function stop_router {
125         do_action "stop" $PID_ROUTER "OpenSRF Router";
126         return 0;
127 }
128
129 function start_perl {
130         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
131         perl -MOpenSRF::System="$OPT_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
132         sleep 3;
133         pid=$(ps ax | grep "OpenSRF System" | grep -v grep | grep -v "System-C"| awk '{print $1}')
134         echo $pid > $PID_OSRF_PERL;
135         return 0;
136 }
137
138 function stop_perl {
139         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
140         sleep 1;
141         return 0;
142 }
143
144 function start_c {
145         host=$OSRF_HOSTNAME
146         if [ "_$host" == "_" ]; then
147                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
148         fi;
149
150         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
151         opensrf-c $host $OPT_CONFIG opensrf;
152         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
153         echo $pid > "$PID_OSRF_C";
154         return 0;
155 }
156
157 function stop_c {
158         do_action "stop" $PID_OSRF_C "OpenSRF C";
159         killall -9 opensrf-c  # hack for now to force kill all C services
160         sleep 1;
161         return 0;
162 }
163
164
165
166 # ---------------------------------------------------------------------------
167 # Do the requested action
168 # ---------------------------------------------------------------------------
169 case $OPT_ACTION in
170         "start_router") start_router;;
171         "stop_router") stop_router;;
172         "restart_router") stop_router; start_router;;
173         "start_perl") start_perl;;
174         "stop_perl") stop_perl;;
175         "restart_perl") stop_perl; start_perl;;
176         "start_c") start_c;;
177         "stop_c") stop_c;;
178         "restart_c") stop_c; start_c;;
179         "start_osrf") start_perl; start_c;;
180         "stop_osrf") stop_perl; stop_c;;
181         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
182         "stop_all") stop_c; stop_perl; stop_router;;
183         "start_all") start_router; start_perl; start_c;;
184         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
185         *) usage;;
186 esac;
187
188
189