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
9754c671
Commit
9754c671
authored
May 13, 2015
by
Matt Martz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a decorator to ensure jit connection, instead of an explicit call to _connect
parent
f7839dee
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
6 deletions
+27
-6
lib/ansible/executor/task_executor.py
+0
-1
lib/ansible/plugins/connections/__init__.py
+11
-1
lib/ansible/plugins/connections/paramiko_ssh.py
+6
-2
lib/ansible/plugins/connections/ssh.py
+5
-1
lib/ansible/plugins/connections/winrm.py
+5
-1
No files found.
lib/ansible/executor/task_executor.py
View file @
9754c671
...
...
@@ -210,7 +210,6 @@ class TaskExecutor:
# get the connection and the handler for this execution
self
.
_connection
=
self
.
_get_connection
(
variables
)
self
.
_connection
.
set_host_overrides
(
host
=
self
.
_host
)
self
.
_connection
.
_connect
()
self
.
_handler
=
self
.
_get_action_handler
(
connection
=
self
.
_connection
,
templar
=
templar
)
...
...
lib/ansible/plugins/connections/__init__.py
View file @
9754c671
...
...
@@ -22,6 +22,7 @@ __metaclass__ = type
from
abc
import
ABCMeta
,
abstractmethod
,
abstractproperty
from
functools
import
wraps
from
six
import
with_metaclass
from
ansible
import
constants
as
C
...
...
@@ -32,7 +33,16 @@ from ansible.errors import AnsibleError
# which may want to output display/logs too
from
ansible.utils.display
import
Display
__all__
=
[
'ConnectionBase'
]
__all__
=
[
'ConnectionBase'
,
'ensure_connect'
]
def
ensure_connect
(
func
):
@wraps
(
func
)
def
wrapped
(
self
,
*
args
,
**
kwargs
):
self
.
_connect
()
return
func
(
self
,
*
args
,
**
kwargs
)
return
wrapped
class
ConnectionBase
(
with_metaclass
(
ABCMeta
,
object
)):
'''
...
...
lib/ansible/plugins/connections/paramiko_ssh.py
View file @
9754c671
...
...
@@ -41,7 +41,7 @@ from binascii import hexlify
from
ansible
import
constants
as
C
from
ansible.errors
import
AnsibleError
,
AnsibleConnectionFailure
,
AnsibleFileNotFound
from
ansible.plugins.connections
import
ConnectionBase
from
ansible.plugins.connections
import
ConnectionBase
,
ensure_connect
from
ansible.utils.path
import
makedirs_safe
AUTHENTICITY_MSG
=
"""
...
...
@@ -61,6 +61,7 @@ with warnings.catch_warnings():
except
ImportError
:
pass
class
MyAddPolicy
(
object
):
"""
Based on AutoAddPolicy in paramiko so we can determine when keys are added
...
...
@@ -188,6 +189,7 @@ class Connection(ConnectionBase):
return
ssh
@ensure_connect
def
exec_command
(
self
,
cmd
,
tmp_path
,
executable
=
'/bin/sh'
,
in_data
=
None
):
''' run a command on the remote host '''
...
...
@@ -248,6 +250,7 @@ class Connection(ConnectionBase):
return
(
chan
.
recv_exit_status
(),
''
,
no_prompt_out
+
stdout
,
no_prompt_out
+
stderr
)
@ensure_connect
def
put_file
(
self
,
in_path
,
out_path
):
''' transfer a file from local to remote '''
...
...
@@ -272,9 +275,10 @@ class Connection(ConnectionBase):
if
cache_key
in
SFTP_CONNECTION_CACHE
:
return
SFTP_CONNECTION_CACHE
[
cache_key
]
else
:
result
=
SFTP_CONNECTION_CACHE
[
cache_key
]
=
self
.
connect
()
.
ssh
.
open_sftp
()
result
=
SFTP_CONNECTION_CACHE
[
cache_key
]
=
self
.
_
connect
()
.
ssh
.
open_sftp
()
return
result
@ensure_connect
def
fetch_file
(
self
,
in_path
,
out_path
):
''' save a remote file to the specified path '''
...
...
lib/ansible/plugins/connections/ssh.py
View file @
9754c671
...
...
@@ -34,7 +34,8 @@ from hashlib import sha1
from
ansible
import
constants
as
C
from
ansible.errors
import
AnsibleError
,
AnsibleConnectionFailure
,
AnsibleFileNotFound
from
ansible.plugins.connections
import
ConnectionBase
from
ansible.plugins.connections
import
ConnectionBase
,
ensure_connect
class
Connection
(
ConnectionBase
):
''' ssh based connections '''
...
...
@@ -269,6 +270,7 @@ class Connection(ConnectionBase):
self
.
_display
.
vvv
(
"EXEC previous known host file not found for {0}"
.
format
(
host
))
return
True
@ensure_connect
def
exec_command
(
self
,
cmd
,
tmp_path
,
executable
=
'/bin/sh'
,
in_data
=
None
):
''' run a command on the remote host '''
...
...
@@ -390,6 +392,7 @@ class Connection(ConnectionBase):
return
(
p
.
returncode
,
''
,
no_prompt_out
+
stdout
,
no_prompt_err
+
stderr
)
@ensure_connect
def
put_file
(
self
,
in_path
,
out_path
):
''' transfer a file from local to remote '''
self
.
_display
.
vvv
(
"PUT {0} TO {1}"
.
format
(
in_path
,
out_path
),
host
=
self
.
_connection_info
.
remote_addr
)
...
...
@@ -425,6 +428,7 @@ class Connection(ConnectionBase):
if
returncode
!=
0
:
raise
AnsibleError
(
"failed to transfer file to {0}:
\n
{1}
\n
{2}"
.
format
(
out_path
,
stdout
,
stderr
))
@ensure_connect
def
fetch_file
(
self
,
in_path
,
out_path
):
''' fetch a file from remote to local '''
self
.
_display
.
vvv
(
"FETCH {0} TO {1}"
.
format
(
in_path
,
out_path
),
host
=
self
.
_connection_info
.
remote_addr
)
...
...
lib/ansible/plugins/connections/winrm.py
View file @
9754c671
...
...
@@ -42,10 +42,11 @@ except ImportError:
from
ansible
import
constants
as
C
from
ansible.errors
import
AnsibleError
,
AnsibleConnectionFailure
,
AnsibleFileNotFound
from
ansible.plugins.connections
import
ConnectionBase
from
ansible.plugins.connections
import
ConnectionBase
,
ensure_connect
from
ansible.plugins
import
shell_loader
from
ansible.utils.path
import
makedirs_safe
class
Connection
(
ConnectionBase
):
'''WinRM connections over HTTP/HTTPS.'''
...
...
@@ -151,6 +152,7 @@ class Connection(ConnectionBase):
self
.
protocol
=
self
.
_winrm_connect
()
return
self
@ensure_connect
def
exec_command
(
self
,
cmd
,
tmp_path
,
executable
=
'/bin/sh'
,
in_data
=
None
):
cmd
=
cmd
.
encode
(
'utf-8'
)
...
...
@@ -172,6 +174,7 @@ class Connection(ConnectionBase):
raise
AnsibleError
(
"failed to exec cmd
%
s"
%
cmd
)
return
(
result
.
status_code
,
''
,
result
.
std_out
.
encode
(
'utf-8'
),
result
.
std_err
.
encode
(
'utf-8'
))
@ensure_connect
def
put_file
(
self
,
in_path
,
out_path
):
self
.
_display
.
vvv
(
"PUT
%
s TO
%
s"
%
(
in_path
,
out_path
),
host
=
self
.
_connection_info
.
remote_addr
)
if
not
os
.
path
.
exists
(
in_path
):
...
...
@@ -210,6 +213,7 @@ class Connection(ConnectionBase):
traceback
.
print_exc
()
raise
AnsibleError
(
"failed to transfer file to
%
s"
%
out_path
)
@ensure_connect
def
fetch_file
(
self
,
in_path
,
out_path
):
out_path
=
out_path
.
replace
(
'
\\
'
,
'/'
)
self
.
_display
.
vvv
(
"FETCH
%
s TO
%
s"
%
(
in_path
,
out_path
),
host
=
self
.
_connection_info
.
remote_addr
)
...
...
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