Package lamson :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module lamson.utils

  1  """ 
  2  Mostly utility functions Lamson uses internally that don't 
  3  really belong anywhere else in the modules.  This module 
  4  is kind of a dumping ground, so if you find something that 
  5  can be improved feel free to work up a patch. 
  6  """ 
  7   
  8  from lamson import server, routing 
  9  import sys, os 
 10  import logging 
 11  import daemon 
 12  from daemon import pidlockfile 
 13  import imp 
 14  import signal 
 15   
 16   
17 -def import_settings(boot_also, from_dir=None, boot_module="config.boot"):
18 """Used to import the settings in a Lamson project.""" 19 if from_dir: 20 sys.path.append(from_dir) 21 22 settings = __import__("config.settings", globals(), locals()).settings 23 24 if boot_also: 25 __import__(boot_module, globals(), locals()) 26 27 return settings
28 29
30 -def daemonize(pid, chdir, chroot, umask, files_preserve=None, do_open=True):
31 """ 32 Uses python-daemonize to do all the junk needed to make a 33 server a server. It supports all the features daemonize 34 has, except that chroot probably won't work at all without 35 some serious configuration on the system. 36 """ 37 context = daemon.DaemonContext() 38 context.pidfile = pidlockfile.PIDLockFile(pid) 39 context.stdout = open(os.path.join(chdir, "logs/lamson.out"),"a+") 40 context.stderr = open(os.path.join(chdir, "logs/lamson.err"),"a+") 41 context.files_preserve = files_preserve or [] 42 context.working_directory = os.path.expanduser(chdir) 43 44 if chroot: 45 context.chroot_directory = os.path.expanduser(chroot) 46 if umask != False: 47 context.umask = umask 48 49 if do_open: 50 context.open() 51 52 return context
53
54 -def drop_priv(uid, gid):
55 """ 56 Changes the uid/gid to the two given, you should give utils.daemonize 57 0,0 for the uid,gid so that it becomes root, which will allow you to then 58 do this. 59 """ 60 logging.debug("Dropping to uid=%d, gid=%d", uid, gid) 61 daemon.daemon.change_process_owner(uid, gid) 62 logging.debug("Now running as uid=%d, gid=%d", os.getgid(), os.getuid())
63 64 65
66 -def make_fake_settings(host, port):
67 """ 68 When running as a logging server we need a fake settings module to work with 69 since the logging server can be run in any directory, so there may not be 70 a config/settings.py file to import. 71 """ 72 logging.basicConfig(filename="logs/logger.log", level=logging.DEBUG) 73 routing.Router.load(['lamson.handlers.log', 'lamson.handlers.queue']) 74 settings = imp.new_module('settings') 75 settings.receiver = server.SMTPReceiver(host, port) 76 settings.relay = None 77 logging.info("Logging mode enabled, will not send email to anyone, just log.") 78 79 return settings
80
81 -def check_for_pid(pid, force):
82 """Checks if a pid file is there, and if it is sys.exit. If force given 83 then it will remove the file and not exit if it's there.""" 84 if os.path.exists(pid): 85 if not force: 86 print "PID file %s exists, so assuming Lamson is running. Give -FORCE to force it to start." % pid 87 sys.exit(1) 88 return # for unit tests mocking sys.exit 89 else: 90 os.unlink(pid)
91 92
93 -def start_server(pid, force, chroot, chdir, uid, gid, umask, settings_loader):
94 """ 95 Starts the server by doing a daemonize and then dropping priv 96 accordingly. It will only drop to the uid/gid given if both are given. 97 """ 98 check_for_pid(pid, force) 99 100 daemonize(pid, chdir, chroot, umask, files_preserve=[]) 101 102 sys.path.append(os.getcwd()) 103 104 settings = settings_loader() 105 106 if uid and gid: 107 drop_priv(uid, gid) 108 elif uid or gid: 109 logging.warning("You probably meant to give a uid and gid, but you gave: uid=%r, gid=%r. Will not change to any user.", uid, gid) 110 111 settings.receiver.start()
112