Skip to content

Commit

Permalink
Refactor into fancy python program
Browse files Browse the repository at this point in the history
Also use pynetbox

The logging function currently doesn't work.
  • Loading branch information
trickeydan committed Nov 10, 2019
1 parent 342ffad commit 87d15e1
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 10 deletions.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

# Created by https://www.gitignore.io/api/vim,python
# Edit at https://www.gitignore.io/?templates=vim,python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

### Vim ###
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
Sessionx.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~

# End of https://www.gitignore.io/api/vim,python
venv
10 changes: 0 additions & 10 deletions build.py

This file was deleted.

11 changes: 11 additions & 0 deletions icingagen/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Icinga Config Generator."""

from .logging import logger_setup
from .render import render

NETBOX_URL = "http://netbox.sown.org.uk"

__all__ = [
"NETBOX_URL",
"render",
]
8 changes: 8 additions & 0 deletions icingagen/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Main file for config generator."""
from pynetbox.api import Api

from icingagen import render, NETBOX_URL

nb = Api(NETBOX_URL, ssl_verify=False)

render(nb.dcim.devices.all())
16 changes: 16 additions & 0 deletions icingagen/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Logger Setup."""
import logging
import sys


def logger_setup() -> None:
"""Setup the logger."""
root = logging.getLogger()
root.setLevel(logging.INFO)

handler = logging.StreamHandler(sys.stderr)

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
handler.setFormatter(formatter)

root.addHandler(handler)
11 changes: 11 additions & 0 deletions icingagen/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Render Icinga2 config."""
import logging
import sys

import jinja2

def render(devices):
jinjaEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./"))
template = jinjaEnv.get_template("config.j2")

print(template.render(devices=devices))

0 comments on commit 87d15e1

Please sign in to comment.