from invirt.common import CodeError
import subprocess
+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)