修改gen_resource api

pull/26/head
ibuler 2015-11-22 17:56:38 +08:00
parent 5f768444ac
commit a143797ac9
4 changed files with 42 additions and 22 deletions

View File

@ -132,13 +132,29 @@ def get_group_asset_perm(ob):
return perm return perm
def gen_resource(ob, perm=None): def gen_resource(ob, ex='', perm=None):
""" """
ob为用户或资产列表或资产queryset ob为用户或资产列表或资产queryset, 如果同时输入用户和资产则获取用户在这些资产上的信息
生成MyInventory需要的 resource文件 生成MyInventory需要的 resource文件
""" """
res = [] res = []
if isinstance(ob, User): if isinstance(ob, User) and isinstance(ex, (list, QuerySet)):
if not perm:
perm = get_group_user_perm(ob)
for asset, asset_info in perm.get('asset').items():
if asset not in ex:
continue
asset_info = get_asset_info(asset)
info = {'hostname': asset.hostname, 'ip': asset.ip, 'port': asset_info.get('port', 22)}
try:
role = sorted(list(perm.get('asset').get(asset).get('role')))[0]
except IndexError:
continue
info['username'] = role.name
info['password'] = role.password
info['ssh_key'] = get_role_key(ob, role)
res.append(info)
elif isinstance(ob, User):
if not perm: if not perm:
perm = get_group_user_perm(ob) perm = get_group_user_perm(ob)

View File

@ -96,9 +96,12 @@ def get_role_key(user, role):
def chown(path, user, group=''): def chown(path, user, group=''):
if not group: if not group:
group = user group = user
uid = pwd.getpwnam(user).pw_uid try:
gid = pwd.getpwnam(group).pwd_gid uid = pwd.getpwnam(user).pw_uid
os.chown(path, uid, gid) gid = pwd.getpwnam(group).pw_gid
os.chown(path, uid, gid)
except KeyError:
pass
def page_list_return(total, current=1): def page_list_return(total, current=1):

View File

@ -48,6 +48,7 @@
{{ af.ip|bootstrap_horizontal }} {{ af.ip|bootstrap_horizontal }}
<p class="col-sm-offset-2">Tips: 如果IP地址不填写, IP默认会设置与主机名一致</p> <p class="col-sm-offset-2">Tips: 如果IP地址不填写, IP默认会设置与主机名一致</p>
<div class="hr-line-dashed"></div> <div class="hr-line-dashed"></div>
<div class="form-group"> <div class="form-group">
<label for="j_group" class="col-sm-2 control-label">管理账号<span class="red-fonts"> *</span></label> <label for="j_group" class="col-sm-2 control-label">管理账号<span class="red-fonts"> *</span></label>

View File

@ -26,7 +26,7 @@
</div> </div>
</div> </div>
<div class="ibox-content"> <div class="ibox-content">
<form method="post" id="userForm" class="form-horizontal" action=""> <form method="post" id="ruleForm" class="form-horizontal" action="">
{% if error %} {% if error %}
<div class="alert alert-warning text-center">{{ error }}</div> <div class="alert alert-warning text-center">{{ error }}</div>
{% endif %} {% endif %}
@ -34,7 +34,7 @@
<div class="alert alert-success text-center">{{ msg }}</div> <div class="alert alert-success text-center">{{ msg }}</div>
{% endif %} {% endif %}
<div class="form-group"> <div class="form-group">
<label for="username" class="col-sm-2 control-label">授权名称<span class="red-fonts">*</span></label> <label for="rulename" class="col-sm-2 control-label">授权名称<span class="red-fonts">*</span></label>
<div class="col-sm-8"> <div class="col-sm-8">
<input id="rulename" name="rulename" placeholder="Rule Name" type="text" class="form-control" {% if error %}value="{{ username }}" {% endif %}> <input id="rulename" name="rulename" placeholder="Rule Name" type="text" class="form-control" {% if error %}value="{{ username }}" {% endif %}>
</div> </div>
@ -48,11 +48,11 @@
<option value="{{ user.name }}">{{ user.name }}</option> <option value="{{ user.name }}">{{ user.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
<span class="help-block m-b-none">用户和用户组必选一个</span>
</div> </div>
</div> </div>
<div class="hr-line-dashed"></div>
<div class="form-group"> <div class="form-group">
<label for="usergroup" class="col-sm-2 control-label">用户组<span class="red-fonts">*</span></label> <label for="usergroup" class="col-sm-2 control-label">用户组</label>
<div class="col-sm-8"> <div class="col-sm-8">
<select name="usergroup" data-placeholder="请选择用户组" class="chosen-select form-control m-b" multiple tabindex="2"> <select name="usergroup" data-placeholder="请选择用户组" class="chosen-select form-control m-b" multiple tabindex="2">
{% for user_group in user_groups %} {% for user_group in user_groups %}
@ -70,11 +70,11 @@
<option value="{{ asset.ip }}">{{ asset.ip }}</option> <option value="{{ asset.ip }}">{{ asset.ip }}</option>
{% endfor %} {% endfor %}
</select> </select>
<span class="help-block m-b-none">资产和资产组必选一个</span>
</div> </div>
</div> </div>
<div class="hr-line-dashed"></div>
<div class="form-group"> <div class="form-group">
<label for="assetgroup" class="col-sm-2 control-label">资产组<span class="red-fonts">*</span></label> <label for="assetgroup" class="col-sm-2 control-label">资产组</label>
<div class="col-sm-8"> <div class="col-sm-8">
<select name="assetgroup" data-placeholder="请选择资产组" class="chosen-select form-control m-b" multiple tabindex="2"> <select name="assetgroup" data-placeholder="请选择资产组" class="chosen-select form-control m-b" multiple tabindex="2">
{% for asset_group in asset_groups %} {% for asset_group in asset_groups %}
@ -120,31 +120,31 @@
<script> <script>
$('#ruleForm').submit(function() { $('#ruleForm').submit(function() {
var result = {} var result = {};
var data = $(this).serializeArray(); var data = $(this).serializeArray();
$.each(data, function(i, field){ $.each(data, function (i, field) {
result[field.name] = field.value; result[field.name] = field.value;
}); });
if (result['user'] || result['usergroup'] || result['asset'] || result['assetgroup'] || result['rulename'] || result['role']) { if (result['user'] || result['usergroup'] || result['asset'] || result['assetgroup'] || result['rulename'] || result['role']) {
if (result['rulename'] === '') { if (result['rulename'] === '') {
alert("请添加授权名称") alert("请添加授权名称");
return false return false
} }
if (! result['user'] && ! result['usergroup']) { if (!result['user'] && !result['usergroup']) {
alert("用户和用户组必选1个") alert("用户和用户组必选1个");
return false return false
} }
if (! result['asset'] && ! result['assetgroup']) { if (!result['asset'] && !result['assetgroup']) {
alert("资产和资产组必选1个") alert("资产和资产组必选1个");
return false return false
} }
if (! result['role']) { if (!result['role']) {
alert("请填写角色") alert("请填写角色");
return false return false
} }
return true return true
} else { } else {
alert("请填必选项") alert("请填必选项");
return false; return false;
} }
}); });