Esempio n. 1
0
def get_config(cfg_path=None, options=None):
    agentConfig = {}

    # Config handling
    try:
        # Find the right config file
        path = os.path.realpath(__file__)
        path = os.path.dirname(path)

        config_path = get_config_path(cfg_path, os_name=get_os())
        config = configparser.ConfigParser()
        with open(config_path) as config_file:
            if is_p3k():
                config.read_file(skip_leading_wsp(config_file))
            else:
                config.readfp(skip_leading_wsp(config_file))

        # bulk import
        for option in config.options('Main'):
            agentConfig[option] = config.get('Main', option)

    except Exception:
        raise CfgNotFound

    return agentConfig
Esempio n. 2
0
def get_config(cfg_path=None, options=None):
    agentConfig = {}

    # Config handling
    try:
        # Find the right config file
        path = os.path.realpath(__file__)
        path = os.path.dirname(path)

        config_path = get_config_path(cfg_path, os_name=get_os())
        config = configparser.ConfigParser()
        config.readfp(skip_leading_wsp(open(config_path)))

        # bulk import
        for option in config.options('Main'):
            agentConfig[option] = config.get('Main', option)

    except configparser.NoSectionError as e:
        sys.stderr.write('Config file not found or incorrectly formatted.\n')
        sys.exit(2)

    except configparser.ParsingError as e:
        sys.stderr.write('Config file not found or incorrectly formatted.\n')
        sys.exit(2)

    except configparser.NoOptionError as e:
        sys.stderr.write('There are some items missing from your config file'
                         ', but nothing fatal [%s]' % e)

    return agentConfig
Esempio n. 3
0
    def load(self, config_file, api_key, app_key):
        config = configparser.ConfigParser()

        if api_key is not None and app_key is not None:
            self['api_key'] = api_key
            self['app_key'] = app_key
        else:
            if os.access(config_file, os.F_OK):
                config.read(config_file)
                if not config.has_section('Connection'):
                    report_errors({
                        'errors':
                        ['%s has no [Connection] section' % config_file]
                    })
            else:
                try:
                    response = ''
                    while response.strip().lower() not in ['y', 'n']:
                        response = get_input(
                            '%s does not exist. Would you like to'
                            ' create it? [Y/n] ' % config_file)
                        if response.strip().lower() in ['', 'y', 'yes']:
                            # Read the api and app keys from stdin
                            api_key = get_input(
                                "What is your api key? (Get it here: "
                                "https://app.datadoghq.com/account/settings#api) "
                            )
                            app_key = get_input(
                                "What is your application key? (Generate one here: "
                                "https://app.datadoghq.com/account/settings#api) "
                            )

                            # Write the config file
                            config.add_section('Connection')
                            config.set('Connection', 'apikey', api_key)
                            config.set('Connection', 'appkey', app_key)

                            f = open(config_file, 'w')
                            config.write(f)
                            f.close()
                            print('Wrote %s' % config_file)
                        elif response.strip().lower() == 'n':
                            # Abort
                            print_err('Exiting\n')
                            sys.exit(1)
                except KeyboardInterrupt:
                    # Abort
                    print_err('\nExiting')
                    sys.exit(1)

            self['api_key'] = config.get('Connection', 'apikey')
            self['app_key'] = config.get('Connection', 'appkey')
            if config.has_section('Proxy'):
                self['proxies'] = dict(config.items('Proxy'))
            if config.has_option('Connection', 'host_name'):
                self['host_name'] = config.get('Connection', 'host_name')
            if config.has_option('Connection', 'api_host'):
                self['api_host'] = config.get('Connection', 'api_host')
        assert self['api_key'] is not None and self['app_key'] is not None
Esempio n. 4
0
    def load(self, config_file, api_key, app_key):
        config = configparser.ConfigParser()

        if api_key is not None and app_key is not None:
            self["api_key"] = api_key
            self["app_key"] = app_key
        else:
            if os.access(config_file, os.F_OK):
                config.read(config_file)
                if not config.has_section("Connection"):
                    report_errors({
                        "errors":
                        ["%s has no [Connection] section" % config_file]
                    })
            else:
                try:
                    response = None
                    while response is None or response.strip().lower() not in [
                            "", "y", "n"
                    ]:
                        response = get_input(
                            "%s does not exist. Would you like to"
                            " create it? [Y/n] " % config_file)
                        if response.strip().lower() in ["", "y"]:
                            # Read the api and app keys from stdin
                            api_key = get_input(
                                "What is your api key? (Get it here: "
                                "https://app.datadoghq.com/account/settings#api) "
                            )
                            app_key = get_input(
                                "What is your application key? (Generate one here: "
                                "https://app.datadoghq.com/account/settings#api) "
                            )

                            # Write the config file
                            config.add_section("Connection")
                            config.set("Connection", "apikey", api_key)
                            config.set("Connection", "appkey", app_key)

                            f = open(config_file, "w")
                            config.write(f)
                            f.close()
                            print("Wrote %s" % config_file)
                        elif response.strip().lower() == "n":
                            # Abort
                            print_err("Exiting\n")
                            sys.exit(1)
                except KeyboardInterrupt:
                    # Abort
                    print_err("\nExiting")
                    sys.exit(1)

            self["api_key"] = config.get("Connection", "apikey")
            self["app_key"] = config.get("Connection", "appkey")
            if config.has_section("Proxy"):
                self["proxies"] = dict(config.items("Proxy"))
            if config.has_option("Connection", "host_name"):
                self["host_name"] = config.get("Connection", "host_name")
            if config.has_option("Connection", "api_host"):
                self["api_host"] = config.get("Connection", "api_host")
        assert self["api_key"] is not None and self["app_key"] is not None