ansible/connection_plugins/lxc/lxc.py

116 lines
3.7 KiB
Python
Raw Normal View History

2014-08-12 15:43:29 +00:00
from __future__ import absolute_import
2015-04-09 10:30:08 +00:00
import os, sys
2014-08-12 15:43:29 +00:00
import subprocess
import shutil
2015-01-14 20:16:30 +00:00
import traceback
2015-04-09 10:30:08 +00:00
import select
2014-08-12 15:43:29 +00:00
from ansible import errors
from ansible.callbacks import vvv
import lxc as _lxc
class Connection(object):
2014-09-17 12:08:42 +00:00
""" Local lxc based connections """
2014-08-12 15:43:29 +00:00
def __init__(self, runner, host, port, *args, **kwargs):
self.has_pipelining = False
self.host = host
self.runner = runner
self.container = _lxc.Container(host)
if self.container.state == "STOPPED":
raise errors.AnsibleError("%s is not running" % host)
def connect(self, port=None):
2014-09-17 12:08:42 +00:00
""" connect to the lxc; nothing to do here """
2014-08-12 15:43:29 +00:00
vvv("THIS IS A LOCAL LXC DIR", host=self.host)
return self
2015-04-09 10:30:08 +00:00
def exec_command(self, cmd, tmp_path, sudo_user=None, become_user=None, sudoable=False, executable="/bin/sh", in_data=None, su=None, su_user=None):
2014-09-17 12:08:42 +00:00
""" run a command on the chroot """
2014-08-12 15:43:29 +00:00
2015-04-09 10:30:08 +00:00
local_cmd = [cmd]
if executable:
local_cmd = [executable, "-c"] + local_cmd
if sudo_user:
local_cmd = ["sudo", "-u", sudo_user, "--"] + local_cmd
read_stdout, write_stdout = os.pipe()
read_stderr, write_stderr = os.pipe()
2014-08-12 15:43:29 +00:00
vvv("EXEC %s" % (local_cmd), host=self.host)
2015-04-09 10:30:08 +00:00
pid = self.container.attach(_lxc.attach_run_command, local_cmd,
stdout=write_stdout,
stderr=write_stderr)
os.close(write_stdout)
os.close(write_stderr)
fds = [read_stdout, read_stderr]
buf = { read_stdout: [], read_stderr: [] }
while len(fds) > 0:
ready_fds, _, _ = select.select(fds, [], [])
for fd in ready_fds:
data = os.read(fd, 32768)
if not data:
fds.remove(fd)
os.close(fd)
buf[fd].append(data)
(pid, returncode) = os.waitpid(pid, 0)
stdout = b"".join(buf[read_stdout])
stderr = b"".join(buf[read_stderr])
return (returncode, "", stdout, stderr)
def put_file(self, in_path, out_path):
""" transfer a file from local to lxc """
vvv("PUT %s TO %s" % (in_path, out_path), host=self.host)
2014-08-12 15:43:29 +00:00
if not os.path.exists(in_path):
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
try:
2015-04-09 10:30:08 +00:00
src_file = open(in_path, "rb")
except IOError:
2014-08-12 15:43:29 +00:00
traceback.print_exc()
2015-04-09 10:30:08 +00:00
raise errors.AnsibleError("failed to open file to %s" % in_path)
def write_file(args):
dst_file = open(out_path, 'wb')
shutil.copyfileobj(src_file, dst_file)
try:
self.container.attach_wait(write_file, None)
2014-08-12 15:43:29 +00:00
except IOError:
traceback.print_exc()
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
def fetch_file(self, in_path, out_path):
2014-09-17 12:08:42 +00:00
""" fetch a file from lxc to local """
2014-08-12 15:43:29 +00:00
vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host)
2015-04-09 10:30:08 +00:00
if not os.path.exists(in_path):
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
try:
dst_file = open(out_path, "wb")
except IOError:
traceback.print_exc()
raise errors.AnsibleError("failed to write to file %s" % out_path)
def write_file(args):
src_file = open(in_path, 'rb')
shutil.copyfileobj(src_file, dst_file)
try:
self.container.attach_wait(write_file, None)
except IOError:
traceback.print_exc()
raise errors.AnsibleError("failed to transfer file from %s to %s" % (in_path, out_path))
2014-08-12 15:43:29 +00:00
def close(self):
2014-09-17 12:08:42 +00:00
""" terminate the connection; nothing to do here """
2014-08-12 15:43:29 +00:00
pass