Skip to content

Commit 6034b5b

Browse files
committed
Split connection code into submodules.
1 parent 0972b76 commit 6034b5b

File tree

4 files changed

+133
-77
lines changed

4 files changed

+133
-77
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# (c) 2012, Michael DeHaan <[email protected]>
2+
#
3+
# This file is part of Ansible
4+
#
5+
# Ansible is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Ansible is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
################################################
20+
21+
import warnings
22+
import traceback
23+
import os
24+
import time
25+
import re
26+
import shutil
27+
import subprocess
28+
import pipes
29+
import socket
30+
import random
31+
32+
from ansible.runner.connection import local
33+
from ansible.runner.connection import paramiko_ssh
34+
35+
class Connection(object):
36+
''' Handles abstract connections to remote hosts '''
37+
38+
def __init__(self, runner, transport,sudo_user):
39+
self.runner = runner
40+
self.transport = transport
41+
self.sudo_user = sudo_user
42+
def connect(self, host, port=None):
43+
conn = None
44+
if self.transport == 'local':
45+
conn = local.LocalConnection(self.runner, host)
46+
elif self.transport == 'paramiko':
47+
conn = paramiko_ssh.ParamikoConnection(self.runner, host, port)
48+
if conn is None:
49+
raise Exception("unsupported connection type")
50+
return conn.connect()
51+
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# (c) 2012, Michael DeHaan <[email protected]>
2+
#
3+
# This file is part of Ansible
4+
#
5+
# Ansible is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Ansible is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
################################################
20+
21+
import warnings
22+
import traceback
23+
import os
24+
import time
25+
import re
26+
import shutil
27+
import subprocess
28+
import pipes
29+
import socket
30+
import random
31+
32+
from ansible import errors
33+
34+
class LocalConnection(object):
35+
''' Local based connections '''
36+
37+
def __init__(self, runner, host):
38+
self.runner = runner
39+
self.host = host
40+
41+
def connect(self, port=None):
42+
''' connect to the local host; nothing to do here '''
43+
44+
return self
45+
46+
def exec_command(self, cmd, tmp_path,sudo_user,sudoable=False):
47+
''' run a command on the local host '''
48+
if self.runner.sudo and sudoable:
49+
cmd = "sudo -s %s" % cmd
50+
if self.runner.sudo_pass:
51+
# NOTE: if someone wants to add sudo w/ password to the local connection type, they are welcome
52+
# to do so. The primary usage of the local connection is for crontab and kickstart usage however
53+
# so this doesn't seem to be a huge priority
54+
raise errors.AnsibleError("sudo with password is presently only supported on the paramiko (SSH) connection type")
55+
56+
p = subprocess.Popen(cmd, shell=True, stdin=None,
57+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
58+
stdout, stderr = p.communicate()
59+
return ("", stdout, stderr)
60+
61+
def put_file(self, in_path, out_path):
62+
''' transfer a file from local to local '''
63+
if not os.path.exists(in_path):
64+
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
65+
try:
66+
shutil.copyfile(in_path, out_path)
67+
except shutil.Error:
68+
traceback.print_exc()
69+
raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
70+
except IOError:
71+
traceback.print_exc()
72+
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
73+
74+
def fetch_file(self, in_path, out_path):
75+
''' fetch a file from local to local -- for copatibility '''
76+
self.put_file(in_path, out_path)
77+
78+
def close(self):
79+
''' terminate the connection; nothing to do here '''
80+
81+
pass

lib/ansible/runner/connection.py lib/ansible/runner/connection/paramiko_ssh.py

-77
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,6 @@
3636
warnings.simplefilter("ignore")
3737
import paramiko
3838

39-
40-
################################################
41-
42-
class Connection(object):
43-
''' Handles abstract connections to remote hosts '''
44-
45-
def __init__(self, runner, transport,sudo_user):
46-
self.runner = runner
47-
self.transport = transport
48-
self.sudo_user = sudo_user
49-
def connect(self, host, port=None):
50-
conn = None
51-
if self.transport == 'local':
52-
conn = LocalConnection(self.runner, host)
53-
elif self.transport == 'paramiko':
54-
conn = ParamikoConnection(self.runner, host, port)
55-
if conn is None:
56-
raise Exception("unsupported connection type")
57-
return conn.connect()
58-
59-
################################################
60-
# want to implement another connection type?
61-
# follow duck-typing of ParamikoConnection
62-
# you may wish to read config files in __init__
63-
# if you have any. Paramiko does not need any.
64-
6539
class ParamikoConnection(object):
6640
''' SSH based connections with Paramiko '''
6741

@@ -171,54 +145,3 @@ def close(self):
171145

172146
self.ssh.close()
173147

174-
############################################
175-
# add other connection types here
176-
177-
class LocalConnection(object):
178-
''' Local based connections '''
179-
180-
def __init__(self, runner, host):
181-
self.runner = runner
182-
self.host = host
183-
184-
def connect(self, port=None):
185-
''' connect to the local host; nothing to do here '''
186-
187-
return self
188-
189-
def exec_command(self, cmd, tmp_path,sudo_user,sudoable=False):
190-
''' run a command on the local host '''
191-
if self.runner.sudo and sudoable:
192-
cmd = "sudo -s %s" % cmd
193-
if self.runner.sudo_pass:
194-
# NOTE: if someone wants to add sudo w/ password to the local connection type, they are welcome
195-
# to do so. The primary usage of the local connection is for crontab and kickstart usage however
196-
# so this doesn't seem to be a huge priority
197-
raise errors.AnsibleError("sudo with password is presently only supported on the paramiko (SSH) connection type")
198-
199-
p = subprocess.Popen(cmd, shell=True, stdin=None,
200-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
201-
stdout, stderr = p.communicate()
202-
return ("", stdout, stderr)
203-
204-
def put_file(self, in_path, out_path):
205-
''' transfer a file from local to local '''
206-
if not os.path.exists(in_path):
207-
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
208-
try:
209-
shutil.copyfile(in_path, out_path)
210-
except shutil.Error:
211-
traceback.print_exc()
212-
raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
213-
except IOError:
214-
traceback.print_exc()
215-
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
216-
217-
def fetch_file(self, in_path, out_path):
218-
''' fetch a file from local to local -- for copatibility '''
219-
self.put_file(in_path, out_path)
220-
221-
def close(self):
222-
''' terminate the connection; nothing to do here '''
223-
224-
pass

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'ansible.inventory',
2626
'ansible.playbook',
2727
'ansible.runner',
28+
'ansible.runner.connection',
2829
],
2930
scripts=[
3031
'bin/ansible',

0 commit comments

Comments
 (0)