Ejemplo n.º 1
0
 def test_scalar_children(self):
     parser = config.DictOf(config.Integer)
     result = parser.parse("my_key", {
         "my_key.a": "1",
         "my_key.b": "2",
     })
     self.assertEqual(result, {"a": 1, "b": 2})
Ejemplo n.º 2
0
def main() -> NoReturn:
    """
    Sidecar that tracks the age of the file monitored by live-data watcher and secrets fetcher.

    Use this with live_data_watcher and secrets_fetcher to monitor their files and ensure that
    these sidecars are not failing silently.
    """
    arg_parser = argparse.ArgumentParser(
        description=sys.modules[__name__].__doc__)
    arg_parser.add_argument("config_file",
                            type=argparse.FileType("r"),
                            help="path to a configuration file")
    args = arg_parser.parse_args()

    parser = configparser.RawConfigParser()
    parser.read(args.config_file.name)
    watcher_config = dict(parser.items("file-age-watcher"))
    cfg = config.parse_config(watcher_config,
                              {"file": config.DictOf({"path": config.String})})

    metrics_client = metrics_client_from_config(watcher_config)
    while True:
        time.sleep(HEARTBEAT_INTERVAL)
        now = time.time()
        for name, file in cfg.file.items():
            try:
                mtime = os.path.getmtime(file.path)
            except OSError:
                mtime = 0

            age = now - mtime
            metrics_client.histogram(
                f"file-age-watcher.{name}.age").add_sample(age)
Ejemplo n.º 3
0
 def test_subparsers(self):
     result = config.parse_config(self.config, {
         "foo": config.DictOf(config.String),
     })
     self.assertEqual(result, {
         "foo": {
             "bar": "33",
             "baz": "a cool guy",
         },
     })
Ejemplo n.º 4
0
def main():
    arg_parser = argparse.ArgumentParser(
        description=sys.modules[__name__].__doc__)
    arg_parser.add_argument("config_file",
                            type=argparse.FileType("r"),
                            help="path to a configuration file")
    arg_parser.add_argument("--debug",
                            default=False,
                            action="store_true",
                            help="enable debug logging")
    args = arg_parser.parse_args()

    if args.debug:
        level = logging.DEBUG
    else:
        level = logging.INFO
    logging.basicConfig(level=level, format="%(message)s")

    # quiet kazoo's verbose logs a bit
    logging.getLogger("kazoo").setLevel(logging.WARNING)

    parser = configparser.RawConfigParser()
    parser.readfp(args.config_file)  # pylint: disable=deprecated-method
    watcher_config = dict(parser.items("live-data"))

    cfg = config.parse_config(
        watcher_config,
        {
            "nodes":
            config.DictOf(
                {
                    "source": config.String,
                    "dest": config.String,
                    "owner": config.Optional(config.UnixUser),
                    "group": config.Optional(config.UnixGroup),
                    "mode": config.Optional(config.Integer(base=8),
                                            default=0o400),
                })
        },
    )
    # pylint: disable=maybe-no-member
    nodes = cfg.nodes.values()

    secrets = secrets_store_from_config(watcher_config, timeout=30)
    zookeeper = zookeeper_client_from_config(secrets,
                                             watcher_config,
                                             read_only=True)
    zookeeper.start()
    try:
        watch_zookeeper_nodes(zookeeper, nodes)
    finally:
        zookeeper.stop()
Ejemplo n.º 5
0
 def test_root_level(self):
     parser = config.DictOf({"a": config.Integer, "b": config.String})
     result = parser.parse(
         "", {
             "first.a": "1",
             "first.b": "test",
             "first.c": "ignored",
             "second.a": "2",
             "second.b": "test",
         })
     self.assertEqual(result, {
         "first": {
             "a": 1,
             "b": "test",
         },
         "second": {
             "a": 2,
             "b": "test",
         },
     })
Ejemplo n.º 6
0
 def test_vector_children(self):
     parser = config.DictOf({"a": config.Integer, "b": config.String})
     result = parser.parse(
         "my_key", {
             "my_key.first.a": "1",
             "my_key.first.b": "test",
             "my_key.first.c": "ignored",
             "my_key.second.a": "2",
             "my_key.second.b": "test",
         })
     self.assertEqual(result, {
         "first": {
             "a": 1,
             "b": "test",
         },
         "second": {
             "a": 2,
             "b": "test",
         },
     })
Ejemplo n.º 7
0
 def test_empty(self):
     parser = config.DictOf(config.Integer)
     result = parser.parse("my_key", {"not_related": "a"})
     self.assertEqual(result, {})