[UI] [Core] Combine common process options into baseargparser

This commit is contained in:
Calum Lind 2016-04-28 10:52:15 +01:00
parent b4dd90ba2b
commit bd65abd3b4
3 changed files with 52 additions and 83 deletions

View File

@ -13,7 +13,7 @@ import os
import sys import sys
from logging import FileHandler, getLogger from logging import FileHandler, getLogger
from deluge.common import run_profiled, windows_check from deluge.common import run_profiled
from deluge.configmanager import get_config_dir from deluge.configmanager import get_config_dir
from deluge.error import DaemonRunningError from deluge.error import DaemonRunningError
from deluge.ui.baseargparser import BaseArgParser from deluge.ui.baseargparser import BaseArgParser
@ -28,17 +28,9 @@ def add_daemon_options(parser):
help=_("Port to listen for UI connections on")) help=_("Port to listen for UI connections on"))
group.add_argument("-i", "--interface", metavar="<ip-addr>", dest="listen_interface", action="store", group.add_argument("-i", "--interface", metavar="<ip-addr>", dest="listen_interface", action="store",
help=_("IP address to listen for BitTorrent connections")) help=_("IP address to listen for BitTorrent connections"))
group.add_argument("-P", "--pidfile", metavar="<pid-file>", action="store",
help=_("Pidfile to store the process id"))
if not windows_check():
group.add_argument("-d", "--do-not-daemonize", dest="donot", action="store_true",
help=_("Do not daemonize (fork) this process"))
group.add_argument("-U", "--user", metavar="<user>", action="store",
help=_("Change to this user on startup (Requires root)"))
group.add_argument("-g", "--group", metavar="<group>", action="store",
help=_("Change to this group on startup (Requires root)"))
group.add_argument("--read-only-config-keys", metavar="<comma-separated-keys>", action="store", group.add_argument("--read-only-config-keys", metavar="<comma-separated-keys>", action="store",
help=_("Config keys to be unmodified by `set_config` RPC"), type=str, default="") help=_("Config keys to be unmodified by `set_config` RPC"), type=str, default="")
parser.add_process_arg_group()
def start_daemon(skip_start=False): def start_daemon(skip_start=False):
@ -78,34 +70,6 @@ def start_daemon(skip_start=False):
file_handler = FileHandler(options.logfile) file_handler = FileHandler(options.logfile)
log.addHandler(file_handler) log.addHandler(file_handler)
# If the donot daemonize is set, then we just skip the forking
if not (windows_check() or options.donot):
if os.fork():
os._exit(0)
os.setsid()
# Do second fork
if os.fork():
os._exit(0)
# Ensure process doesn't keep any directory in use that may prevent a filesystem unmount.
os.chdir(get_config_dir())
# Write pid file before chuid
if options.pidfile:
with open(options.pidfile, "wb") as _file:
_file.write("%d\n" % os.getpid())
if not windows_check():
if options.user:
if not options.user.isdigit():
import pwd
options.user = pwd.getpwnam(options.user)[2]
os.setuid(options.user)
if options.group:
if not options.group.isdigit():
import grp
options.group = grp.getgrnam(options.group)[2]
os.setuid(options.group)
def run_daemon(options): def run_daemon(options):
try: try:
from deluge.core.daemon import Daemon from deluge.core.daemon import Daemon

View File

