-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpipam-integrator.py
executable file
·166 lines (115 loc) · 4.56 KB
/
phpipam-integrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env python3
import argparse
import configparser
import hashlib
import os
import sys
import tempfile
import jinja2
import requests
import requests.auth
class API(object):
""" This is a (very limited) read-only wrapper for the phpIPAM API.
"""
def __init__(self,
url,
app,
username,
password):
self.__url = '%s/api/%s' % (url, app)
# Open session
self.__session = requests.session()
# Get authentication token
authentication = self.__post(('user',),
auth=requests.auth.HTTPBasicAuth(username, password))
# Use token for every other request
self.__session.headers['phpipam-token'] = authentication['token']
def __query(self,
method,
endpoint,
**kwargs):
# Flatten and stringify the endpoint
if isinstance(endpoint, (list, tuple)):
endpoint = '/'.join(str(e) for e in endpoint)
else:
endpoint = str(endpoint)
# Get the executor for the selected method
executor = getattr(self.__session, method)
# Execute the request
response = executor('%s/%s/' % (self.__url, endpoint), **kwargs)
# Handle errors
response.raise_for_status()
# Return the data
return response.json()['data']
def __get(self, endpoint, **kwargs):
return self.__query('get', endpoint, **kwargs)
def __post(self, endpoint, data=None, **kwargs):
return self.__query('post', endpoint, data=data, **kwargs)
def subnets(self,
network):
return self.__get(('subnets', 'cidr', network))
def subnet_addresses(self, subnet_id):
return self.__get(('subnets', subnet_id, 'addresses'))
def address(self, address_id):
return self.__get(('addresses', address_id))
def main():
argparser = argparse.ArgumentParser("phpIPAM integrator")
argparser.add_argument('-c', '--config',
dest='config',
type=argparse.FileType('r', encoding='utf-8'),
default='/etc/phpipam-integrator')
args = argparser.parse_args()
# Read the configuration file
config = configparser.ConfigParser()
config.read_file(args.config)
# Connect to the API
api = API(url=config.get(configparser.DEFAULTSECT, 'url'),
app=config.get(configparser.DEFAULTSECT, 'app'),
username=config.get(configparser.DEFAULTSECT, 'username'),
password=config.get(configparser.DEFAULTSECT, 'password'))
# Collect all triggers for execution
triggers = set()
# Handle all defined sections
for section in config.sections():
template = config.get(section, 'template')
output = config.get(section, 'output')
trigger = config.get(section, 'trigger')
original_hash = hashlib.sha1()
modified_hash = hashlib.sha1()
# Get the hash of the unmodified output
try:
with open(output, mode='r', encoding='utf-8') as original:
for chunk in iter(lambda: original.read(128 * original_hash.block_size), ''):
original_hash.update(chunk.encode('utf-8'))
except FileNotFoundError:
pass
# Load the template
with open(template, mode='r', encoding='utf-8') as f:
template = jinja2.Template(f.read())
# Render the template to a temporary file and get the modified hash
with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) as modified:
context = {'api': api,
'section': section}
context.update(config.items(section))
# Render the template in chunks
stream = template.stream(context)
stream.enable_buffering()
# Write the chunk
for chunk in stream:
modified.write(chunk)
modified_hash.update(chunk.encode('utf-8'))
# Check if the file was changed
if original_hash.digest() != modified_hash.digest():
# Copy the temporary to the output file
os.rename(modified.name, output)
# Remember trigger for execution
triggers.add(trigger)
else:
# Delete the temporary file
os.unlink(modified.name)
# Execute triggers
for trigger in triggers:
# Execute the trigger
os.system(trigger)
if __name__ == '__main__':
main()