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
32 Print help for the OpenSRF Python application process manager
36 Manage OpenSRF application processes
40 list_all -- List all services
41 start -- Start a service
42 stop -- stop a service
43 restart -- restart a service
44 start_all -- Start all services
45 stop_all -- Stop all services
46 restart_all -- Restart all services
52 The OpenSRF config file
55 The OpenSRF config file context
58 The location of application PID files. Default is @PID_DIR@/run/opensrf
61 If set, run in daemon (background) mode. This creates a PID
62 file for managing the process.
65 If set, run in 'localhost' mode
72 def get_pid_file(service):
74 Return the PID file for the named service
77 return "%s/%s.pid" % (pid_dir, service)
81 Initialize the Python service environment
88 # connect to the OpenSRF network
89 osrf.system.System.net_connect(
90 config_file = config_file, config_context = config_ctx)
91 except dns.resolver.NXDOMAIN:
93 ERROR: Could not initialize the OpenSRF Python environment. A DNS query
94 failed to resolve the network address of the XMPP server.
101 domain = osrf.conf.get('domain')
104 osrf.set.load(domain)
105 except osrf.net.XMPPNoRecipient:
106 print "* Unable to communicate with opensrf.settings. Giving up..."
109 settings = osrf.set.get('apps')
110 activeapps = osrf.set.get('activeapps')
112 for key in (set(settings.keys()) & set(activeapps['appname'])):
114 if isinstance(svc, dict) and 'language' in svc and svc['language'] == 'python':
117 def do_start(service):
119 Start the named Python service
122 pidfile = get_pid_file(service)
124 if service not in services:
125 print "* service %s is not a 'python' application" % service
128 if os.path.exists(pidfile):
130 pid_fd = open(pidfile, 'r')
131 alive = os.getsid(int(pid_fd.read()))
132 print "* service %s already running" % service
137 print "* starting %s" % service
141 if osrf.system.System.daemonize(False):
142 return # parent process returns
145 pid_fd = open(pidfile, 'w')
146 pid_fd.write(str(os.getpid()))
149 svc_settings = services[service]
151 osrf.app.Application.load(service, svc_settings['implementation'])
152 osrf.app.Application.register_sysmethods()
153 osrf.app.Application.application.global_init()
155 controller = osrf.server.Controller(service)
156 controller.max_requests = svc_settings['unix_config']['max_requests']
157 controller.max_children = svc_settings['unix_config']['max_children']
158 controller.min_children = svc_settings['unix_config']['min_children']
159 controller.keepalive = svc_settings['keepalive']
166 List all Python services listed in the OpenSRF configuration file
168 for service in services.keys():
173 Start all Python services listed in the OpenSRF configuration file
176 # You can't start more than one service without daemonizing
180 print "* starting all services for %s " % domain
181 for service in services.keys():
186 Stop all Python services listed in the OpenSRF configuration file
189 print "* stopping all services for %s " % domain
190 for service in services.keys():
193 def do_stop(service):
195 Stop the named Python service
198 pidfile = get_pid_file(service)
200 if not os.path.exists(pidfile):
201 print "* %s is not running" % service
204 print "* stopping %s" % service
206 pid_fd = open(pidfile)
210 os.kill(int(pid), signal.SIGTERM)
215 # -----------------------------------------------------
217 # Parse the command line options
218 ops, args = None, None
220 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dhl')
221 except getopt.GetoptError, e:
222 print '* %s' % str(e)
227 if '-a' not in options:
230 action = options['-a']
232 config_file = options.get('-f', '@CONF_DIR@/opensrf_core.xml')
233 pid_dir = options.get('-p', '@PID_DIR@/run/opensrf')
235 service_name = options.get('-s')
236 config_ctx = options.get('-c', 'config.opensrf')
237 as_localhost = '-l' in options
238 as_daemon = '-d' in options
246 if action == 'start':
247 do_start(service_name)
249 elif action == 'stop':
250 do_stop(service_name)
252 elif action == 'restart':
253 do_stop(service_name)
254 do_start(service_name)
256 elif action == 'list_all':
259 elif action == 'start_all':
262 elif action == 'stop_all':
265 elif action == 'restart_all':
269 elif action == 'help':