Run pylint in build pipeline ()

* Run pylint in build pipeline

* Only report on pylint errors and fix them
This commit is contained in:
Nick Satterly 2020-02-19 22:45:42 +01:00 committed by GitHub
parent e072f005e7
commit 4d0961a003
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 77 additions and 62 deletions

2
.gitignore vendored
View file

@ -38,3 +38,5 @@ nosetests.xml
.idea .idea
TODO TODO
*.log *.log
venv

5
.pylintrc Normal file
View file

@ -0,0 +1,5 @@
[MESSAGES CONTROL]
disable=R,C,W,import-error,broad-except
[TYPECHECK]
ignored-classes=_socketobject

View file

@ -27,4 +27,7 @@ install:
- pip install -r requirements-dev.txt - pip install -r requirements-dev.txt
script: script:
- pylint integrations/*/*.py
- pylint plugins/*/*.py
- pylint webhooks/*/*.py
- pytest -v webhooks/*/test* - pytest -v webhooks/*/test*

View file

@ -1,12 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
from alertaclient.api import Client
import sys
import os
import time
import json import json
import os
import consul import consul
import sys
import time import time
from alertaclient.api import Client
CONSUL_HOST = os.environ.get('CONSUL_HOST', '127.0.0.1') CONSUL_HOST = os.environ.get('CONSUL_HOST', '127.0.0.1')
CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500)) CONSUL_PORT = int(os.environ.get('CONSUL_PORT', 8500))
@ -19,13 +19,13 @@ print(j)
try: try:
url = client.kv.get('alerta/apiurl')[1]['Value'] url = client.kv.get('alerta/apiurl')[1]['Value']
except: except Exception:
print("No URL defined, exiting") print("No URL defined, exiting")
sys.exit(1) sys.exit(1)
try: try:
key = client.kv.get('alerta/apikey')[1]['Value'] key = client.kv.get('alerta/apikey')[1]['Value']
except: except Exception:
print("No key defined, exiting") print("No key defined, exiting")
sys.exit(1) sys.exit(1)
@ -72,13 +72,13 @@ SEVERITY_MAP = {
def createalert( data ): def createalert( data ):
try: try:
environment = client.kv.get('alerta/env/{0}'.format(data['Node']))[1]['Value'] environment = client.kv.get('alerta/env/{0}'.format(data['Node']))[1]['Value']
except: except Exception:
try: try:
environment = client.kv.get('alerta/defaultenv')[1]['Value'] environment = client.kv.get('alerta/defaultenv')[1]['Value']
except: except Exception:
environment = "Production" environment = "Production"
for i in range(max_retries): for _ in range(max_retries):
try: try:
print("Response:") print("Response:")
response = api.send_alert( response = api.send_alert(

View file

@ -17,7 +17,7 @@ origin = client.kv.get('alerta/origin')[1]['Value']
api = Client(endpoint=url, key=key) api = Client(endpoint=url, key=key)
def createheartbeat(): def createheartbeat():
for i in range(max_retries): for _ in range(max_retries):
try: try:
print(api.heartbeat(origin=origin, timeout=timeout)) print(api.heartbeat(origin=origin, timeout=timeout))
except Exception as e: except Exception as e:

View file

@ -1,5 +1,5 @@
from setuptools import setup, find_packages from setuptools import setup
version = '1.1.1' version = '1.1.1'

View file

@ -9,6 +9,8 @@ import re
import signal import signal
import smtplib import smtplib
import socket import socket
from functools import reduce
import sys import sys
import threading import threading
import time import time
@ -213,7 +215,7 @@ class MailSender(threading.Thread):
return True return True
LOG.debug('Regex %s matches nothing', regex) LOG.debug('Regex %s matches nothing', regex)
return False return False
elif isinstance(value, str) or isinstance(value, unicode): elif isinstance(value, str) or isinstance(value, unicode): # pylint: disable=undefined-variable
LOG.debug('Trying to match %s to %s', LOG.debug('Trying to match %s to %s',
value, regex) value, regex)
return re.search(regex, value) is not None return re.search(regex, value) is not None
@ -302,14 +304,14 @@ class MailSender(threading.Thread):
LOG.debug('%s : Email sent to %s' % (alert.get_id(), LOG.debug('%s : Email sent to %s' % (alert.get_id(),
','.join(contacts))) ','.join(contacts)))
return (msg, contacts) return (msg, contacts)
except (socket.error, socket.herror, socket.gaierror) as e:
LOG.error('Mail server connection error: %s', e)
return None
except smtplib.SMTPException as e: except smtplib.SMTPException as e:
LOG.error('Failed to send mail to %s on %s:%s : %s', LOG.error('Failed to send mail to %s on %s:%s : %s',
", ".join(contacts), ", ".join(contacts),
OPTIONS['smtp_host'], OPTIONS['smtp_port'], e) OPTIONS['smtp_host'], OPTIONS['smtp_port'], e)
return None return None
except (socket.error, socket.herror, socket.gaierror) as e:
LOG.error('Mail server connection error: %s', e)
return None
except Exception as e: except Exception as e:
LOG.error('Unexpected error while sending email: {}'.format(str(e))) # nopep8 LOG.error('Unexpected error while sending email: {}'.format(str(e))) # nopep8
return None return None

View file

@ -8,7 +8,7 @@ checks = [
"resource": "www.google.com", "resource": "www.google.com",
"url": "http://www.google.com?q=foo#q=foo", "url": "http://www.google.com?q=foo#q=foo",
"environment": "Production", "environment": "Production",
"service": ["Google", "Search"] "service": ["Google", "Search"],
"api_endpoint": "http://localhost:8080", "api_endpoint": "http://localhost:8080",
"api_key": None, "api_key": None,
}, },

View file

@ -1,20 +1,20 @@
import platform import datetime
import sys
import time
import urllib.request, urllib.error, urllib.parse
import json import json
import threading import logging
import platform
import queue import queue
import re import re
import logging
import datetime
import ssl
import socket import socket
import ssl
from alertaclient.api import Client import threading
from http.server import BaseHTTPRequestHandler as BHRH from http.server import BaseHTTPRequestHandler as BHRH
from urllib.error import URLError # pylint: disable=no-name-in-module
from urllib.parse import urlparse # pylint: disable=no-name-in-module
from urllib.request import build_opener, ProxyHandler, HTTPBasicAuthHandler, install_opener, Request, urlopen # pylint: disable=no-name-in-module
import sys
import time
from alertaclient.api import Client
HTTP_RESPONSES = dict([(k, v[0]) for k, v in list(BHRH.responses.items())]) HTTP_RESPONSES = dict([(k, v[0]) for k, v in list(BHRH.responses.items())])
@ -240,8 +240,8 @@ class WorkerThread(threading.Thread):
if check_ssl: if check_ssl:
ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z' ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'
context = ssl.create_default_context() context = ssl.create_default_context()
domain = '{uri.netloc}'.format(uri=urllib.parse.urlparse(check.get('url'))) domain = '{uri.netloc}'.format(uri=urlparse(check.get('url')))
port = urllib.parse.urlparse(check.get('url')).port or 443 port = urlparse(check.get('url')).port or 443
conn = context.wrap_socket( conn = context.wrap_socket(
socket.socket(socket.AF_INET), socket.socket(socket.AF_INET),
server_hostname=domain server_hostname=domain
@ -311,42 +311,42 @@ class WorkerThread(threading.Thread):
start = time.time() start = time.time()
if username and password: if username and password:
auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler = HTTPBasicAuthHandler()
auth_handler.add_password(realm=realm, auth_handler.add_password(realm=realm,
uri=uri, uri=uri,
user=username, user=username,
passwd=password) passwd=password)
if proxy: if proxy:
opener = urllib.request.build_opener(auth_handler, urllib.request.ProxyHandler(proxy)) opener = build_opener(auth_handler, ProxyHandler(proxy))
else: else:
opener = urllib.request.build_opener(auth_handler) opener = build_opener(auth_handler)
else: else:
if proxy: if proxy:
opener = urllib.request.build_opener(urllib.request.ProxyHandler(proxy)) opener = build_opener(ProxyHandler(proxy))
else: else:
opener = urllib.request.build_opener() opener = build_opener()
urllib.request.install_opener(opener) install_opener(opener)
if 'User-agent' not in headers: if 'User-agent' not in headers:
headers['User-agent'] = 'alert-urlmon/%s' % (__version__) headers['User-agent'] = 'alert-urlmon/%s' % (__version__)
try: try:
if post: if post:
req = urllib.request.Request(url, json.dumps(post), headers=headers) req = Request(url, json.dumps(post), headers=headers)
else: else:
req = urllib.request.Request(url, headers=headers) req = Request(url, headers=headers)
response = urllib.request.urlopen(req, None, MAX_TIMEOUT) response = urlopen(req, None, MAX_TIMEOUT)
except ValueError as e: except ValueError as e:
LOG.error('Request failed: %s', e) LOG.error('Request failed: %s' % e)
except urllib.error.URLError as e: except URLError as e:
if hasattr(e, 'reason'): if hasattr(e, 'reason'):
reason = str(e.reason) reason = str(e.reason)
status = None status = None
elif hasattr(e, 'code'): elif hasattr(e, 'code'):
reason = None reason = None
status = e.code status = e.code # pylint: disable=no-member
except Exception as e: except Exception as e:
LOG.warning('Unexpected error: %s', e) LOG.warning('Unexpected error: %s' % e)
else: else:
status = response.getcode() status = response.getcode()
body = response.read() body = response.read()
@ -412,7 +412,7 @@ class UrlmonDaemon(object):
event='big queue for http checks', event='big queue for http checks',
value=self.queue.qsize(), value=self.queue.qsize(),
severity=severity, severity=severity,
text='URL check queue length is %d', self.queue.qsize(), text='URL check queue length is %d' % self.queue.qsize(),
event_type='serviceAlert', event_type='serviceAlert',
) )
except Exception as e: except Exception as e:

View file

@ -22,22 +22,24 @@ class TriggerEvent(PluginBase):
def pre_receive(self, alert, **kwargs): def pre_receive(self, alert, **kwargs):
return alert return alert
@staticmethod
def _event_type(severity):
if severity in ['cleared', 'normal', 'ok']:
return "close"
else:
return "open"
def post_receive(self, alert, **kwargs): def post_receive(self, alert, **kwargs):
if alert.repeat: if alert.repeat:
return return
message = "%s: %s alert for %s - %s" %( alert.environment, alert.severity.capitalize(), ','.join(alert.service), alert.resource) message = "%s: %s alert for %s - %s" %( alert.environment, alert.severity.capitalize(), ','.join(alert.service), alert.resource)
if alert.severity in ['cleared', 'normal', 'ok']:
event_type = "close"
else:
event_type = "open"
payload = { payload = {
"source_id": alert.id, "source_id": alert.id,
"source_status": event_type, "source_status": TriggerEvent._event_type(alert.severity),
"description": message, "description": message,
"resource": alert.resource, "resource": alert.resource,
"source": "alerta", "source": "alerta",
@ -54,11 +56,12 @@ class TriggerEvent(PluginBase):
def status_change(self, alert, status, text, **kwargs): def status_change(self, alert, status, text, **kwargs):
if status not in ['ack', 'assign']: if status not in ['ack', 'assign']:
return return
message = "%s: %s alert for %s - %s" %( alert.environment, alert.severity.capitalize(), ','.join(alert.service), alert.resource) message = "%s: %s alert for %s - %s" %( alert.environment, alert.severity.capitalize(), ','.join(alert.service), alert.resource)
payload = { payload = {
"source_id": alert.id, "source_id": alert.id,
"source_status": event_type, "source_status": TriggerEvent._event_type(alert.severity),
"description": message, "description": message,
"resource": alert.resource, "resource": alert.resource,
"source": "alerta", "source": "alerta",

View file

@ -22,15 +22,12 @@ MATTERMOST_USERNAME = os.environ.get(
class ServiceIntegration(PluginBase): class ServiceIntegration(PluginBase):
def __init__(self, name=None):
super().__init__(name)
def pre_receive(self, alert): def pre_receive(self, alert):
return alert
LOG.debug('Mattermost: %s', alert) LOG.debug('Mattermost: %s', alert)
return alert
def get_icon(self, status): def get_icon(self, status):
LOG.debug('Mattermost: %s', status)
return { return {
'security': ':closed_lock_with_key:', 'security': ':closed_lock_with_key:',
'critical': ':bangbang:', 'critical': ':bangbang:',
@ -42,9 +39,9 @@ class ServiceIntegration(PluginBase):
'trace': ':signal_strength:', 'trace': ':signal_strength:',
'ok': ':ok:' 'ok': ':ok:'
}.get(status, ':ok:') }.get(status, ':ok:')
LOG.debug('Mattermost: %s', status)
def _prepare_payload(self, alert): def _prepare_payload(self, alert):
LOG.debug('Mattermost: %s', alert)
return "{} **{}** **{}**\n`{}` ```{}```".format( return "{} **{}** **{}**\n`{}` ```{}```".format(
self.get_icon(alert.severity), self.get_icon(alert.severity),
alert.severity, alert.severity,
@ -52,7 +49,6 @@ class ServiceIntegration(PluginBase):
alert.event, alert.event,
alert.text, alert.text,
) )
LOG.debug('Mattermost: %s', alert)
def post_receive(self, alert): def post_receive(self, alert):
if alert.repeat: if alert.repeat:

View file

@ -5,6 +5,8 @@ import os
import requests import requests
import traceback import traceback
LOG = logging.getLogger('alerta.plugins.slack')
try: try:
from jinja2 import Template from jinja2 import Template
except Exception as e: except Exception as e:
@ -56,6 +58,7 @@ SLACK_HEADERS = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
class ServiceIntegration(PluginBase): class ServiceIntegration(PluginBase):
def __init__(self, name=None): def __init__(self, name=None):

View file

@ -1,2 +1,3 @@
pylint
pytest pytest
alerta-server[postgres] alerta-server[postgres]