修改settings

pull/26/head
Administrator 2015-10-05 23:48:03 +08:00
parent 6482a5e214
commit 699046dad5
7 changed files with 59 additions and 38 deletions

View File

@ -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)

View File

@ -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
)

View File

@ -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,

View File

@ -5,7 +5,7 @@ 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:

View File

@ -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):

View File

@ -42,27 +42,24 @@
<div class="form-group"><label class="col-sm-2 control-label"> IP地址<span class="red-fonts">*</span> </label>
<div class="col-sm-8"><input type="text" name="ip" placeholder="IP" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label class="col-sm-2 control-label"> 端口号<span class="red-fonts">*</span> </label>
<div class="col-sm-8">
<input type="text" placeholder="Port" name="port" class="form-control">
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="j_group" class="col-sm-2 control-label">使用默认管理账号</label>
<label for="j_group" class="col-sm-2 control-label">使用默认</label>
<div class="col-sm-1">
<div class="radio i-checks">
<label>
<input type="checkbox" checked="" value="1" id="use_default_auth" name="use_default_auth">
<input type="checkbox" checked="" value="1" id="use_default" name="use_default">
</label>
</div>
</div>
</div>
<div class="form-group" id="port" style="display: none">
<label class="col-sm-2 control-label"> 端口号<span class="red-fonts">*</span> </label>
<div class="col-sm-8">
<input type="text" placeholder="Port" name="port" class="form-control">
</div>
</div>
<div class="form-group" id="admin_account" style="display: none">
<label class="col-sm-2 control-label"> 管理用户名<span class="red-fonts">*</span> </label>
<div class="col-sm-3">
@ -125,12 +122,14 @@
<script>
$('document').ready(function(){
$('#use_default_auth').click(function(){
$('#use_default').click(function(){
if ($(this).is(':checked')){
$('#admin_account').css('display', 'none')
$('#admin_account').css('display', 'none');
$('#port').css('display', 'none')
}
else {
$('#admin_account').css('display', 'block')
$('#admin_account').css('display', 'block');
$('#port').css('display', 'block')
}
})
});
@ -148,12 +147,6 @@ $('#assetForm').validator({
tip: "输入IP",
ok: "",
msg: {required: "必须填写!"}
},
"port": {
rule: "required;check_port",
tip: "输入端口号",
ok: "",
msg: {required: "必须填写!"}
}
},
valid: function(form) {

View File

@ -52,16 +52,16 @@
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="groups" class="col-sm-2 control-label">默认密码</label>
<label for="port" class="col-sm-2 control-label">默认ssh端口<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input id="password" name="password" placeholder="Password" type="password" class="form-control">
<input id="port" name="port" placeholder="Port" type="text" class="form-control">
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">默认密钥<span class="red-fonts"></span></label>
<label for="key" class="col-sm-2 control-label">默认密钥<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<textarea class="form-control" placeholder="" rows="5"></textarea>
<textarea class="form-control" name="key" placeholder="" rows="10" style="font-size: 9px;"></textarea>
</div>
</div>
<div class="hr-line-dashed"></div>
@ -79,12 +79,10 @@
<thead>
<tr>
<th class="text-center">组名</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>