From e041a49ad7b3988138de485890c4ebcaa623651c Mon Sep 17 00:00:00 2001 From: ibuler Date: Sat, 21 Nov 2015 14:42:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B5=84=E4=BA=A7=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=9F=A5=E8=AF=A2=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=94=9F?= =?UTF-8?q?=E6=88=90resource=E6=96=87=E4=BB=B6api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/ansible_api.py | 3 ++- jperm/models.py | 2 +- jperm/perm_api.py | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/jperm/ansible_api.py b/jperm/ansible_api.py index e94b79dc3..2bf6973c1 100644 --- a/jperm/ansible_api.py +++ b/jperm/ansible_api.py @@ -79,12 +79,13 @@ class MyInventory(object): for host in hosts: # set connection variables hostname = host.get("hostname") + hostip = host.get('ip', hostname) hostport = host.get("port") username = host.get("username") password = host.get("password") ssh_key = host.get("ssh_key") my_host = Host(name=hostname, port=hostport) - my_host.set_variable('ansible_ssh_host', hostname) + my_host.set_variable('ansible_ssh_host', hostip) my_host.set_variable('ansible_ssh_port', hostport) my_host.set_variable('ansible_ssh_user', username) my_host.set_variable('ansible_ssh_pass', password) diff --git a/jperm/models.py b/jperm/models.py index 39bb9a3f5..0a01f7593 100644 --- a/jperm/models.py +++ b/jperm/models.py @@ -27,4 +27,4 @@ class PermRule(models.Model): role = models.ManyToManyField(PermRole, related_name='perm_rule') def __unicode__(self): - return self.name \ No newline at end of file + return self.name diff --git a/jperm/perm_api.py b/jperm/perm_api.py index 5ea443e64..9d893181e 100644 --- a/jperm/perm_api.py +++ b/jperm/perm_api.py @@ -1,6 +1,6 @@ # coding: utf-8 - +from django.db.models.query import QuerySet from jumpserver.api import * import uuid import re @@ -130,6 +130,40 @@ def get_group_asset_perm(ob): return perm +def gen_resource(ob): + """ + 生成MyInventory需要的 resource文件 + """ + res = [] + if isinstance(ob, User): + perm = get_group_user_perm(ob) + for asset, asset_info in perm.get('asset').items(): + info = {'hostname': asset.hostname, 'ip': asset.ip, 'port': asset.port} + try: + role = sorted(list(asset_info.get('role')))[0] + except IndexError: + continue + info['username'] = role.name + info['password'] = role.password + info['key_path'] = role.key_path + res.append(info) + elif isinstance(ob, (list, QuerySet)): + default = get_object(Setting, name='default') + for asset in ob: + info = {'hostname': asset.hostname, 'ip': asset.ip} + if asset.use_default_auth: + if default: + info['port'] = default.default_port + info['username'] = default.default_user + info['password'] = default.default_password + info['ssh_key'] = default.default_pri_key_path + else: + info['port'] = asset.port + info['username'] = asset.username + res.append(info) + return res + + def get_object_list(model, id_list): """根据id列表获取对象列表""" object_list = []