mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-18 18:01:31 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -64,6 +64,7 @@ from ansible.parsing.ajson import AnsibleJSONEncoder
|
|||
|
||||
try:
|
||||
from logdna import LogDNAHandler
|
||||
|
||||
HAS_LOGDNA = True
|
||||
except ImportError:
|
||||
HAS_LOGDNA = False
|
||||
|
|
@ -72,12 +73,12 @@ except ImportError:
|
|||
# Getting MAC Address of system:
|
||||
def get_mac():
|
||||
mac = f"{getnode():012x}"
|
||||
return ":".join(map(lambda index: mac[index:index + 2], range(int(len(mac) / 2))))
|
||||
return ":".join(map(lambda index: mac[index : index + 2], range(int(len(mac) / 2))))
|
||||
|
||||
|
||||
# Getting hostname of system:
|
||||
def get_hostname():
|
||||
return str(socket.gethostname()).split('.local', 1)[0]
|
||||
return str(socket.gethostname()).split(".local", 1)[0]
|
||||
|
||||
|
||||
# Getting IP of system:
|
||||
|
|
@ -87,10 +88,10 @@ def get_ip():
|
|||
except Exception:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
s.connect(('10.255.255.255', 1))
|
||||
s.connect(("10.255.255.255", 1))
|
||||
IP = s.getsockname()[0]
|
||||
except Exception:
|
||||
IP = '127.0.0.1'
|
||||
IP = "127.0.0.1"
|
||||
finally:
|
||||
s.close()
|
||||
return IP
|
||||
|
|
@ -107,10 +108,9 @@ def isJSONable(obj):
|
|||
|
||||
# LogDNA Callback Module:
|
||||
class CallbackModule(CallbackBase):
|
||||
|
||||
CALLBACK_VERSION = 0.1
|
||||
CALLBACK_TYPE = 'notification'
|
||||
CALLBACK_NAME = 'community.general.logdna'
|
||||
CALLBACK_TYPE = "notification"
|
||||
CALLBACK_NAME = "community.general.logdna"
|
||||
CALLBACK_NEEDS_WHITELIST = True
|
||||
|
||||
def __init__(self, display=None):
|
||||
|
|
@ -127,27 +127,27 @@ class CallbackModule(CallbackBase):
|
|||
def set_options(self, task_keys=None, var_options=None, direct=None):
|
||||
super().set_options(task_keys=task_keys, var_options=var_options, direct=direct)
|
||||
|
||||
self.conf_key = self.get_option('conf_key')
|
||||
self.plugin_ignore_errors = self.get_option('plugin_ignore_errors')
|
||||
self.conf_hostname = self.get_option('conf_hostname')
|
||||
self.conf_tags = self.get_option('conf_tags')
|
||||
self.conf_key = self.get_option("conf_key")
|
||||
self.plugin_ignore_errors = self.get_option("plugin_ignore_errors")
|
||||
self.conf_hostname = self.get_option("conf_hostname")
|
||||
self.conf_tags = self.get_option("conf_tags")
|
||||
self.mac = get_mac()
|
||||
self.ip = get_ip()
|
||||
|
||||
if self.conf_hostname is None:
|
||||
self.conf_hostname = get_hostname()
|
||||
|
||||
self.conf_tags = self.conf_tags.split(',')
|
||||
self.conf_tags = self.conf_tags.split(",")
|
||||
|
||||
if HAS_LOGDNA:
|
||||
self.log = logging.getLogger('logdna')
|
||||
self.log = logging.getLogger("logdna")
|
||||
self.log.setLevel(logging.INFO)
|
||||
self.options = {'hostname': self.conf_hostname, 'mac': self.mac, 'index_meta': True}
|
||||
self.options = {"hostname": self.conf_hostname, "mac": self.mac, "index_meta": True}
|
||||
self.log.addHandler(LogDNAHandler(self.conf_key, self.options))
|
||||
self.disabled = False
|
||||
else:
|
||||
self.disabled = True
|
||||
self._display.warning('WARNING:\nPlease, install LogDNA Python Package: `pip install logdna`')
|
||||
self._display.warning("WARNING:\nPlease, install LogDNA Python Package: `pip install logdna`")
|
||||
|
||||
def metaIndexing(self, meta):
|
||||
invalidKeys = []
|
||||
|
|
@ -159,25 +159,25 @@ class CallbackModule(CallbackBase):
|
|||
if ninvalidKeys > 0:
|
||||
for key in invalidKeys:
|
||||
del meta[key]
|
||||
meta['__errors'] = f"These keys have been sanitized: {', '.join(invalidKeys)}"
|
||||
meta["__errors"] = f"These keys have been sanitized: {', '.join(invalidKeys)}"
|
||||
return meta
|
||||
|
||||
def sanitizeJSON(self, data):
|
||||
try:
|
||||
return json.loads(json.dumps(data, sort_keys=True, cls=AnsibleJSONEncoder))
|
||||
except Exception:
|
||||
return {'warnings': ['JSON Formatting Issue', json.dumps(data, sort_keys=True, cls=AnsibleJSONEncoder)]}
|
||||
return {"warnings": ["JSON Formatting Issue", json.dumps(data, sort_keys=True, cls=AnsibleJSONEncoder)]}
|
||||
|
||||
def flush(self, log, options):
|
||||
if HAS_LOGDNA:
|
||||
self.log.info(json.dumps(log), options)
|
||||
|
||||
def sendLog(self, host, category, logdata):
|
||||
options = {'app': 'ansible', 'meta': {'playbook': self.playbook_name, 'host': host, 'category': category}}
|
||||
logdata['info'].pop('invocation', None)
|
||||
warnings = logdata['info'].pop('warnings', None)
|
||||
options = {"app": "ansible", "meta": {"playbook": self.playbook_name, "host": host, "category": category}}
|
||||
logdata["info"].pop("invocation", None)
|
||||
warnings = logdata["info"].pop("warnings", None)
|
||||
if warnings is not None:
|
||||
self.flush({'warn': warnings}, options)
|
||||
self.flush({"warn": warnings}, options)
|
||||
self.flush(logdata, options)
|
||||
|
||||
def v2_playbook_on_start(self, playbook):
|
||||
|
|
@ -188,21 +188,21 @@ class CallbackModule(CallbackBase):
|
|||
result = dict()
|
||||
for host in stats.processed.keys():
|
||||
result[host] = stats.summarize(host)
|
||||
self.sendLog(self.conf_hostname, 'STATS', {'info': self.sanitizeJSON(result)})
|
||||
self.sendLog(self.conf_hostname, "STATS", {"info": self.sanitizeJSON(result)})
|
||||
|
||||
def runner_on_failed(self, host, res, ignore_errors=False):
|
||||
if self.plugin_ignore_errors:
|
||||
ignore_errors = self.plugin_ignore_errors
|
||||
self.sendLog(host, 'FAILED', {'info': self.sanitizeJSON(res), 'ignore_errors': ignore_errors})
|
||||
self.sendLog(host, "FAILED", {"info": self.sanitizeJSON(res), "ignore_errors": ignore_errors})
|
||||
|
||||
def runner_on_ok(self, host, res):
|
||||
self.sendLog(host, 'OK', {'info': self.sanitizeJSON(res)})
|
||||
self.sendLog(host, "OK", {"info": self.sanitizeJSON(res)})
|
||||
|
||||
def runner_on_unreachable(self, host, res):
|
||||
self.sendLog(host, 'UNREACHABLE', {'info': self.sanitizeJSON(res)})
|
||||
self.sendLog(host, "UNREACHABLE", {"info": self.sanitizeJSON(res)})
|
||||
|
||||
def runner_on_async_failed(self, host, res, jid):
|
||||
self.sendLog(host, 'ASYNC_FAILED', {'info': self.sanitizeJSON(res), 'job_id': jid})
|
||||
self.sendLog(host, "ASYNC_FAILED", {"info": self.sanitizeJSON(res), "job_id": jid})
|
||||
|
||||
def runner_on_async_ok(self, host, res, jid):
|
||||
self.sendLog(host, 'ASYNC_OK', {'info': self.sanitizeJSON(res), 'job_id': jid})
|
||||
self.sendLog(host, "ASYNC_OK", {"info": self.sanitizeJSON(res), "job_id": jid})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue