Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
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
OpenEdx
ansible
Commits
b053df41
Commit
b053df41
authored
Feb 05, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some refactoring of runner.py -- more to come to eliminate repeated code
parent
6cceaa5f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
91 deletions
+79
-91
lib/ansible/runner.py
+79
-91
No files found.
lib/ansible/runner.py
View file @
b053df41
...
@@ -98,111 +98,99 @@ class Runner(object):
...
@@ -98,111 +98,99 @@ class Runner(object):
except
:
except
:
return
[
False
,
traceback
.
format_exc
()
]
return
[
False
,
traceback
.
format_exc
()
]
def
_executor
(
self
,
host
):
def
_return_from_module
(
self
,
conn
,
host
,
result
):
'''
conn
.
close
()
callback executed in parallel for each host.
try
:
returns (hostname, connected_ok, extra)
return
[
host
,
True
,
json
.
loads
(
result
)
]
where extra is the result of a successful connect
except
:
or a traceback string
return
[
host
,
False
,
result
]
'''
# TODO: try/catch around JSON handling
ok
,
conn
=
self
.
_connect
(
host
)
def
_delete_remote_files
(
self
,
conn
,
files
):
if
not
ok
:
for
filename
in
files
:
return
[
host
,
False
,
conn
]
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
filename
)
if
self
.
module_name
not
in
[
'copy'
,
'template'
]:
def
_execute_normal_module
(
self
,
conn
,
host
):
# transfer a module, set it executable, and run it
''' transfer a module, set it executable, and run it '''
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
cmd
=
self
.
_command
(
outpath
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
outpath
)
conn
.
close
()
try
:
return
[
host
,
True
,
json
.
loads
(
result
)
]
except
:
return
[
host
,
False
,
result
]
elif
self
.
module_name
==
'copy'
:
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
cmd
=
self
.
_command
(
outpath
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
self
.
_delete_remote_files
(
conn
,
outpath
)
return
self
.
_return_from_module
(
conn
,
host
,
result
)
# TODO: major refactoring pending
def
_execute_copy
(
self
,
conn
,
host
):
# do sftp then run actual copy module to get change info
''' transfer a file + copy module, run copy module, clean up '''
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
self
.
module_args
[
0
],
self
.
module_args
[
1
]))
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
self
.
module_args
[
0
],
self
.
module_args
[
1
]))
source
=
self
.
module_args
[
0
]
source
=
self
.
module_args
[
0
]
dest
=
self
.
module_args
[
1
]
dest
=
self
.
module_args
[
1
]
tmp_dest
=
self
.
_get_tmp_path
(
conn
,
dest
.
split
(
"/"
)[
-
1
])
tmp_dest
=
self
.
_get_tmp_path
(
conn
,
dest
.
split
(
"/"
)[
-
1
])
ftp
=
conn
.
open_sftp
()
ftp
=
conn
.
open_sftp
()
ftp
.
put
(
source
,
tmp_dest
)
ftp
.
put
(
source
,
tmp_dest
)
ftp
.
close
()
ftp
.
close
()
# install the copy module
# install the copy module
self
.
module_name
=
'copy'
self
.
module_name
=
'copy'
outpath
=
self
.
_copy_module
(
conn
)
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
# run the copy module
# run the copy module
self
.
module_args
=
[
tmp_dest
,
dest
]
self
.
module_args
=
[
tmp_dest
,
dest
]
cmd
=
self
.
_command
(
outpath
)
cmd
=
self
.
_command
(
outpath
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
self
.
_delete_remote_files
(
conn
,
[
outpath
,
tmp_dest
])
# remove the module
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
outpath
)
return
self
.
_return_from_module
(
conn
,
host
,
result
)
# remove the temp file
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
tmp_dest
)
def
_execute_template
(
self
,
conn
,
host
):
source
=
self
.
module_args
[
0
]
dest
=
self
.
module_args
[
1
]
metadata
=
'/etc/ansible/setup'
conn
.
close
()
# first copy the source template over
try
:
tempname
=
os
.
path
.
split
(
source
)[
-
1
]
return
[
host
,
True
,
json
.
loads
(
result
)
]
temppath
=
self
.
_get_tmp_path
(
conn
,
tempname
)
except
:
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
source
,
temppath
))
traceback
.
print_exc
()
ftp
=
conn
.
open_sftp
()
return
[
host
,
False
,
result
]
ftp
.
put
(
source
,
temppath
)
ftp
.
close
()
return
[
host
,
True
,
1
]
# install the template module
self
.
module_name
=
'template'
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
# run the template module
self
.
module_args
=
[
temppath
,
dest
,
metadata
]
result
=
self
.
_exec_command
(
conn
,
self
.
_command
(
outpath
))
# clean up
self
.
_delete_remote_files
(
conn
,
[
outpath
,
temppath
])
return
self
.
_return_from_module
(
conn
,
host
,
result
)
def
_executor
(
self
,
host
):
'''
callback executed in parallel for each host.
returns (hostname, connected_ok, extra)
where extra is the result of a successful connect
or a traceback string
'''
ok
,
conn
=
self
.
_connect
(
host
)
if
not
ok
:
return
[
host
,
False
,
conn
]
if
self
.
module_name
not
in
[
'copy'
,
'template'
]:
return
self
.
_execute_normal_module
(
conn
,
host
)
elif
self
.
module_name
==
'copy'
:
return
self
.
_execute_copy
(
conn
,
host
)
elif
self
.
module_name
==
'template'
:
elif
self
.
module_name
==
'template'
:
# template runs COPY then the template module
return
self
.
_execute_template
(
conn
,
host
)
# TODO: DRY/refactor these
else
:
# TODO: things like _copy_module should take the name as a param
raise
Exception
(
"???"
)
# TODO: make it possible to override the /etc/ansible/setup file
# location for templating files as non-root
source
=
self
.
module_args
[
0
]
dest
=
self
.
module_args
[
1
]
metadata
=
'/etc/ansible/setup'
# first copy the source template over
tempname
=
os
.
path
.
split
(
source
)[
-
1
]
temppath
=
self
.
_get_tmp_path
(
conn
,
tempname
)
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
source
,
temppath
))
ftp
=
conn
.
open_sftp
()
ftp
.
put
(
source
,
temppath
)
ftp
.
close
()
# install the template module
self
.
module_name
=
'template'
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
# run the template module
self
.
module_args
=
[
temppath
,
dest
,
metadata
]
result
=
self
.
_exec_command
(
conn
,
self
.
_command
(
outpath
))
# clean up
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
outpath
)
self
.
_exec_command
(
conn
,
"rm -f
%
s"
%
temppath
)
conn
.
close
()
try
:
return
[
host
,
True
,
json
.
loads
(
result
)
]
except
:
traceback
.
print_exc
()
return
[
host
,
False
,
result
]
return
[
host
,
False
,
1
]
def
_command
(
self
,
outpath
):
def
_command
(
self
,
outpath
):
''' form up a command string '''
''' form up a command string '''
...
...
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