Ejemplo n.º 1
0
def test_existent_config_file():
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               'config.yaml')
    config = Config(config_file)

    assert config.get_config('context') == {'component': 'frontend'}
    assert config.get_config('unknown') == {}
Ejemplo n.º 2
0
def test_nonexistent_config_file():
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               'no_file.yaml')
    config = Config(config_file)

    assert config.get_config('context') == {}
    assert config.get_config('unknown') == {}
Ejemplo n.º 3
0
 def read_config(self):
     self.config = Config()
     if self.args.config:
         fp = self.args.config
     else:
         fp = '/etc/grafyaml/grafyaml.conf'
     self.config.read(os.path.expanduser(fp))
Ejemplo n.º 4
0
class TestCaseConfig(TestCase):
    def setUp(self):
        super(TestCaseConfig, self).setUp()
        self.config = Config()

    def test_defaults(self):
        self.assertTrue(self.config.getboolean('cache', 'enabled'))
        self.assertEqual(self.config.get('cache', 'cachedir'),
                         '~/.cache/grafyaml')
        self.assertEqual(self.config.get('grafana', 'apikey'), '')
        self.assertEqual(self.config.get('grafana', 'url'),
                         'http://localhost:8080')
Ejemplo n.º 5
0
 def read_config(self):
     self.config = Config()
     if self.args.config:
         fp = self.args.config
     else:
         fp = '/etc/grafyaml/grafyaml.conf'
     self.config.read(os.path.expanduser(fp))
     if self.args.grafana_url:
         self.config.set('grafana', 'url', self.args.grafana_url)
         LOG.debug('Grafana URL override: {}'.format(self.args.grafana_url))
     if self.args.grafana_apikey:
         self.config.set('grafana', 'apikey', self.args.grafana_apikey)
         LOG.debug('Grafana APIKey overridden')
Ejemplo n.º 6
0
class TestCaseConfig(TestCase):
    def setUp(self):
        super(TestCaseConfig, self).setUp()
        self.config = Config()

    def test_defaults(self):
        self.assertTrue(self.config.getboolean("cache", "enabled"))
        self.assertEqual(self.config.get("cache", "cachedir"),
                         "~/.cache/grafyaml")
        self.assertEqual(self.config.get("grafana", "apikey"), "")
        self.assertEqual(self.config.get("grafana", "folderid"), "0")
        self.assertEqual(self.config.get("grafana", "url"),
                         "http://localhost:8080")
Ejemplo n.º 7
0
def main():
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--path', required=True, nargs='+', type=str,
                        help='List of path to YAML definition files')
    parser.add_argument('--project',
                        help='(deprecated, use path) Location of the file containing project definition.')
    parser.add_argument('-o', '--out',
                        help='(deprecated, use config file and file exporter) Path to output folder')
    parser.add_argument('-c', '--config', default='./.grafana/grafana_dashboards.yaml',
                        help='Configuration file containing fine-tuned setup of builder\'s components.')
    parser.add_argument('--context', default='{}',
                        help='YAML structure defining parameters for dashboard definition.'
                             ' Effectively overrides any parameter defined on project level.')
    parser.add_argument('--plugins', nargs='+', type=str,
                        help='List of external component plugins to load')
    parser.add_argument('--exporter', nargs='+', type=str, default=set(), dest='exporters',
                        help='List of dashboard exporters')

    args = parser.parse_args()

    if args.plugins:
        for plugin in args.plugins:
            try:
                imp.load_source('grafana_dashboards.components.$loaded', plugin)
            except Exception as e:
                print('Cannot load plugin %s: %s' % (plugin, str(e)))

    if args.project:
        logging.warn("Using deprecated option '--project'")
        args.path.add(args.project)
    paths = _process_paths(args.path)

    config = Config(args.config)
    exporters = set(args.exporters)
    if args.out:
        logging.warn("Using deprecated option '-o/--out'")
        exporters.add('file')
        config.get_config('file').update(output_folder=args.out)

    dashboard_exporters = _initialize_exporters(exporters, [FileExporter, ElasticSearchExporter, GrafanaExporter],
                                                config)

    context = config.get_config('context')
    context.update(yaml.load(args.context))

    projects = DefinitionParser().load_projects(paths)
    project_processor = ProjectProcessor(dashboard_exporters)
    project_processor.process_projects(projects, context)
