2015-01-07 10:35:09 +00:00
|
|
|
|
# coding:utf-8
|
2015-02-09 11:02:25 +00:00
|
|
|
|
|
|
|
|
|
from django.db.models import Q
|
2015-09-08 16:19:17 +00:00
|
|
|
|
from jasset.asset_api import *
|
2015-03-26 10:42:52 +00:00
|
|
|
|
from jumpserver.api import *
|
2015-11-17 15:34:13 +00:00
|
|
|
|
from jumpserver.models import Setting
|
2015-11-12 16:03:51 +00:00
|
|
|
|
from jasset.forms import AssetForm, IdcForm
|
2015-11-03 15:53:12 +00:00
|
|
|
|
from jasset.models import Asset, IDC, AssetGroup, ASSET_TYPE, ASSET_STATUS
|
2015-11-14 15:09:13 +00:00
|
|
|
|
from ansible_api import Tasks
|
2014-12-22 09:18:51 +00:00
|
|
|
|
|
2015-01-19 02:09:16 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def group_add(request):
|
|
|
|
|
"""
|
2015-11-19 13:58:44 +00:00
|
|
|
|
Group add view
|
2015-09-08 16:19:17 +00:00
|
|
|
|
添加资产组
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'添加资产组', u'资产管理', u'添加资产组'
|
|
|
|
|
asset_all = Asset.objects.all()
|
2015-03-24 10:34:00 +00:00
|
|
|
|
|
2015-01-05 10:21:34 +00:00
|
|
|
|
if request.method == 'POST':
|
2015-09-08 16:19:17 +00:00
|
|
|
|
name = request.POST.get('name', '')
|
|
|
|
|
asset_select = request.POST.getlist('asset_select', [])
|
|
|
|
|
comment = request.POST.get('comment', '')
|
2015-01-07 10:35:09 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
try:
|
|
|
|
|
if not name:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
emg = u'组名不能为空'
|
|
|
|
|
raise ServerError(emg)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
asset_group_test = get_object(AssetGroup, name=name)
|
|
|
|
|
if asset_group_test:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
emg = u"该组名 %s 已存在" % name
|
|
|
|
|
raise ServerError(emg)
|
2015-04-14 09:25:56 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
except ServerError:
|
|
|
|
|
pass
|
2015-11-03 09:07:46 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
else:
|
|
|
|
|
db_add_group(name=name, comment=comment, asset_select=asset_select)
|
2015-11-03 09:07:46 +00:00
|
|
|
|
smg = u"主机组 %s 添加成功" % name
|
2015-04-14 09:25:56 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
return my_render('jasset/group_add.html', locals(), request)
|
2015-04-14 09:25:56 +00:00
|
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
2015-11-03 09:07:46 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def group_edit(request):
|
|
|
|
|
"""
|
2015-11-19 13:58:44 +00:00
|
|
|
|
Group edit view
|
2015-11-03 09:07:46 +00:00
|
|
|
|
编辑资产组
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组'
|
|
|
|
|
group_id = request.GET.get('id', '')
|
|
|
|
|
group = get_object(AssetGroup, id=group_id)
|
|
|
|
|
|
|
|
|
|
asset_all = Asset.objects.all()
|
|
|
|
|
asset_select = Asset.objects.filter(group=group)
|
|
|
|
|
asset_no_select = [a for a in asset_all if a not in asset_select]
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
name = request.POST.get('name', '')
|
|
|
|
|
asset_select = request.POST.getlist('asset_select', [])
|
|
|
|
|
comment = request.POST.get('comment', '')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if not name:
|
|
|
|
|
emg = u'组名不能为空'
|
|
|
|
|
raise ServerError(emg)
|
|
|
|
|
|
|
|
|
|
if group.name != name:
|
|
|
|
|
asset_group_test = get_object(AssetGroup, name=name)
|
|
|
|
|
if asset_group_test:
|
|
|
|
|
emg = u"该组名 %s 已存在" % name
|
|
|
|
|
raise ServerError(emg)
|
|
|
|
|
|
|
|
|
|
except ServerError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
group.asset_set.clear()
|
|
|
|
|
db_update_group(id=group_id, name=name, comment=comment, asset_select=asset_select)
|
|
|
|
|
smg = u"主机组 %s 添加成功" % name
|
|
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/jasset/group_list')
|
|
|
|
|
|
|
|
|
|
return my_render('jasset/group_edit.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def group_list(request):
|
|
|
|
|
"""
|
|
|
|
|
list asset group
|
|
|
|
|
列出资产组
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'查看资产组', u'资产管理', u'查看资产组'
|
|
|
|
|
keyword = request.GET.get('keyword', '')
|
|
|
|
|
asset_group_list = AssetGroup.objects.all()
|
2015-02-05 10:53:57 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
if keyword:
|
2015-09-10 14:51:42 +00:00
|
|
|
|
asset_group_list = asset_group_list.filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
|
2015-02-05 10:53:57 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
asset_group_list, p, asset_groups, page_range, current_page, show_first, show_end = pages(asset_group_list, request)
|
|
|
|
|
return my_render('jasset/group_list.html', locals(), request)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
|
|
|
|
|
2015-09-10 14:51:42 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def group_del(request):
|
|
|
|
|
"""
|
2015-11-19 13:58:44 +00:00
|
|
|
|
Group delete view
|
2015-09-10 14:51:42 +00:00
|
|
|
|
删除主机组
|
|
|
|
|
"""
|
|
|
|
|
group_ids = request.GET.get('id', '')
|
|
|
|
|
group_id_list = group_ids.split(',')
|
|
|
|
|
|
|
|
|
|
for group_id in group_id_list:
|
|
|
|
|
AssetGroup.objects.filter(id=group_id).delete()
|
|
|
|
|
|
|
|
|
|
return HttpResponse(u'删除成功')
|
|
|
|
|
|
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_add(request):
|
|
|
|
|
"""
|
|
|
|
|
Asset add view
|
|
|
|
|
添加资产
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产'
|
2015-09-10 14:51:42 +00:00
|
|
|
|
asset_group_all = AssetGroup.objects.all()
|
2015-10-31 01:23:10 +00:00
|
|
|
|
af = AssetForm()
|
2015-01-31 04:55:10 +00:00
|
|
|
|
if request.method == 'POST':
|
2015-11-03 09:07:46 +00:00
|
|
|
|
af_post = AssetForm(request.POST)
|
2015-11-08 14:39:30 +00:00
|
|
|
|
ip = request.POST.get('ip', '')
|
2015-11-21 10:28:25 +00:00
|
|
|
|
hostname = request.POST.get('hostname', '')
|
2015-11-08 14:39:30 +00:00
|
|
|
|
is_active = True if request.POST.get('is_active') == '1' else False
|
|
|
|
|
use_default_auth = request.POST.get('use_default_auth', '')
|
2015-09-08 16:19:17 +00:00
|
|
|
|
try:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if Asset.objects.filter(hostname=str(hostname)):
|
|
|
|
|
error = u'该主机名 %s 已存在!' % hostname
|
2015-09-08 16:19:17 +00:00
|
|
|
|
raise ServerError(error)
|
2015-04-07 11:15:45 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
except ServerError:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2015-11-03 09:07:46 +00:00
|
|
|
|
if af_post.is_valid():
|
|
|
|
|
asset_save = af_post.save(commit=False)
|
2015-11-08 14:39:30 +00:00
|
|
|
|
if not use_default_auth:
|
|
|
|
|
password = request.POST.get('password', '')
|
2015-11-16 15:44:53 +00:00
|
|
|
|
password_encode = password
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset_save.password = password_encode
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if not ip:
|
|
|
|
|
asset_save.ip = hostname
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset_save.is_active = True if is_active else False
|
2015-11-03 09:07:46 +00:00
|
|
|
|
asset_save.save()
|
|
|
|
|
af_post.save_m2m()
|
2015-11-08 14:39:30 +00:00
|
|
|
|
|
2015-11-21 10:28:25 +00:00
|
|
|
|
msg = u'主机 %s 添加成功' % hostname
|
2015-11-03 09:07:46 +00:00
|
|
|
|
else:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
esg = u'主机 %s 添加失败' % hostname
|
2015-09-08 16:19:17 +00:00
|
|
|
|
|
|
|
|
|
return my_render('jasset/asset_add.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
2015-11-14 15:09:13 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_add_batch(request):
|
|
|
|
|
header_title, path1, path2 = u'添加资产', u'资产管理', u'批量添加'
|
|
|
|
|
return my_render('jasset/asset_add_batch.html', locals(), request)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_del(request):
|
|
|
|
|
"""
|
|
|
|
|
del a asset
|
|
|
|
|
删除主机
|
|
|
|
|
"""
|
|
|
|
|
asset_id = request.GET.get('id', '')
|
|
|
|
|
if asset_id:
|
|
|
|
|
Asset.objects.filter(id=asset_id).delete()
|
2015-11-03 15:53:12 +00:00
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
asset_batch = request.GET.get('arg', '')
|
|
|
|
|
asset_id_all = str(request.POST.get('asset_id_all', ''))
|
|
|
|
|
|
|
|
|
|
if asset_batch:
|
|
|
|
|
for asset_id in asset_id_all.split(','):
|
|
|
|
|
asset = get_object(Asset, id=asset_id)
|
|
|
|
|
asset.delete()
|
|
|
|
|
|
|
|
|
|
return HttpResponse(u'删除成功')
|
2015-01-19 02:09:16 +00:00
|
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role(role='super')
|
|
|
|
|
def asset_edit(request):
|
2015-11-03 09:07:46 +00:00
|
|
|
|
"""
|
|
|
|
|
edit a asset
|
|
|
|
|
修改主机
|
|
|
|
|
"""
|
2015-09-08 16:19:17 +00:00
|
|
|
|
header_title, path1, path2 = u'修改资产', u'资产管理', u'修改资产'
|
2015-04-13 13:25:43 +00:00
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
asset_id = request.GET.get('id', '')
|
2015-11-08 14:39:30 +00:00
|
|
|
|
username = request.session.get('username', 'admin')
|
2015-09-08 16:19:17 +00:00
|
|
|
|
asset = get_object(Asset, id=asset_id)
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if asset:
|
|
|
|
|
password_old = asset.password
|
|
|
|
|
# asset_old = copy_model_instance(asset)
|
2015-11-03 09:07:46 +00:00
|
|
|
|
af = AssetForm(instance=asset)
|
2015-01-11 11:27:21 +00:00
|
|
|
|
if request.method == 'POST':
|
2015-11-03 09:07:46 +00:00
|
|
|
|
af_post = AssetForm(request.POST, instance=asset)
|
|
|
|
|
ip = request.POST.get('ip', '')
|
2015-11-21 10:28:25 +00:00
|
|
|
|
hostname = request.POST.get('hostname', '')
|
|
|
|
|
password = request.POST.get('password', '')
|
2015-11-22 10:57:47 +00:00
|
|
|
|
is_active = True if request.POST.get('is_active') == '1' else False
|
2015-11-21 10:28:25 +00:00
|
|
|
|
use_default_auth = request.POST.get('use_default_auth', '')
|
2015-01-11 11:27:21 +00:00
|
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
|
try:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
asset_test = get_object(Asset, hostname=hostname)
|
2015-11-08 14:39:30 +00:00
|
|
|
|
if asset_test and asset_id != unicode(asset_test.id):
|
2015-11-21 10:28:25 +00:00
|
|
|
|
error = u'该主机名 %s 已存在!' % hostname
|
2015-09-08 16:19:17 +00:00
|
|
|
|
raise ServerError(error)
|
|
|
|
|
except ServerError:
|
2015-04-13 13:25:43 +00:00
|
|
|
|
pass
|
2015-01-11 11:27:21 +00:00
|
|
|
|
else:
|
2015-11-03 09:07:46 +00:00
|
|
|
|
if af_post.is_valid():
|
|
|
|
|
af_save = af_post.save(commit=False)
|
2015-11-09 16:11:54 +00:00
|
|
|
|
if use_default_auth:
|
|
|
|
|
af_save.username = ''
|
|
|
|
|
af_save.password = ''
|
2015-11-21 10:28:25 +00:00
|
|
|
|
else:
|
|
|
|
|
if password_old != password:
|
|
|
|
|
password_encode = CRYPTOR.encrypt(password)
|
|
|
|
|
af_save.password = password_encode
|
2015-11-22 10:57:47 +00:00
|
|
|
|
af_save.is_active = True if is_active else False
|
2015-11-03 09:07:46 +00:00
|
|
|
|
af_save.save()
|
|
|
|
|
af_post.save_m2m()
|
2015-11-14 15:09:13 +00:00
|
|
|
|
# asset_new = get_object(Asset, id=asset_id)
|
|
|
|
|
# asset_diff_one(asset_old, asset_new)
|
2015-11-08 14:39:30 +00:00
|
|
|
|
info = asset_diff(af_post.__dict__.get('initial'), request.POST)
|
|
|
|
|
db_asset_alert(asset, username, info)
|
|
|
|
|
|
2015-11-03 09:07:46 +00:00
|
|
|
|
msg = u'主机 %s 修改成功' % ip
|
|
|
|
|
else:
|
|
|
|
|
emg = u'主机 %s 修改失败' % ip
|
2015-09-08 16:19:17 +00:00
|
|
|
|
return HttpResponseRedirect('/jasset/asset_detail/?id=%s' % asset_id)
|
|
|
|
|
|
|
|
|
|
return my_render('jasset/asset_edit.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
2015-11-03 15:53:12 +00:00
|
|
|
|
@require_role('user')
|
2015-11-08 14:39:30 +00:00
|
|
|
|
def asset_list(request):
|
2015-11-03 15:53:12 +00:00
|
|
|
|
"""
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset list view
|
2015-11-03 15:53:12 +00:00
|
|
|
|
"""
|
2015-11-22 10:57:47 +00:00
|
|
|
|
header_title, path1, path2 = u'查看资产', u'资产管理', u'查看资产'
|
2015-11-03 15:53:12 +00:00
|
|
|
|
idc_all = IDC.objects.filter()
|
|
|
|
|
asset_group_all = AssetGroup.objects.all()
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset_types = ASSET_TYPE
|
2015-11-03 15:53:12 +00:00
|
|
|
|
asset_status = ASSET_STATUS
|
2015-09-08 16:19:17 +00:00
|
|
|
|
|
2015-11-03 15:53:12 +00:00
|
|
|
|
idc_name = request.GET.get('idc', '')
|
|
|
|
|
group_name = request.GET.get('group', '')
|
|
|
|
|
asset_type = request.GET.get('asset_type', '')
|
|
|
|
|
status = request.GET.get('status', '')
|
|
|
|
|
keyword = request.GET.get('keyword', '')
|
2015-11-08 14:39:30 +00:00
|
|
|
|
export = request.GET.get("export", False)
|
2015-11-21 10:28:25 +00:00
|
|
|
|
group_id = request.GET.get("group_id", '')
|
|
|
|
|
idc_id = request.GET.get("idc_id", '')
|
|
|
|
|
if group_id:
|
|
|
|
|
group = get_object(AssetGroup, id=group_id)
|
|
|
|
|
if group:
|
|
|
|
|
asset_find = Asset.objects.filter(group=group)
|
|
|
|
|
elif idc_id:
|
|
|
|
|
idc = get_object(IDC, id=idc_id)
|
|
|
|
|
if idc:
|
|
|
|
|
asset_find = Asset.objects.filter(idc=idc)
|
|
|
|
|
else:
|
|
|
|
|
asset_find = Asset.objects.all()
|
2015-09-08 16:19:17 +00:00
|
|
|
|
|
2015-11-08 14:39:30 +00:00
|
|
|
|
if idc_name:
|
|
|
|
|
asset_find = asset_find.filter(idc__name__contains=idc_name)
|
|
|
|
|
|
|
|
|
|
if group_name:
|
|
|
|
|
asset_find = asset_find.filter(group__name__contains=group_name)
|
|
|
|
|
|
|
|
|
|
if asset_type:
|
|
|
|
|
asset_find = asset_find.filter(asset_type__contains=asset_type)
|
|
|
|
|
|
|
|
|
|
if status:
|
|
|
|
|
asset_find = asset_find.filter(status__contains=status)
|
|
|
|
|
|
|
|
|
|
if keyword:
|
2015-11-03 15:53:12 +00:00
|
|
|
|
asset_find = asset_find.filter(
|
|
|
|
|
Q(hostname__contains=keyword) |
|
2015-11-08 14:39:30 +00:00
|
|
|
|
Q(other_ip__contains=keyword) |
|
2015-11-03 15:53:12 +00:00
|
|
|
|
Q(ip__contains=keyword) |
|
|
|
|
|
Q(remote_ip__contains=keyword) |
|
|
|
|
|
Q(comment__contains=keyword) |
|
|
|
|
|
Q(group__name__contains=keyword) |
|
|
|
|
|
Q(cpu__contains=keyword) |
|
|
|
|
|
Q(memory__contains=keyword) |
|
|
|
|
|
Q(disk__contains=keyword))
|
|
|
|
|
|
2015-11-08 14:39:30 +00:00
|
|
|
|
if export:
|
|
|
|
|
s = write_excel(asset_find)
|
|
|
|
|
if s[0]:
|
|
|
|
|
file_name = s[1]
|
2015-11-19 13:58:44 +00:00
|
|
|
|
smg = u'excel文件已生成,请点击下载!'
|
2015-11-08 14:39:30 +00:00
|
|
|
|
return my_render('jasset/asset_excel_download.html', locals(), request)
|
|
|
|
|
assets_list, p, assets, page_range, current_page, show_first, show_end = pages(asset_find, request)
|
|
|
|
|
return my_render('jasset/asset_list.html', locals(), request)
|
|
|
|
|
|
2015-11-03 15:53:12 +00:00
|
|
|
|
|
2015-11-08 14:39:30 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_edit_batch(request):
|
|
|
|
|
af = AssetForm()
|
2015-11-21 10:28:25 +00:00
|
|
|
|
name = request.session.get('username', 'admin')
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset_group_all = AssetGroup.objects.all()
|
2015-11-19 16:03:36 +00:00
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
env = request.POST.get('env', '')
|
|
|
|
|
idc_id = request.POST.get('idc', '')
|
|
|
|
|
port = request.POST.get('port', '')
|
|
|
|
|
use_default_auth = request.POST.get('use_default_auth', '')
|
|
|
|
|
username = request.POST.get('username', '')
|
|
|
|
|
password = request.POST.get('password', '')
|
|
|
|
|
group = request.POST.getlist('group', [])
|
|
|
|
|
cabinet = request.POST.get('cabinet', '')
|
|
|
|
|
comment = request.POST.get('comment', '')
|
|
|
|
|
asset_id_all = unicode(request.GET.get('asset_id_all', ''))
|
|
|
|
|
asset_id_all = asset_id_all.split(',')
|
|
|
|
|
for asset_id in asset_id_all:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
alert_list = []
|
2015-11-19 16:03:36 +00:00
|
|
|
|
asset = get_object(Asset, id=asset_id)
|
|
|
|
|
if asset:
|
|
|
|
|
if env:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if asset.env != env:
|
|
|
|
|
asset.env = env
|
|
|
|
|
alert_list.append([u'运行环境', asset.env, env])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if idc_id:
|
|
|
|
|
idc = get_object(IDC, id=idc_id)
|
2015-11-21 10:28:25 +00:00
|
|
|
|
name_old = asset.idc.name if asset.idc else u''
|
|
|
|
|
if idc and idc.name != name_old:
|
2015-11-19 16:03:36 +00:00
|
|
|
|
asset.idc = idc
|
2015-11-21 10:28:25 +00:00
|
|
|
|
alert_list.append([u'机房', name_old, idc.name])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if port:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if unicode(asset.port) != port:
|
|
|
|
|
asset.port = port
|
|
|
|
|
alert_list.append([u'端口号', asset.port, port])
|
|
|
|
|
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if use_default_auth:
|
|
|
|
|
if use_default_auth == 'default':
|
|
|
|
|
asset.use_default_auth = 1
|
|
|
|
|
asset.username = ''
|
|
|
|
|
asset.password = ''
|
2015-11-21 10:28:25 +00:00
|
|
|
|
alert_list.append([u'使用默认管理账号', asset.use_default_auth, u'默认'])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
elif use_default_auth == 'user_passwd':
|
|
|
|
|
asset.use_default_auth = 0
|
|
|
|
|
asset.username = username
|
|
|
|
|
password_encode = CRYPTOR.encrypt(password)
|
|
|
|
|
asset.password = password_encode
|
2015-11-21 10:28:25 +00:00
|
|
|
|
alert_list.append([u'使用默认管理账号', asset.use_default_auth, username])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if group:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
group_new, group_old, group_new_name, group_old_name = [], asset.group.all(), [], []
|
2015-11-19 16:03:36 +00:00
|
|
|
|
for group_id in group:
|
|
|
|
|
g = get_object(AssetGroup, id=group_id)
|
|
|
|
|
if g:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
group_new.append(g)
|
|
|
|
|
if not set(group_new) < set(group_old):
|
|
|
|
|
group_instance = list(set(group_new) | set(group_old))
|
|
|
|
|
for g in group_instance:
|
|
|
|
|
group_new_name.append(g.name)
|
|
|
|
|
for g in group_old:
|
|
|
|
|
group_old_name.append(g.name)
|
|
|
|
|
asset.group = group_instance
|
|
|
|
|
alert_list.append([u'主机组', ','.join(group_old_name), ','.join(group_new_name)])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if cabinet:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if asset.cabinet != cabinet:
|
|
|
|
|
asset.cabinet = cabinet
|
|
|
|
|
alert_list.append([u'机柜号', asset.cabinet, cabinet])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
if comment:
|
2015-11-21 10:28:25 +00:00
|
|
|
|
if asset.comment != comment:
|
|
|
|
|
asset.comment = comment
|
|
|
|
|
alert_list.append([u'备注', asset.comment, comment])
|
2015-11-19 16:03:36 +00:00
|
|
|
|
asset.save()
|
2015-11-21 10:28:25 +00:00
|
|
|
|
|
|
|
|
|
if alert_list:
|
2015-11-22 10:57:47 +00:00
|
|
|
|
recode_name = unicode(name) + ' - ' + u'批量'
|
|
|
|
|
AssetRecord.objects.create(asset=asset, username=recode_name, content=alert_list)
|
2015-11-19 16:03:36 +00:00
|
|
|
|
return HttpResponse('ok')
|
|
|
|
|
|
2015-11-08 14:39:30 +00:00
|
|
|
|
return my_render('jasset/asset_edit_batch.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
2015-09-08 16:19:17 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_detail(request):
|
2015-11-08 14:39:30 +00:00
|
|
|
|
"""
|
2015-11-14 15:09:13 +00:00
|
|
|
|
Asset detail view
|
2015-11-08 14:39:30 +00:00
|
|
|
|
"""
|
2015-09-08 16:19:17 +00:00
|
|
|
|
header_title, path1, path2 = u'主机详细信息', u'资产管理', u'主机详情'
|
|
|
|
|
asset_id = request.GET.get('id', '')
|
|
|
|
|
asset = get_object(Asset, id=asset_id)
|
2015-11-08 14:39:30 +00:00
|
|
|
|
asset_record = AssetRecord.objects.filter(asset=asset).order_by('-alert_time')
|
2015-09-08 16:19:17 +00:00
|
|
|
|
|
|
|
|
|
return my_render('jasset/asset_detail.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
2015-11-14 15:09:13 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_update(request):
|
|
|
|
|
"""
|
|
|
|
|
Asset update host info via ansible view
|
|
|
|
|
"""
|
|
|
|
|
asset_id = request.GET.get('id', '')
|
|
|
|
|
asset = get_object(Asset, id=asset_id)
|
2015-11-22 10:57:47 +00:00
|
|
|
|
name = request.session.get('username', 'admin')
|
2015-11-14 15:09:13 +00:00
|
|
|
|
if not asset:
|
|
|
|
|
return HttpResponseRedirect('/jasset/asset_detail/?id=%s' % asset_id)
|
|
|
|
|
if asset.use_default_auth:
|
2015-11-17 15:34:13 +00:00
|
|
|
|
default = Setting.objects.all()
|
|
|
|
|
if default:
|
|
|
|
|
default = default[0]
|
|
|
|
|
username = default.default_user
|
2015-11-22 10:57:47 +00:00
|
|
|
|
password = CRYPTOR.decrypt(default.default_password)
|
2015-11-17 15:34:13 +00:00
|
|
|
|
port = default.default_port
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse(u'没有设置默认用户名和密码!')
|
2015-11-14 15:09:13 +00:00
|
|
|
|
else:
|
|
|
|
|
username = asset.username
|
2015-11-22 10:57:47 +00:00
|
|
|
|
password = CRYPTOR.decrypt(asset.password)
|
2015-11-17 15:34:13 +00:00
|
|
|
|
port = asset.port
|
2015-11-14 15:09:13 +00:00
|
|
|
|
|
2015-11-17 15:34:13 +00:00
|
|
|
|
resource = [{"hostname": asset.ip, "port": port,
|
2015-11-14 15:09:13 +00:00
|
|
|
|
"username": username, "password": password}]
|
|
|
|
|
|
|
|
|
|
ansible_instance = Tasks(resource)
|
|
|
|
|
ansible_asset_info = ansible_instance.get_host_info()
|
|
|
|
|
if ansible_asset_info['status'] == 'ok':
|
|
|
|
|
asset_info = ansible_asset_info['result'][asset.ip]
|
|
|
|
|
if asset_info:
|
|
|
|
|
hostname = asset_info.get('hostname')
|
2015-11-22 10:57:47 +00:00
|
|
|
|
all_ip = asset_info.get('other_ip')
|
|
|
|
|
other_ip_list = all_ip.remove(asset.ip) if asset.ip in all_ip else []
|
|
|
|
|
other_ip = ','.join(other_ip_list) if other_ip_list else ''
|
2015-11-14 15:09:13 +00:00
|
|
|
|
cpu_type = asset_info.get('cpu_type')[1]
|
|
|
|
|
cpu_cores = asset_info.get('cpu_cores')
|
|
|
|
|
cpu = cpu_type + ' * ' + unicode(cpu_cores)
|
|
|
|
|
memory = asset_info.get('memory')
|
|
|
|
|
disk = asset_info.get('disk')
|
|
|
|
|
sn = asset_info.get('sn')
|
|
|
|
|
brand = asset_info.get('brand')
|
|
|
|
|
system_type = asset_info.get('system_type')
|
|
|
|
|
system_version = asset_info.get('system_version')
|
|
|
|
|
|
|
|
|
|
asset_dic = {"hostname": hostname, "other_ip": other_ip, "cpu": cpu,
|
|
|
|
|
"memory": memory, "disk": disk, "system_type": system_type,
|
|
|
|
|
"system_version": system_version, "brand": brand, "sn": sn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ansible_record(asset, asset_dic, name)
|
|
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/jasset/asset_detail/?id=%s' % asset_id)
|
|
|
|
|
|
|
|
|
|
|
2015-11-12 16:03:51 +00:00
|
|
|
|
@require_role('admin')
|
|
|
|
|
def idc_add(request):
|
|
|
|
|
"""
|
|
|
|
|
IDC add view
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'添加IDC', u'资产管理', u'添加IDC'
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
idc_form = IdcForm(request.POST)
|
|
|
|
|
if idc_form.is_valid():
|
|
|
|
|
idc_name = idc_form.cleaned_data['name']
|
|
|
|
|
|
|
|
|
|
if IDC.objects.filter(name=idc_name):
|
|
|
|
|
emg = u'添加失败, 此IDC %s 已存在!' % idc_name
|
|
|
|
|
return my_render('jasset/idc_add.html', locals(), request)
|
|
|
|
|
else:
|
|
|
|
|
idc_form.save()
|
|
|
|
|
smg = u'IDC: %s添加成功' % idc_name
|
|
|
|
|
return HttpResponseRedirect("/jasset/idc_list/")
|
|
|
|
|
else:
|
|
|
|
|
idc_form = IdcForm()
|
|
|
|
|
return render_to_response('jasset/idc_add.html',
|
|
|
|
|
locals(),
|
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_role('admin')
|
|
|
|
|
def idc_list(request):
|
2015-11-14 15:09:13 +00:00
|
|
|
|
"""
|
|
|
|
|
IDC list view
|
|
|
|
|
"""
|
2015-11-12 16:03:51 +00:00
|
|
|
|
header_title, path1, path2 = u'查看IDC', u'资产管理', u'查看IDC'
|
|
|
|
|
posts = IDC.objects.all()
|
|
|
|
|
keyword = request.GET.get('keyword', '')
|
|
|
|
|
if keyword:
|
|
|
|
|
posts = IDC.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
|
|
|
|
|
else:
|
|
|
|
|
posts = IDC.objects.exclude(name='ALL').order_by('id')
|
|
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
|
|
|
|
return render_to_response('jasset/idc_list.html',
|
|
|
|
|
locals(),
|
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_role('admin')
|
|
|
|
|
def idc_edit(request):
|
2015-11-14 15:09:13 +00:00
|
|
|
|
"""
|
|
|
|
|
IDC edit view
|
|
|
|
|
"""
|
|
|
|
|
header_title, path1, path2 = u'编辑IDC', u'资产管理', u'编辑IDC'
|
2015-11-12 16:03:51 +00:00
|
|
|
|
idc_id = request.GET.get('id', '')
|
|
|
|
|
idc = get_object(IDC, id=idc_id)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
idc_form = IdcForm(request.POST, instance=idc)
|
|
|
|
|
if idc_form.is_valid():
|
|
|
|
|
idc_form.save()
|
|
|
|
|
return HttpResponseRedirect("/jasset/idc_list/")
|
|
|
|
|
else:
|
|
|
|
|
idc_form = IdcForm(instance=idc)
|
|
|
|
|
return my_render('jasset/idc_edit.html', locals(), request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_role('admin')
|
|
|
|
|
def idc_del(request):
|
2015-11-14 15:09:13 +00:00
|
|
|
|
"""
|
|
|
|
|
IDC delete view
|
|
|
|
|
"""
|
2015-11-21 10:28:25 +00:00
|
|
|
|
idc_ids = request.GET.get('id', '')
|
|
|
|
|
idc_id_list = idc_ids.split(',')
|
|
|
|
|
|
|
|
|
|
for idc_id in idc_id_list:
|
|
|
|
|
IDC.objects.filter(id=idc_id).delete()
|
|
|
|
|
|
2015-11-12 16:03:51 +00:00
|
|
|
|
return HttpResponseRedirect('/jasset/idc_list/')
|
2015-11-14 15:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_role('admin')
|
|
|
|
|
def asset_upload(request):
|
|
|
|
|
"""
|
2015-11-19 13:58:44 +00:00
|
|
|
|
Upload asset excel file view
|
2015-11-14 15:09:13 +00:00
|
|
|
|
"""
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
excel_file = request.FILES.get('file_name', '')
|
|
|
|
|
ret = excel_to_db(excel_file)
|
|
|
|
|
if ret:
|
|
|
|
|
smg = u'批量添加成功'
|
|
|
|
|
else:
|
|
|
|
|
emg = u'批量添加失败,请检查格式.'
|
|
|
|
|
return my_render('jasset/asset_add_batch.html', locals(), request)
|