rueckstiess / mtools

A collection of scripts to set up MongoDB test environments and parse and visualize MongoDB log files.
Apache License 2.0
1.88k stars 400 forks source link

--version and --help broken #846

Closed themattman closed 2 years ago

themattman commented 2 years ago

The argparse behavior requires mongod to be installed in PATH, otherwise the tool barfs.

Prior to parsing arguments, the MLaunchTool constructor requires getMongoDVersion() to return a valid value.

This exposes an underlying circular dependency between construction and argument parsing.

allowed arguments -> mongod version number -> binarypath argument

Expected behavior

The MLaunchTool should be capable of successfully running a few meta commands regardless of the existence of mongod in PATH. e.g. The mtools version string gets printed or the help output of the script is printed

$ mlaunch --version
mtools version 1.7.0-dev || Python 3.9.2 (default, Jan 25 2022, 16:35:56) [GCC 7.5.0]
$ mlaunch --help
usage: mlaunch [-h] [--version] [--no-progressbar] {init,start,stop,restart,list,kill} ...

script to launch MongoDB stand-alone servers, replica sets and shards.

commands:
  init is the default command and can be omitted. To get help on individual commands, run mlaunch <command> --help. Command line arguments which are not handled by mlaunch will be passed
  through to mongod/mongos if those options are listed in the --help output for the current binary. For example: --storageEngine, --logappend, or --config.

  {init,start,stop,restart,list,kill}
    init                initialize a new MongoDB environment and start stand-alone instances, replica sets, or sharded clusters.
    start               starts existing MongoDB instances. Example: "mlaunch start config" will start all config servers.
    stop                stops running MongoDB instances. Example: "mlaunch stop shard 2 secondary" will stop all secondary nodes of shard 2.
    restart             stops, then restarts MongoDB instances.
    list                list MongoDB instances of this environment.
    kill                kills (or sends another signal to) MongoDB instances of this environment.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --no-progressbar      disables progress bar

Actual/current behavior

$ mlaunch --version
Failed to launch mongod
Traceback (most recent call last):
  File "/home/ubuntu/mongo/python3-venv/bin/mlaunch", line 33, in <module>
    sys.exit(load_entry_point('mtools==1.7.0.dev0', 'console_scripts', 'mlaunch')())
  File ".../mtools-1.7.0.dev0-py3.9.egg/mtools/mlaunch/mlaunch.py", line 2214, in main
    tool = MLaunchTool()
  File "/home/ubuntu/mongo/python3-venv/lib/python3.9/site-packages/mtools-1.7.0.dev0-py3.9.egg/mtools/mlaunch/mlaunch.py", line 198, in __init__
    self.current_version = self.getMongoDVersion()
  File ".../mtools-1.7.0.dev0-py3.9.egg/mtools/mlaunch/mlaunch.py", line 913, in getMongoDVersion
    out = check_mongo_server_output(binary, '--version')
  File "/home/ubuntu/mongo/python3-venv/lib/python3.9/site-packages/mtools-1.7.0.dev0-py3.9.egg/mtools/mlaunch/mlaunch.py", line 149, in check_mongo_server_output
    raise exc
  File ".../mtools-1.7.0.dev0-py3.9.egg/mtools/mlaunch/mlaunch.py", line 144, in check_mongo_server_output
    proc = subprocess.Popen(['%s' % binary, argument],
  File ".../stow/python3-v3.bmk/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File ".../stow/python3-v3.bmk/lib/python3.9/subprocess.py", line 1823, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'mongod'

Steps to reproduce the actual/current behavior

  1. Ensure mongod not in PATH
  2. Install mtools
  3. mlaunch --version or mlaunch --help

Environment

mtools version 1.7.0-dev || Python 3.9.2 (default, Jan 25 2022, 16:35:56) [GCC 7.5.0]

Software Version
mtools tip b96568cf
Python 3.9.2
MongoDB server local install / tip
Operating system Ubuntu 5.4.0-1059-aws
stennie commented 2 years ago

Thanks for the bug report @themattman ! It has clearly been awhile since someone using mtools didn't have MongoDB in their path :P

Cheers, Stennie