Ejemplo n.º 8
0
def main():
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--path', required=True, nargs='+', type=str,
                        help='List of path to YAML definition files')
    parser.add_argument('--project',
                        help='(deprecated, use path) Location of the file containing project definition.')
    parser.add_argument('-o', '--out',
                        help='(deprecated, use config file and file exporter) Path to output folder')
    parser.add_argument('-c', '--config', default='./.grafana/grafana_dashboards.yaml',
                        help='Configuration file containing fine-tuned setup of builder\'s components.')
    parser.add_argument('--context', default='{}',
                        help='YAML structure defining parameters for dashboard definition.'
                             ' Effectively overrides any parameter defined on project level.')
    parser.add_argument('--plugins', nargs='+', type=str,
                        help='List of external component plugins to load')
    parser.add_argument('--exporter', nargs='+', type=str, default=set(), dest='exporters',
                        help='List of dashboard exporters')

    args = parser.parse_args()

    if args.plugins:
        for plugin in args.plugins:
            try:
                imp.load_source('grafana_dashboards.components.$loaded', plugin)
            except Exception as e:
                print('Cannot load plugin %s: %s' % (plugin, str(e)))

    if args.project:
        logging.warn("Using deprecated option '--project'")
        args.path.add(args.project)
    paths = _process_paths(args.path)

    config = Config(args.config)
    exporters = set(args.exporters)
    if args.out:
        logging.warn("Using deprecated option '-o/--out'")
        exporters.add('file')
        config.get_config('file').update(output_folder=args.out)

    dashboard_exporters = _initialize_exporters(exporters, [FileExporter, ElasticSearchExporter, GrafanaExporter],
                                                config)

    context = config.get_config('context')
    context.update(yaml.load(args.context, Loader=yaml.FullLoader))

    projects = DefinitionParser().load_projects(paths)
    project_processor = ProjectProcessor(dashboard_exporters)
    project_processor.process_projects(projects, context)
Ejemplo n.º 9
0
class TestCaseConfig(TestCase):

    def setUp(self):
        super(TestCaseConfig, self).setUp()
        self.config = Config()

    def test_defaults(self):
        self.assertTrue(
            self.config.getboolean('cache', 'enabled'))
        self.assertEqual(
            self.config.get('cache', 'cachedir'), '~/.cache/grafyaml')
        self.assertEqual(
            self.config.get('grafana', 'apikey'), '')
        self.assertEqual(
            self.config.get('grafana', 'url'), 'http://localhost:8080')
Ejemplo n.º 10
0
 def read_config(self):
     self.config = Config()
     if self.args.config:
         fp = self.args.config
     else:
         fp = "/etc/grafyaml/grafyaml.conf"
     self.config.read(os.path.expanduser(fp))
     if self.args.grafana_url:
         self.config.set("grafana", "url", self.args.grafana_url)
         LOG.debug("Grafana URL override: {}".format(self.args.grafana_url))
     if self.args.grafana_apikey:
         self.config.set("grafana", "apikey", self.args.grafana_apikey)
         LOG.debug("Grafana APIKey overridden")
     if self.args.grafana_folderid:
         self.config.set("grafana", "folderid", self.args.grafana_folderid)
         LOG.debug("Grafana Folderid overridden")
Ejemplo n.º 11
0
 def read_config(self):
     self.config = Config()
     if self.args.config:
         fp = self.args.config
     else:
         fp = '/etc/grafyaml/grafyaml.conf'
     self.config.read(os.path.expanduser(fp))
Ejemplo n.º 12
0
class TestCase(testtools.TestCase):
    """Test case base class for all unit tests."""
    def setUp(self):
        super(TestCase, self).setUp()
        self.log_fixture = self.useFixture(
            fixtures.FakeLogger(level=logging.DEBUG))
        self.setup_config()
        self.cachedir = tempfile.mkdtemp()
        self.config.set('cache', 'cachedir', self.cachedir)
        self.addCleanup(self.cleanup_cachedir)

    def setup_config(self):
        self.config = Config()
        self.config.read(os.path.join(FIXTURE_DIR, 'grafyaml.conf'))

    def cleanup_cachedir(self):
        shutil.rmtree(self.cachedir)
Ejemplo n.º 13
0
class TestCase(testtools.TestCase):
    """Test case base class for all unit tests."""

    def setUp(self):
        super(TestCase, self).setUp()
        self.log_fixture = self.useFixture(fixtures.FakeLogger(
            level=logging.DEBUG))
        self.setup_config()
        self.cachedir = tempfile.mkdtemp()
        self.config.set('cache', 'cachedir', self.cachedir)
        self.addCleanup(self.cleanup_cachedir)

    def setup_config(self):
        self.config = Config()
        self.config.read(os.path.join(FIXTURE_DIR, 'grafyaml.conf'))

    def cleanup_cachedir(self):
        shutil.rmtree(self.cachedir)
Ejemplo n.º 14
0
def test_initialize_exporters():
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               'config.yaml')
    config = Config(config_file)
    # noinspection PyProtectedMember
    exporters = cli._initialize_exporters('dummy', [DummyExporter], config)

    assert exporters is not None
    assert len(exporters) == 1
    assert exporters[0].prop == 'value'
    assert exporters[0].kwargs == {'other': True}
