-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigpy.py
305 lines (237 loc) · 8.78 KB
/
configpy.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from flask import Flask, render_template, redirect, url_for, request, jsonify
import requests
from jinja2 import Environment, FileSystemLoader, meta
from flask_bootstrap import Bootstrap
import yaml, json
from datetime import datetime
from get_ext_repo import get_ext_repo, pushtorepo
from flask_socketio import SocketIO
import os
import redis
import time
import logging
import fnmatch
from logging.handlers import RotatingFileHandler
from flask_apscheduler import APScheduler
# Needed for SocketIO to actually work...
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
app.config['secret'] = 's;ldi3r#$R@lkjedf$'
app.config['slax_host'] = '10.0.0.204'
bootstrap = Bootstrap(app)
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
socketio = SocketIO(app, message_queue='redis://redis')
if not os.path.exists('logs'):
os.mkdir('logs')
file_handler = RotatingFileHandler('logs/microblog.log', maxBytes=10240, backupCount=10)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('Microblog startup')
REDIS_URI = os.environ.get('REDIS_URI')
if not REDIS_URI:
REDIS_URI = '127.0.0.1'
r = redis.Redis(host=REDIS_URI, port=6379, db=0)
dbcheck_stat = 0
def current_time():
current_time = str(datetime.now().time())
no_sec = current_time.split('.')
time = no_sec.pop(0)
return time
def static_error(error):
new_data = {}
print(error)
new_data['event_time'] = current_time()
new_data['event'] = str(error)
return new_data
def dbcheck_logic(data, **kwargs):
#print(kwargs)
current_time = str(datetime.now().time())
no_sec = current_time.split('.')
poll = no_sec.pop(0)
data['last_poll'] = poll
try:
found_devices = r.keys()
#print(found_devices)
device_dict = {}
for key in found_devices:
key = key.decode("utf-8")
values = r.hgetall(key)
device_values = {}
#print(values)
for x, y in values.items():
device_values[x.decode("utf-8")] = y.decode("utf-8")
device_dict[key] = device_values
#print(device_values)
if device_dict:
data['device'] = device_dict
else:
data['device'] = ''
except Exception as e:
socketio.emit('dsc', {'db_error': str(e)}, broadcast=True)
return
if 'loopcheck' in kwargs:
socketio.emit('dsc', data, broadcast=True)
socketio.emit('dsc', data)
def dbcheck_loop():
data = {'Get': 'devices'}
with app.test_request_context('/'):
while True:
dbcheck_logic(data, loopcheck='yes')
# Loop delay
time.sleep(1)
@app.route('/hub', methods=['GET', 'POST'])
def hub():
return render_template('hub.html', title='Hub')
@app.route('/hub/device/<serialnumber>', methods=['GET'])
def hub_api(serialnumber):
response = r.hgetall(serialnumber)
api_request = {}
for k, v in response.items():
api_request[k.decode("utf-8")] = v.decode("utf-8")
response = jsonify(api_request)
return response
@app.route('/render', methods=['GET', 'POST'])
def render():
repo_dir = 'repo/'
debug = ''
if request.get_json():
received = request.get_json()
repo_url = yaml.load(received["repo_url"])
ext_repo_info = get_ext_repo(repo_url)
# Convert python dict to JSON, so AJAX can read it.
result = jsonify(ext_repo_info)
return result
foundtemplates = fnmatch.filter(os.listdir(repo_dir), '*.j2')
return render_template('renderform.html', title='Render Template', foundtemplates=foundtemplates, debug=debug)
@app.route('/process', methods=['POST'])
def process():
'''
Note: This whole route needs to be rewritten.
Today, it takes untrusted and unverified data and saves it to the server.
Although this isn't a big deal if your source is trusted, I would consider it a very bad practice.
If it absolutely has to save data to the server (doubt), it should be run in a container to isolate it.
'''
received = request.get_json()
r = requests.get(received["template"])
if '---' not in received["answers"]:
return 'Answers must begin with ---'
with open("repo/render.tmp", "w") as file:
file.write(r.text)
env = Environment(loader=FileSystemLoader('repo/'), trim_blocks=True, lstrip_blocks=True)
ast = env.parse(r.text)
dependencies = list(meta.find_referenced_templates(ast))
# print(dependencies)
if dependencies:
repo_url = yaml.load(received["repo_url"])
ext_repo_info = get_ext_repo(repo_url, 'all')
for key, value in ext_repo_info["files"].items():
# print(key, value)
for dependency in dependencies:
if dependency == key:
# print('Process: ' + value)
r = requests.get(value, timeout=5)
with open("repo/{0}".format(key), "w") as file:
file.write(r.text)
env = Environment(loader=FileSystemLoader('repo/'), trim_blocks=True, lstrip_blocks=True)
try:
# Load data from YAML into Python dictionary
answerfile = yaml.load(received["answers"])
# Load Jinja2 template
template = env.get_template("render.tmp")
# Render the template
rendered_template = template.render(answerfile)
except Exception as e:
# If errors, return them to UI.
return str(e)
return str(rendered_template)
@app.route('/show', methods=['POST'])
def show():
received = request.get_json()
#print(received)
answerfile = str(received[0]['value']).replace('.j2', '.yml')
r = requests.get(answerfile, timeout=5)
return r.text
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
# @login_required
def index():
return redirect(url_for('hub'))
@socketio.on('console')
def test_connect(data):
current_time = str(datetime.now().time())
no_sec = current_time.split('.')
time = no_sec.pop(0)
data['event_time'] = time
socketio.emit('console', data)
@socketio.on('hub_console')
def hub_console(data):
socketio.emit('hub_console', data)
@socketio.on('getDevice')
def getDevice(data):
#print(data)
if data['device']:
values = r.hgetall(data['device'])
device_values = {}
#print(values)
for x, y in values.items():
device_values[x.decode("utf-8")] = y.decode("utf-8")
socketio.emit('foundDevice', device_values)
@socketio.on('deleteDevice')
def deleteDevice(data):
#print(data)
if data['deleteDevice']:
r.delete(data['deleteDevice'])
#socketio.emit('dsc', 'Delete device refresh')
socketio.emit('deletedDevice', f'Removed: {data["deleteDevice"]}')
@socketio.on('dsc')
def dsc(data):
# define global var
global dbcheck_stat
# If var is 0, then start the while loop to check DB.
# It's important to only run this loop once, else, each new user starts a new run.
if dbcheck_stat == 0:
# update the global var so the loop is not run again.
dbcheck_stat = 1
# Start the loop
app.apscheduler.add_job(func=dbcheck_loop, trigger='date', id='loop')
# If the loops been started but this is a new user request, run the loop logic once.
# That way, the users web request completes with valid data.
# Else, the page will load with no data (until the loop runs.)
dbcheck_logic(data)
@socketio.on('gitlabPush')
def gitlabPush(data):
data = json.loads(data)
if data['clientID'] \
and data['repo_auth_token'] \
and data['serialNumber'] \
and data['device_config'] \
and data['repo_uri']:
for node, serialNumber in data['serialNumber'].items():
if serialNumber:
print(f'Pushing node: {node} - serial: {serialNumber}')
new_data = pushtorepo(data=data, REDIS_URI=REDIS_URI, serialNumber=serialNumber, node=node)
socketio.emit('git_console', new_data)
else:
new_data = dict()
new_data['event_time'] = current_time()
new_data['event'] = f'Ignored: {node}'
socketio.emit('git_console', new_data)
else:
new_data = dict()
new_data['event_time'] = current_time()
new_data['event'] = 'The form submitted is missing values.'
socketio.emit('git_console', new_data)
@socketio.on('connect')
def connect():
print('Client connected!')
@socketio.on('disconnect')
def disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", port=80, debug=True)