2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008 Equinox Software, Inc.
4 # Bill Erickson <erickson@esilibrary.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # -----------------------------------------------------------------------
23 Provides an environment for managing OpenSRF services written in Python
26 import sys, getopt, os, signal
27 import osrf.system, osrf.server, osrf.app, osrf.set, osrf.json
31 Print help for the OpenSRF Python application process manager
35 Manage OpenSRF application processes
39 list_all -- List all services
40 start -- Start a service
41 stop -- stop a service
42 restart -- restart a service
43 start_all -- Start all services
44 stop_all -- Stop all services
45 restart_all -- Restart all services
51 The OpenSRF config file
54 The OpenSRF config file context
57 The location of application PID files. Default is @PID_DIR@/run/opensrf
60 If set, run in daemon (background) mode. This creates a PID
61 file for managing the process.
64 If set, run in 'localhost' mode
71 def get_pid_file(service):
73 Return the PID file for the named service
76 return "%s/%s.pid" % (pid_dir, service)
80 Initialize the Python service environment
86 # connect to the OpenSRF network
87 osrf.system.System.net_connect(
88 config_file = config_file, config_context = config_ctx)
93 domain = osrf.conf.get('domain')
97 except osrf.net.XMPPNoRecipient:
98 print "* Unable to communicate with opensrf.settings. Giving up..."
101 settings = osrf.set.get('apps')
102 activeapps = osrf.set.get('activeapps')
104 for key in (set(settings.keys()) & set(activeapps['appname'])):
106 if isinstance(svc, dict) and 'language' in svc and svc['language'] == 'python':
109 def do_start(service):
111 Start the named Python service
114 pidfile = get_pid_file(service)
116 if service not in services:
117 print "* service %s is not a 'python' application" % service
120 if os.path.exists(pidfile):
122 pid_fd = open(pidfile, 'r')
123 alive = os.getsid(int(pid_fd.read()))
124 print "* service %s already running" % service
129 print "* starting %s" % service
133 if osrf.system.System.daemonize(False):
134 return # parent process returns
137 pid_fd = open(pidfile, 'w')
138 pid_fd.write(str(os.getpid()))
141 svc_settings = services[service]
143 osrf.app.Application.load(service, svc_settings['implementation'])
144 osrf.app.Application.register_sysmethods()
145 osrf.app.Application.application.global_init()
147 controller = osrf.server.Controller(service)
148 controller.max_requests = svc_settings['unix_config']['max_requests']
149 controller.max_children = svc_settings['unix_config']['max_children']
150 controller.min_children = svc_settings['unix_config']['min_children']
151 controller.keepalive = svc_settings['keepalive']
158 List all Python services listed in the OpenSRF configuration file
160 for service in services.keys():
165 Start all Python services listed in the OpenSRF configuration file
168 # You can't start more than one service without daemonizing
172 print "* starting all services for %s " % domain
173 for service in services.keys():
178 Stop all Python services listed in the OpenSRF configuration file
181 print "* stopping all services for %s " % domain
182 for service in services.keys():
185 def do_stop(service):
187 Stop the named Python service
190 pidfile = get_pid_file(service)
192 if not os.path.exists(pidfile):
193 print "* %s is not running" % service
196 print "* stopping %s" % service
198 pid_fd = open(pidfile)
202 os.kill(int(pid), signal.SIGTERM)
207 # -----------------------------------------------------
209 # Parse the command line options
210 ops, args = None, None
212 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dhl')
213 except getopt.GetoptError, e:
214 print '* %s' % str(e)
219 if '-a' not in options:
222 action = options['-a']
224 config_file = options.get('-f', '@CONF_DIR@/opensrf_core.xml')
225 pid_dir = options.get('-p', '@PID_DIR@/run/opensrf')
227 service_name = options.get('-s')
228 config_ctx = options.get('-c', 'config.opensrf')
229 as_localhost = '-l' in options
230 as_daemon = '-d' in options
238 if action == 'start':
239 do_start(service_name)
241 elif action == 'stop':
242 do_stop(service_name)
244 elif action == 'restart':
245 do_stop(service_name)
246 do_start(service_name)
248 elif action == 'list_all':
251 elif action == 'start_all':
254 elif action == 'stop_all':
257 elif action == 'restart_all':
261 elif action == 'help':