forked from MirantisLabs/rubick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webui.py
208 lines (154 loc) · 5.98 KB
/
webui.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
from StringIO import StringIO
from bson.objectid import ObjectId
import os.path
from flask import Flask, request, json, send_file
from flask_wtf import Form
from paramiko.dsskey import DSSKey
from paramiko.rsakey import RSAKey
from paramiko.ssh_exception import SSHException
from wtforms import StringField, SelectMultipleField
from wtforms.validators import DataRequired
import wtforms_json
from rubick.celery import app as celery, \
ostack_discover_task, ostack_inspect_task, InspectionRequest
from rubick.common import Inspection, Issue
from rubick.database import get_db, Cluster, RuleGroup
from rubick.discovery import OpenstackDiscovery
from rubick.json import openstack_for_json
from rubick.model import Openstack
app = Flask(__name__)
app.secret_key = 'A0Zr98j/3fooN]LWX/,?RT'
wtforms_json.init()
def is_key_valid(private_key):
for key_klass in [RSAKey, DSSKey]:
try:
key_klass.from_private_key(StringIO(private_key))
return True
except SSHException:
pass
return False
class ValidateClusterForm(Form):
cluster_id = StringField('Cluster', validators=[DataRequired()])
rules = SelectMultipleField('Rules')
def __init__(self):
super(ValidateClusterForm, self).__init__(csrf_enabled=False)
@app.template_filter()
def to_label(s):
if s in [Issue.FATAL, Issue.ERROR]:
return 'label-danger'
elif s == Issue.WARNING:
return 'label-warning'
else:
return 'label-info'
@app.route('/')
def index():
return send_file(os.path.join(app.static_folder, 'index.html'))
@app.route('/clusters')
def get_clusters():
db = get_db()
return json.dumps([Cluster.from_doc(doc) for doc in db['clusters'].find()])
@app.route('/clusters', methods=['POST'])
def add_cluster():
data = json.loads(request.data)
errors = {}
if 'nodes' in data and (isinstance(data['nodes'], str) or
isinstance(data['nodes'], unicode)):
data['nodes'] = data['nodes'].split()
if not 'name' in data or data['name'] == '':
errors['name'] = ['Cluster name is required']
if not 'nodes' in data or data['nodes'] == []:
errors['nodes'] = ['At least one cluster node is required']
if 'private_key' not in data:
errors['private_key'] = ['Private key for accessing nodes is required']
elif not is_key_valid(data['private_key']):
errors['private_key'] = ['Private key format is unknown']
if len(errors) > 0:
return json.dumps(dict(errors=errors)), 422
cluster = Cluster(**data)
cluster_id = get_db()['clusters'].save(cluster.as_doc())
ostack_discover_task.delay(str(cluster_id))
return json.dumps(dict(id=str(cluster_id))), 201
@app.route('/clusters/<cluster_id>', methods=['GET'])
def get_cluster(cluster_id):
cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
if not cluster_doc:
return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404
cluster = Cluster.from_doc(cluster_doc)
return json.dumps(cluster.for_json()), 200
@app.route('/clusters/<cluster_id>', methods=['DELETE'])
def del_cluster(cluster_id):
get_db()['clusters'].remove({'_id': ObjectId(cluster_id)})
return '', 200
@app.route('/clusters/<cluster_id>/rediscover', methods=['GET'])
def discover_cluster(cluster_id):
cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
if not cluster_doc:
return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404
ostack_discover_task.delay(cluster_id)
return '', 200
@app.route('/clusters/test', methods=['POST'])
def test_cluster():
data = json.loads(request.data)
errors = {}
if not 'nodes' in data or data['nodes'] == []:
errors['nodes'] = ['At least one cluster node is required']
if 'private_key' not in data:
errors['private_key'] = ['Private key for accessing nodes is required']
elif not is_key_valid(data['private_key']):
errors['private_key'] = ['Private key format is unknown']
if len(errors) == 0:
d = OpenstackDiscovery()
if d.test_connection(data['nodes'], private_key=data['private_key']):
return '', 200
else:
return '', 409
else:
return json.dumps(dict(errors=errors)), 422
@app.route('/rules')
def get_rules():
rules = []
for inspection in Inspection.all_inspections():
rules.extend(inspection.rules())
return json.dumps(rules)
@app.route('/rules/<group>')
def get_rules_group(group):
if group not in RuleGroup.all:
return 'Unknown rule group "%s"' % group, 404
#db = get_db()
#rules = [Rule.from_doc(doc) for doc in db['rules'].find({'group': group})]
#return json.dumps(rules)
return get_rules()
@app.route('/validation', methods=['POST'])
def launch_validation():
form = ValidateClusterForm()
if form.validate_on_submit():
db = get_db()
cluster_doc = db['clusters'].find_one({
'_id': ObjectId(form.cluster_id.data)})
if not cluster_doc:
return json.dumps({'errors': {
'cluster_id': 'Cluster not found'}}), 404
cluster = Cluster.from_doc(cluster_doc)
request = InspectionRequest(
cluster.nodes,
username='root',
private_key=cluster.private_key)
job = ostack_inspect_task.delay(request)
return json.dumps({'id': job.id}), 202
else:
return json.dumps(dict(errors=form.errors)), 422
@app.route('/validation/<id>')
def job(id):
job = celery.AsyncResult(id)
if job.ready():
result = job.result.value
if isinstance(result, Openstack):
return json.dumps({
'state': 'success',
'result': openstack_for_json(result)})
else:
return json.dumps({'state': 'failure', 'message': result})
else:
return json.dumps({'state': str(job.state).lower()})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)