From 867532a25a4265db2ac092378f9ba569f63caa00 Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Fri, 18 Nov 2022 00:53:07 -0500 Subject: [PATCH 1/2] Fixes for spaces in the galaxy root path, fix the `galaxy` entrypoint. --- gravity/process_manager/__init__.py | 14 +++++++++++--- gravity/process_manager/systemd.py | 5 +---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/gravity/process_manager/__init__.py b/gravity/process_manager/__init__.py index da5b486..9fd3349 100644 --- a/gravity/process_manager/__init__.py +++ b/gravity/process_manager/__init__.py @@ -82,7 +82,7 @@ def _service_program_name(self, instance_name, service): def _service_format_vars(self, config, service, pm_format_vars=None): pm_format_vars = pm_format_vars or {} virtualenv_dir = config.virtualenv - virtualenv_bin = f'{os.path.join(virtualenv_dir, "bin")}{os.path.sep}' if virtualenv_dir else "" + virtualenv_bin = shlex.quote(f'{os.path.join(virtualenv_dir, "bin")}{os.path.sep}') if virtualenv_dir else "" format_vars = { "config_type": service.config_type, @@ -91,7 +91,7 @@ def _service_format_vars(self, config, service, pm_format_vars=None): "galaxy_conf": config.galaxy_config_file, "galaxy_root": config.galaxy_root, "virtualenv_bin": virtualenv_bin, - "gravity_data_dir": config.gravity_data_dir, + "gravity_data_dir": shlex.quote(config.gravity_data_dir), "app_config": config.app_config, } @@ -105,6 +105,9 @@ def _service_format_vars(self, config, service, pm_format_vars=None): if config.service_command_style in (ServiceCommandStyle.direct, ServiceCommandStyle.exec): format_vars["command_arguments"] = service.get_command_arguments(format_vars) format_vars["command"] = service.command_template.format(**format_vars) + # normalize quoting to replace '/foo/bar baz'/quux with '/foo/bar baz/quux', either is valid but the former + # is ugly and difficult to read + format_vars["command"] = shlex.join(shlex.split(format_vars["command"])) # template env vars environment = service.environment @@ -116,12 +119,17 @@ def _service_format_vars(self, config, service, pm_format_vars=None): config_file = shlex.quote(config.gravity_config_file) # is there a click way to do this? galaxyctl = sys.argv[0] - if not galaxyctl.endswith("galaxyctl"): + if galaxyctl.endswith(f"{os.path.sep}galaxy"): + # handle when called using the `galaxy` entrypoint + galaxyctl += "ctl" + if not galaxyctl.endswith(f"{os.path.sep}galaxyctl"): gravity.io.warn(f"Unable to determine galaxyctl command, sys.argv[0] is: {galaxyctl}") + galaxyctl = shlex.quote(galaxyctl) instance_number_opt = "" if service.count > 1: instance_number_opt = f" --service-instance {pm_format_vars['instance_number']}" format_vars["command"] = f"{galaxyctl} --config-file {config_file} exec{instance_number_opt} {config.instance_name} {service.service_name}" + format_vars["command"] = shlex.join(shlex.split(format_vars["command"])) environment = {} format_vars["environment"] = self._service_environment_formatter(environment, format_vars) diff --git a/gravity/process_manager/systemd.py b/gravity/process_manager/systemd.py index 098b6ae..d3c6a7c 100644 --- a/gravity/process_manager/systemd.py +++ b/gravity/process_manager/systemd.py @@ -250,7 +250,7 @@ def __update_service(self, config, service, systemd_service: SystemdService, for # systemd-specific format vars systemd_format_vars = { - "virtualenv_bin": f'{os.path.join(virtualenv_dir, "bin")}{os.path.sep}' if virtualenv_dir else "", + "virtualenv_bin": shlex.quote(f'{os.path.join(virtualenv_dir, "bin")}{os.path.sep}'), "instance_number": "%i", "systemd_user_group": "", "systemd_exec_reload": exec_reload or "", @@ -265,9 +265,6 @@ def __update_service(self, config, service, systemd_service: SystemdService, for format_vars = self._service_format_vars(config, service, systemd_format_vars) - if not format_vars["command"].startswith("/"): - format_vars["command"] = f"{format_vars['virtualenv_bin']}{format_vars['command']}" - unit_file = systemd_service.unit_file_name conf = os.path.join(self.__systemd_unit_dir, unit_file) template = SYSTEMD_SERVICE_TEMPLATE From 18ebc281540933d37a63b0e95fb0c8590bf0640f Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Fri, 18 Nov 2022 01:01:10 -0500 Subject: [PATCH 2/2] Drop shlex.join since it is not in Python 3.7 --- gravity/process_manager/__init__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gravity/process_manager/__init__.py b/gravity/process_manager/__init__.py index 9fd3349..caaf4bf 100644 --- a/gravity/process_manager/__init__.py +++ b/gravity/process_manager/__init__.py @@ -105,9 +105,6 @@ def _service_format_vars(self, config, service, pm_format_vars=None): if config.service_command_style in (ServiceCommandStyle.direct, ServiceCommandStyle.exec): format_vars["command_arguments"] = service.get_command_arguments(format_vars) format_vars["command"] = service.command_template.format(**format_vars) - # normalize quoting to replace '/foo/bar baz'/quux with '/foo/bar baz/quux', either is valid but the former - # is ugly and difficult to read - format_vars["command"] = shlex.join(shlex.split(format_vars["command"])) # template env vars environment = service.environment @@ -129,7 +126,6 @@ def _service_format_vars(self, config, service, pm_format_vars=None): if service.count > 1: instance_number_opt = f" --service-instance {pm_format_vars['instance_number']}" format_vars["command"] = f"{galaxyctl} --config-file {config_file} exec{instance_number_opt} {config.instance_name} {service.service_name}" - format_vars["command"] = shlex.join(shlex.split(format_vars["command"])) environment = {} format_vars["environment"] = self._service_environment_formatter(environment, format_vars)