diff --git a/jasset/urls.py b/jasset/urls.py index 3b8fd120d..a859e209a 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -5,15 +5,18 @@ from jasset.views import * urlpatterns = patterns('', url(r'^$', index), url(r'^host_add/$', jadd_host), + url(r'^host_add_multi/$', jadd_host_multi), url(r'^host_list/$', jlist_host), url(r"^(\d+.\d+.\d+.\d+)/$",jlist_ip), url(r'^idc_add/$', jadd_idc), url(r'^idc_list/$', jlist_idc), + url(r'^idc_detail/(\d+)$', idc_detail), url(r'^idc_del/(\d+)/$', idc_del), url(r'^group_add/$', jadd_group), url(r'^group_list/$', jlist_group), + url(r'^group_detail/(\d)/$', group_detail), url(r'^group_del/(\d+)/$', group_del), - url(r'^host_del/(\d+)/$', host_del), + url(r'^host_del/(\w+)/$', host_del), url(r'^host_edit/(\d+)$', host_edit), url(r'^host_edit/batch/$', batch_host_edit), url(r'^test/$', test), diff --git a/jasset/views.py b/jasset/views.py index f50f1e583..81a0c7dd3 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -8,7 +8,6 @@ from models import IDC, Asset, BisGroup from juser.models import UserGroup from connect import PyCrypt, KEY from jumpserver.views import jasset_group_add, jasset_host_edit -from jperm.models import Perm cryptor = PyCrypt(KEY) @@ -35,27 +34,25 @@ def jadd_host(request): j_comment = request.POST.get('j_comment') j_idc = IDC.objects.get(name=j_idc) + all_group = BisGroup.objects.get(name='ALL') for group in j_group: c = BisGroup.objects.get(name=group) groups.append(c) + groups.append(all_group) if Asset.objects.filter(ip=str(j_ip)): - emg = u'该IP已存在!' + emg = u'该IP %s 已存在!' %j_ip return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) if j_type == 'M': j_user = request.POST.get('j_user') j_password = cryptor.encrypt(request.POST.get('j_password')) - j_root = request.POST.get('j_root') - j_passwd = cryptor.encrypt(request.POST.get('j_passwd')) a = Asset(ip=j_ip, port=j_port, login_type=j_type, idc=j_idc, is_active=int(j_active), comment=j_comment, username_common=j_user, - password_common=j_password, - username_super=j_root, - password_super=j_passwd,) + password_common=j_password) else: a = Asset(ip=j_ip, port=j_port, login_type=j_type, idc=j_idc, @@ -66,13 +63,63 @@ def jadd_host(request): a.bis_group = groups a.save() smg = u'主机 %s 添加成功' %j_ip + return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) + +def jadd_host_multi(request): + header_title, path1, path2 = u'批量添加主机 | Add Hosts', u'资产管理', u'批量添加主机' + login_types = {'LDAP': 'L', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} + if request.method == 'POST': + multi_hosts = request.POST.get('j_multi').split('\n') + for host in multi_hosts: + if host == '': + break + groups = [] + j_ip, j_port, j_type, j_idc, j_group, j_user_group, j_active, j_comment = host.split() + j_idc = IDC.objects.get(name=j_idc) + j_type = login_types[j_type] + all_group = BisGroup.objects.get(name='ALL') + j_group = j_group.split(',') + for group in j_group: + g = group.strip('[]') + print g + c = BisGroup.objects.get(name=g) + groups.append(c) + groups.append(all_group) + + if Asset.objects.filter(ip=str(j_ip)): + emg = u'该IP %s 已存在!' %j_ip + return render_to_response('jasset/host_add_multi.html', locals(), context_instance=RequestContext(request)) + + if j_type == 'M': + j_user = request.POST.get('j_user') + j_password = cryptor.encrypt(request.POST.get('j_password')) + a = Asset(ip=j_ip, port=j_port, + login_type=j_type, idc=j_idc, + is_active=int(j_active), + comment=j_comment, + username_common=j_user, + password_common=j_password) + else: + a = Asset(ip=j_ip, port=j_port, + login_type=j_type, idc=j_idc, + is_active=int(j_active), + comment=j_comment) + jasset_group_add(j_ip, j_ip, 'P') + a.save() + a.bis_group = groups + a.save() + smg = u'批量添加添加成功' + return HttpResponseRedirect('/jasset/host_list/') + return render_to_response('jasset/host_add_multi.html', locals(), context_instance=RequestContext(request)) + + def batch_host_edit(request): if request.method == 'POST': len_table = request.POST.get('len_table') - print len_table for i in range(int(len_table)): + j_id = "editable["+str(i)+"][j_id]" j_ip = "editable["+str(i)+"][j_ip]" j_port = "editable["+str(i)+"][j_port]" j_idc = "editable["+str(i)+"][j_idc]" @@ -81,6 +128,7 @@ def batch_host_edit(request): j_active = "editable["+str(i)+"][j_active]" j_comment = "editable["+str(i)+"][j_comment]" + j_id = request.POST.get(j_id).strip() j_ip = request.POST.get(j_ip).strip() j_port = request.POST.get(j_port).strip() j_idc = request.POST.get(j_idc).strip() @@ -90,7 +138,7 @@ def batch_host_edit(request): j_comment = request.POST.get(j_comment).strip() print j_ip - jasset_host_edit(j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment) + jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment) return render_to_response('jasset/host_list.html') @@ -100,7 +148,7 @@ def jlist_host(request): login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} posts = contact_list = Asset.objects.all().order_by('ip') print posts - p = paginator = Paginator(contact_list, 5) + p = paginator = Paginator(contact_list, 20) try: page = int(request.GET.get('page', '1')) except ValueError: @@ -113,16 +161,23 @@ def jlist_host(request): return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) + def host_del(request, offset): - len_list = request.POST.get("len_list") - for i in range(int(len_list)): - key = "id_list["+str(i)+"]" - print key - jid = request.POST.get(key) - print jid + print offset + if offset == 'multi': + len_list = request.POST.get("len_list") + for i in range(int(len_list)): + key = "id_list["+str(i)+"]" + print key + jid = request.POST.get(key) + print jid + Asset.objects.filter(id=jid).delete() + else: + jid = int(offset) Asset.objects.filter(id=jid).delete() return HttpResponseRedirect('/jasset/host_list/') + def host_edit(request, offset): actives = {1: u'激活', 0: u'禁用'} login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} @@ -132,7 +187,7 @@ def host_edit(request, offset): egroup = BisGroup.objects.filter(type='A') for g in Asset.objects.get(id=int(offset)).bis_group.all(): e_group.append(g) - post = Asset.objects.get(id = int(offset)) + post = Asset.objects.get(id=int(offset)) if request.method == 'POST': j_ip = request.POST.get('j_ip') j_idc = request.POST.get('j_idc') @@ -142,27 +197,24 @@ def host_edit(request, offset): j_active = request.POST.get('j_active') j_comment = request.POST.get('j_comment') j_idc = IDC.objects.get(name=j_idc) + print j_group for group in j_group: c = BisGroup.objects.get(name=group) groups.append(c) - a = Asset.objects.get(ip=int(offset)) + a = Asset.objects.get(id=int(offset)) if j_type == 'M': j_user = request.POST.get('j_user') j_password = cryptor.encrypt(request.POST.get('j_password')) - j_root = request.POST.get('j_root') - j_passwd = cryptor.encrypt(request.POST.get('j_passwd')) a.ip = j_ip a.port = j_port a.login_type = j_type a.idc = j_idc a.is_active = j_active a.comment = j_comment - a.username_common = j_user - a.password_common = j_password - a.username_super = j_root - a.password_super = j_passwd + a.username = j_user + a.password = j_password else: a.ip = j_ip a.port = j_port @@ -183,9 +235,10 @@ def host_edit(request, offset): def jlist_ip(request, offset): header_title, path1, path2 = u'主机详细信息 | Host Detail.', u'资产管理', u'主机详情' print offset - post = contact_list = Asset.objects.get(ip = str(offset)) + post = contact_list = Asset.objects.get(ip=str(offset)) return render_to_response('jasset/jlist_ip.html', locals(), context_instance=RequestContext(request)) + def jadd_idc(request): header_title, path1, path2 = u'添加IDC | Add IDC', u'资产管理', u'添加IDC' if request.method == 'POST': @@ -204,14 +257,16 @@ def jadd_idc(request): def jlist_idc(request): - header_title, path1, path2 = u'查看IDC | List Host', u'资产管理', u'查看IDC' + header_title, path1, path2 = u'查看IDC | List IDC', u'资产管理', u'查看IDC' posts = IDC.objects.all().order_by('id') return render_to_response('jasset/idc_list.html', locals(), context_instance=RequestContext(request)) + def idc_del(request, offset): IDC.objects.filter(id=offset).delete() return HttpResponseRedirect('/jasset/idc_list/') + def jadd_group(request): header_title, path1, path2 = u'添加主机组 | Add Group', u'资产管理', u'添加主机组' if request.method == 'POST': @@ -229,13 +284,53 @@ def jadd_group(request): def jlist_group(request): - header_title, path1, path2 = u'查看业务组 | Add Group', u'资产管理', u'查看业务组' + header_title, path1, path2 = u'查看主机组 | List Group', u'资产管理', u'查看主机组' posts = BisGroup.objects.filter(type='A').order_by('id') return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request)) + +def group_detail(request, offset): + header_title, path1, path2 = u'主机组详情 | Group Detail', u'资产管理', u'主机组详情' + login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} + group_name = BisGroup.objects.get(id=offset).name + b = BisGroup.objects.get(id=offset) + posts = contact_list = Asset.objects.filter(bis_group=b).order_by('ip') + p = paginator = Paginator(contact_list, 5) + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + try: + contacts = paginator.page(page) + except (EmptyPage, InvalidPage): + contacts = paginator.page(paginator.num_pages) + return render_to_response('jasset/group_detail.html', locals(), context_instance=RequestContext(request)) + + +def idc_detail(request, offset): + header_title, path1, path2 = u'主机组详情 | Group Detail', u'资产管理', u'主机组详情' + login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} + idc_name = IDC.objects.get(id=offset).name + b = IDC.objects.get(id=offset) + posts = contact_list = Asset.objects.filter(idc=b).order_by('ip') + p = paginator = Paginator(contact_list, 5) + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + try: + contacts = paginator.page(page) + except (EmptyPage, InvalidPage): + contacts = paginator.page(paginator.num_pages) + return render_to_response('jasset/idc_detail.html', locals(), context_instance=RequestContext(request)) + + def group_del(request, offset): BisGroup.objects.filter(id=offset).delete() return HttpResponseRedirect('/jasset/group_list/') + def test(request): return render_to_response('jasset/test.html', locals()) diff --git a/jlog/urls.py b/jlog/urls.py index 75f4111fe..55a17bd7b 100644 --- a/jlog/urls.py +++ b/jlog/urls.py @@ -4,6 +4,6 @@ from jlog.views import * urlpatterns = patterns('', url(r'^$', jlog_list), - url(r'^log_list/$', jlog_list), + url(r'^log_list/(\w+)/$', jlog_list), url(r'^log_kill/(\d+)', jlog_kill), ) diff --git a/jlog/views.py b/jlog/views.py index 6ce5714b5..af4758224 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -13,12 +13,14 @@ from jlog.models import Log CONF = ConfigParser.ConfigParser() CONF.read('%s/jumpserver.conf' % BASE_DIR) -def jlog_list(request): + +def jlog_list(request, offset='online'): header_title, path1, path2 = u'查看日志 | Log List.', u'查看日志', u'日志列表' + web_socket_host = CONF.get('websocket', 'web_socket_host') online = Log.objects.filter(is_finished=0) offline = Log.objects.filter(is_finished=1) - web_socket_host = CONF.get('websocket', 'web_socket_host') - return render_to_response('jlog/log_list.html',locals()) + + return render_to_response('jlog/log_list.html', locals()) def jlog_kill(request, offset): @@ -26,4 +28,4 @@ def jlog_kill(request, offset): if pid: os.kill(int(pid), 9) Log.objects.filter(pid=pid).update(is_finished=1, end_time=datetime.now()) - return render_to_response('jlog/log_list.html', locals()) \ No newline at end of file + return HttpResponseRedirect('jlog/log_list.html', locals()) \ No newline at end of file diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 43bfca702..20ba40fd4 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -5,6 +5,7 @@ from django import template from django.db.models import Q from juser.models import User, UserGroup from jperm.views import perm_user_asset +from jasset.models import BisGroup register = template.Library() @@ -85,6 +86,15 @@ def group_type_to_str(type_name): } return group_types.get(type_name) + @register.filter(name='perm_asset_count') def perm_asset_count(user_id): return len(perm_user_asset(user_id)) + + +@register.filter(name='filter_private') +def filter_private(group): + agroup = [] + p = BisGroup.objects.get(name='ALL') + [agroup.append(g) for g in group if g != p] + return agroup diff --git a/jumpserver/views.py b/jumpserver/views.py index b19e73644..44e5d1f5d 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -30,17 +30,23 @@ def jasset_group_add(name, comment, type): BisGroup.objects.create(name=name, comment=comment, type=type) smg = u'业务组%s添加成功' %name -def jasset_host_edit(j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment): + +def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment): + print 'ok' groups = [] is_active = {u'是': '1', u'否': '2'} login_types = {'LDAP': 'L', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} - for group in j_group: + for group in j_group[0].split(): + print group.strip() c = BisGroup.objects.get(name=group.strip()) groups.append(c) j_type = login_types[j_type] + print j_type j_idc = IDC.objects.get(name=j_idc) - a = Asset.objects.get(ip=str(j_ip)) - + print j_idc + print + a = Asset.objects.get(id=j_id) + print '123' if j_type == 'M': a.ip = j_ip a.port = j_port @@ -48,10 +54,8 @@ def jasset_host_edit(j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment): a.idc = j_idc a.is_active = j_active a.comment = j_comment - a.username_common = j_user - a.password_common = j_password - a.username_super = j_root - a.password_super = j_passwd + a.username = j_user + a.password = j_password else: a.ip = j_ip a.port = j_port @@ -59,7 +63,6 @@ def jasset_host_edit(j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment): a.login_type = j_type a.is_active = is_active[j_active] a.comment = j_comment - a.save() a.bis_group = groups a.save() diff --git a/templates/head_script.html b/templates/head_script.html index 4e4716ded..a2d50f12e 100644 --- a/templates/head_script.html +++ b/templates/head_script.html @@ -1,48 +1,54 @@ + + + + + + + + - - - - + - + + + diff --git a/templates/jasset/group_detail.html b/templates/jasset/group_detail.html new file mode 100644 index 000000000..650404d06 --- /dev/null +++ b/templates/jasset/group_detail.html @@ -0,0 +1,151 @@ +{% extends 'base.html' %} +{% load mytags %} +{% block content %} +{% include 'nav_cat_bar.html' %} + +
+ + + +{% endblock %} \ No newline at end of file diff --git a/templates/jasset/group_list.html b/templates/jasset/group_list.html index bd57a5abf..69ceff7e3 100644 --- a/templates/jasset/group_list.html +++ b/templates/jasset/group_list.html @@ -22,6 +22,7 @@