Ejemplo n.º 15
0
 def read_config(self):
     self.config = Config()
     if self.args.config:
         fp = self.args.config
     else:
         fp = '/etc/grafyaml/grafyaml.conf'
     self.config.read(os.path.expanduser(fp))
     if self.args.grafana_url:
         self.config.set('grafana', 'url', self.args.grafana_url)
         LOG.debug('Grafana URL override: {}'.format(self.args.grafana_url))
     if self.args.grafana_apikey:
         self.config.set('grafana', 'apikey', self.args.grafana_apikey)
         LOG.debug('Grafana APIKey overridden')
     if self.args.grafana_folderid:
         self.config.set('grafana', 'folderid', self.args.grafana_folderid)
         LOG.debug('Grafana Folderid overridden')
Ejemplo n.º 16
0
class Client(object):

    def delete(self):
        LOG.info('Deleting schema in %s', self.args.path)
        builder = Builder(self.config)
        builder.delete(self.args.path)

    def main(self):
        self.parse_arguments()
        self.read_config()
        self.setup_logging()

        self.args.func()

    def parse_arguments(self):
        parser = argparse.ArgumentParser()
        parser.add_argument(
            '--config-file', dest='config', help='Path to a config file to '
            'use. The default file used is: /etc/grafyaml/grafyaml.conf')
        parser.add_argument(
            '--debug', dest='debug', action='store_true',
            help='Print debugging output (set logging level to DEBUG instead '
            ' of default INFO level)')
        parser.add_argument(
            '--version', dest='version', action='version',
            version=__version__, help="show "
            "program's version number and exit")

        subparsers = parser.add_subparsers(
            title='commands')

        parser_delete = subparsers.add_parser('delete')
        parser_delete.add_argument(
            'path', help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_delete.set_defaults(func=self.delete)

        parser_update = subparsers.add_parser('update')
        parser_update.add_argument(
            'path', help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_update.set_defaults(func=self.update)

        parser_validate = subparsers.add_parser('validate')
        parser_validate.add_argument(
            'path', help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_validate.set_defaults(func=self.validate)

        self.args = parser.parse_args()

    def read_config(self):
        self.config = Config()
        if self.args.config:
            fp = self.args.config
        else:
            fp = '/etc/grafyaml/grafyaml.conf'
        self.config.read(os.path.expanduser(fp))

    def setup_logging(self):
        if self.args.debug:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)

    def update(self):
        LOG.info('Updating schema in %s', self.args.path)
        builder = Builder(self.config)
        builder.update(self.args.path)

    def validate(self):
        LOG.info('Validating schema in %s', self.args.path)
        # NOTE(pabelanger): Disable caching support by default, in an effort
        # to improve performance.
        self.config.set('cache', 'enabled', 'false')
        builder = Builder(self.config)

        try:
            builder.load_files(self.args.path)
            print('SUCCESS!')
        except Exception as e:
            print('%s: ERROR: %s' % (self.args.path, e))
            sys.exit(1)
Ejemplo n.º 17
0
 def setUp(self):
     super(TestCaseConfig, self).setUp()
     self.config = Config()
Ejemplo n.º 18
0
 def setup_config(self):
     self.config = Config()
     self.config.read(os.path.join(FIXTURE_DIR, 'grafyaml.conf'))
Ejemplo n.º 19
0
class Client(object):
    def delete(self):
        LOG.info("Deleting schema in %s", self.args.path)
        builder = Builder(self.config)
        builder.delete(self.args.path)

    def main(self):
        self.parse_arguments()
        self.setup_logging()
        self.read_config()

        self.args.func()

    def parse_arguments(self):
        parser = argparse.ArgumentParser()
        parser.add_argument(
            "--config-file",
            dest="config",
            help="Path to a config file to "
            "use. The default file used is: /etc/grafyaml/grafyaml.conf",
        )
        parser.add_argument(
            "--debug",
            dest="debug",
            action="store_true",
            help="Print debugging output (set logging level to DEBUG instead "
            " of default INFO level)",
        )
        parser.add_argument(
            "--grafana-url",
            dest="grafana_url",
            help="URL for grafana "
            "server. The default used is: http://localhost:8080",
        )
        parser.add_argument(
            "--grafana-apikey",
            dest="grafana_apikey",
            help="API key to access grafana.",
            default=os.getenv("GRAFANA_API_KEY", None),
        )
        parser.add_argument(
            "--grafana-folderid",
            dest="grafana_folderid",
            help="The id of the folder to save the dashboard in.",
        )
        parser.add_argument(
            "--version",
            dest="version",
            action="version",
            version=__version__,
            help="show " "program's version number and exit",
        )

        subparsers = parser.add_subparsers(title="commands")
        subparsers.required = True

        parser_delete = subparsers.add_parser("delete")
        parser_delete.add_argument(
            "path", help="colon-separated list of paths to YAML files or" " directories"
        )
        parser_delete.set_defaults(func=self.delete)

        parser_update = subparsers.add_parser("update")
        parser_update.add_argument(
            "path", help="colon-separated list of paths to YAML files or" " directories"
        )
        parser_update.set_defaults(func=self.update)

        parser_validate = subparsers.add_parser("validate")
        parser_validate.add_argument(
            "path", help="colon-separated list of paths to YAML files or" " directories"
        )
        parser_validate.set_defaults(func=self.validate)

        self.args = parser.parse_args()

    def read_config(self):
        self.config = Config()
        if self.args.config:
            fp = self.args.config
        else:
            fp = "/etc/grafyaml/grafyaml.conf"
        self.config.read(os.path.expanduser(fp))
        if self.args.grafana_url:
            self.config.set("grafana", "url", self.args.grafana_url)
            LOG.debug("Grafana URL override: {}".format(self.args.grafana_url))
        if self.args.grafana_apikey:
            self.config.set("grafana", "apikey", self.args.grafana_apikey)
            LOG.debug("Grafana APIKey overridden")
        if self.args.grafana_folderid:
            self.config.set("grafana", "folderid", self.args.grafana_folderid)
            LOG.debug("Grafana Folderid overridden")

    def setup_logging(self):
        if self.args.debug:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)

    def update(self):
        LOG.info("Updating schema in %s", self.args.path)
        builder = Builder(self.config)
        builder.update(self.args.path)

    def validate(self):
        LOG.info("Validating schema in %s", self.args.path)
        # NOTE(pabelanger): Disable caching support by default, in an effort
        # to improve performance.
        self.config.set("cache", "enabled", "false")
        builder = Builder(self.config)

        try:
            builder.load_files(self.args.path)
            print("SUCCESS!")
        except Exception as e:
            print("%s: ERROR: %s" % (self.args.path, e))
            sys.exit(1)
Ejemplo n.º 20
0
 def setUp(self):
     super(TestCaseConfig, self).setUp()
     self.config = Config()
def test_existent_config_file():
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.yaml')
    config = Config(config_file)

    assert config.get_config('context') == {'component': 'frontend'}
    assert config.get_config('unknown') == {}
def test_nonexistent_config_file():
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'no_file.yaml')
    config = Config(config_file)

    assert config.get_config('context') == {}
    assert config.get_config('unknown') == {}
