Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
47d87e38
Commit
47d87e38
authored
Dec 19, 2016
by
yumaojun03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
补充task需要的一些接口
parent
150e1030
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
23 deletions
+80
-23
serializers.py
apps/ops/api/serializers.py
+6
-0
views.py
apps/ops/api/views.py
+6
-0
__init__.py
apps/ops/models/__init__.py
+1
-0
ansible.py
apps/ops/models/ansible.py
+1
-18
sudo.py
apps/ops/models/sudo.py
+1
-2
task.py
apps/ops/models/task.py
+59
-0
api_urls.py
apps/ops/urls/api_urls.py
+2
-0
ansible_api.py
apps/ops/utils/ansible_api.py
+4
-3
No files found.
apps/ops/api/serializers.py
View file @
47d87e38
...
...
@@ -53,7 +53,13 @@ class CronTableSerializer(serializers.ModelSerializer):
model
=
CronTable
class
TaskSerializer
(
serializers
.
ModelSerializer
):
sub_tasks
=
serializers
.
PrimaryKeyRelatedField
(
many
=
True
,
read_only
=
True
)
class
Meta
:
model
=
Task
read_only_fields
=
(
'record'
,)
class
SubTaskSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
SubTask
apps/ops/api/views.py
View file @
47d87e38
...
...
@@ -14,6 +14,7 @@ __all__ = ["HostAliaViewSet",
"SudoViewSet"
,
"CronTableViewSet"
,
"TaskViewSet"
,
"SubTaskViewSet"
,
]
...
...
@@ -69,5 +70,10 @@ class TaskViewSet(viewsets.ModelViewSet):
serializer_class
=
TaskSerializer
permission_classes
=
(
AdminUserRequired
,)
class
SubTaskViewSet
(
viewsets
.
ModelViewSet
):
queryset
=
SubTask
.
objects
.
all
()
serializer_class
=
SubTaskSerializer
permission_classes
=
(
AdminUserRequired
,)
apps/ops/models/__init__.py
View file @
47d87e38
...
...
@@ -2,4 +2,5 @@ from ansible import *
from
cron
import
*
from
sudo
import
*
from
utils
import
*
from
task
import
*
apps/ops/models/ansible.py
View file @
47d87e38
...
...
@@ -4,12 +4,10 @@ from __future__ import unicode_literals, absolute_import
import
logging
import
json
from
assets.models
import
Asset
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
__all__
=
[
"Task
"
,
"Task
Record"
,
"AnsiblePlay"
,
"AnsibleTask"
,
"AnsibleHostResult"
]
__all__
=
[
"TaskRecord"
,
"AnsiblePlay"
,
"AnsibleTask"
,
"AnsibleHostResult"
]
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -291,18 +289,3 @@ class AnsibleHostResult(models.Model):
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
Task
(
models
.
Model
):
record
=
models
.
OneToOneField
(
TaskRecord
)
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Name'
))
module_name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
'Ansible Module Name'
))
module_args
=
models
.
CharField
(
max_length
=
512
,
blank
=
True
,
verbose_name
=
_
(
"Ansible Module Args"
))
register
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Ansible Task Register'
))
is_gather_facts
=
models
.
BooleanField
(
default
=
False
,
verbose_name
=
_
(
'Is Gather Ansible Facts'
))
asset
=
models
.
ManyToManyField
(
Asset
,
related_name
=
'tasks'
)
def
__unicode__
(
self
):
return
"
%
s
%
s"
%
(
self
.
module_name
,
self
.
module_args
)
def
run
(
self
):
pass
apps/ops/models/sudo.py
View file @
47d87e38
...
...
@@ -11,8 +11,7 @@ from django.utils.translation import ugettext_lazy as _
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
"HostAlia"
,
"UserAlia"
,
"CmdAlia"
,
"RunasAlia"
,
"Privilege"
,
"Extra_conf"
,
"Sudo"
]
__all__
=
[
"HostAlia"
,
"UserAlia"
,
"CmdAlia"
,
"RunasAlia"
,
"Privilege"
,
"Extra_conf"
,
"Sudo"
]
class
HostAlia
(
models
.
Model
):
...
...
apps/ops/models/task.py
0 → 100644
View file @
47d87e38
# ~*~ coding: utf-8 ~*~
from
__future__
import
unicode_literals
,
absolute_import
import
logging
from
uuid
import
uuid4
from
assets.models
import
Asset
from
ops.models
import
TaskRecord
from
ops.utils.ansible_api
import
ADHocRunner
,
Config
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
__all__
=
[
"Task"
,
"SubTask"
]
logger
=
logging
.
getLogger
(
__name__
)
class
Task
(
models
.
Model
):
record
=
models
.
OneToOneField
(
TaskRecord
)
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Name'
))
is_gather_facts
=
models
.
BooleanField
(
default
=
False
,
verbose_name
=
_
(
'Is Gather Ansible Facts'
))
assets
=
models
.
ManyToManyField
(
Asset
,
related_name
=
'tasks'
)
def
__unicode__
(
self
):
return
"
%
s"
%
self
.
name
@property
def
ansible_assets
(
self
):
return
[]
def
run
(
self
):
conf
=
Config
()
gather_facts
=
"yes"
if
self
.
is_gather_facts
else
"no"
play_source
=
{
"name"
:
"Ansible Play"
,
"hosts"
:
"default"
,
"gather_facts"
:
gather_facts
,
"tasks"
:
[
dict
(
action
=
dict
(
module
=
'ping'
)),
]
}
hoc
=
ADHocRunner
(
conf
,
play_source
,
*
self
.
ansible_assets
)
uuid
=
"tasker-"
+
uuid4
()
.
hex
ext_code
,
result
=
hoc
.
run
(
"test_task"
,
uuid
)
print
(
ext_code
)
print
(
result
)
class
SubTask
(
models
.
Model
):
task
=
models
.
ForeignKey
(
Task
,
related_name
=
'sub_tasks'
,
verbose_name
=
_
(
'Ansible Task'
))
module_name
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
_
(
'Ansible Module Name'
))
module_args
=
models
.
CharField
(
max_length
=
512
,
blank
=
True
,
verbose_name
=
_
(
"Ansible Module Args"
))
register
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Ansible Task Register'
))
def
__unicode__
(
self
):
return
"
%
s
%
s"
%
(
self
.
module_name
,
self
.
module_args
)
\ No newline at end of file
apps/ops/urls/api_urls.py
View file @
47d87e38
...
...
@@ -16,5 +16,6 @@ api_router.register(r'v1/privilege', v1_api.PrivilegeViewSet)
api_router
.
register
(
r'v1/sudo'
,
v1_api
.
SudoViewSet
)
api_router
.
register
(
r'v1/cron'
,
v1_api
.
CronTableViewSet
)
api_router
.
register
(
r'v1/task'
,
v1_api
.
TaskViewSet
)
api_router
.
register
(
r'v1/subtask'
,
v1_api
.
SubTaskViewSet
)
urlpatterns
=
api_router
.
urls
\ No newline at end of file
apps/ops/utils/ansible_api.py
View file @
47d87e38
...
...
@@ -20,6 +20,8 @@ from ansible.plugins.callback import CallbackBase
from
ops.models
import
TaskRecord
,
AnsiblePlay
,
AnsibleTask
,
AnsibleHostResult
__all__
=
[
"ADHocRunner"
,
"Config"
]
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -392,7 +394,7 @@ class PlayBookRunner(InventoryMixin):
class
ADHocRunner
(
InventoryMixin
):
"""ADHoc接口
"""
def
__init__
(
self
,
config
,
play_data
,
*
hosts
,
**
group_vars
):
def
__init__
(
self
,
play_data
,
config
=
None
,
*
hosts
,
**
group_vars
):
"""
:param hosts: 见PlaybookRunner参数
:param group_vars: 见PlaybookRunner参数
...
...
@@ -406,8 +408,7 @@ class ADHocRunner(InventoryMixin):
tasks=[dict(action=dict(module='service', args={'name': 'vsftpd', 'state': 'restarted'}), async=async, poll=poll)]
)
"""
self
.
options
=
config
self
.
options
=
config
if
config
!=
None
else
Config
()
# 设置verbosity级别, 及命令行的--verbose选项
self
.
display
=
Display
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment