Skip to content

Commit

Permalink
use env var for mq uri
Browse files Browse the repository at this point in the history
  • Loading branch information
cbartz committed Jul 12, 2024
1 parent 6e90783 commit f8d8fb3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
20 changes: 14 additions & 6 deletions scripts/reactive_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"""Script to spawn a reactive runner."""
import argparse
import logging
import os

from reactive.runner import reactive_runner
from reactive.runner_manager import REACTIVE_RUNNER_LOG_PATH
from reactive.runner_manager import MQ_URI_ENV_VAR, REACTIVE_RUNNER_LOG_PATH


def setup_root_logging() -> None:
Expand All @@ -21,14 +22,21 @@ def setup_root_logging() -> None:

if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument(
"mq_uri",
help="URI of the message queue database. This should include authentication information."
" E.g. : mongodb://user:[email protected]/github-runner-webhook-router"
"?replicaSet=mongodb&authSource=admin",
"queue_name", help="Name of the message queue to consume from. E.g. : large"
)

mq_uri = os.environ.get(MQ_URI_ENV_VAR)
argument_opts: dict = {"default": mq_uri} if mq_uri else {"required": True}
parser.add_argument(
"queue_name", help="Name of the message queue to consume from. E.g. : large"
"--mq-uri",
help="URI of the message queue database. This should include authentication information."
f"The argument is required but can also be set via the {MQ_URI_ENV_VAR} "
"environment variable."
" Example URI : mongodb://user:[email protected]/github-runner-webhook-router"
"?replicaSet=mongodb&authSource=admin",
**argument_opts,
)
arguments = parser.parse_args()

Expand Down
1 change: 1 addition & 0 deletions src-docs/reactive.runner_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Module for managing reactive runners.

**Global Variables**
---------------
- **MQ_URI_ENV_VAR**
- **REACTIVE_RUNNER_SCRIPT_FILE**
- **REACTIVE_RUNNER_TIMEOUT_STR**
- **PYTHON_BIN**
Expand Down
13 changes: 6 additions & 7 deletions src/reactive/runner_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

logger = logging.getLogger(__name__)


MQ_URI_ENV_VAR = "MQ_URI"
REACTIVE_RUNNER_LOG_DIR = Path("/var/log/reactive_runner")
REACTIVE_RUNNER_LOG_PATH = REACTIVE_RUNNER_LOG_DIR / "reactive_runner.log"
REACTIVE_STDOUT_STD_ERR_SUFFIX_PATH = REACTIVE_RUNNER_LOG_DIR / "error.log"
Expand Down Expand Up @@ -134,7 +134,10 @@ def _spawn_runner(reactive_runner_config: ReactiveRunnerConfig) -> None:
Raises:
ReactiveRunnerError: If the runner fails to spawn.
"""
env = {"PYTHONPATH": "src:lib:venv"}
env = {
"PYTHONPATH": "src:lib:venv",
MQ_URI_ENV_VAR: reactive_runner_config.mq_uri,
}
# We do not want to wait for the process to finish, so we do not use with statement.
# We trust the command.
command = " ".join(
Expand All @@ -143,7 +146,6 @@ def _spawn_runner(reactive_runner_config: ReactiveRunnerConfig) -> None:
REACTIVE_RUNNER_TIMEOUT_STR,
PYTHON_BIN,
REACTIVE_RUNNER_SCRIPT_FILE,
f'"{reactive_runner_config.mq_uri}"',
f'"{reactive_runner_config.queue_name}"',
">>",
# $$ will be replaced by the PID of the process, so we can track the error log easily.
Expand All @@ -152,10 +154,7 @@ def _spawn_runner(reactive_runner_config: ReactiveRunnerConfig) -> None:
]
)
# replace the mq_uri with **** to avoid leaking sensitive information (mongodb password)
logger.debug(
"Spawning a new reactive runner process with command: %s",
command.replace(reactive_runner_config.mq_uri, "****"),
)
logger.debug("Spawning a new reactive runner process with command: %s", command)
process = subprocess.Popen( # pylint: disable=consider-using-with # nosec
command,
shell=True,
Expand Down

0 comments on commit f8d8fb3

Please sign in to comment.