summaryrefslogtreecommitdiff
path: root/paste/app_setup.py
diff options
context:
space:
mode:
authorIan Bicking <ian@ianbicking.org>2005-05-25 18:23:10 +0000
committerIan Bicking <ian@ianbicking.org>2005-05-25 18:23:10 +0000
commit2d05a6f63be942dc63207ded94cf08df4369cfed (patch)
tree14e2658734ab548597f406d80c1b48dd6469113c /paste/app_setup.py
parent5fefef14cbc213ada2cf71c47ce336623c2ca8da (diff)
downloadpaste-git-2d05a6f63be942dc63207ded94cf08df4369cfed.tar.gz
Added --daemon and related options
Diffstat (limited to 'paste/app_setup.py')
-rw-r--r--paste/app_setup.py91
1 files changed, 90 insertions, 1 deletions
diff --git a/paste/app_setup.py b/paste/app_setup.py
index 6516c1c..06ef3cf 100644
--- a/paste/app_setup.py
+++ b/paste/app_setup.py
@@ -270,10 +270,98 @@ class CommandServe(Command):
name = 'serve'
summary = 'Run server'
parser = standard_parser(simulate=False)
+ parser.add_option(
+ '--daemon',
+ help="Run in daemon (background) mode",
+ dest="daemon",
+ action="store_true")
+ parser.add_option(
+ '--user',
+ metavar="USER_OR_UID",
+ help="User to run as (only works if started as root)",
+ dest="user")
+ parser.add_option(
+ '--group',
+ metavar="GROUP_OR_GID",
+ help="Group to run as (only works if started as root)",
+ dest="group")
+ parser.add_option(
+ '--pid-file',
+ metavar="FILENAME",
+ help="Write PID to FILENAME",
+ dest="pid_file")
+ parser.add_option(
+ '--log-file',
+ metavar="FILENAME",
+ help="File to write stdout/stderr to (other log files may still be generated by the application)",
+ dest="log_file")
def command(self):
+ conf = self.config
+ verbose = conf.get('verbose') or 0
+ if conf.get('daemon'):
+ # We must enter daemon mode!
+ if verbose > 1:
+ print 'Entering daemon mode'
+ pid = os.fork()
+ if pid:
+ sys.exit()
+ # Always record PID and output when daemonized
+ if not conf.get('pid_file'):
+ conf['pid_file'] = 'server.pid'
+ if not conf.get('log_file'):
+ conf['log_file'] = 'server.log'
+ if conf.get('pid_file'):
+ # @@: We should check if the pid file exists and has
+ # an active process in it
+ if verbose > 1:
+ print 'Writing pid %s to %s' % (os.getpid(), conf['pid_file'])
+ f = open(conf['pid_file'], 'w')
+ f.write(str(os.getpid()))
+ f.close()
+ if conf.get('log_file'):
+ if verbose > 1:
+ print 'Logging to %s' % conf['log_file']
+ f = open(conf['log_file'], 'a', 1) # 1==line buffered
+ sys.stdout = sys.stderr = f
+ f.write('-'*20
+ + ' Starting server PID: %s ' % os.getpid()
+ + '-'*20 + '\n')
+ self.change_user_group(conf.get('user'), conf.get('group'))
sys.exit(server.run_commandline(self.args))
+ def change_user_group(self, user, group):
+ if not user and not group:
+ return
+ uid = gid = None
+ if group:
+ try:
+ gid = int(group)
+ except ValueError:
+ import grp
+ try:
+ entry = grp.getgrnam(group)
+ except KeyError:
+ raise KeyError(
+ "Bad group: %r; no such group exists" % group)
+ gid = entry[2]
+ try:
+ uid = int(user)
+ except ValueError:
+ import pwd
+ try:
+ entry = pwd.getpwnam(user)
+ except KeyError:
+ raise KeyError(
+ "Bad username: %r; no such user exists" % user)
+ if not gid:
+ gid = entry[3]
+ uid = entry[2]
+ if gid:
+ os.setgid(gid)
+ if uid:
+ os.setuid(uid)
+
def parse_args(self, args):
# Unlike most commands, this takes arbitrary options and folds
# them into the configuration
@@ -287,8 +375,9 @@ class CommandServe(Command):
self.list_servers(conf)
sys.exit()
CONFIG.push_process_config(conf)
- sys.exit(server.run_server(conf, app))
self.config = conf
+ self.options = None
+ self.args = []
def help(self, config):
# Here we make a fake parser just to get the help