-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebApp.py
executable file
·90 lines (79 loc) · 2.74 KB
/
webApp.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, render_template, request, send_from_directory
from gevent.pywsgi import WSGIServer
import inspect
import json
import sys
from inventory import getGroups, getUsers
from ansible import getGroupsPlayBook, getUsersPlayBook, writePlayBook
app = Flask(__name__)
#DEBUG=True
DEBUG=False
app.debug = DEBUG
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
@app.route("/inventory")
def index():
if DEBUG:
print(inspect.currentframe().f_code.co_name)
return render_template("/inventory/index.html")
@app.route('/inventory/groups', methods=["GET", "POST"])
def sendGroups():
if DEBUG:
print(inspect.currentframe().f_code.co_name)
info = request.get_json(force=True)
ig = [] # groups should be included
eg = [] # groups should be excluded
eu = [] # users should be excluded
try:
if 'ig' in info:
ig = info['ig']
if 'eg' in info:
eg = info['eg']
if 'eu' in info:
eu = info['eu']
except:
text = "Incorrect json data filling!"
return jsonify(result=text)
groups = getGroups(excluded = eg, included = ig, excludedUsers = eu);
playbook = getGroupsPlayBook(groups)
writePlayBook(playbook, "groups_playbook.yml")
return send_from_directory(app.root_path, "groups_playbook.yml")
@app.route('/inventory/users', methods=["GET", "POST"])
def sendUsers():
if DEBUG:
print(inspect.currentframe().f_code.co_name)
info = request.get_json(force=True)
ig = [] # groups should be included
eg = [] # groups should be excluded
eu = [] # users should be excluded
iu = [] # users should be included
arguments = {'expires' : 0, 'create_home' : 'no'}
try:
if 'ig' in info:
ig = info['ig']
if 'eg' in info:
eg = info['eg']
if 'eu' in info:
eu = info['eu']
if 'iu' in info:
iu = info['iu']
if 'args' in info:
if len(info['args']) == 2:
if 'expires' in info['args'] and 'create_home' in info['args']:
arguments = info['args']
except:
text = "Incorrect json data filling!"
return jsonify(result=text)
groups = getGroups(excluded = eg, included = ig, excludedUsers = eu);
users = getUsers(excluded = eu, included = iu);
playbook = getUsersPlayBook(users, groups, arguments)
writePlayBook(playbook, "users_playbook.yml")
return send_from_directory(app.root_path, "users_playbook.yml")
if __name__ == '__main__':
if DEBUG:
app.run(host='0.0.0.0', port=8080)
else:
http_server = WSGIServer(('0.0.0.0', 8080), app)
http_server.serve_forever()