-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.py
119 lines (98 loc) · 3.38 KB
/
routes.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
#
# Copyright 2014 ABC Startsiden
#
# Author: Morten Bekkelund <[email protected]>
#
#
import re
import urllib
import urllib2
import simplejson as json
from flask import Flask, render_template, request
from config import *
app = Flask(__name__)
app.config.from_object('config')
@app.route('/')
def home():
minions = salt_api("runner","manage.status")
return render_template('home.html', minions=minions)
@app.route('/grains')
def grains():
minions = salt_api("local","grains.items",target="*")
print minions
return render_template('grains.html', minions=minions)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/version')
def version():
minions = salt_api("local","pkg.version",target="*",arg="salt-minion")
return render_template('version.html', minions=minions)
@app.route('/pillars')
def pillars():
minions = salt_api("local","pillar.raw",target="*")
return render_template('pillars.html', minions=minions)
@app.route('/disk_percent')
def disk_percent():
minions = salt_api("local","disk.percent",target="*")
return render_template('disk_percent.html', minions=minions)
@app.route('/loadavg')
def loadavg():
minions = salt_api("local","status.loadavg",target="*")
return render_template('loadavg.html', minions=minions)
@app.route('/pkgsearch', methods=['GET', 'POST'])
def pkgsearch():
arg = ""
if request.method == "POST":
arg = request.form['pkg']
elif request.method == "GET":
arg = request.args.get('pkg')
print request.args.get('pkg')
minions = salt_api("local","pkg.version",target="*",arg=arg)
return render_template('pkgsearch.html', minions=minions, pkg=arg)
@app.route('/full')
def full():
minion = request.args.get('minion')
salt_version = salt_api("local","pkg.version",target=minion,arg="salt-minion")
grains = salt_api("local","grains.items",target=minion)
pillars = salt_api("local", "pillar.raw",target=minion)
return render_template('full.html', minion=minion, salt_version=salt_version, grains=grains, pillars=pillars)
def salt_api(client,function,**kwargs):
""" call salt-api...
"client" must be either "runner" or "local"
"target" is the salt-minion, for instance "*" (all) or "my-minion-name"
"function" is the salt-python-function, for instance test.ping or pkg.version
"arg" is the functions argument, for instance packagename (as in: pkg.version salt-minion)
"""
arg = None
target = None
for k, v in kwargs.iteritems():
if k == "arg":
arg = v
elif k == "target":
target = v
#url = "https://salt.startsiden.no:8000/run"
url = app.config['SALT_API_URL']
values = dict()
values["client"] = client
values["fun"] = function
if target: values["tgt"] = target
if arg: values["arg"] = arg
values["username"] = app.config['SALT_USER']
values["password"] = app.config['SALT_PASSWORD']
values["eauth"] = "pam"
print url
print values
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
res = urllib2.urlopen(req)
page = res.read()
minions = json.loads(page)
print data
print minions['return'][0]
return minions['return'][0]
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')