improve 'multi' backend #16

This commit is contained in:
ziirish 2014-09-17 10:59:38 +02:00
parent b1195226cb
commit 7210b96953
5 changed files with 158 additions and 46 deletions

View file

@ -275,8 +275,12 @@ class Burp(BUIbackend):
returns a dict
"""
r = {}
if not name or name not in self.running:
return r
if agent:
if not name or name not in self.running[agent]:
return r
else:
if not name or name not in self.running:
return r
f = self.status('c:{0}\n'.format(name))
if not f:
return r
@ -338,7 +342,7 @@ class Burp(BUIbackend):
except BUIserverException:
return r
for c in cls:
if self.is_backup_running(c['name']):
if self.is_backup_running(c['name'], agent):
r.append(c['name'])
self.running = r
return r

View file

@ -16,6 +16,7 @@ class Burp(BUIbackend):
self.app = app
self.servers = {}
self.app.config['SERVERS'] = []
self.running = {}
if conf:
config = ConfigParser.ConfigParser()
with open(conf) as fp:
@ -73,13 +74,24 @@ class Burp(BUIbackend):
is_one_backup_running returns a list of clients name that are currently
running a backup
"""
return self.servers[agent].is_one_backup_running()
r = []
if agent:
r = self.servers[agent].is_one_backup_running(agent)
self.running[agent] = r
else:
r = {}
for a in self.servers:
r[a] = self.servers[a].is_one_backup_running(a)
self.running = r
return r
def get_all_clients(self, agent=None):
"""
get_all_clients returns a list of dict representing each clients with their
name, state and last backup date
"""
if agent not in self.servers:
return []
return self.servers[agent].get_all_clients()
def get_client(self, name=None, agent=None):
@ -173,7 +185,7 @@ class NClient(BUIbackend):
buf += newbuf
received += len(newbuf)
if debug:
self.app.logger.debug('result (%d/%d): %s', length, len(buf), buf)
self.app.logger.debug('result (%d/%d): %s', len(buf), length, buf)
return buf
"""
@ -217,7 +229,7 @@ class NClient(BUIbackend):
is_one_backup_running returns a list of clients name that are currently
running a backup
"""
data = {'func': 'is_one_backup_running', 'args': None}
data = {'func': 'is_one_backup_running', 'args': {'agent': agent}}
return json.loads(self.do_command(data))
def get_all_clients(self, agent=None):

View file

@ -47,7 +47,7 @@ def running_clients(server=None):
"""
if not server:
server = request.args.get('server')
r = bui.cli.is_one_backup_running(agent=server)
r = bui.cli.is_one_backup_running(server)
return jsonify(results=r)
@app.route('/api/render-live-template', methods=['GET'])
@ -64,13 +64,22 @@ def render_live_tpl(server=None, name=None):
"""
if not server:
server = request.args.get('server')
c = request.args.get('name')
if not name and not c:
abort(500)
if not name:
name = c
if name not in bui.cli.running:
abort(404)
name = request.args.get('name')
if not name:
abort(500)
if isinstance(bui.cli.running, dict):
if server and name not in bui.cli.running[server]:
abort(404)
else:
found = False
for k, a in bui.cli.running.iteritems():
found = found or (name in a)
if not found:
abort(404)
else:
if name not in bui.cli.running:
abort(404)
try:
counters = bui.cli.get_counters(name, agent=server)
except BUIserverException:
@ -96,14 +105,30 @@ def live(server=None):
if not server:
server = request.args.get('server')
r = []
for c in bui.cli.is_one_backup_running(agent=server):
s = {}
s['client'] = c
try:
s['status'] = bui.cli.get_counters(c, agent=server)
except BUIserverException:
s['status'] = []
r.append(s)
if server:
l = (bui.cli.is_one_backup_running(server))[server]
else:
l = bui.cli.is_one_backup_running()
if isinstance(l, dict):
for k, a in l.iteritems():
for c in a:
s = {}
s['client'] = c
s['agent'] = k
try:
s['status'] = bui.cli.get_counters(c, agent=k)
except BUIserverException:
s['status'] = []
r.append(s)
else:
for c in l:
s = {}
s['client'] = c
try:
s['status'] = bui.cli.get_counters(c, agent=server)
except BUIserverException:
s['status'] = []
r.append(s)
return jsonify(results=r)
@app.route('/api/running.json')
@ -114,10 +139,13 @@ def backup_running(server=None):
API: backup_running
:returns: true if at least one backup is running
"""
if not server:
server = request.args.get('server')
j = bui.cli.is_one_backup_running(agent=server)
r = len(j) > 0
j = bui.cli.is_one_backup_running(server)
r = False
if isinstance(j, dict):
for k, v in j.iteritems():
r = r or (len(v) > 0)
else:
r = len(j) > 0
return jsonify(results=r)
@app.route('/api/client-tree.json/<name>/<int:backup>', methods=['GET'])
@ -278,9 +306,18 @@ def live_monitor(server=None, name=None):
"""
if not server:
server = request.args.get('server')
if not bui.cli.running:
flash('Sorry, there are no running backups', 'warning')
return redirect(url_for('home'))
if bui.standalone:
if not bui.cli.running:
flash('Sorry, there are no running backups', 'warning')
return redirect(url_for('home'))
else:
run = False
for a in bui.cli.servers:
run = run or (a in bui.cli.running and bui.cli.running[a])
if not run:
flash('Sorry, there are no running backups', 'warning')
return redirect(url_for('home'))
return render_template('live-monitor.html', live=True, cname=name, server=server)
@app.route('/client-browse/<name>', methods=['GET'])

