Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
gjl2004yn
jumpserver
提交
b7021b5e
J
jumpserver
项目概览
gjl2004yn
/
jumpserver
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jumpserver
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b7021b5e
编写于
9月 19, 2019
作者:
baltery
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Update] 支持收集资产上的用户
上级
5464c884
变更
13
展开全部
隐藏空白更改
内联
并排
Showing
13 changed file
with
236 addition
and
168 deletion
+236
-168
apps/assets/api/asset.py
apps/assets/api/asset.py
+1
-0
apps/assets/api/gathered_user.py
apps/assets/api/gathered_user.py
+2
-0
apps/assets/filters.py
apps/assets/filters.py
+28
-32
apps/assets/migrations/0041_gathereduser.py
apps/assets/migrations/0041_gathereduser.py
+2
-2
apps/assets/models/gathered_user.py
apps/assets/models/gathered_user.py
+2
-2
apps/assets/serializers/gathered_user.py
apps/assets/serializers/gathered_user.py
+6
-1
apps/assets/tasks/gather_asset_users.py
apps/assets/tasks/gather_asset_users.py
+2
-1
apps/common/filters.py
apps/common/filters.py
+4
-2
apps/locale/zh/LC_MESSAGES/django.mo
apps/locale/zh/LC_MESSAGES/django.mo
+0
-0
apps/locale/zh/LC_MESSAGES/django.po
apps/locale/zh/LC_MESSAGES/django.po
+161
-105
apps/ops/models/adhoc.py
apps/ops/models/adhoc.py
+20
-16
apps/orgs/mixins/api.py
apps/orgs/mixins/api.py
+7
-6
apps/perms/api/user_permission/mixin.py
apps/perms/api/user_permission/mixin.py
+1
-1
未找到文件。
apps/assets/api/asset.py
浏览文件 @
b7021b5e
...
...
@@ -36,6 +36,7 @@ class AssetViewSet(OrgBulkModelViewSet):
serializer_class
=
serializers
.
AssetSerializer
permission_classes
=
(
IsOrgAdminOrAppUser
,)
extra_filter_backends
=
[
AssetByNodeFilterBackend
,
LabelFilterBackend
]
custom_filter_fields
=
[
'admin_user_id'
]
def
set_assets_node
(
self
,
assets
):
if
not
isinstance
(
assets
,
list
):
...
...
apps/assets/api/gathered_user.py
浏览文件 @
b7021b5e
...
...
@@ -6,6 +6,7 @@ from assets.models import GatheredUser
from
common.permissions
import
IsOrgAdmin
from
..serializers
import
GatheredUserSerializer
from
..filters
import
AssetRelatedByNodeFilterBackend
__all__
=
[
'GatheredUserViewSet'
]
...
...
@@ -15,6 +16,7 @@ class GatheredUserViewSet(OrgModelViewSet):
queryset
=
GatheredUser
.
objects
.
all
()
serializer_class
=
GatheredUserSerializer
permission_classes
=
[
IsOrgAdmin
]
extra_filter_backends
=
[
AssetRelatedByNodeFilterBackend
]
filter_fields
=
[
'asset'
,
'username'
,
'present'
]
search_fields
=
[
'username'
,
'asset__ip'
,
'asset__hostname'
]
...
...
apps/assets/filters.py
浏览文件 @
b7021b5e
...
...
@@ -12,63 +12,54 @@ from .models import Node, Label
class
AssetByNodeFilterBackend
(
filters
.
BaseFilterBackend
):
fields
=
[
'node'
,
'all'
]
# def filter_node(self, queryset):
# node_id = self.request.query_params.get("node_id")
# if not node_id:
# return queryset
#
# node = get_object_or_404(Node, id=node_id)
# show_current_asset = self.request.query_params.get("show_current_asset") in ('1', 'true')
#
# # 当前节点是顶层节点, 并且仅显示直接资产
# if node.is_org_root() and show_current_asset:
# queryset = queryset.filter(
# Q(nodes=node_id) | Q(nodes__isnull=True)
# ).distinct()
# # 当前节点是顶层节点,显示所有资产
# elif node.is_org_root() and not show_current_asset:
# return queryset
# # 当前节点不是鼎城节点,只显示直接资产
# elif not node.is_org_root() and show_current_asset:
# queryset = queryset.filter(nodes=node)
# else:
# children = node.get_all_children(with_self=True)
# queryset = queryset.filter(nodes__in=children).distinct()
# return queryset
def
get_schema_fields
(
self
,
view
):
return
[
coreapi
.
Field
(
name
=
field
,
location
=
'query'
,
required
=
False
,
type
=
'string'
,
example
=
''
,
description
=
''
type
=
'string'
,
example
=
''
,
description
=
''
,
schema
=
None
,
)
for
field
in
self
.
fields
]
def
filter_queryset
(
self
,
request
,
queryset
,
view
):
node_id
=
dict_get_any
(
request
.
query_params
,
[
'node'
,
'node_id'
])
if
not
node_id
:
return
queryset
@
staticmethod
def
is_query_all
(
request
):
query_all_arg
=
request
.
query_params
.
get
(
'all'
)
show_current_asset_arg
=
request
.
query_params
.
get
(
'show_current_asset'
)
query_all
=
query_all_arg
==
'1'
if
show_current_asset_arg
is
not
None
:
query_all
=
show_current_asset_arg
!=
'1'
return
query_all
@
staticmethod
def
get_query_node
(
request
):
node_id
=
dict_get_any
(
request
.
query_params
,
[
'node'
,
'node_id'
])
if
not
node_id
:
return
None
,
False
if
is_uuid
(
node_id
):
node
=
get_object_or_none
(
Node
,
id
=
node_id
)
else
:
node
=
get_object_or_none
(
Node
,
key
=
node_id
)
return
node
,
True
if
not
node
:
return
queryset
.
none
()
@
staticmethod
def
perform_query
(
pattern
,
queryset
):
return
queryset
.
filter
(
nodes__key__regex
=
pattern
)
def
filter_queryset
(
self
,
request
,
queryset
,
view
):
node
,
has_query_arg
=
self
.
get_query_node
(
request
)
if
not
has_query_arg
:
return
queryset
if
node
is
None
:
return
queryset
.
none
()
query_all
=
self
.
is_query_all
(
request
)
if
query_all
:
pattern
=
node
.
get_all_children_pattern
(
with_self
=
True
)
else
:
pattern
=
node
.
get_children_key_pattern
(
with_self
=
True
)
return
queryset
.
filter
(
nodes__key__regex
=
pattern
)
return
self
.
perform_query
(
pattern
,
queryset
)
class
LabelFilterBackend
(
filters
.
BaseFilterBackend
):
...
...
@@ -117,3 +108,8 @@ class LabelFilterBackend(filters.BaseFilterBackend):
return
queryset
class
AssetRelatedByNodeFilterBackend
(
AssetByNodeFilterBackend
):
@
staticmethod
def
perform_query
(
pattern
,
queryset
):
return
queryset
.
filter
(
asset__nodes__key__regex
=
pattern
).
distinct
()
apps/assets/migrations/0041_gathereduser.py
浏览文件 @
b7021b5e
...
...
@@ -18,10 +18,10 @@ class Migration(migrations.Migration):
(
'org_id'
,
models
.
CharField
(
blank
=
True
,
db_index
=
True
,
default
=
''
,
max_length
=
36
,
verbose_name
=
'Organization'
)),
(
'id'
,
models
.
UUIDField
(
default
=
uuid
.
uuid4
,
primary_key
=
True
,
serialize
=
False
)),
(
'username'
,
models
.
CharField
(
blank
=
True
,
db_index
=
True
,
max_length
=
32
,
verbose_name
=
'Username'
)),
(
'present'
,
models
.
BooleanField
(
default
=
True
)),
(
'present'
,
models
.
BooleanField
(
default
=
True
,
verbose_name
=
'Present'
)),
(
'date_created'
,
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Date created'
)),
(
'date_updated'
,
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
'Date updated'
)),
(
'asset'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'assets.Asset'
)),
(
'asset'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'assets.Asset'
,
verbose_name
=
'Asset'
)),
],
options
=
{
'ordering'
:
[
'asset'
],
'verbose_name'
:
'GatherUser'
},
),
...
...
apps/assets/models/gathered_user.py
浏览文件 @
b7021b5e
...
...
@@ -11,10 +11,10 @@ __all__ = ['GatheredUser']
class
GatheredUser
(
OrgModelMixin
):
id
=
models
.
UUIDField
(
default
=
uuid
.
uuid4
,
primary_key
=
True
)
asset
=
models
.
ForeignKey
(
'assets.Asset'
,
on_delete
=
models
.
CASCADE
)
asset
=
models
.
ForeignKey
(
'assets.Asset'
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
_
(
"Asset"
)
)
username
=
models
.
CharField
(
max_length
=
32
,
blank
=
True
,
db_index
=
True
,
verbose_name
=
_
(
'Username'
))
present
=
models
.
BooleanField
(
default
=
True
)
present
=
models
.
BooleanField
(
default
=
True
,
verbose_name
=
_
(
"Present"
)
)
date_created
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
_
(
"Date created"
))
date_updated
=
models
.
DateTimeField
(
auto_now
=
True
,
...
...
apps/assets/serializers/gathered_user.py
浏览文件 @
b7021b5e
# -*- coding: utf-8 -*-
#
from
..models
import
GatheredUser
from
django.utils.translation
import
ugettext_lazy
as
_
from
orgs.mixins.serializers
import
OrgResourceModelSerializerMixin
from
..models
import
GatheredUser
class
GatheredUserSerializer
(
OrgResourceModelSerializerMixin
):
...
...
@@ -14,3 +15,7 @@ class GatheredUserSerializer(OrgResourceModelSerializerMixin):
'present'
,
'date_created'
,
'date_updated'
]
read_only_fields
=
fields
labels
=
{
'hostname'
:
_
(
"Hostname"
),
'ip'
:
"IP"
}
apps/assets/tasks/gather_asset_users.py
浏览文件 @
b7021b5e
...
...
@@ -116,8 +116,9 @@ def gather_asset_users(assets, task_name=None):
for
k
,
value
in
hosts_category
.
items
():
if
not
value
[
'hosts'
]:
continue
_task_name
=
'{}: {}'
.
format
(
task_name
,
k
)
task
,
created
=
update_or_create_ansible_task
(
task_name
=
task_name
,
hosts
=
value
[
'hosts'
],
tasks
=
value
[
'tasks'
],
task_name
=
_
task_name
,
hosts
=
value
[
'hosts'
],
tasks
=
value
[
'tasks'
],
pattern
=
'all'
,
options
=
const
.
TASK_OPTIONS
,
run_as_admin
=
True
,
created_by
=
value
[
'hosts'
][
0
].
org_id
,
)
...
...
apps/common/filters.py
浏览文件 @
b7021b5e
...
...
@@ -70,7 +70,6 @@ class IDSpmFilter(filters.BaseFilterBackend):
class
CustomFilter
(
filters
.
BaseFilterBackend
):
custom_filter_fields
=
[]
# ["node", "asset"]
def
get_schema_fields
(
self
,
view
):
fields
=
[]
...
...
@@ -79,7 +78,10 @@ class CustomFilter(filters.BaseFilterBackend):
type
=
'string'
,
example
=
''
,
description
=
''
)
for
field
in
self
.
custom_filter_fields
:
if
not
hasattr
(
view
,
'custom_filter_fields'
):
return
[]
for
field
in
view
.
custom_filter_fields
:
if
isinstance
(
field
,
str
):
defaults
[
'name'
]
=
field
elif
isinstance
(
field
,
dict
):
...
...
apps/locale/zh/LC_MESSAGES/django.mo
浏览文件 @
b7021b5e
无法预览此类型文件
apps/locale/zh/LC_MESSAGES/django.po
浏览文件 @
b7021b5e
此差异已折叠。
点击以展开。
apps/ops/models/adhoc.py
浏览文件 @
b7021b5e
...
...
@@ -218,28 +218,32 @@ class AdHoc(models.Model):
hid
=
str
(
uuid
.
uuid4
())
history
=
AdHocRunHistory
(
id
=
hid
,
adhoc
=
self
,
task
=
self
.
task
)
time_start
=
time
.
time
()
date_start
=
timezone
.
now
()
is_success
=
False
try
:
date_start
=
datetime
.
datetime
.
now
().
strftime
(
'%Y-%m-%d %H:%M:%S'
)
history
.
date_start
=
timezone
.
now
()
print
(
_
(
"{} Start task: {}"
).
format
(
date_start
,
self
.
task
.
name
))
date_start_s
=
datetime
.
datetime
.
now
().
strftime
(
'%Y-%m-%d %H:%M:%S'
)
print
(
_
(
"{} Start task: {}"
).
format
(
date_start_s
,
self
.
task
.
name
))
raw
,
summary
=
self
.
_run_only
()
date_end
=
datetime
.
datetime
.
now
().
strftime
(
'%Y-%m-%d %H:%M:%S'
)
print
(
_
(
"{} Task finish"
).
format
(
date_end
))
history
.
is_finished
=
True
if
summary
.
get
(
'dark'
):
history
.
is_success
=
False
else
:
history
.
is_success
=
True
history
.
result
=
raw
history
.
summary
=
summary
is_success
=
summary
.
get
(
'success'
,
False
)
return
raw
,
summary
except
Exception
as
e
:
logger
.
error
(
e
,
exc_info
=
True
)
return
{},
{
"dark"
:
{
"all"
:
str
(
e
)},
"contacted"
:
[]}
summary
=
{}
raw
=
{
"dark"
:
{
"all"
:
str
(
e
)},
"contacted"
:
[]}
return
raw
,
summary
finally
:
history
.
date_finished
=
timezone
.
now
()
history
.
timedelta
=
time
.
time
()
-
time_start
history
.
save
()
date_end
=
timezone
.
now
()
date_end_s
=
date_end
.
strftime
(
'%Y-%m-%d %H:%M:%S'
)
print
(
_
(
"{} Task finish"
).
format
(
date_end_s
))
print
(
'.
\n\n
.'
)
AdHocRunHistory
.
objects
.
filter
(
id
=
history
.
id
).
update
(
date_start
=
date_start
,
is_finished
=
True
,
is_success
=
is_success
,
date_finished
=
timezone
.
now
(),
timedelta
=
time
.
time
()
-
time_start
)
def
_run_only
(
self
):
runner
=
AdHocRunner
(
self
.
inventory
,
options
=
self
.
options
)
...
...
apps/orgs/mixins/api.py
浏览文件 @
b7021b5e
...
...
@@ -20,12 +20,7 @@ class RootOrgViewMixin:
return
super
().
dispatch
(
request
,
*
args
,
**
kwargs
)
class
OrgModelViewSet
(
CommonApiMixin
,
ModelViewSet
):
def
get_queryset
(
self
):
return
super
().
get_queryset
().
all
()
class
OrgBulkModelViewSet
(
CommonApiMixin
,
BulkModelViewSet
):
class
OrgQuerySetMixin
:
def
get_queryset
(
self
):
queryset
=
super
().
get_queryset
().
all
()
if
hasattr
(
self
,
'swagger_fake_view'
):
...
...
@@ -36,6 +31,12 @@ class OrgBulkModelViewSet(CommonApiMixin, BulkModelViewSet):
queryset
=
self
.
serializer_class
.
setup_eager_loading
(
queryset
)
return
queryset
class
OrgModelViewSet
(
CommonApiMixin
,
OrgQuerySetMixin
,
ModelViewSet
):
pass
class
OrgBulkModelViewSet
(
CommonApiMixin
,
OrgQuerySetMixin
,
BulkModelViewSet
):
def
allow_bulk_destroy
(
self
,
qs
,
filtered
):
if
qs
.
count
()
<=
filtered
.
count
():
return
False
...
...
apps/perms/api/user_permission/mixin.py
浏览文件 @
b7021b5e
...
...
@@ -7,7 +7,7 @@ from common.tree import TreeNodeSerializer
class
UserAssetPermissionMixin
(
UserPermissionMixin
):
util
=
AssetPermissionUtilV2
(
None
)
util
=
None
tree
=
None
def
initial
(
self
,
*
args
,
**
kwargs
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录