Browse Source

1. update role push

pull/26/head
yumaojun 9 years ago
parent
commit
83c2704d53
  1. 2
      jperm/models.py
  2. 40
      jperm/views.py
  3. 29
      templates/jperm/perm_role_push.html

2
jperm/models.py

@ -20,7 +20,7 @@ class SysUser(models.Model):
class PermRole(models.Model):
name = models.CharField(max_length=100)
name = models.CharField(max_length=100, unique=True)
comment = models.CharField(max_length=100, null=True, blank=True, default='')
password = models.CharField(max_length=100)
key_path = models.CharField(max_length=100)

40
jperm/views.py

@ -344,6 +344,46 @@ def perm_role_push(request):
return my_render('jperm/perm_role_push.html', render_data, request)
if request.method == "POST":
# 获取推荐角色的名称列表
role_names = request.POST.getlist("roles")
# 计算出需要推送的资产列表
asset_ips = request.POST.getlist("assets")
asset_group_names = request.POST.getlist("asset_groups")
assets_obj = [Asset.objects.get(ip=asset_ip) for asset_ip in asset_ips]
asset_groups_obj = [AssetGroup.objects.get(name=asset_group_name) for asset_group_name in asset_group_names]
group_assets_obj = []
for asset_group in asset_groups_obj:
group_assets_obj.extend(asset_group.asset_set.all())
calc_assets = set(assets_obj) | set(group_assets_obj)
# 生成Inventory
hosts = [{"hostname": asset.ip,
"port": asset.port,
"username": asset.username,
"password": asset.password} for asset in calc_assets]
# 获取角色的推送方式,以及推送需要的信息
roles_obj = [PermRole.objects.get(name=role_name) for role_name in role_names]
roles_info = {}
for role in roles_obj:
roles_info[role.name] = {"password": role.password, "key": role.key_path}
# 推送
password_push = request.POST.get("use_password")
key_push = request.POST.get("use_publicKey")
if password_push:
pass
if key_push:
pass
# 调用Ansible API 执行 password方式的授权 TODO: Surport sudo
# tasks = Tasks(hosts)
# ret = tasks.add_multi_user(*role_names)
return HttpResponse(u"未实现")

29
templates/jperm/perm_role_push.html

@ -36,7 +36,7 @@
<div class="form-group">
<label for="asset" class="col-sm-2 control-label">资产<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select name="asset" data-placeholder="请选择资产" class="chosen-select form-control m-b" multiple tabindex="2">
<select name="assets" data-placeholder="请选择资产" class="chosen-select form-control m-b" multiple tabindex="2">
{% for asset in assets %}
<option value="{{ asset.ip }}">{{ asset.ip }}</option>
{% endfor %}
@ -47,7 +47,7 @@
<div class="form-group">
<label for="assetgroup" class="col-sm-2 control-label">资产组<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select name="assetgroup" data-placeholder="请选择资产组" class="chosen-select form-control m-b" multiple tabindex="2">
<select name="asset_groups" data-placeholder="请选择资产组" class="chosen-select form-control m-b" multiple tabindex="2">
{% for asset_group in asset_groups %}
<option value="{{ asset_group.name }}">{{ asset_group.name }}</option>
{% endfor %}
@ -58,7 +58,7 @@
<div class="form-group">
<label for="role" class="col-sm-2 control-label">角色<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select name="role" data-placeholder="请选择角色" class="chosen-select form-control m-b" multiple tabindex="2">
<select name="roles" data-placeholder="请选择角色" class="chosen-select form-control m-b" multiple tabindex="2">
{% for role in roles %}
<option value="{{ role.name }}">{{ role.name }}</option>
{% endfor %}
@ -66,6 +66,29 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="row">
<div class="form-group">
<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" value="1" id="use_password" name="use_password">
</label>
</div>
</div>
</div>
<div class="form-group">
<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" value="1" id="use_publicKey" name="use_publicKey">
</label>
</div>
</div>
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">备注</label>
<div class="col-sm-8">

Loading…
Cancel
Save