Skip to content

Commit

Permalink
Add a command line flag for controlling systemd user mode
Browse files Browse the repository at this point in the history
  • Loading branch information
natefoo committed Dec 20, 2024
1 parent e61c14b commit ebb17c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion gravity/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ def galaxy(ctx, debug, config_file, state_dir, quiet):
@options.debug_option()
@options.config_file_option()
@options.state_dir_option()
@options.user_mode_option()
@click.pass_context
def galaxyctl(ctx, debug, config_file, state_dir):
def galaxyctl(ctx, debug, config_file, state_dir, user):
"""Manage Galaxy server configurations and processes."""
set_debug(debug)
ctx.cm_kwargs = {
"config_file": config_file,
"state_dir": state_dir,
"user_mode": user,
}
7 changes: 4 additions & 3 deletions gravity/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@


@contextlib.contextmanager
def config_manager(config_file=None, state_dir=None):
yield ConfigManager(config_file=config_file, state_dir=state_dir)
def config_manager(config_file=None, state_dir=None, user_mode=None):
yield ConfigManager(config_file=config_file, state_dir=state_dir, user_mode=user_mode)


class ConfigManager(object):
galaxy_server_config_section = "galaxy"
gravity_config_section = "gravity"
app_config_file_option = "galaxy_config_file"

def __init__(self, config_file=None, state_dir=None):
def __init__(self, config_file=None, state_dir=None, user_mode=None):
self.__configs = {}
self.state_dir = None
if state_dir is not None:
# convert from pathlib.Path
self.state_dir = str(state_dir)
self.user_mode = user_mode

gravity.io.debug(f"Gravity state dir: {state_dir}")

Expand Down
8 changes: 8 additions & 0 deletions gravity/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def config_file_option():
)


def user_mode_option():
return click.option(
"--user/--no-user",
default=None,
help="Use `systemctl/journalctl --user` (default: automatic depeending on whether run as root)",
)


def no_log_option():
return click.option(
'--quiet', is_flag=True, default=False, help="Only output supervisor logs, do not include process logs"
Expand Down
8 changes: 4 additions & 4 deletions gravity/process_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def decorator(self, *args, instance_names=None, **kwargs):


class BaseProcessExecutionEnvironment(metaclass=ABCMeta):
def __init__(self, state_dir=None, config_file=None, config_manager=None, **kwargs):
self.config_manager = config_manager or ConfigManager(state_dir=state_dir, config_file=config_file)
def __init__(self, state_dir=None, config_file=None, config_manager=None, user_mode=None):
self.config_manager = config_manager or ConfigManager(state_dir=state_dir, config_file=config_file, user_mode=user_mode)
self.tail = which("tail")

@abstractmethod
Expand Down Expand Up @@ -291,8 +291,8 @@ def exec(self, config, service, service_instance_number=None, no_exec=False):


class ProcessManagerRouter:
def __init__(self, state_dir=None, config_file=None, config_manager=None, **kwargs):
self.config_manager = config_manager or ConfigManager(state_dir=state_dir, config_file=config_file)
def __init__(self, state_dir=None, config_file=None, config_manager=None, user_mode=None, **kwargs):
self.config_manager = config_manager or ConfigManager(state_dir=state_dir, config_file=config_file, user_mode=user_mode)
self._load_pm_modules(**kwargs)
self._process_executor = ProcessExecutor(config_manager=self.config_manager)

Expand Down
4 changes: 3 additions & 1 deletion gravity/process_manager/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ class SystemdProcessManager(BaseProcessManager):

def __init__(self, foreground=False, **kwargs):
super(SystemdProcessManager, self).__init__(**kwargs)
self.user_mode = not self.config_manager.is_root
self.user_mode = self.config_manager.user_mode
if self.user_mode is None:
self.user_mode = not self.config_manager.is_root

@property
def __systemd_unit_dir(self):
Expand Down

0 comments on commit ebb17c4

Please sign in to comment.