def __init__(self, parser="json", provider="local-file"): try: if parser == "json": _parser = JsonParser() else: raise if provider == "metadata-service": self._get_value_for = MetadataService( parser=_parser).get_value_for elif provider == "local-file": self._get_value_for = LocalFile(parser=_parser).get_value_for else: raise agent_logger.log( logging.info, "DataHandler registered. Parser: %s, Provider: %s" % (parser, provider)) except Exception, e: agent_logger.log( logging.error, "DataHandler register error, Parser: %s, Provider: %s.\n%s" % (parser, provider, e.message))
def _cache_data(self, key, value=None): """ [WIP] YET FAKE Cache config data state for later comparison and only configure on difference with current cached config. """ agent_logger.log(logging.info, "[TBD] Caching data need to implemented.") return None
def run_command(cmd_name): try: if not cmd_name in commands.cmd_list.keys(): raise return commands.run(cmd_name) except: agent_logger.log(logging.error, "%s is not a registered command to run." % cmd_name) return None
def get_value_for(self, key): """ Fetch value from provider, cache and return if it's new else return None to indicate no action required. """ try: value = self._get_value_for(key) cached_value = self._cache_data(key) if value == cached_value: return None self._cache_data(key, value) return value except Exception, e: agent_logger.log(logging.error, "Exception in value for %s: %s" % (key, e.message)) return None
def get_value_for(self, key): """ Fetch value from provider, cache and return if it's new else return None to indicate no action required. """ try: value = self._get_value_for(key) cached_value = self._cache_data(key) if value == cached_value: return None self._cache_data(key, value) return value except Exception, e: agent_logger.log( logging.error, "Exception in value for %s: %s" % (key, e.message)) return None
def __init__(self, parser="json", provider="local-file"): try: if parser == "json": _parser = JsonParser() else: raise if provider == "metadata-service": self._get_value_for = MetadataService(parser=_parser).get_value_for elif provider == "local-file": self._get_value_for = LocalFile(parser=_parser).get_value_for else: raise agent_logger.log(logging.info, "DataHandler registered. Parser: %s, Provider: %s" % (parser, provider)) except Exception, e: agent_logger.log(logging.error, "DataHandler register error, Parser: %s, Provider: %s.\n%s" % (parser, provider, e.message))
def run(cmd, get_value_for): try: data_keys = cmd_list[cmd]["data_keys"] data_values = {} for _data_key in data_keys: data_values[_data_key] = get_value_for(_data_key) agent_logger.log(logging.info, ">>>> %s" % repr(data_keys)) agent_logger.log(logging.info, ">>>> %s" % repr(data_values)) config_result = cmd_list[cmd]["func"](data_values) agent_logger.log(logging.info, "Running '%s'" % cmd, data=data_values, result=config_result) return config_result except Exception, e: agent_logger.log(logging.error, e.message) return None
def wrap(_func): agent_logger.log(logging.error, str(data_keys)) if cmd_name in cmd_list.keys(): agent_logger.log( logging.error, "%s already exists in feature list. Duplicacy." % cmd_name) else: _data_keys = data_keys if isinstance(_data_keys, str): _data_keys = _data_keys.split() with open("/tmp/pylog", "a") as fyl: fyl.write(repr(_data_keys)) cmd_list[cmd_name] = {"func": _func, "data_keys": _data_keys} agent_logger.log( logging.info, "%s added to feature list with keys: %s." % (cmd_name, _data_keys)) return _func
def wrap(_func): agent_logger.log(logging.error, str(data_keys)) if cmd_name in cmd_list.keys(): agent_logger.log(logging.error, "%s already exists in feature list. Duplicacy." % cmd_name) else: _data_keys = data_keys if isinstance(_data_keys, str): _data_keys = _data_keys.split() with open("/tmp/pylog", "a") as fyl: fyl.write(repr(_data_keys)) cmd_list[cmd_name] = {"func": _func, "data_keys": _data_keys} agent_logger.log(logging.info, "%s added to feature list with keys: %s." % (cmd_name, _data_keys)) return _func