def main(): signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) spec = YapconfSpec(SPECIFICATION, env_prefix="BG_") parser = ArgumentParser() spec.add_arguments(parser) args = parser.parse_args(sys.argv[1:]) bartender.setup_bartender(spec=spec, cli_args=vars(args)) # Ensure we have a brew-view connection progressive_backoff( partial(bartender.bv_client.can_connect, timeout=5), bartender.application, "Unable to connect to brew-view, is it started?", ) # Ensure we have a mongo connection progressive_backoff( partial(setup_database, bartender.config), bartender.application, "Unable to connect to mongo, is it started?", ) # Ensure we have message queue connections progressive_backoff( bartender.application.clients["pika"].is_alive, bartender.application, "Unable to connect to rabbitmq, is it started?", ) progressive_backoff( bartender.application.clients["pyrabbit"].is_alive, bartender.application, "Unable to connect to rabbitmq admin interface. " "Is the management plugin enabled?", ) # Since we wait for RabbitMQ and brew-view we could already be shutting down # In that case we don't want to start if not bartender.application.stopped(): # Make sure that the bartender user has admin permissions bartender.ensure_admin() bartender.logger.info("Hi, what can I get you to drink?") bartender.application.start() bartender.logger.info("Let me know if you need anything else!") # You may be wondering why we don't just call bartender.application.join() or .wait(). # Well, you're in luck because I'm going to tell you why. Either of these methods # cause the main python thread to lock out our signal handler, which means we cannot # shut down gracefully in some circumstances. So instead we simply use pause() to wait # for a signal to be sent to us. If you choose to change this please test thoroughly # when deployed via system packages (apt/yum) as well as python packages and docker. # Thanks! signal.pause() bartender.logger.info("Don't forget to drive safe!")
def _parse_args(args: Sequence[str]) -> Tuple[YapconfSpec, dict]: """Construct a spec and parse command line arguments Args: args: Command line arguments Returns: Config object with only the named items """ spec = YapconfSpec(_SPECIFICATION, env_prefix="BG_") parser = ArgumentParser() spec.add_arguments(parser) cli_vars = vars(parser.parse_args(args)) return spec, cli_vars
def main(): signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) spec = YapconfSpec(SPECIFICATION, env_prefix="BG_") parser = ArgumentParser() spec.add_arguments(parser) args = parser.parse_args(sys.argv[1:]) # Logging isn't set up until after this... brew_view.setup(spec, vars(args)) # Schedule things to happen after the ioloop comes up brew_view.io_loop.add_callback(brew_view.startup) brew_view.logger.info("Starting IO loop") brew_view.io_loop.start() brew_view.logger.info("Application is shut down. Goodbye!")
def load_config(cli_args=None, argument_parser=None, **kwargs): """Load configuration using Yapconf Configuration will be loaded from these sources, with earlier sources having higher priority: 1. ``**kwargs`` passed to this method 2. ``cli_args`` passed to this method 3. Environment variables using the ``BG_`` prefix 4. Default values in the brewtils specification Args: cli_args (list, optional): List of command line arguments for configuration loading argument_parser (ArgumentParser, optional): Argument parser to use when parsing cli_args. Supplying this allows adding additional arguments prior to loading the configuration. This can be useful if your startup script takes additional arguments. See get_argument_parser for additional information. **kwargs: Additional configuration overrides Returns: :obj:`box.Box`: The resolved configuration object """ spec = YapconfSpec(SPECIFICATION, env_prefix="BG_") sources = [] if kwargs: # Do a little kwarg massaging for backwards compatibility if "bg_host" not in kwargs and "host" in kwargs: warnings.warn( "brewtils.load_config called with 'host' keyword " "argument. This name will be removed in version 3.0, " "please use 'bg_host' instead.", DeprecationWarning, stacklevel=2, ) kwargs["bg_host"] = kwargs.pop("host") if "bg_port" not in kwargs and "port" in kwargs: warnings.warn( "brewtils.load_config called with 'port' keyword " "argument. This name will be removed in version 3.0, " "please use 'bg_port' instead.", DeprecationWarning, stacklevel=2, ) kwargs["bg_port"] = kwargs.pop("port") sources.append(("kwargs", kwargs)) if cli_args: if not argument_parser: argument_parser = ArgumentParser() spec.add_arguments(argument_parser) parsed_args = argument_parser.parse_args(cli_args) sources.append(("cli_args", vars(parsed_args))) sources.append("ENVIRONMENT") try: config = spec.load_config(*sources) except YapconfItemNotFound as ex: if ex.item.name == "bg_host": raise ValidationError( "Unable to create a plugin without a " "beer-garden host. Please specify one on the " "command line (--bg-host), in the " "environment (BG_HOST), or in kwargs " "(bg_host)" ) raise # Make sure the url_prefix is normal config.url_prefix = normalize_url_prefix(config.url_prefix) return config
def load_config(cli_args=True, environment=True, argument_parser=None, bootstrap=False, **kwargs): """Load configuration using Yapconf Configuration will be loaded from these sources, with earlier sources having higher priority: 1. ``**kwargs`` passed to this method 2. Command line arguments (if ``cli_args`` argument is not False) 3. Environment variables using the ``BG_`` prefix (if ``environment`` argument is not False) 4. Default values in the brewtils specification Args: cli_args (Union[bool, list], optional): Specifies whether command line should be used as a configuration source - True: Argparse will use the standard sys.argv[1:] - False: Command line arguments will be ignored when loading configuration - List of strings: Will be parsed as CLI args (instead of using sys.argv) environment (bool): Specifies whether environment variables (with the ``BG_`` prefix) should be used when loading configuration argument_parser (ArgumentParser, optional, deprecated): Argument parser to use when parsing cli_args. Supplying this allows adding additional arguments prior to loading the configuration. This can be useful if your startup script takes additional arguments. See get_argument_parser for additional information. **kwargs: Additional configuration overrides Returns: box.Box: The resolved configuration object """ spec = YapconfSpec(SPECIFICATION, env_prefix="BG_") sources = [] if kwargs: # First deprecate / translate items with multiple names mangled_kwargs = _translate_kwargs(**kwargs) # Metadata is a little weird because yapconf doesn't support raw dicts, so we # need to make it a json string in that case metadata = kwargs.get("metadata") if isinstance(metadata, dict): mangled_kwargs["metadata"] = json.dumps(metadata) sources.append(("kwargs", mangled_kwargs)) if cli_args: if cli_args is True: sources.append("CLI") else: if not argument_parser: argument_parser = ArgumentParser() spec.add_arguments(argument_parser) parsed_args, unknown = argument_parser.parse_known_args(cli_args) sources.append(("cli_args", vars(parsed_args))) if environment: sources.append("ENVIRONMENT") try: config = spec.load_config(*sources, bootstrap=bootstrap) except YapconfItemNotFound as ex: if ex.item.name == "bg_host": raise ValidationError( "Unable to create a plugin without a Beer-garden host. Please specify " "one on the command line (--bg-host), in the environment (BG_HOST), or " "in kwargs (bg_host).") raise # Make sure the url_prefix is normal if "bg_url_prefix" in config: config.bg_url_prefix = normalize_url_prefix(config.bg_url_prefix) return config