@ -37,7 +37,7 @@ import ansible.errors
import ansible . plugins . connection
import mitogen . unix
from mitogen . utils import cast
import mitogen . utils
import ansible_mitogen . helpers
import ansible_mitogen . process
@ -137,25 +137,34 @@ class Connection(ansible.plugins.connection.ConnectionBase):
def connected ( self ) :
return self . broker is not None
def _wrap_connect ( self , args ) :
dct = mitogen . service . call (
context = self . parent ,
handle = ContextService . handle ,
obj = mitogen . utils . cast ( args ) ,
)
if dct [ ' msg ' ] :
raise ansible . errors . AnsibleConnectionFailure ( dct [ ' msg ' ] )
return dct [ ' context ' ] , dct [ ' home_dir ' ]
def _connect_local ( self ) :
"""
Fetch a reference to the local ( ) Context from ContextService in the
master process .
"""
return mitogen . service . call ( self . parent , ContextService . handle , cast ( {
return self . _wrap_connec t( {
' method ' : ' local ' ,
' python_path ' : self . python_path ,
} ) )
} )
def _connect_ssh ( self ) :
"""
Fetch a reference to an SSH Context matching the play context from
ContextService in the master process .
"""
return mitogen . service . call (
self . parent ,
ContextService . handle ,
cast ( {
return self . _wrap_connect ( {
' method ' : ' ssh ' ,
' check_host_keys ' : False , # TODO
' hostname ' : self . _play_context . remote_addr ,
@ -177,19 +186,14 @@ class Connection(ansible.plugins.connection.ConnectionBase):
for term in shlex . split ( s or ' ' )
]
} )
)
def _connect_docker ( self ) :
return mitogen . service . call (
self . parent ,
ContextService . handle ,
cast ( {
return self . _wrap_connect ( {
' method ' : ' docker ' ,
' container ' : self . _play_context . remote_addr ,
' python_path ' : self . python_path ,
' connect_timeout ' : self . _play_context . timeout ,
} )
)
def _connect_sudo ( self , via = None , python_path = None ) :
"""
@ -200,10 +204,7 @@ class Connection(ansible.plugins.connection.ConnectionBase):
Parent Context of the sudo Context . For Ansible , this should always
be a Context returned by _connect_ssh ( ) .
"""
return mitogen . service . call (
self . parent ,
ContextService . handle ,
cast ( {
return self . _wrap_connect ( {
' method ' : ' sudo ' ,
' username ' : self . _play_context . become_user ,
' password ' : self . _play_context . become_pass ,
@ -216,7 +217,6 @@ class Connection(ansible.plugins.connection.ConnectionBase):
self . _play_context . become_flags or ' '
) ,
} )
)
def _connect ( self ) :
"""
@ -318,8 +318,8 @@ class Connection(ansible.plugins.connection.ConnectionBase):
emulate_tty = ( not in_data and sudoable )
rc , stdout , stderr = self . call (
ansible_mitogen . helpers . exec_command ,
cmd = cast( cmd ) ,
in_data = cast( in_data ) ,
cmd = mitogen. utils . cast( cmd ) ,
in_data = mitogen. utils . cast( in_data ) ,
chdir = mitogen_chdir ,
emulate_tty = emulate_tty ,
)
@ -341,7 +341,7 @@ class Connection(ansible.plugins.connection.ConnectionBase):
Local filesystem path to write .
"""
output = self . call ( ansible_mitogen . helpers . read_path ,
cast( in_path ) )
mitogen. utils . cast( in_path ) )
ansible_mitogen . helpers . write_path ( out_path , output )
def put_data ( self , out_path , data ) :
@ -355,7 +355,8 @@ class Connection(ansible.plugins.connection.ConnectionBase):
Remote filesystem path to write .
"""
self . call ( ansible_mitogen . helpers . write_path ,
cast ( out_path ) , cast ( data ) )
mitogen . utils . cast ( out_path ) ,
mitogen . utils . cast ( data ) )
def put_file ( self , in_path , out_path ) :
"""