Re-designed the startup script to be more cross platform and robust.

This commit is contained in:
Greg Taylor 2009-01-13 17:06:03 +00:00
parent 4cb2617324
commit abe17a2965
3 changed files with 109 additions and 62 deletions

102
game/evennia.py Executable file
View file

@ -0,0 +1,102 @@
#!/usr/bin/env python
"""
EVENNIA SERVER STARTUP SCRIPT
Sets the appropriate environmental variables and launches the server
process. Run the script with the -h flag to see usage information.
"""
import getopt # for parsing command line arguments
from optparse import OptionParser
import os # for OS related fonctions
import sys # for getting command line arguments
from subprocess import Popen, call
# Set the Python path up so we can get to settings.py from here.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
SERVER_PY_FILE = os.path.join(settings.SRC_DIR, 'server.py')
# Add this to the environmental variable for the 'twistd' command.
os.environ['PYTHONPATH'] = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def cycle_logfile():
"""
Move the old log file to evennia.log (by default).
"""
if os.path.exists(settings.DEFAULT_LOG_FILE):
os.rename(settings.DEFAULT_LOG_FILE,
settings.DEFAULT_LOG_FILE+'.old')
def start_daemon(parser, options, args):
"""
Start the server in daemon mode. This means that all logging output will
be directed to logs/evennia.log by default, and the process will be
backgrounded.
"""
if os.path.exists('twistd.pid'):
print "A twistd.pid file exists in the current directory, which suggests that the server is already running."
sys.exit()
print 'Starting in daemon mode...'
# Move the old evennia.log file out of the way.
cycle_logfile()
# Start it up
Popen(['twistd',
'--logfile=%s' % settings.DEFAULT_LOG_FILE,
'--python=%s' % SERVER_PY_FILE])
def start_interactive(parser, options, args):
"""
Start in interactive mode, which means the process is foregrounded and
all logging output is directed to stdout.
"""
print 'Starting in interactive mode...'
call(['twistd',
'-n',
'--python=%s' % SERVER_PY_FILE])
def stop_server(parser, options, args):
"""
Gracefully stop the server process.
"""
if os.name == 'posix':
if os.path.exists('twistd.pid'):
print 'Stoping The Server'
f = open('twistd.pid', 'r')
pid = f.read()
Popen(['kill', pid])
else:
print "No twistd.pid file exists, the server doesn't appear to be running."
elif os.name == 'nt':
print 'TODO not implented'
else:
print 'Unknown OS delected, can not kill'
def main():
"""
Beginning of the program logic.
"""
parser = OptionParser(usage="%prog [options] <start|stop>",
description="")
parser.add_option('-i', '--interactive', action='store_true',
dest='interactive', default=False,
help='Start in interactive mode')
parser.add_option('-d', '--daemon', action='store_false',
dest='interactive',
help='Start in daemon mode (default)')
(options, args) = parser.parse_args()
if "start" in args:
if options.interactive:
start_interactive(parser, options, args)
else:
start_daemon(parser, options, args)
elif "stop" in args:
stop_server(parser, options, args)
else:
parser.print_help()
if __name__ == '__main__':
main()

View file

@ -1,62 +0,0 @@
#!/usr/bin/env python
import getopt # for parsing command line arguments
import os # for OS related fonctions
import sys # for getting command line arguments
def init():
"""main fonction for configuring tne system for start-up"""
print 'Configuring evirontment variables'
os.putenv('PYTHONPATH','..')
os.putenv('DJANGO_SETTINGS_MODULE','game.settings')
print 'Renaming old logs as .old'
os.rename('logs/evennia.log','logs/evennia.log.old')
# no error checking for rename for now
def start_daemon():
"""start the server in daemon mode by using os.sysytem to run twistd"""
print 'Starting in Daemon Modea'
os.system('twistd --logfile=logs/evennia.log --python=../src/server.py')
def start_interactive():
"""start in inretactive mode by using os.sysytem to run twistd. this is default for windows for now"""
print 'Starting in Interactive Mode'
os.system('twistd --logfile=logs/evennia.log --python=../src/server.py')
def stop_server():
"""kill the running server this fonction is unix only,
windows impletation will come with subprocess module for everything."""
if os.name == 'posix':
print 'Stoping The Server'
os.system('kill `cat twistd.pid`')
elif os.name == 'nt':
print 'TODO not implented'
else:
print 'Unknown OS delected, can not kill'
def usage():
print 'Sets the appropriate environmental variables and launches the server\nprocess. Run without flags for daemon mode.\n\nFLAGS\n -i Interactive mode\n -d Daemon mode\n -s Stop the running server\n -h Show help display\n, No Default Behavour Exits',
def main(argv):
""" main program body """
try:
opts, args = getopt.getopt(argv, "hids",[help])
except getopt.getopterror:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h","--help"):
usage()
sys.exit()
elif opt == '-i':
start_interactive()
elif opt == '-d':
start_daemon()
elif opt == '-s':
stop_server()
else:
usage()
if __name__ == '__main__':
main(sys.argv[1:])

View file

@ -33,6 +33,13 @@ BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Path to the game directory (containing the database file if using sqlite).
GAME_DIR = os.path.join(BASE_PATH, 'game')
# Logging paths
LOG_DIR = os.path.join(GAME_DIR, 'logs')
DEFAULT_LOG_FILE = os.path.join(LOG_DIR, 'evennia.log')
# Path to the src directory containing the bulk of the codebase's code.
SRC_DIR = os.path.join(BASE_PATH, 'src')
# Absolute path to the directory that holds media (no trailing slash).
# Example: "/home/media/media.lawrence.com"
MEDIA_ROOT = os.path.join(GAME_DIR, 'web', 'media')