@ -14,9 +14,9 @@ import platform
import sys import sys
import textwrap import textwrap
import deluge.configmanager
import deluge.log import deluge.log
from deluge import common from deluge import common
from deluge.configmanager import get_config_dir, set_config_dir
from deluge.log import setup_logger from deluge.log import setup_logger
@ -139,7 +139,7 @@ class BaseArgParser(argparse.ArgumentParser):
logrotate=logrotate) logrotate=logrotate)
if options.config: if options.config:
if not deluge.configmanager.set_config_dir(options.config): if not set_config_dir(options.config):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.error("There was an error setting the config dir! Exiting..") log.error("There was an error setting the config dir! Exiting..")
sys.exit(1) sys.exit(1)
@ -147,4 +147,50 @@ class BaseArgParser(argparse.ArgumentParser):
if not os.path.exists(common.get_default_config_dir()): if not os.path.exists(common.get_default_config_dir()):
os.makedirs(common.get_default_config_dir()) os.makedirs(common.get_default_config_dir())
if self.process_group:
# If donotdaemonize is set, skip process forking.
if not (common.windows_check() or options.donotdaemonize):
if os.fork():
os._exit(0)
os.setsid()
# Do second fork
if os.fork():
os._exit(0)
# Ensure process doesn't keep any directory in use that may prevent a filesystem unmount.
os.chdir(get_config_dir())
# Write pid file before chuid
if options.pidfile:
with open(options.pidfile, "wb") as _file:
_file.write("%d\n" % os.getpid())
if not common.windows_check():
if options.user:
if not options.user.isdigit():
import pwd
options.user = pwd.getpwnam(options.user)[2]
os.setuid(options.user)
if options.group:
if not options.group.isdigit():
import grp
options.group = grp.getgrnam(options.group)[2]
os.setuid(options.group)
return options return options
def add_process_arg_group(self):
"""Adds a grouping of common process args to control a daemon to the parser"""
self.process_group = True
self.group = self.add_argument_group(_("Process Control Options"))
self.group.add_argument("-P", "--pidfile", metavar="<pidfile>", action="store",
help=_("Pidfile to store the process id"))
if not common.windows_check():
self.group.add_argument("-d", "--do-not-daemonize", dest="donotdaemonize", action="store_true",
help=_("Do not daemonize (fork) this process"))
self.group.add_argument("-f", "--fork", dest="donotdaemonize", action="store_false",
help=argparse.SUPPRESS) # Deprecated arg
self.group.add_argument("-U", "--user", metavar="<user>", action="store",
help=_("Change to this user on startup (Requires root)"))
self.group.add_argument("-g", "--group", metavar="<group>", action="store",
help=_("Change to this group on startup (Requires root)"))

View File

@ -9,12 +9,9 @@
from __future__ import print_function from __future__ import print_function
import argparse
import logging import logging
import os
from deluge.common import run_profiled, windows_check from deluge.common import run_profiled
from deluge.configmanager import get_config_dir
from deluge.ui.ui import UI from deluge.ui.ui import UI
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -41,17 +38,6 @@ class Web(UI):
help=_("IP address for web server to listen on")) help=_("IP address for web server to listen on"))
group.add_argument("-p", "--port", metavar="<port>", type=int, action="store", group.add_argument("-p", "--port", metavar="<port>", type=int, action="store",
help=_("Port for web server to listen on")) help=_("Port for web server to listen on"))
group.add_argument("-P", "--pidfile", metavar="<pidfile>", action="store",
help=_("Pidfile to store the process id"))
if not windows_check():
group.add_argument("-d", "--do-not-daemonize", dest="donotdaemonize", action="store_true",
help=_("Do not daemonize (fork) this process"))
group.add_argument("-f", "--fork", dest="donotdaemonize", action="store_false",
help=argparse.SUPPRESS) # Deprecated arg
group.add_argument("-U", "--user", metavar="<user>", action="store",
help=_("Change to this user on startup (Requires root)"))
group.add_argument("-g", "--group", metavar="<group>", action="store",
help=_("Change to this group on startup (Requires root)"))
group.add_argument("-b", "--base", metavar="<path>", action="store", group.add_argument("-b", "--base", metavar="<path>", action="store",
help=_("Set the base path that the ui is running on")) help=_("Set the base path that the ui is running on"))
try: try:
@ -62,6 +48,7 @@ class Web(UI):
else: else:
group.add_argument("--ssl", action="store_true", help=_("Force the web server to use SSL")) group.add_argument("--ssl", action="store_true", help=_("Force the web server to use SSL"))
group.add_argument("--no-ssl", action="store_true", help=_("Force the web server to disable SSL")) group.add_argument("--no-ssl", action="store_true", help=_("Force the web server to disable SSL"))
self.parser.add_process_arg_group()
@property @property
def server(self): def server(self):
@ -70,34 +57,6 @@ class Web(UI):
def start(self, args=None): def start(self, args=None):
super(Web, self).start(args) super(Web, self).start(args)
# If donotdaemonize is set, skip process forking.
if not (windows_check() or self.options.donotdaemonize):
if os.fork():
os._exit(0)
os.setsid()
# Do second fork
if os.fork():
os._exit(0)
# Ensure process doesn't keep any directory in use that may prevent a filesystem unmount.
os.chdir(get_config_dir())
# Write pid file before chuid
if self.options.pidfile:
with open(self.options.pidfile, "wb") as _file:
_file.write("%d\n" % os.getpid())
if not windows_check():
if self.options.user:
if not self.options.user.isdigit():
import pwd
self.options.user = pwd.getpwnam(self.options.user)[2]
os.setuid(self.options.user)
if self.options.group:
if not self.options.group.isdigit():
import grp
self.options.group = grp.getgrnam(self.options.group)[2]
os.setuid(self.options.group)
from deluge.ui.web import server from deluge.ui.web import server
self.__server = server.DelugeWeb(options=self.options) self.__server = server.DelugeWeb(options=self.options)