View file

@ -34,10 +34,12 @@ var notif = function(type, message) {
};
{% if not login -%}
{% if config.STANDALONE -%}
var _check_running = function() {
{% if server -%}
url = '{{ url_for("backup_running", server=server) }}';
{% else -%}
url = '{{ url_for("backup_running") }}';
{% endif -%}
$.getJSON(url, function(data) {
if (data.results) {
$('#toblink').addClass('blink');
@ -46,11 +48,6 @@ var _check_running = function() {
}
});
};
{% else -%}
var _check_running = function() {
return false;
};
{% endif -%}
{% endif -%}
{% if config.STANDALONE -%}
@ -236,7 +233,7 @@ $(function() {
* initialize our page if needed
*/
{% if not login -%}
//_check_running();
_check_running();
{% endif -%}
{% if clients -%}
_clients();
@ -274,7 +271,7 @@ $(function() {
{% if not login -%}
var refresh_running = setInterval(function () {
//_check_running();
_check_running();
}, {{ config.REFRESH * 1000 }});
{% endif -%}

View file

@ -1,17 +1,79 @@
_live = function() {
url = '{{ url_for("running_clients", server=server) }}';
html = ''
$.getJSON(url, function(data) {
if (!data.results || data.results.length === 0) {
document.location = '{{ url_for("home", server=server) }}';
}
$.each(data.results, function(i, c) {
_parse_live_result = function(data, serv) {
{% if server -%}
redirect = '{{ url_for("home", server=server) }}';
{% else -%}
if (serv) {
redirect = '{{ url_for("home") }}?server='+serv;
} else {
redirect = '{{ url_for("home") }}';
}
{% endif -%}
if (!data.results || data.results.length === 0) {
document.location = redirect;
}
var res = '';
$.each(data.results, function(i, c) {
if (c instanceof String || typeof c == 'string') {
{% if server -%}
u = '{{ url_for("render_live_tpl", server=server) }}?name='+c;
{% else -%}
if (serv) {
u = '{{ url_for("render_live_tpl") }}?name='+c+'&server='+serv;
} else {
u = '{{ url_for("render_live_tpl") }}?name='+c;
}
{% endif -%}
$.get(u, function(d) {
html += d;
res += d;
});
} else {
$.each(c, function(j, a) {
$.each(a, function(k, cl) {
{% if server -%}
u = '{{ url_for("render_live_tpl", server=server) }}?name='+cl;
{% else -%}
if (serv) {
u = '{{ url_for("render_live_tpl") }}?name='+cl+'&server='+serv;
} else {
u = '{{ url_for("render_live_tpl") }}?name='+cl;
}
{% endif -%}
$.get(u, function(d) {
res += d;
});
});
});
}
});
return res;
};
{% if not config.STANDALONE and not server -%}
_live = function() {
urls = Array();
{% for s in config.SERVERS -%}
urls.append({'url': '{{ url_for("running_clients") }}'+?server={{ s }}, 'serv': {{ s }}});
{% endfor -%}
html = '';
$.each(urls, function(i, rec) {
$.getJSON(rec['url'], function(data) {
html += _parse_live_result(data, rec['serv']);
});
});
$('#live-container').html(html);
};
{% else -%}
_live = function() {
{% if config.STANDALONE -%}
url = '{{ url_for("running_clients") }}';
{% else -%}
url = '{{ url_for("running_clients", server=server) }}';
{% endif -%}
html = ''
$.getJSON(url, function(data) {
html += _parse_live_result(data);
});
$('#live-container').html(html);
};
{% endif -%}