Skip to content

Commit

Permalink
Do the config file management better.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedshaw committed Mar 31, 2013
1 parent ad2419f commit 671071e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
5 changes: 3 additions & 2 deletions examples/gevent_server.py
Expand Up @@ -8,6 +8,8 @@ def handle(socket, address):

class Service(server.Simple):

name = 'geventserver'

def before_jail(self, args):
from gevent.server import StreamServer

Expand All @@ -30,7 +32,6 @@ def shutdown(self, signal):
if __name__ == "__main__":

log.setup('/tmp/geventserver.log')
server = Service('geventserver', config_base=os.getcwd() + '/examples',
config_name='master')
server = Service(config_file=os.getcwd() + '/examples/master.conf')
server.run(sys.argv)

4 changes: 3 additions & 1 deletion examples/thread_server.py
Expand Up @@ -18,6 +18,8 @@ class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):

class ThreadDaemon(server.Simple):

name = 'threadserver'

def before_drop_privs(self, args):
HOST = "0.0.0.0"
if self.config:
Expand Down Expand Up @@ -52,6 +54,6 @@ def shutdown(self, signal):

if __name__ == "__main__":
# if you're on OSX then change this to whatever user nd group
server = ThreadDaemon("threadserver", uid='nobody', gid='nogroup')
server = ThreadDaemon(uid='nobody', gid='nogroup')
server.run(sys.argv)

30 changes: 18 additions & 12 deletions lust/server.py
Expand Up @@ -4,14 +4,14 @@

class Simple(object):

def __init__(self, name, run_base="/var/run", log_dir="/var/log",
name = None

def __init__(self, run_base="/var/run", log_dir="/var/log",
pid_file_path="/var/run",
uid="nobody", gid="nogroup", config_base="/etc",
config_name=None):
self.name = name
self.config_name = config_name or name
uid="nobody", gid="nogroup", config_file=None):
assert self.name, "You must set the service's name."

self.config_file = os.path.join(config_base, self.config_name + ".conf")
self.config_file = config_file or os.path.join('/etc', self.config + ".conf")
log.debug("Config file at %s" % self.config_file)

if os.path.exists(self.config_file):
Expand All @@ -22,13 +22,13 @@ def __init__(self, name, run_base="/var/run", log_dir="/var/log",
log.warn("No config file at %s, using defaults." % self.config_file)
self.config = {}

self.run_dir = self.config.get(name + '.run_dir',
self.run_dir = self.config.get(self.name + '.run_dir',
os.path.join(run_base, self.name))
self.pid_path = self.config.get(name + '.pid_path', pid_file_path)
self.log_file = self.config.get(name + '.log_file',
self.pid_path = self.config.get(self.name + '.pid_path', pid_file_path)
self.log_file = self.config.get(self.name + '.log_file',
os.path.join(log_dir, self.name + ".log"))
self.uid = self.config.get(name + '.uid', uid)
self.gid = self.config.get(name + '.gid', gid)
self.uid = self.config.get(self.name + '.uid', uid)
self.gid = self.config.get(self.name + '.gid', gid)
log.debug("UID and GID are %s:%s" % (self.uid, self.gid))

self.unum, self.gnum = unix.get_user_info(self.uid, self.gid)
Expand Down Expand Up @@ -119,4 +119,10 @@ def run(self, args):
else:
log.error("Invalid command: %s. Commands are: start, stop, reload, status.")
sys.exit(1)


def get(self, name):
"""Simple convenience method that just uses the service's configured
name to get a config value."""

return self.config.get(self.name + '.' + name, None)

2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -10,7 +10,7 @@
'url': 'http://pypi.python.org/pypi/python-lust',
'download_url': 'http://pypi.python.org/pypi/python-lust',
'author_email': 'zedshaw@zedshaw.com',
'version': '1.0.6',
'version': '1.0.7',
'scripts': [],
'install_requires': [],
'packages': ['lust'],
Expand Down
4 changes: 3 additions & 1 deletion tests/simple_test_server.py
Expand Up @@ -10,6 +10,8 @@

class SimpleDaemon(server.Simple):

name = "tests.simple_test_server"

def before_daemonize(self, args):
with open("/tmp/before_daemonize.txt", "w") as f:
f.write("%r" % args)
Expand All @@ -34,7 +36,7 @@ def shutdown(self, signal):

if __name__ == "__main__":
# if you're on OSX then change this to whatever user nd group
server = SimpleDaemon("simpledaemon", uid='nobody', gid='nogroup',
server = SimpleDaemon(uid='nobody', gid='nogroup',
pid_file_path=RUN_DIR, run_base=RUN_DIR, log_dir=RUN_DIR)
server.run(sys.argv)

0 comments on commit 671071e

Please sign in to comment.