@ -61,9 +61,9 @@ _SNIPPET_PATH = os.path.join(os.path.dirname(__file__), '..', 'module_utils')
# ******************************************************************************
# ******************************************************************************
ZIPLOADER _TEMPLATE = u ''' %(shebang)s
ANSIBALLZ _TEMPLATE = u ''' %(shebang)s
% ( coding ) s
% ( coding ) s
ZIPLOADER_WRAPPER = True # For test-module script to tell this is a ZIPLOADER _WRAPPER
ANSIBALLZ_WRAPPER = True # For test-module script to tell this is a ANSIBALLZ _WRAPPER
# This code is part of Ansible, but is an independent component.
# This code is part of Ansible, but is an independent component.
# The code in this particular templatable string, and this templatable string
# The code in this particular templatable string, and this templatable string
# only, is BSD licensed. Modules which end up using this snippet, which is
# only, is BSD licensed. Modules which end up using this snippet, which is
@ -140,7 +140,7 @@ def debug(command, zipped_mod, json_params):
# The code here normally doesn't run. It's only used for debugging on the
# The code here normally doesn't run. It's only used for debugging on the
# remote machine.
# remote machine.
#
#
# The subcommands in this function make it easier to debug ziploader
# The subcommands in this function make it easier to debug ansiballz
# modules. Here's the basic steps:
# modules. Here's the basic steps:
#
#
# Run ansible with the environment variable: ANSIBLE_KEEP_REMOTE_FILES=1 and -vvv
# Run ansible with the environment variable: ANSIBLE_KEEP_REMOTE_FILES=1 and -vvv
@ -262,9 +262,9 @@ if __name__ == '__main__':
# See comments in the debug() method for information on debugging
# See comments in the debug() method for information on debugging
#
#
ZIPLOADER _PARAMS = % ( params ) s
ANSIBALLZ _PARAMS = % ( params ) s
if PY3 :
if PY3 :
ZIPLOADER_PARAMS = ZIPLOADER _PARAMS. encode ( ' utf-8 ' )
ANSIBALLZ_PARAMS = ANSIBALLZ _PARAMS. encode ( ' utf-8 ' )
try :
try :
# There's a race condition with the controller removing the
# There's a race condition with the controller removing the
# remote_tmpdir and this module executing under async. So we cannot
# remote_tmpdir and this module executing under async. So we cannot
@ -277,7 +277,7 @@ if __name__ == '__main__':
modlib . close ( )
modlib . close ( )
if len ( sys . argv ) == 2 :
if len ( sys . argv ) == 2 :
exitcode = debug ( sys . argv [ 1 ] , zipped_mod , ZIPLOADER _PARAMS)
exitcode = debug ( sys . argv [ 1 ] , zipped_mod , ANSIBALLZ _PARAMS)
else :
else :
z = zipfile . ZipFile ( zipped_mod , mode = ' r ' )
z = zipfile . ZipFile ( zipped_mod , mode = ' r ' )
module = os . path . join ( temp_path , ' ansible_module_ %(ansible_module)s .py ' )
module = os . path . join ( temp_path , ' ansible_module_ %(ansible_module)s .py ' )
@ -297,7 +297,7 @@ if __name__ == '__main__':
z . writestr ( ' sitecustomize.py ' , sitecustomize )
z . writestr ( ' sitecustomize.py ' , sitecustomize )
z . close ( )
z . close ( )
exitcode = invoke_module ( module , zipped_mod , ZIPLOADER _PARAMS)
exitcode = invoke_module ( module , zipped_mod , ANSIBALLZ _PARAMS)
finally :
finally :
try :
try :
shutil . rmtree ( temp_path )
shutil . rmtree ( temp_path )
@ -320,10 +320,10 @@ def _strip_comments(source):
if C . DEFAULT_KEEP_REMOTE_FILES :
if C . DEFAULT_KEEP_REMOTE_FILES :
# Keep comments when KEEP_REMOTE_FILES is set. That way users will see
# Keep comments when KEEP_REMOTE_FILES is set. That way users will see
# the comments with some nice usage instructions
# the comments with some nice usage instructions
ACTIVE_ ZIPLOADER_TEMPLATE = ZIPLOADER _TEMPLATE
ACTIVE_ ANSIBALLZ_TEMPLATE = ANSIBALLZ _TEMPLATE
else :
else :
# ZIPLOADER _TEMPLATE stripped of comments for smaller over the wire size
# ANSIBALLZ _TEMPLATE stripped of comments for smaller over the wire size
ACTIVE_ ZIPLOADER_TEMPLATE = _strip_comments ( ZIPLOADER _TEMPLATE)
ACTIVE_ ANSIBALLZ_TEMPLATE = _strip_comments ( ANSIBALLZ _TEMPLATE)
class ModuleDepFinder ( ast . NodeVisitor ) :
class ModuleDepFinder ( ast . NodeVisitor ) :
# Caveats:
# Caveats:
@ -526,7 +526,7 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
# a separate arguments file needs to be sent over the wire.
# a separate arguments file needs to be sent over the wire.
# module_substyle is extra information that's useful internally. It tells
# module_substyle is extra information that's useful internally. It tells
# us what we have to look to substitute in the module files and whether
# us what we have to look to substitute in the module files and whether
# we're using module replacer or ziploader to format the module itself.
# we're using module replacer or ansiballz to format the module itself.
if _is_binary ( module_data ) :
if _is_binary ( module_data ) :
module_substyle = module_style = ' binary '
module_substyle = module_style = ' binary '
elif REPLACER in module_data :
elif REPLACER in module_data :
@ -566,33 +566,33 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
display . warning ( u ' Bad module compression string specified: %s . Using ZIP_STORED (no compression) ' % module_compression )
display . warning ( u ' Bad module compression string specified: %s . Using ZIP_STORED (no compression) ' % module_compression )
compression_method = zipfile . ZIP_STORED
compression_method = zipfile . ZIP_STORED
lookup_path = os . path . join ( C . DEFAULT_LOCAL_TMP , ' ziploader _cache' )
lookup_path = os . path . join ( C . DEFAULT_LOCAL_TMP , ' ansiballz _cache' )
cached_module_filename = os . path . join ( lookup_path , " %s - %s " % ( module_name , module_compression ) )
cached_module_filename = os . path . join ( lookup_path , " %s - %s " % ( module_name , module_compression ) )
zipdata = None
zipdata = None
# Optimization -- don't lock if the module has already been cached
# Optimization -- don't lock if the module has already been cached
if os . path . exists ( cached_module_filename ) :
if os . path . exists ( cached_module_filename ) :
display . debug ( ' ZIPLOADER : using cached module: %s ' % cached_module_filename )
display . debug ( ' ANSIBALLZ : using cached module: %s ' % cached_module_filename )
zipdata = open ( cached_module_filename , ' rb ' ) . read ( )
zipdata = open ( cached_module_filename , ' rb ' ) . read ( )
else :
else :
if module_name in strategy . action_write_locks :
if module_name in strategy . action_write_locks :
display . debug ( ' ZIPLOADER : Using lock for %s ' % module_name )
display . debug ( ' ANSIBALLZ : Using lock for %s ' % module_name )
lock = strategy . action_write_locks [ module_name ]
lock = strategy . action_write_locks [ module_name ]
else :
else :
# If the action plugin directly invokes the module (instead of
# If the action plugin directly invokes the module (instead of
# going through a strategy) then we don't have a cross-process
# going through a strategy) then we don't have a cross-process
# Lock specifically for this module. Use the "unexpected
# Lock specifically for this module. Use the "unexpected
# module" lock instead
# module" lock instead
display . debug ( ' ZIPLOADER : Using generic lock for %s ' % module_name )
display . debug ( ' ANSIBALLZ : Using generic lock for %s ' % module_name )
lock = strategy . action_write_locks [ None ]
lock = strategy . action_write_locks [ None ]
display . debug ( ' ZIPLOADER : Acquiring lock' )
display . debug ( ' ANSIBALLZ : Acquiring lock' )
with lock :
with lock :
display . debug ( ' ZIPLOADER : Lock acquired: %s ' % id ( lock ) )
display . debug ( ' ANSIBALLZ : Lock acquired: %s ' % id ( lock ) )
# Check that no other process has created this while we were
# Check that no other process has created this while we were
# waiting for the lock
# waiting for the lock
if not os . path . exists ( cached_module_filename ) :
if not os . path . exists ( cached_module_filename ) :
display . debug ( ' ZIPLOADER : Creating module' )
display . debug ( ' ANSIBALLZ : Creating module' )
# Create the module zip data
# Create the module zip data
zipoutput = BytesIO ( )
zipoutput = BytesIO ( )
zf = zipfile . ZipFile ( zipoutput , mode = ' w ' , compression = compression_method )
zf = zipfile . ZipFile ( zipoutput , mode = ' w ' , compression = compression_method )
@ -613,19 +613,19 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
# Note -- if we have a global function to setup, that would
# Note -- if we have a global function to setup, that would
# be a better place to run this
# be a better place to run this
os . mkdir ( lookup_path )
os . mkdir ( lookup_path )
display . debug ( ' ZIPLOADER : Writing module' )
display . debug ( ' ANSIBALLZ : Writing module' )
with open ( cached_module_filename + ' -part ' , ' wb ' ) as f :
with open ( cached_module_filename + ' -part ' , ' wb ' ) as f :
f . write ( zipdata )
f . write ( zipdata )
# Rename the file into its final position in the cache so
# Rename the file into its final position in the cache so
# future users of this module can read it off the
# future users of this module can read it off the
# filesystem instead of constructing from scratch.
# filesystem instead of constructing from scratch.
display . debug ( ' ZIPLOADER : Renaming module' )
display . debug ( ' ANSIBALLZ : Renaming module' )
os . rename ( cached_module_filename + ' -part ' , cached_module_filename )
os . rename ( cached_module_filename + ' -part ' , cached_module_filename )
display . debug ( ' ZIPLOADER : Done creating module' )
display . debug ( ' ANSIBALLZ : Done creating module' )
if zipdata is None :
if zipdata is None :
display . debug ( ' ZIPLOADER : Reading module after lock' )
display . debug ( ' ANSIBALLZ : Reading module after lock' )
# Another process wrote the file while we were waiting for
# Another process wrote the file while we were waiting for
# the write lock. Go ahead and read the data from disk
# the write lock. Go ahead and read the data from disk
# instead of re-creating it.
# instead of re-creating it.
@ -649,7 +649,7 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
# string
# string
interpreter = u " ' {0} ' " . format ( interpreter )
interpreter = u " ' {0} ' " . format ( interpreter )
output . write ( to_bytes ( ACTIVE_ ZIPLOADER _TEMPLATE % dict (
output . write ( to_bytes ( ACTIVE_ ANSIBALLZ _TEMPLATE % dict (
zipdata = zipdata ,
zipdata = zipdata ,
ansible_module = module_name ,
ansible_module = module_name ,
params = python_repred_params ,
params = python_repred_params ,
@ -693,7 +693,7 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
# these strings could be included in a third-party module but
# these strings could be included in a third-party module but
# officially they were included in the 'basic' snippet for new-style
# officially they were included in the 'basic' snippet for new-style
# python modules (which has been replaced with something else in
# python modules (which has been replaced with something else in
# ziploader ) If we remove them from jsonargs-style module replacer
# ansiballz ) If we remove them from jsonargs-style module replacer
# then we can remove them everywhere.
# then we can remove them everywhere.
python_repred_args = to_bytes ( repr ( module_args_json ) )
python_repred_args = to_bytes ( repr ( module_args_json ) )
module_data = module_data . replace ( REPLACER_VERSION , to_bytes ( repr ( __version__ ) ) )
module_data = module_data . replace ( REPLACER_VERSION , to_bytes ( repr ( __version__ ) ) )