Commit fbcdfeec authored by Morten Brekkevold's avatar Morten Brekkevold

add ability to log to slack if a webhook is set in environment

parent ee056c99
......@@ -16,6 +16,8 @@ import requests
import argparse
from IPy import IP
from slack_log_handler import SlackLogHandler
from kindnavsync.navapi import NAVAPI, ObjectNotFound
KIND_SERVICE_URL = "https://kind.cs.uninett.no/autoregister/utstyr_eksport.json"
......@@ -67,7 +69,9 @@ def parse_args():
description=
"Synchronizes devices in NAV with those from Kind",
epilog="Since Kind only stores names of devices, while NAV requires "
"specific IP addresses, up-to-date DNS entries are required.",
"specific IP addresses, up-to-date DNS entries are required. "
"If the environment variable SLACK_URL is set, this program "
"will post log messages to Slack using this as a webhook URL.",
)
parser.add_argument("--dry-run", "-n", action="store_true",
help="don't change anything in NAV, just print what "
......@@ -97,12 +101,19 @@ def parse_args():
def initlogging(debug=False, no_timestamps=False):
format = "[%(levelname)s] [%(name)s] %(message)s"
format_ = "[%(levelname)s] [%(name)s] %(message)s"
if not no_timestamps:
format = "%(asctime)s " + format
logging.basicConfig(format=format)
format_ = "%(asctime)s " + format_
logging.basicConfig(format=format_)
LOG.setLevel(logging.DEBUG if debug else logging.INFO)
webhook_url = os.getenv('SLACK_URL')
if webhook_url:
slack_handler = SlackLogHandler(webhook_url)
slack_format = logging.Formatter('*%(levelname)s*: %(message)s')
slack_handler.setFormatter(slack_format)
LOG.addHandler(slack_handler)
def get_kind_data():
LOG.debug("Henter data fra %s", KIND_SERVICE_URL)
......@@ -274,7 +285,8 @@ def update_nav(kind_devices, api_url, api_token, dry_run=False):
def insert_into_nav(nav_api, kind_devices):
LOG.info("Legger inn %d nye bokser i NAV", len(kind_devices))
if kind_devices:
LOG.info("Legger inn %d nye bokser i NAV", len(kind_devices))
for dev in kind_devices:
try:
insert_one_into_nav(nav_api, dev)
......
......@@ -3,7 +3,7 @@ from setuptools import setup
setup(
name="kindnavsync",
version="0.2.3",
version="0.2.4",
author="Morten Brekkevold",
author_email="morten.brekkevold@uninett.no",
description="Synchronizes device information from Kind to NAV",
......@@ -15,6 +15,7 @@ setup(
install_requires=[
'IPy>=0.81',
'requests',
'slack_log_handler',
],
python_requires='>3.4.0',
classifiers=[
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment