-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (112 loc) · 4 KB
/
main.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
import re
from flask import Flask, request, send_from_directory, render_template, Response, send_file
from pathlib import Path
import requests
import xmltodict
import pynetbox
import json
app = Flask(__name__, template_folder="templates")
current_dir = Path(__file__)
SERIAL_NUM_RE = re.compile(r'PID:(?P<product_id>\w+),VID:(?P<hw_version>\w+),SN:(?P<serial_number>\w+)')
session = requests.Session()
session.verify = False
nb = pynetbox.api(
'https://192.0.2.10',
token='12345678'
)
nb.http_session = session
def work_request(host, type="device_info"):
url = f"http://{host}/pnp/WORK-REQUEST"
with open(current_dir / f"{type}.xml") as f:
data = f.read()
return requests.post(url, data)
def get_device_info(host):
url = f"http://{host}/pnp/WORK-REQUEST"
@app.route('/test-xml')
def test_xml():
result = render_template('load_config.xml', correlator_id="123", config_filename="test.cfg", udi="123")
return Response(result, mimetype='text/xml')
@app.route('/')
def root():
return 'Hello Stream!'
@app.route('/configs/<path:path>')
def serve_configs(path):
return send_from_directory('configs', path)
@app.route('/config/provision.cfg')
def config():
return render_template('provision.cfg')
@app.route('/images/<path:path>')
def serve_sw_images(path):
return send_from_directory('sw_images', path)
@app.route('/pnp/HELLO')
def pnp_hello():
return '', 200
@app.route('/pnp/WORK-REQUEST', methods=['POST'])
def pnp_work_request():
print(request.data)
data = xmltodict.parse(request.data)
print(data)
correlator_id = data['pnp']['info']['@correlator']
print(correlator_id)
udi = data['pnp']['@udi']
print(udi)
udi_match = re.match(r'PID:(?P<product_id>[\w-]+),VID:(?P<hw_version>\w+),SN:(?P<serial_number>\w+)', udi)
serial_number = udi_match.group('serial_number')
product_id = udi_match.group('product_id')
print (product_id, serial_number)
print("count: " + str(nb.dcim.devices.count(serial=serial_number)))
if nb.dcim.devices.count(serial=serial_number) == 0:
print ("Device not in netbox. Adding ...")
device = nb.dcim.devices.create(
name = 'staging',
serial = serial_number,
status='planned',
site = nb.dcim.sites.get(name='home').id,
device_type=nb.dcim.device_types.get(model=product_id).id,
role=nb.dcim.device_roles.get(name='switch').id
)
return '', 200
else:
device = nb.dcim.devices.get(serial=serial_number)
if device.status.value == 'staged':
config_filename = device.serial + '.cfg'
print(config_filename)
#
jinja_context = {
"device": device
}
config_text = render_template('provision.jinja', **jinja_context)
print (config_text)
#
url = "http://192.0.2.10/api/dcim/devices/" + str(device.id) + "/render-config/"
headers = {"Content-Type": "application/json", "Authorization": "Token 12345678"}
response = requests.post(url, headers=headers)
# print("JSON Response ", response.json.content())
print(response.json().get("content") )
#
with open("configs/"+config_filename, "w") as f:
f.write(response.json().get("content"))
#
jinja_context = {
"udi": udi,
"correlator_id": correlator_id,
"config_filename": config_filename,
}
result_data = render_template('config-upgrade.xml', **jinja_context)
print (result_data)
#
return Response(result_data, mimetype='text/xml')
else:
return '',200
@app.route('/pnp/WORK-RESPONSE', methods=['POST'])
def pnp_work_response():
print(request.data)
data = xmltodict.parse(request.data)
correlator_id = data['pnp']['response']['@correlator']
udi = data['pnp']['@udi']
jinja_context = {
"udi": udi,
"correlator_id": correlator_id,
}
result_data = render_template('bye.xml', **jinja_context)
return Response(result_data, mimetype='text/xml')