2015-02-10 10:53:01 +00:00
|
|
|
#coding:utf-8
|
|
|
|
import django
|
|
|
|
import os
|
|
|
|
import sys
|
2015-03-03 10:42:06 +00:00
|
|
|
import random
|
|
|
|
import datetime
|
2015-02-10 10:53:01 +00:00
|
|
|
|
|
|
|
sys.path.append('../')
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings'
|
|
|
|
django.setup()
|
|
|
|
|
2015-03-03 10:42:06 +00:00
|
|
|
|
|
|
|
from juser.views import db_add_user, md5_crypt, CRYPTOR, db_add_group
|
2015-02-11 15:32:24 +00:00
|
|
|
from jasset.models import Asset, IDC, BisGroup
|
2015-03-14 09:54:27 +00:00
|
|
|
from juser.models import UserGroup, DEPT, User
|
2015-02-11 15:32:24 +00:00
|
|
|
from jperm.models import CmdGroup
|
2015-03-04 15:14:07 +00:00
|
|
|
from jlog.models import Log
|
2015-02-11 15:32:24 +00:00
|
|
|
|
|
|
|
|
2015-03-14 09:54:27 +00:00
|
|
|
def install():
|
|
|
|
IDC.objects.create(name='ALL', comment='ALL')
|
|
|
|
IDC.objects.create(name='默认', comment='默认')
|
|
|
|
DEPT.objects.create(name="默认", comment="默认部门")
|
|
|
|
DEPT.objects.create(name="超管部", comment="超级管理员部门")
|
|
|
|
dept = DEPT.objects.get(name='超管部')
|
|
|
|
dept2 = DEPT.objects.get(name='默认')
|
|
|
|
UserGroup.objects.create(name='ALL', dept=dept, comment='ALL')
|
|
|
|
UserGroup.objects.create(name='默认', dept=dept, comment='默认')
|
|
|
|
|
|
|
|
BisGroup.objects.create(name='ALL', dept=dept, comment='ALL')
|
|
|
|
BisGroup.objects.create(name='默认', dept=dept, comment='默认')
|
|
|
|
|
|
|
|
User(id=5000, username="admin", password=md5_crypt('admin'),
|
|
|
|
name='admin', email='admin@jumpserver.org', role='SU', is_active=True, dept=dept).save()
|
|
|
|
User(id=5001, username="group_admin", password=md5_crypt('group_admin'),
|
|
|
|
name='group_admin', email='group_admin@jumpserver.org', role='DA', is_active=True, dept=dept2).save()
|
|
|
|
|
|
|
|
|
2015-02-11 15:32:24 +00:00
|
|
|
def test_add_idc():
|
|
|
|
for i in range(1, 20):
|
|
|
|
name = 'IDC' + str(i)
|
|
|
|
IDC.objects.create(name=name, comment='')
|
|
|
|
print 'Add: %s' % name
|
|
|
|
|
|
|
|
|
2015-03-03 10:42:06 +00:00
|
|
|
def test_add_dept():
|
|
|
|
for i in range(1, 100):
|
|
|
|
name = 'DEPT' + str(i)
|
|
|
|
print "Add: %s" % name
|
|
|
|
DEPT.objects.create(name=name, comment=name)
|
2015-02-11 15:32:24 +00:00
|
|
|
|
2015-03-03 10:42:06 +00:00
|
|
|
|
|
|
|
def test_add_group():
|
|
|
|
dept_all = DEPT.objects.all()
|
|
|
|
for i in range(1, 100):
|
2015-02-11 15:32:24 +00:00
|
|
|
name = 'UserGroup' + str(i)
|
2015-03-03 10:42:06 +00:00
|
|
|
UserGroup.objects.create(name=name, dept=random.choice(dept_all), comment=name)
|
2015-02-11 15:32:24 +00:00
|
|
|
print 'Add: %s' % name
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_cmd_group():
|
|
|
|
for i in range(1, 20):
|
|
|
|
name = 'CMD' + str(i)
|
|
|
|
cmd = '/sbin/ping%s, /sbin/ifconfig/' % str(i)
|
|
|
|
CmdGroup.objects.create(name=name, cmd=cmd, comment=name)
|
|
|
|
print 'Add: %s' % name
|
2015-02-10 10:53:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_user():
|
|
|
|
for i in range(1, 500):
|
|
|
|
username = "test" + str(i)
|
2015-03-03 10:42:06 +00:00
|
|
|
dept_all = DEPT.objects.all()
|
|
|
|
group_all = UserGroup.objects.all()
|
|
|
|
group_all_id = [group.id for group in group_all]
|
2015-02-10 10:53:01 +00:00
|
|
|
db_add_user(username=username,
|
|
|
|
password=md5_crypt(username),
|
2015-03-03 10:42:06 +00:00
|
|
|
dept=random.choice(dept_all),
|
2015-02-10 10:53:01 +00:00
|
|
|
name=username, email='%s@jumpserver.org' % username,
|
2015-03-03 10:42:06 +00:00
|
|
|
groups=[random.choice(group_all_id) for i in range(1, 4)], role='CU',
|
2015-02-10 10:53:01 +00:00
|
|
|
ssh_key_pwd=CRYPTOR.encrypt(username),
|
|
|
|
ldap_pwd=CRYPTOR.encrypt(username),
|
|
|
|
is_active=True,
|
2015-03-03 10:42:06 +00:00
|
|
|
date_joined=datetime.datetime.now())
|
2015-02-10 10:53:01 +00:00
|
|
|
print "Add: %s" % username
|
|
|
|
|
|
|
|
|
2015-03-05 16:24:17 +00:00
|
|
|
def test_add_asset_group():
|
2015-03-14 09:54:27 +00:00
|
|
|
dept = DEPT.objects.get(name='默认')
|
2015-03-05 16:24:17 +00:00
|
|
|
for i in range(1, 20):
|
|
|
|
name = 'AssetGroup' + str(i)
|
2015-03-14 09:54:27 +00:00
|
|
|
group = BisGroup(name=name, dept=dept, comment=name)
|
2015-03-05 16:24:17 +00:00
|
|
|
group.save()
|
|
|
|
print 'Add: %s' % name
|
|
|
|
|
|
|
|
|
2015-02-10 10:53:01 +00:00
|
|
|
def test_add_asset():
|
2015-03-05 16:24:17 +00:00
|
|
|
idc_all = IDC.objects.all()
|
|
|
|
test_idc = random.choice(idc_all)
|
|
|
|
bis_group_all = BisGroup.objects.all()
|
2015-03-14 09:54:27 +00:00
|
|
|
dept_all = DEPT.objects.all()
|
2015-02-10 10:53:01 +00:00
|
|
|
for i in range(1, 500):
|
|
|
|
ip = '192.168.1.' + str(i)
|
2015-03-05 16:24:17 +00:00
|
|
|
asset = Asset(ip=ip, port=22, login_type='L', idc=test_idc, is_active=True, comment='test')
|
|
|
|
asset.save()
|
|
|
|
asset.bis_group = [random.choice(bis_group_all) for i in range(2)]
|
2015-03-14 09:54:27 +00:00
|
|
|
asset.dept = [random.choice(dept_all) for i in range(2)]
|
2015-02-10 10:53:01 +00:00
|
|
|
print "Add: %s" % ip
|
|
|
|
|
|
|
|
|
2015-03-04 15:14:07 +00:00
|
|
|
def test_add_log():
|
|
|
|
li_date = []
|
|
|
|
today = datetime.date.today()
|
|
|
|
oneday = datetime.timedelta(days=1)
|
|
|
|
for i in range(0, 7):
|
|
|
|
today = today-oneday
|
|
|
|
li_date.append(today)
|
|
|
|
user_list = ['马云', '马化腾', '丁磊', '周鸿祎', '雷军', '柳传志', '陈天桥', '李彦宏', '李开复', '罗永浩']
|
|
|
|
for i in range(1, 1000):
|
|
|
|
user = random.choice(user_list)
|
|
|
|
ip = random.randint(1, 20)
|
|
|
|
start_time = random.choice(li_date)
|
|
|
|
end_time = datetime.datetime.now()
|
|
|
|
log_path = '/var/log/jumpserver/test.log'
|
|
|
|
host = '192.168.1.' + str(ip)
|
2015-03-19 10:32:10 +00:00
|
|
|
Log.objects.create(user=user, host=host, remote_ip='8.8.8.8', dept_name='运维部', log_path=log_path, pid=168, start_time=start_time,
|
2015-03-04 15:14:07 +00:00
|
|
|
is_finished=1, log_finished=1, end_time=end_time)
|
|
|
|
|
|
|
|
|
2015-02-10 10:53:01 +00:00
|
|
|
if __name__ == '__main__':
|
2015-03-21 07:08:16 +00:00
|
|
|
install()
|
|
|
|
test_add_dept()
|
|
|
|
test_add_group()
|
|
|
|
test_add_user()
|
|
|
|
test_add_idc()
|
|
|
|
test_add_asset_group()
|
|
|
|
test_add_asset()
|
2015-03-04 15:14:07 +00:00
|
|
|
test_add_log()
|
2015-02-10 10:53:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|