summaryrefslogtreecommitdiff
path: root/grafana_dashboards/cmd.py
blob: 232bf957234ef3033282ab8c4af51e22a092d561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
import logging
import os
import sys

from grafana_dashboards.builder import Builder
from grafana_dashboards.config import Config
from grafana_dashboards import version

LOG = logging.getLogger(__name__)


class Client(object):

    def delete(self):
        LOG.info('Deleting dashboards in %s', self.args.path)
        builder = Builder(self.config)
        builder.delete_dashboard(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.version_info.release_string(), 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 dashboards in %s', self.args.path)
        builder = Builder(self.config)
        builder.update_dashboard(self.args.path)

    def validate(self):
        LOG.info('Validating dashboards 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)


def main():
    client = Client()
    client.main()
    sys.exit(0)