Skip to content

Instantly share code, notes, and snippets.

@kalkin
Created October 12, 2015 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalkin/fcee81dbfecc9ab41522 to your computer and use it in GitHub Desktop.
Save kalkin/fcee81dbfecc9ab41522 to your computer and use it in GitHub Desktop.
#!/bin/python
import fcntl
from ansible.callbacks import vvv, vv
from ansible import errors
from ansible import utils
from qubes.qubes import QubesVmCollection
import subprocess
import os
import logging
log = logging.getLogger('Ansible Qubes Connection')
logging.basicConfig(level=logging.WARNING)
class Connection(object):
''' Qubes connection plugin '''
started = False
def __init__(self, runner, host, port, user='user', gui=False, *args, **kwargs):
self.runner = runner
self.host = host
self.user = str(user)
self.vm = self._get_vm(host)
self.gui = gui
log.debug('Start gui %s' % self.gui)
log.debug('Using user: %s' % self.user)
fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_EX)
self.cp_dir = utils.prepare_writeable_dir('$HOME/.ansible/cp',mode=0700)
fcntl.lockf(self.runner.process_lockfile, fcntl.LOCK_UN)
self.has_pipelining = True
def connect(self, port=None):
''' Starts the vm is if not already running. If the vm was not running
and is started, the vm will be shutdown when the connection is
closed.
'''
if not self.vm.is_running():
#self.vm.start(verbose=True, start_guid=self.gui)
#self.started = True
raise errors.AnsibleConnectionFailed("%s is not running" % self.host)
return self
def close(self):
''' Shutdown vm if it was started by this connection '''
if self.started:
pass
#self.vm.shutdown()
def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', become_user=None, in_data=None, su=None, su_user=None):
if su or su_user or become_user:
raise errors.AnsibleError("Internal Error: this module does not support running commands via su")
if in_data:
raise errors.AnsibleError("Internal Error: this module does not support optimized module pipelining")
p = self.vm.run(cmd, self.user, passio=True, passio_popen=True, passio_stderr=True,
wait=True, gui=self.gui)
stdout, stderr = p.communicate()
log.debug(stdout)
if stderr.strip():
log.warn(stderr)
return (p.returncode, '', stdout, stderr)
def _get_vm(self, name):
''' Returns vm object by name '''
col = QubesVmCollection()
col.lock_db_for_reading()
col.load()
col.unlock_db()
vm = col.get_vm_by_name(name)
if not vm:
raise errors.AnsibleError('No %s vm exists' % vm)
return vm
def put_file(self, in_path, out_path):
vvv("PUT %s TO %s" % (in_path, out_path), host=self.host)
if not os.path.exists(in_path):
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
f = subprocess.Popen(('cat', in_path), stdout=subprocess.PIPE)
p = subprocess.Popen(('qvm-run', '-u', self.user, '-p', self.vm.name, 'cat > %s' %
out_path), stdin=f.stdout)
stdout, stderr = p.communicate()
return (p.returncode, '', stdout, stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment