diff --git a/jasset/models.py b/jasset/models.py index 8f51f8989..cfa8392e1 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -60,11 +60,11 @@ class AssetGroup(models.Model): class Asset(models.Model): ip = models.IPAddressField(unique=True) - port = models.IntegerField(max_length=6) + port = models.IntegerField(max_length=6, blank=True, null=True) group = models.ManyToManyField(AssetGroup) username = models.CharField(max_length=20, blank=True, null=True) password = models.CharField(max_length=80, blank=True, null=True) - use_default_auth = models.BooleanField(default=True) + use_default = models.BooleanField(default=True) date_added = models.DateTimeField(auto_now=True, default=datetime.datetime.now(), null=True) is_active = models.BooleanField(default=True) comment = models.CharField(max_length=100, blank=True, null=True) diff --git a/jasset/views.py b/jasset/views.py index 259a7357d..eb500050d 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -87,18 +87,19 @@ def asset_add(request): asset_group_all = AssetGroup.objects.all() if request.method == 'POST': ip = request.POST.get('ip') - port = request.POST.get('port') groups = request.POST.getlist('groups') - use_default_auth = True if request.POST.getlist('use_default_auth', []) else False + use_default = True if request.POST.getlist('use_default', []) else False is_active = True if request.POST.get('is_active') else False comment = request.POST.get('comment') - if not use_default_auth: + if not use_default: username = request.POST.get('username') password = request.POST.get('password') + port = request.POST.get('port') password_encode = CRYPTOR.encrypt(password) else: username = None + port = None password_encode = None try: @@ -110,7 +111,7 @@ def asset_add(request): pass else: db_asset_add( - ip=ip, port=port, use_default_auth=use_default_auth, is_active=is_active, comment=comment, + ip=ip, port=port, use_default=use_default, is_active=is_active, comment=comment, groups=groups, username=username, password=password_encode ) diff --git a/jperm/perm_api.py b/jperm/perm_api.py index cbaede5a3..015d0a8a8 100644 --- a/jperm/perm_api.py +++ b/jperm/perm_api.py @@ -7,6 +7,8 @@ import re from ansible.playbook import PlayBook from ansible import callbacks, utils +from jumpserver.models import Setting + def get_object_list(model, id_list): object_list = [] @@ -92,12 +94,17 @@ def perm_user_api(user, asset_new, asset_del, asset_group_new, asset_group_del): playbook = get_playbook(os.path.join(BASE_DIR, 'playbook', 'user_perm.yaml'), {'the_new_group': 'new', 'the_del_group': 'del', 'the_user': user.username, 'the_pub_key': '/tmp/id_rsa.pub'}) - print host_list, playbook + settings = get_object(Setting, id=1) + if settings: + default_user = settings.default_user + default_pri_key_path = settings.default_pri_key_path + else: + default_user = default_pri_key_path = '' results = PlayBook(host_list=host_list, playbook=playbook, forks=5, - remote_user='web', - remote_pass='redhat', + remote_user=default_user, + private_key_file=default_pri_key_path, callbacks=playbook_cb, runner_callbacks=runner_cb, stats=stats, diff --git a/jumpserver/models.py b/jumpserver/models.py index aa186301b..046ebb730 100644 --- a/jumpserver/models.py +++ b/jumpserver/models.py @@ -5,8 +5,8 @@ from django.db import models class Setting(models.Model): default_user = models.CharField(max_length=100, null=True, blank=True) - default_password = models.CharField(max_length=100, null=True, blank=True) + default_port = models.IntegerField(max_length=10, null=True, blank=True) default_pri_key_path = models.CharField(max_length=100, null=True, blank=True) class Meta: - db_table = u'setting' \ No newline at end of file + db_table = u'setting' diff --git a/jumpserver/views.py b/jumpserver/views.py index 9b0ce396c..cdf1b3591 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -12,7 +12,7 @@ from django.http import HttpResponse # from jperm.models import Apply import paramiko from jumpserver.api import * - +from jumpserver.models import Setting def getDaysByNum(num): @@ -231,6 +231,28 @@ def logout(request): def setting(request): + header_title, path1 = '项目设置', '设置' + if request.method == "POST": + username = request.POST.get('username', '') + port = request.POST.get('port', '') + private_key = request.POST.get('key', '') + + if '' in [username, port, private_key]: + return HttpResponse('所填内容不能为空') + else: + settings = get_object(Setting, id=1) + private_key_path = os.path.join(BASE_DIR, 'keys', 'default', 'default_private_key.pem') + with open(private_key_path, 'w') as f: + f.write(private_key) + os.chmod(private_key_path, 0600) + if settings: + Setting.objects.filter(id=1).update(default_user=username, default_port=port, + default_pri_key_path=private_key_path) + else: + settings = Setting(default_user=username, default_port=port, + default_pri_key_path=private_key_path).save() + + msg = "设置成功" return my_render('setting.html', locals(), request) # # def filter_ajax_api(request): diff --git a/templates/jasset/asset_add.html b/templates/jasset/asset_add.html index bbf8b196f..13125ca0d 100644 --- a/templates/jasset/asset_add.html +++ b/templates/jasset/asset_add.html @@ -42,27 +42,24 @@