X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-base.git/blobdiff_plain/4e706695a3bce2ff5cc2010a2a496d7b403bdd15..refs/heads/quentin:/python/invirt/remctl.py diff --git a/python/invirt/remctl.py b/python/invirt/remctl.py index 8f93984..956ca2d 100644 --- a/python/invirt/remctl.py +++ b/python/invirt/remctl.py @@ -4,37 +4,42 @@ Functions to perform remctls. from invirt.common import CodeError import subprocess +import sys +from socket import getfqdn -def kinit(): +def kinit(principal=None, keytab=None): """Kinit with a given username and keytab""" - p = subprocess.Popen(['kinit', "-k", "-t", '/etc/invirt/keytab', - 'daemon/'+config.web.hostname], + if principal is None: + principal = 'daemon/' + getfqdn() + if keytab is None: + keytab = '/etc/invirt/keytab' + p = subprocess.Popen(['kinit', "-k", "-t", keytab, principal], stderr=subprocess.PIPE) e = p.wait() if e: raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read())) -def checkKinit(): +def checkKinit(principal=None, keytab=None): """If we lack tickets, kinit.""" p = subprocess.Popen(['klist', '-s']) if p.wait(): - kinit() + kinit(principal, keytab) -def remctl(*args, **kws): +def remctl(host, *args, **kws): """Perform a remctl and return the output. kinits if necessary, and outputs errors to stderr. """ - checkKinit() - p = subprocess.Popen(['remctl', config.remote.hostname] + checkKinit(kws.get('principal'), kws.get('keytab')) + p = subprocess.Popen(['remctl', host] + list(args), stdout=subprocess.PIPE, stderr=subprocess.PIPE) - v = p.wait() + stdout, stderr = p.communicate() if kws.get('err'): - return p.stdout.read(), p.stderr.read() - if v: - print >> sys.stderr, 'Error', v, 'on remctl', args, ':' - print >> sys.stderr, p.stderr.read() + return stdout, stderr + if p.returncode: + print >> sys.stderr, 'Error', p.returncode, 'on remctl', args, ':' + print >> sys.stderr, stderr raise CodeError('ERROR on remctl') - return p.stdout.read() + return stdout