def api_group_hosts_json(grp_name): group = HostGroup.read(where='id = %s', params=[grp_name]) if not group: group = HostGroup.read(where='grp_name = %s', params=[grp_name]) if not group: return jsonify(msg='no such group %s' % grp_name) vs, _ = Host.query(1, 10000000, '', '0', group.id) names = [v.hostname for v in vs] return jsonify(msg='', data=names)
def group_update_post(group_id): group_id = int(group_id) new_name = request.form['new_name'].strip() if not new_name: return jsonify(msg='new name is blank') group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group') if not group.writable(g.user): return jsonify(msg='no permission') HostGroup.update_dict({'grp_name': new_name}, 'id=%s', [group_id]) return jsonify(msg='')
def host_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h.strip() for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>' return jsonify(msg='', data=data)
def group_hosts_list(group_id): g.xbox = request.args.get('xbox', '') group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '') maintaining = request.args.get('maintaining', '0') vs, total = Host.query(page, limit, query, maintaining, group_id) return render_template( 'portal/host/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'maintaining': maintaining, 'group': group, } )
def cluster_creator_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) return render_template('portal/cluster/creator.html', **locals())
def host_groups_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids] return render_template('portal/host/groups.html', groups=groups, host=h)
def cluster_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) clusters = Cluster.select_vs(where='grp_id = %s', params=[group_id]) return render_template('portal/cluster/list.html', **locals())
def group_templates_get(grp_id): grp_id = int(grp_id) grp = HostGroup.read(where='id = %s', params=[grp_id]) if not grp: return jsonify(msg='no such group') ts = GrpTpl.tpl_list(grp_id) return render_template('portal/group/templates.html', group=grp, ts=ts)
def plugin_bind_post(): group_id = int(request.form['group_id'].strip()) plugin_dir = request.form['plugin_dir'].strip() group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) PluginDir.insert({'grp_id': group_id, 'dir': plugin_dir, 'create_user': g.user.name}) return jsonify(msg='')
def plugin_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) plugins = PluginDir.select_vs(where='grp_id = %s', params=[group_id]) return render_template('portal/plugin/list.html', group=group, plugins=plugins)
def group_delete_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group') if not group.writable(g.user): return jsonify(msg='no permission') return jsonify(msg=group_service.delete_group(group_id))
def group_create_post(): grp_name = request.form['grp_name'].strip() if not grp_name: return jsonify(msg="group name is blank") grp_id = HostGroup.create(grp_name, g.user.name, 1) if grp_id > 0: return jsonify(msg='') else: return jsonify(msg='grp_name has already existent')
def host_add_get(): group_id = request.args.get('group_id', '') if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') return render_template('portal/host/add.html', group=group)
def group_hosts_export(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] response = make_response('\n'.join(names)) response.headers["content-type"] = "text/plain" return response
def group_hosts_export(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] response = make_response('\n'.join(names)) response.headers["content-type"] = "text/plain" return response
def plugin_bind_post(): group_id = int(request.form['group_id'].strip()) plugin_dir = request.form['plugin_dir'].strip() group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) PluginDir.insert({ 'grp_id': group_id, 'dir': plugin_dir, 'create_user': g.user.name }) return jsonify(msg='')
def template_bind_node_post(): node = request.form['node'].strip() tpl_id = request.form['tpl_id'].strip() if not node: return jsonify(msg='node is blank') if not tpl_id: return jsonify(msg='tpl id is blank') hg = HostGroup.read('grp_name=%s', [node]) if not hg: return jsonify(msg='no such node') GrpTpl.bind(hg.id, tpl_id, g.user.name) return jsonify(msg="")
def template_unbind_grp_name_get(): tpl_id = request.args.get('tpl_id', '') if not tpl_id: return jsonify(msg="tpl_id is blank") grp_name = request.args.get('grp_name', '') if not grp_name: return jsonify(msg='grp_name is blank') hg = HostGroup.read('grp_name=%s', [grp_name]) if not hg: return jsonify(msg='no such host group') GrpTpl.unbind(hg.id, tpl_id) return jsonify(msg='')
def template_unbind_grp_name_get(): tpl_id = request.args.get('tpl_id', '') if not tpl_id: return jsonify(msg="tpl_id is blank") grp_name = request.args.get('grp_name', '') if not grp_name: return jsonify(msg='grp_name is blank') hg = HostGroup.read('grp_name=%s', [grp_name]) if not hg: return jsonify(msg='no such host group') GrpTpl.unbind(hg.id, tpl_id) return jsonify(msg='')
def template_bind_node_post(): node = request.form['node'].strip() tpl_id = request.form['tpl_id'].strip() if not node: return jsonify(msg='node is blank') if not tpl_id: return jsonify(msg='tpl id is blank') hg = HostGroup.read('grp_name=%s', [node]) if not hg: return jsonify(msg='no such node') GrpTpl.bind(hg.id, tpl_id, g.user.name) return jsonify(msg="")
def cluster_node_post(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) numerator = request.form['numerator'].strip() denominator = request.form['denominator'].strip() endpoint = request.form['endpoint'].strip() metric = request.form['metric'].strip() tags = request.form['tags'].strip() ds_type = 'GAUGE' step = request.form['step'].strip() msg = required_chk({ 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'ds_type': ds_type, 'step': step, }) if msg: return jsonify(msg=msg) if Cluster.exists('endpoint=%s and metric=%s and tags=%s', [endpoint, metric, tags]): return jsonify(msg='%s/%s/%s is already existent' % (endpoint, metric, tags)) last_id = Cluster.insert({ 'grp_id': group_id, 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'tags': tags, 'ds_type': ds_type, 'step': step, 'creator': g.user.name, }) if last_id > 0: return jsonify(msg='') else: return jsonify(msg='occur error')
def cluster_node_post(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) numerator = request.form['numerator'].strip() denominator = request.form['denominator'].strip() endpoint = request.form['endpoint'].strip() metric = request.form['metric'].strip() tags = request.form['tags'].strip() ds_type = 'GAUGE' step = request.form['step'].strip() msg = required_chk({ 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'ds_type': ds_type, 'step': step, }) if msg: return jsonify(msg=msg) if Cluster.exists('endpoint=%s and metric=%s and tags=%s', [endpoint, metric, tags]): return jsonify(msg='%s/%s/%s is already existent' % (endpoint, metric, tags)) last_id = Cluster.insert({ 'grp_id': group_id, 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'tags': tags, 'ds_type': ds_type, 'step': step, 'creator': g.user.name, }) if last_id > 0: return jsonify(msg='') else: return jsonify(msg='occur error')
def host_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() host_id = request.form['host_id'] if host_id: host_id = int(host_id) msg = GroupHost.bind_host_id(group_id, host_id) if not msg: return jsonify(msg='', data='success') else: return jsonify(msg=msg, data='error') else: if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure ) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join( success) + '</div>' return jsonify(msg='', data=data)
def home_get(): page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '').strip() mine = request.args.get('mine', '0') me = g.user.name if mine == '1' else None vs, total = HostGroup.query(page, limit, query, me) log.debug(vs) return render_template( 'portal/group/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'mine': mine, 'is_root': g.user.name in config.MAINTAINERS, } )
def home_get(): page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '').strip() mine = request.args.get('mine', '1') me = g.user.name if mine == '1' else None vs, total = HostGroup.query(page, limit, query, me) log.debug(vs) return render_template( 'portal/group/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'mine': mine, 'is_root': g.user.name in config.MAINTAINERS, } )
def home_get(): page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 20)) query = request.args.get('q', '').strip() mine = request.args.get('mine', '0') # default show all data manaul = request.args.get('manaul', '0') # 是否是手工创建 me = g.user.name if mine == '1' else None come_from = 1 if manaul == '1' else None # come_from = 1表示是手工创建的 vs, total = HostGroup.query(page, limit, query, me, come_from) log.debug(vs) return render_template('portal/group/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'mine': mine, 'manaul': manaul, 'is_root': g.user.name in config.MAINTAINERS, })
def variable_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) variables = Variable.select_vs(where='grp_id = %s', params=[group_id]) new_vars = [] for v in variables: new_vars.append({ "name": v.name, "content": v.content, "note": v.note, "create_user": v.create_user }) return render_template('portal/variable/list.html', group=group, variables=new_vars)
def group_hosts_list(group_id): g.xbox = request.args.get('xbox', '') group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '') maintaining = request.args.get('maintaining', '0') vs, total = Host.query(page, limit, query, maintaining, group_id) return render_template('portal/host/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'maintaining': maintaining, 'group': group, })