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
a4504dc0
Commit
a4504dc0
authored
Mar 15, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Change] 拆分tasks
parent
0fbd9843
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
132 additions
and
40 deletions
+132
-40
utils.py
apps/common/utils.py
+6
-6
callback.py
apps/ops/ansible/callback.py
+0
-30
tasks.py
apps/ops/tasks.py
+1
-4
utils.py
apps/ops/utils.py
+77
-0
__init__.py
apps/ops/utils/__init__.py
+0
-0
tasks.py
apps/perms/tasks.py
+48
-0
No files found.
apps/common/utils.py
View file @
a4504dc0
...
@@ -303,12 +303,12 @@ def iso8601_to_unixtime(time_string):
...
@@ -303,12 +303,12 @@ def iso8601_to_unixtime(time_string):
return
to_unixtime
(
time_string
,
_ISO8601_FORMAT
)
return
to_unixtime
(
time_string
,
_ISO8601_FORMAT
)
def
http_to_unixtime
(
time_string
):
#
def http_to_unixtime(time_string):
"""把HTTP Date格式的字符串转换为UNIX时间(自1970年1月1日UTC零点的秒数)。
#
"""把HTTP Date格式的字符串转换为UNIX时间(自1970年1月1日UTC零点的秒数)。
#
HTTP Date形如 `Sat, 05 Dec 2015 11:10:29 GMT` 。
#
HTTP Date形如 `Sat, 05 Dec 2015 11:10:29 GMT` 。
"""
#
"""
return
to_unixtime
(
time_string
,
"
%
a,
%
d
%
b
%
Y
%
H:
%
M:
%
S GMT"
)
#
return to_unixtime(time_string, "%a, %d %b %Y %H:%M:%S GMT")
def
make_signature
(
access_key_secret
,
date
=
None
):
def
make_signature
(
access_key_secret
,
date
=
None
):
...
...
apps/ops/ansible/callback.py
View file @
a4504dc0
...
@@ -62,36 +62,6 @@ class AdHocResultCallback(CallbackBase):
...
@@ -62,36 +62,6 @@ class AdHocResultCallback(CallbackBase):
pass
pass
class
SingleAdHocResultCallback
(
CallbackBase
):
"""
AdHoc result Callback
"""
def
__init__
(
self
,
display
=
None
):
self
.
result_q
=
dict
(
contacted
=
{},
dark
=
{})
super
(
SingleAdHocResultCallback
,
self
)
.
__init__
(
display
)
def
gather_result
(
self
,
n
,
res
):
self
.
result_q
[
n
][
res
.
_host
.
name
]
.
append
(
res
.
_result
)
def
v2_runner_on_ok
(
self
,
result
):
self
.
gather_result
(
"contacted"
,
result
)
def
v2_runner_on_failed
(
self
,
result
,
ignore_errors
=
False
):
self
.
gather_result
(
"dark"
,
result
)
def
v2_runner_on_unreachable
(
self
,
result
):
self
.
gather_result
(
"dark"
,
result
)
def
v2_runner_on_skipped
(
self
,
result
):
self
.
gather_result
(
"dark"
,
result
)
def
v2_playbook_on_task_start
(
self
,
task
,
is_conditional
):
pass
def
v2_playbook_on_play_start
(
self
,
play
):
pass
class
PlaybookResultCallBack
(
CallbackBase
):
class
PlaybookResultCallBack
(
CallbackBase
):
"""
"""
Custom callback model for handlering the output data of
Custom callback model for handlering the output data of
...
...
apps/ops/tasks.py
View file @
a4504dc0
...
@@ -10,14 +10,11 @@ from django.utils import timezone
...
@@ -10,14 +10,11 @@ from django.utils import timezone
from
assets.models
import
Asset
from
assets.models
import
Asset
from
common.utils
import
get_logger
,
encrypt_password
from
common.utils
import
get_logger
,
encrypt_password
from
ops
.ansible.runner
import
AdHocRunner
from
.ansible.runner
import
AdHocRunner
logger
=
get_logger
(
__file__
)
logger
=
get_logger
(
__file__
)
@shared_task
(
bind
=
True
)
@shared_task
(
bind
=
True
)
def
run_AdHoc
(
self
,
task_tuple
,
assets
,
def
run_AdHoc
(
self
,
task_tuple
,
assets
,
task_name
=
'Ansible AdHoc runner'
,
task_name
=
'Ansible AdHoc runner'
,
...
...
apps/ops/utils.py
0 → 100644
View file @
a4504dc0
# ~*~ coding: utf-8 ~*~
from
__future__
import
absolute_import
,
unicode_literals
import
json
import
time
import
uuid
from
django.utils
import
timezone
from
assets.models
import
Asset
from
common.utils
import
get_logger
from
.ansible.runner
import
AdHocRunner
logger
=
get_logger
(
__file__
)
def
run_AdHoc
(
task_tuple
,
assets
,
task_name
=
'Ansible AdHoc runner'
,
task_id
=
None
,
pattern
=
'all'
,
record
=
True
):
if
not
assets
:
logger
.
warning
(
'Empty assets, runner cancel'
)
return
if
isinstance
(
assets
[
0
],
Asset
):
assets
=
[
asset
.
_to_secret_json
()
for
asset
in
assets
]
if
task_id
is
None
:
task_id
=
str
(
uuid
.
uuid4
())
runner
=
AdHocRunner
(
assets
)
if
record
:
from
.models
import
TaskRecord
if
not
TaskRecord
.
objects
.
filter
(
uuid
=
task_id
):
record
=
TaskRecord
(
uuid
=
task_id
,
name
=
task_name
,
assets
=
','
.
join
(
str
(
asset
[
'id'
])
for
asset
in
assets
),
module_args
=
task_tuple
,
pattern
=
pattern
)
record
.
save
()
else
:
record
=
TaskRecord
.
objects
.
get
(
uuid
=
task_id
)
record
.
date_start
=
timezone
.
now
()
ts_start
=
time
.
time
()
logger
.
warn
(
'Start runner {}'
.
format
(
task_name
))
result
=
runner
.
run
(
task_tuple
,
pattern
=
pattern
,
task_name
=
task_name
)
timedelta
=
round
(
time
.
time
()
-
ts_start
,
2
)
summary
=
runner
.
clean_result
()
if
record
:
record
.
date_finished
=
timezone
.
now
()
record
.
is_finished
=
True
record
.
result
=
json
.
dumps
(
result
)
record
.
summary
=
json
.
dumps
(
summary
)
record
.
timedelta
=
timedelta
if
len
(
summary
[
'failed'
])
==
0
:
record
.
is_success
=
True
else
:
record
.
is_success
=
False
record
.
save
()
return
summary
,
result
def
rerun_AdHoc
(
task_id
):
from
.models
import
TaskRecord
record
=
TaskRecord
.
objects
.
get
(
uuid
=
task_id
)
assets
=
record
.
assets_json
task_tuple
=
record
.
module_args
pattern
=
record
.
pattern
task_name
=
record
.
name
return
run_AdHoc
(
task_tuple
,
assets
,
pattern
=
pattern
,
task_name
=
task_name
,
task_id
=
task_id
)
apps/ops/utils/__init__.py
deleted
100644 → 0
View file @
0fbd9843
apps/perms/tasks.py
0 → 100644
View file @
a4504dc0
# ~*~ coding: utf-8 ~*~
from
__future__
import
absolute_import
,
unicode_literals
from
celery
import
shared_task
from
common.utils
import
get_logger
,
encrypt_password
from
ops.utils
import
run_AdHoc
logger
=
get_logger
(
__file__
)
@shared_task
(
bind
=
True
)
def
push_users
(
self
,
assets
,
users
):
"""
user: {
name: 'web',
username: 'web',
shell: '/bin/bash',
password: '123123123',
public_key: 'string',
sudo: '/bin/whoami,/sbin/ifconfig'
}
"""
if
isinstance
(
users
,
dict
):
users
=
[
users
]
if
isinstance
(
assets
,
dict
):
assets
=
[
assets
]
task_tuple
=
[]
for
user
in
users
:
# 添加用户, 设置公钥, 设置sudo
task_tuple
.
extend
([
(
'user'
,
'name={} shell={} state=present password={}'
.
format
(
user
[
'username'
],
user
.
get
(
'shell'
,
'/bin/bash'
),
encrypt_password
(
user
.
get
(
'password'
,
None
)))),
(
'authorized_key'
,
"user={} state=present key='{}'"
.
format
(
user
[
'username'
],
user
[
'public_key'
])),
(
'lineinfile'
,
"name=/etc/sudoers state=present regexp='^{0} ALL=(ALL)' "
"line='{0} ALL=(ALL) NOPASSWD: {1}' "
"validate='visudo -cf
%
s'"
.
format
(
user
[
'username'
],
user
.
get
(
'sudo'
,
'/bin/whoami'
)
))
])
task_name
=
'Push user {}'
.
format
(
','
.
join
([
user
[
'name'
]
for
user
in
users
]))
task
=
run_AdHoc
(
task_tuple
,
assets
,
pattern
=
'all'
,
task_name
=
task_name
,
task_id
=
self
.
request
.
id
)
return
task
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