diff --git a/gravity/cli.py b/gravity/cli.py index aaf7257..af99aad 100644 --- a/gravity/cli.py +++ b/gravity/cli.py @@ -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, } diff --git a/gravity/config_manager.py b/gravity/config_manager.py index d202e82..1b26ece 100644 --- a/gravity/config_manager.py +++ b/gravity/config_manager.py @@ -35,8 +35,8 @@ @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): @@ -44,12 +44,13 @@ class ConfigManager(object): 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}") diff --git a/gravity/options.py b/gravity/options.py index 1917672..edf9d69 100644 --- a/gravity/options.py +++ b/gravity/options.py @@ -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" diff --git a/gravity/process_manager/__init__.py b/gravity/process_manager/__init__.py index b07a3aa..f0fa7bd 100644 --- a/gravity/process_manager/__init__.py +++ b/gravity/process_manager/__init__.py @@ -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 @@ -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) diff --git a/gravity/process_manager/systemd.py b/gravity/process_manager/systemd.py index efea36d..3d94fe2 100644 --- a/gravity/process_manager/systemd.py +++ b/gravity/process_manager/systemd.py @@ -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):