Ejemplo n.º 23
0
class Client(object):
    def delete(self):
        LOG.info('Deleting schema in %s', self.args.path)
        builder = Builder(self.config)
        builder.delete(self.args.path)

    def main(self):
        self.parse_arguments()
        self.read_config()
        self.setup_logging()

        self.args.func()

    def parse_arguments(self):
        parser = argparse.ArgumentParser()
        parser.add_argument(
            '--config-file',
            dest='config',
            help='Path to a config file to '
            'use. The default file used is: /etc/grafyaml/grafyaml.conf')
        parser.add_argument(
            '--debug',
            dest='debug',
            action='store_true',
            help='Print debugging output (set logging level to DEBUG instead '
            ' of default INFO level)')
        parser.add_argument('--version',
                            dest='version',
                            action='version',
                            version=__version__,
                            help="show "
                            "program's version number and exit")

        subparsers = parser.add_subparsers(title='commands')

        parser_delete = subparsers.add_parser('delete')
        parser_delete.add_argument(
            'path',
            help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_delete.set_defaults(func=self.delete)

        parser_update = subparsers.add_parser('update')
        parser_update.add_argument(
            'path',
            help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_update.set_defaults(func=self.update)

        parser_validate = subparsers.add_parser('validate')
        parser_validate.add_argument(
            'path',
            help='colon-separated list of paths to YAML files or'
            ' directories')
        parser_validate.set_defaults(func=self.validate)

        self.args = parser.parse_args()

    def read_config(self):
        self.config = Config()
        if self.args.config:
            fp = self.args.config
        else:
            fp = '/etc/grafyaml/grafyaml.conf'
        self.config.read(os.path.expanduser(fp))

    def setup_logging(self):
        if self.args.debug:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)

    def update(self):
        LOG.info('Updating schema in %s', self.args.path)
        builder = Builder(self.config)
        builder.update(self.args.path)

    def validate(self):
        LOG.info('Validating schema in %s', self.args.path)
        # NOTE(pabelanger): Disable caching support by default, in an effort
        # to improve performance.
        self.config.set('cache', 'enabled', 'false')
        builder = Builder(self.config)

        try:
            builder.load_files(self.args.path)
            print('SUCCESS!')
        except Exception as e:
            print('%s: ERROR: %s' % (self.args.path, e))
            sys.exit(1)