Skip to content

Commit

Permalink
take lxc_ssh.py from PR andreasscherbaum#38
Browse files Browse the repository at this point in the history
  • Loading branch information
stefangweichinger committed Jun 22, 2021
1 parent 9df8f65 commit 226f118
Showing 1 changed file with 63 additions and 22 deletions.
85 changes: 63 additions & 22 deletions lxc_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,13 @@
The lxc host to connect to.
env:
- name: LXC_HOST
- name: LXC_CONTAINER
ini:
- key: lxc_host
section: lxc_ssh_connection
- key: lxc_container
section: lxc_ssh_connection
vars:
- name: lxc_host
- name: lxc_container
cli:
- name: lxc_host
- name: lxc_container
"""


Expand Down Expand Up @@ -523,6 +518,27 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
self.control_path = None
self.control_path_dir = None

def _set_version(self):
# LXC v1 uses 'lxc-info', 'lxc-attach' and so on
# LXC v2 uses just 'lxc'
(returncode2, stdout2, stderr2) = self._exec_command("which lxc", None, False)
(returncode1, stdout1, stderr1) = self._exec_command(
"which lxc-info", None, False
)
if returncode2 == 0:
self.lxc_version = 2
display.vvv("LXC v2")
elif returncode1 == 0:
self.lxc_version = 1
display.vvv("LXC v1")
else:
raise AnsibleConnectionFailure("Cannot identify LXC version")
sys.exit(1)

def set_options(self, *args, **kwargs):
super(Connection, self).set_options(*args, **kwargs)
self._set_version()

# The connection is created by running ssh/scp/sftp from the exec_command,
# put_file, and fetch_file methods, so we don't need to do any connection
# management here.
Expand Down Expand Up @@ -678,7 +694,7 @@ def _build_command(self, binary, subsystem, *other_args):
to_bytes(a, errors="surrogate_or_strict")
for a in self._split_ssh_args(ssh_args)
]
self._add_args(b_command, b_args, "ansible.cfg set ssh_args")
self._add_args(b_command, b_args, u"ansible.cfg set ssh_args")

# Now we add various arguments that have their own specific settings
# defined in docs above.
Expand Down Expand Up @@ -738,6 +754,7 @@ def _build_command(self, binary, subsystem, *other_args):
)

self.user = self.get_option("remote_user")

if self.user:
self._add_args(
b_command,
Expand Down Expand Up @@ -1319,10 +1336,16 @@ def exec_command(self, cmd, in_data=None, sudoable=False):

ssh_executable = self.get_option("ssh_executable")
h = self.container_name
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if self.lxc_version == 2:
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
elif self.lxc_version == 1:
lxc_cmd = "lxc-attach --name %s -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if in_data:
cmd = self._build_command(ssh_executable, "ssh", self.host, lxc_cmd)
else:
Expand Down Expand Up @@ -1352,10 +1375,16 @@ def put_file(self, in_path, out_path):
# regular command
cmd = "cat > %s; echo -n done" % pipes.quote(out_path)
h = self.container_name
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if self.lxc_version == 2:
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
elif self.lxc_version == 1:
lxc_cmd = "lxc-attach --name %s -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if in_data:
cmd = self._build_command(ssh_executable, "ssh", self.host, lxc_cmd)
else:
Expand All @@ -1375,10 +1404,16 @@ def put_file(self, in_path, out_path):
# regular command
cmd = "cat > %s; echo -n done" % pipes.quote(out_path)
h = self.container_name
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if self.lxc_version == 2:
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
elif self.lxc_version == 1:
lxc_cmd = "lxc-attach --name %s -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if in_data:
cmd = self._build_command(ssh_executable, "ssh", self.host, lxc_cmd)
else:
Expand All @@ -1396,10 +1431,16 @@ def fetch_file(self, in_path, out_path):

cmd = "cat < %s" % pipes.quote(in_path)
h = self.container_name
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
if self.lxc_version == 2:
lxc_cmd = "lxc exec %s --mode=non-interactive -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)
elif self.lxc_version == 1:
lxc_cmd = "lxc-attach --name %s -- /bin/sh -c %s" % (
pipes.quote(h),
pipes.quote(cmd),
)

cmd = self._build_command(ssh_executable, "ssh", self.host, lxc_cmd)
(returncode, stdout, stderr) = self._run(cmd, None, sudoable=False)
Expand Down

0 comments on commit 226f118

Please sign in to comment.