R-2.9.12
This commit is contained in:
+32
-33
@@ -7,37 +7,37 @@ class LinuxService(ServiceBase):
|
||||
ServiceBase.__init__(self, name, label, stdout, stderr)
|
||||
self.pidfile = '/tmp/%s.pid' % name
|
||||
self.config_file = '/etc/%s.conf' % name
|
||||
|
||||
|
||||
def daemonize(self):
|
||||
"""
|
||||
do the UNIX double-fork magic, see Stevens' "Advanced
|
||||
do the UNIX double-fork magic, see Stevens' "Advanced
|
||||
Programming in the UNIX Environment" for details (ISBN 0201563177)
|
||||
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
|
||||
"""
|
||||
try:
|
||||
pid = os.fork()
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# exit first parent
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
|
||||
return
|
||||
|
||||
|
||||
# decouple from parent environment
|
||||
os.chdir("/")
|
||||
os.setsid()
|
||||
os.umask(0)
|
||||
|
||||
os.chdir("/")
|
||||
os.setsid()
|
||||
os.umask(0)
|
||||
|
||||
# do second fork
|
||||
try:
|
||||
pid = os.fork()
|
||||
try:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
# exit from second parent
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
sys.exit(0)
|
||||
except OSError, e:
|
||||
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
|
||||
return
|
||||
|
||||
|
||||
# redirect standard file descriptors
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
@@ -47,25 +47,25 @@ class LinuxService(ServiceBase):
|
||||
os.dup2(si.fileno(), sys.stdin.fileno())
|
||||
os.dup2(so.fileno(), sys.stdout.fileno())
|
||||
os.dup2(se.fileno(), sys.stderr.fileno())
|
||||
|
||||
|
||||
def getpid(self):
|
||||
# Check for a pidfile to see if the daemon already runs
|
||||
try:
|
||||
try:
|
||||
pf = file(self.pidfile,'r')
|
||||
pid = int(pf.read().strip())
|
||||
pf.close()
|
||||
except IOError:
|
||||
pid = None
|
||||
|
||||
|
||||
return pid
|
||||
|
||||
|
||||
def status(self):
|
||||
pid = self.getpid()
|
||||
if pid:
|
||||
return 'Service running with PID %s.' % pid
|
||||
else:
|
||||
return 'Service is not running.'
|
||||
|
||||
|
||||
def check_permissions(self):
|
||||
if not os.geteuid() == 0:
|
||||
return (False, 'This script must be run with root permissions.')
|
||||
@@ -77,12 +77,12 @@ class LinuxService(ServiceBase):
|
||||
Start the daemon
|
||||
"""
|
||||
pid = self.getpid()
|
||||
|
||||
|
||||
if pid:
|
||||
message = "Service already running under PID %s\n"
|
||||
sys.stderr.write(message % self.pidfile)
|
||||
return
|
||||
|
||||
|
||||
# Start the daemon
|
||||
self.daemonize()
|
||||
self.run()
|
||||
@@ -92,13 +92,13 @@ class LinuxService(ServiceBase):
|
||||
Stop the daemon
|
||||
"""
|
||||
pid = self.getpid()
|
||||
|
||||
|
||||
if not pid:
|
||||
message = "Service is not running\n"
|
||||
sys.stderr.write(message)
|
||||
return # not an error in a restart
|
||||
|
||||
# Try killing the daemon process
|
||||
# Try killing the daemon process
|
||||
try:
|
||||
while 1:
|
||||
os.kill(pid, SIGTERM)
|
||||
@@ -118,19 +118,19 @@ class LinuxService(ServiceBase):
|
||||
"""
|
||||
self.stop()
|
||||
self.start()
|
||||
|
||||
|
||||
def run(self):
|
||||
atexit.register(self.terminate)
|
||||
|
||||
|
||||
args = self.load_configuration()[0]
|
||||
stdout = open(self.stdout, 'a+')
|
||||
stderr = open(self.stderr, 'a+')
|
||||
process = subprocess.Popen(args, stdout=stdout, stderr=stderr)
|
||||
file(self.pidfile,'w+').write("%s\n" % process.pid)
|
||||
process.wait()
|
||||
|
||||
|
||||
self.terminate()
|
||||
|
||||
|
||||
def terminate(self):
|
||||
try:
|
||||
os.remove(self.pidfile)
|
||||
@@ -152,7 +152,7 @@ class LinuxService(ServiceBase):
|
||||
install_command = self.get_service_installer_command(env)
|
||||
result = self.run_command(*install_command)
|
||||
self.start()
|
||||
|
||||
|
||||
def uninstall(self):
|
||||
self.stop()
|
||||
env = self.detect_environment()
|
||||
@@ -164,7 +164,7 @@ class LinuxService(ServiceBase):
|
||||
# remove link to the script from the service directory
|
||||
path = env['rc.d-path'] + self.name
|
||||
os.remove(path)
|
||||
|
||||
|
||||
def detect_environment(self):
|
||||
"""
|
||||
Returns a dictionary of command/path to the required command-line applications.
|
||||
@@ -196,7 +196,7 @@ class LinuxService(ServiceBase):
|
||||
env['rc.d-path'] = '/dev/null/'
|
||||
|
||||
return env
|
||||
|
||||
|
||||
def get_service_installer_command(self, env):
|
||||
"""
|
||||
Returns list of args required to set a service to run on boot.
|
||||
@@ -218,4 +218,3 @@ class LinuxService(ServiceBase):
|
||||
else:
|
||||
cmd = env['update-rc.d']
|
||||
return [cmd, self.name, 'remove']
|
||||
|
||||
|
||||
+28
-28
@@ -17,14 +17,14 @@ class ServiceBase(Base):
|
||||
self.stdout = stdout
|
||||
self.stderr = stderr
|
||||
self.config_file = None
|
||||
|
||||
|
||||
def load_configuration(self):
|
||||
"""
|
||||
Loads the configuration required to build the command-line string
|
||||
for running web2py. Returns a tuple (command_args, config_dict).
|
||||
"""
|
||||
s = os.path.sep
|
||||
|
||||
|
||||
default = dict(
|
||||
python = 'python',
|
||||
web2py = os.path.join(s.join(__file__.split(s)[:-3]), 'web2py.py'),
|
||||
@@ -38,14 +38,14 @@ class ServiceBase(Base):
|
||||
https_cert = '',
|
||||
password = '<recycle>',
|
||||
)
|
||||
|
||||
|
||||
config = default
|
||||
if self.config_file:
|
||||
try:
|
||||
f = open(self.config_file, 'r')
|
||||
lines = f.readlines()
|
||||
f.close()
|
||||
|
||||
|
||||
for line in lines:
|
||||
fields = line.split('=', 1)
|
||||
if len(fields) == 2:
|
||||
@@ -55,10 +55,10 @@ class ServiceBase(Base):
|
||||
config[key] = value
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
web2py_path = os.path.dirname(config['web2py'])
|
||||
os.chdir(web2py_path)
|
||||
|
||||
|
||||
args = [config['python'], config['web2py']]
|
||||
interfaces = []
|
||||
ports = []
|
||||
@@ -78,68 +78,68 @@ class ServiceBase(Base):
|
||||
ports.append(ports)
|
||||
if len(interfaces) == 0:
|
||||
sys.exit('Configuration error. Must have settings for http and/or https')
|
||||
|
||||
|
||||
password = config['password']
|
||||
if not password == '<recycle>':
|
||||
from gluon import main
|
||||
for port in ports:
|
||||
main.save_password(password, port)
|
||||
|
||||
|
||||
password = '<recycle>'
|
||||
|
||||
|
||||
args.append('-a "%s"' % password)
|
||||
|
||||
|
||||
interfaces = ';'.join(interfaces)
|
||||
args.append('--interfaces=%s' % interfaces)
|
||||
|
||||
|
||||
if 'log_filename' in config.key():
|
||||
log_filename = config['log_filename']
|
||||
args.append('--log_filename=%s' % log_filename)
|
||||
|
||||
|
||||
return (args, config)
|
||||
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
|
||||
def stop(self):
|
||||
pass
|
||||
|
||||
|
||||
def restart(self):
|
||||
pass
|
||||
|
||||
|
||||
def status(self):
|
||||
pass
|
||||
|
||||
|
||||
def run(self):
|
||||
pass
|
||||
|
||||
|
||||
def install(self):
|
||||
pass
|
||||
|
||||
|
||||
def uninstall(self):
|
||||
pass
|
||||
|
||||
|
||||
def check_permissions(self):
|
||||
"""
|
||||
Does the script have permissions to install, uninstall, start, and stop services?
|
||||
Return value must be a tuple (True/False, error_message_if_False).
|
||||
"""
|
||||
return (False, 'Permissions check not implemented')
|
||||
|
||||
|
||||
class WebServerBase(Base):
|
||||
def install(self):
|
||||
pass
|
||||
|
||||
|
||||
def uninstall(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def get_service():
|
||||
service_name = 'web2py'
|
||||
service_label = 'web2py Service'
|
||||
|
||||
|
||||
if sys.platform == 'linux2':
|
||||
from linux import LinuxService as Service
|
||||
from linux import LinuxService as Service
|
||||
elif sys.platform == 'darwin':
|
||||
# from mac import MacService as Service
|
||||
sys.exit('Mac OS X is not yet supported.\n')
|
||||
@@ -148,16 +148,16 @@ def get_service():
|
||||
sys.exit('Windows is not yet supported.\n')
|
||||
else:
|
||||
sys.exit('The following platform is not supported: %s.\n' % sys.platform)
|
||||
|
||||
|
||||
service = Service(service_name, service_label)
|
||||
return service
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
service = get_service()
|
||||
is_root, error_message = service.check_permissions()
|
||||
if not is_root:
|
||||
sys.exit(error_message)
|
||||
|
||||
|
||||
if len(sys.argv) >= 2:
|
||||
command = sys.argv[1]
|
||||
if command == 'start':
|
||||
|
||||
Reference in New Issue
Block a user