From a143797ac910b5056f398ed2678d5054e15a47c8 Mon Sep 17 00:00:00 2001 From: ibuler Date: Sun, 22 Nov 2015 17:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9gen=5Fresource=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/perm_api.py | 22 +++++++++++++++++--- jumpserver/api.py | 9 ++++++--- templates/jasset/asset_add.html | 1 + templates/jperm/perm_rule_add.html | 32 +++++++++++++++--------------- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/jperm/perm_api.py b/jperm/perm_api.py index 856c13005..f79040c60 100644 --- a/jperm/perm_api.py +++ b/jperm/perm_api.py @@ -132,13 +132,29 @@ def get_group_asset_perm(ob): return perm -def gen_resource(ob, perm=None): +def gen_resource(ob, ex='', perm=None): """ - ob为用户或资产列表或资产queryset + ob为用户或资产列表或资产queryset, 如果同时输入用户和资产,则获取用户在这些资产上的信息 生成MyInventory需要的 resource文件 """ 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: perm = get_group_user_perm(ob) diff --git a/jumpserver/api.py b/jumpserver/api.py index 3ffa7566b..cbf7eebc8 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -96,9 +96,12 @@ def get_role_key(user, role): def chown(path, user, group=''): if not group: group = user - uid = pwd.getpwnam(user).pw_uid - gid = pwd.getpwnam(group).pwd_gid - os.chown(path, uid, gid) + try: + uid = pwd.getpwnam(user).pw_uid + gid = pwd.getpwnam(group).pw_gid + os.chown(path, uid, gid) + except KeyError: + pass def page_list_return(total, current=1): diff --git a/templates/jasset/asset_add.html b/templates/jasset/asset_add.html index 7ef74920a..35903a469 100644 --- a/templates/jasset/asset_add.html +++ b/templates/jasset/asset_add.html @@ -48,6 +48,7 @@ {{ af.ip|bootstrap_horizontal }}

Tips: 如果IP地址不填写, IP默认会设置与主机名一致

+
diff --git a/templates/jperm/perm_rule_add.html b/templates/jperm/perm_rule_add.html index 3eef4eb00..a6d0ce530 100644 --- a/templates/jperm/perm_rule_add.html +++ b/templates/jperm/perm_rule_add.html @@ -26,7 +26,7 @@
-
+ {% if error %}
{{ error }}
{% endif %} @@ -34,7 +34,7 @@
{{ msg }}
{% endif %}
- +
@@ -48,11 +48,11 @@ {% endfor %} + 用户和用户组必选一个
-
- +
+ 资产和资产组必选一个
-
- +